Dsa PS
Dsa PS
SET - 1:
1> Given an array arr[] and an integer K where K is smaller than size of array, the
task is to find the Kth smallest element in the given array. It is given that all
array elements are distinct.
Note :- l and r denotes the starting and ending index of the array.
Example 1:
Input: N = 6
arr[] = 7 10 4 3 20 15
K = 3
Output : 7
Explanation : 3rd smallest element in the given array is 7.
Example 2:
Input: N = 5
arr[] = 7 10 4 20 15
K = 4
Output : 15
Explanation : 4th smallest element in the given array is 15.
2> Given two strings A and B consisting of lowercase english characters. Find the
characters that are not common in the two strings.
Example 1:
Input: A = geeksforgeeks
B = geeksquiz
Output: fioqruz
Explanation: The characters 'f', 'i', 'o', 'q', 'r', 'u','z' are either present in
A or B, but not in both.
Example 2:
Input: A = characters
B = alphabets
Output: bclpr
Explanation: The characters 'b','c','l','p','r' are either present in A or B, but
not in both.
Examples:
Input: “The quick brown fox jumps over the lazy dog”
Output: Pangram
Explanation: Contains all the characters from ‘a’ to ‘z’]
SET - 2:
1> Given an array of N integers, and an integer K, find the number of pairs of
elements in the array whose sum is equal to K.
Example 1:
Input: N = 4, K = 6
arr[] = {1, 5, 7, 1}
Output: 2
Explanation: arr[0] + arr[1] = 1 + 5 = 6 and arr[1] + arr[3] = 5 + 1 = 6.
Example 2:
Input: N = 4, K = 2
arr[] = {1, 1, 1, 1}
Output: 6
Explanation: Each 1 will produce sum 2 with any 1.
2> Given a String. Reverse each word in it where the words are separated by dots.
Example 1:
Input: S = "i.like.this.program.very.much"
Output: i.ekil.siht.margorp.yrev.hcum
Explanation: The words are reversed as follows:"i" -> "i", "like"->"ekil", "this"-
>"siht", "program" -> "margorp", "very" -> "yrev","much" -> "hcum".
Example 2:
Input: S = "pqr.mno"
Output: rqp.onm
Explanation: The words are reversed as follows:"pqr" -> "rqp" , "mno" -> "onm"
3> Given a number to find the first and last digit of a number.
Examples:
Input : 12345
Output : First digit: 1
last digit : 5
Input : 98562
Output : First digit: 9
last digit : 2
SET - 3:
1> Given an array of N positive integers where all numbers occur even number of
times except one number which occurs odd number of times. Find the exceptional
number.
Example 1:
Input: N = 7
Arr[] = {1, 2, 3, 2, 3, 1, 3}
Output: 3
Explaination: 3 occurs three times.
Example 2:
Input: N = 7
Arr[] = {5, 7, 2, 7, 5, 2, 5}
Output: 5
Explaination: 5 occurs three times.
2> Given an array A of N elements. Find the majority element in the array. A
majority element in an array A of size N is an element that appears strictly more
than N/2 times in the array.
Example 1:
Input: N = 3
A[] = {1,2,3}
Output: -1
Explanation: Since, each element in {1,2,3} appears only once so there is no
majority element.
Example 2:
Input: N = 5
A[] = {3,1,3,3,2}
Output: 3
Explanation: Since, 3 is present more than N/2 times, so it is the majority
element.
3> Given a String S, reverse the string without reversing its individual words.
Words are separated by space.
Example:
Input:
S = "i like this program very much"
Output: "much very program this like i"
Explanation: After reversing the whole string(not individual words), the input
string becomes : "much very program this like i"
SET - 4:
1> Your task is to implement the function atoi. The function takes a string(str)
as argument and converts it to an integer and returns it.
Note:
a. You are not allowed to use inbuilt function.
b. Numerical strings are the string where either every character is in between
0-9 or where the first character is '-' and the rest all characters are in between
0-9.
Example 1:
Input:
str = 123
Output: 123
Example 2:
Input:
str = 21a
Output: -1
Explanation: Output is -1 as all
characters are not digit only.
Examples:
Input: "aaabbb"
output: "a3b3"
Input: "aaabccc"
output: "a3b1c3"
Input: "a"
ouput: "a"
Explanation: By compressing 'a' followed by number of repetitions it will give
"a1" which is larger than the original string, So output will be original string
"a".
3> Given a String. Reverse each word in it where the words are separated by dots.
Example 1:
Input: S = "i.like.this.program.very.much"
Output: i.ekil.siht.margorp.yrev.hcum
Explanation: The words are reversed as follows:"i" -> "i", "like"->"ekil", "this"-
>"siht", "program" -> "margorp", "very" -> "yrev","much" -> "hcum".
Example 2:
Input: S = "pqr.mno"
Output: rqp.onm
Explanation: The words are reversed as follows:"pqr" -> "rqp" , "mno" -> "onm"
SET - 5:
1> Given a string s consisting of words and spaces, return the length of the last
word in the string.
Example 1:
Example 2:
Example 3:
2> Given an array, print the Next Greater Element (NGE) for every element. The Next
greater Element for an element x is the first greater element on the right side of
x in the array. Elements for which no greater element exist, consider the next
greater element as -1.
Input: arr[] = [ 4 , 5 , 2 , 25 ]
Output: [5,25,25,-1]
Explanation: except 25 every element has an element greater than them present on
the right side
Input: arr[] = [ 13 , 7, 6 , 12 ]
Output: [-1,12,12,-1]
Explanation: 13 and 12 don’t have any element greater than them present on the
right side
3> Given two strings A and B consisting of lowercase english characters. Find the
characters that are not common in the two strings.
Example 1:
Input: A = geeksforgeeks
B = geeksquiz
Output: fioqruz
Explanation: The characters 'f', 'i', 'o', 'q', 'r', 'u','z' are either present in
A or B, but not in both.
Example 2:
Input: A = characters
B = alphabets
Output: bclpr
Explanation: The characters 'b','c','l','p','r' are either present in A or B, but
not in both.