coding questions
coding questions
Problem statement
Two arrays of integers are “Equal” if rotating one of them with some number of steps makes it
identical to the other array. A single rotation of an array shifts each element at index at i to i+1, except
for the last element, which is moved to the first position.
Given two arrays arr1 and arr2, print “true” if they are “Equal”, and “false” otherwise.
Input format:
The first line has an integer n, the size of both arrays arr1 and arr2.
Output format:
false, otherwise
2. MODE OF AN ARRAY
You are given an array of \(N\) numbers \(A_1, A_2,\ldots, A_N\). In one operation, you can pick any
index \(i\) and change \(A_i \) to \(x (1 \leq x \leq 10^9)\). Given an integer \(K\), find the minimum
number of operations required to make \(K\) the mode of the array.
Note: A number is called the mode of the array if it is more frequent than any other number in the
array.
Example: The mode of array \([1, 1, 3]\) is \(1\). The array \([1, 1, 3, 3]\) does not have any mode.
Input format
• The first line contains the number of test cases \(T (1 \leq T \leq 1000)\).
• The first line of each test case contains two integers, \(N\) and \(K (1 \leq K \leq
10^9)\) where N denotes the number of elements in the array.
• The second line contains \(N\) integers \(A_1,A_2, \ldots, A_N (1 \leq A_i \leq
10^9)\) denoting the contents of the array.
DSA PROBLEMS
Note: Sum of \(N\) over all test cases does not exceed \(2 \times 10^5\).
Output format
For each test case output a line containing the minimum number of operations required to make \(K\)
the mode of array \(A\).
3.Likeable arrays
Problem statement
Bob and Alice are two friends, they have an array A consisting of N integers, A1,A2,A3 ..., AN. Alice
likes the arrays in which if element X is present it must have exactly X or zero occurrences. So, Bob
has decided to convert this array to an array which Alice likes. To do that, he can perform the
following two operations:
Find the minimum number of operations Bob has to perform so that array is liked by Alice.
Input format
• The first line contains an integer T denoting the number of test cases.
• The first line of each test case contains an integer N denoting the number of elements in
array A.
• The second line of each test case contains N space-separated integers of array A.
Output format
4. Double inversions
Problem statement
Consider a certain permutation of integers from 1 to n as A={a1,a2,...,an} and reverse of array A as R,
that is, R={an,an−1,...,a1}. You are given the inversions at each position of array A and R as
{IA1,IA2,…..,IAn} and {IR1,IR2,…..,IRn} respectively.
Find the original array A. If, there are multiple solutions, print any of them. If there is no solution,
then print -1.
Note: The inversion of array arr for position i is defined as the count of positions j satisfying the
following condition arrj>arri and 1≤j<i.
Input format
Output format
DSA PROBLEMS
For each test case, print the array A in the space-separated format or -1 if no solution exists. Each test
case should be answered in a new line.
5, Bob's empire
Problem statement
Bob built an empire with five cities (C1,C2,C3,C4,C5). The only mode of transport in this empire is
the train.
1. One can travel from C1to C2 in one minute. This train can occupy at most A people.
2. One can travel from C2to C3 in one minute. This train can occupy at most B people.
3. One can travel from C3to C4 in one minute. This train can occupy at most C people.
4. One can travel from C4to C5 in one minute. This train can occupy at most D people.
For each of them, a train leaves the city at each integer time (0,1,2,...).
There is a group of N people at C1, and they all want to go to C5.
At least how long does it take for all of them to reach there? You can ignore the time needed to
transfer.
Input format
• The first line contains an integer T denoting the number of test cases.
• The first line of each test case contains a single integer N.
• The second line of each test case contains four space-separated integers A,B,C,
and D respectively.
Output format
For each test case, print the minimum time required for all of the people to reach C5 (in minutes) in a
new line.
6. Mode of an array
Problem statement
You are given an array of N numbers A1,A2,…,AN. In one operation, you can pick any index i and
change Ai to x(1≤x≤109). Given an integer K, find the minimum number of operations required to
make K the mode of the array.
Note: A number is called the mode of the array if it is more frequent than any other number in the
array.
Example: The mode of array [1,1,3] is 1. The array [1,1,3,3] does not have any mode.
Input format
Note: Sum of N over all test cases does not exceed 2×105.
DSA PROBLEMS
Output format
For each test case output a line containing the minimum number of operations required to make K the
mode of array A.
Problem statement
Given an array A consisting of integers, find the lexicographically smallest array possible, if we can
swap the elements of the array with a distance greater than or equal to K.Note: We can swap array ai
and aj, if |i−j|>K−1.
Input:The first line contains two positive integers N and K.The next line contains an array consisting
of N integers.
Output:The output should be the lexicographic smallest array possible after applying the operations.
8. Sort String
Problem statement
You are given a binary string S of length N. You can apply the following operation to the string S :
• Choose two distinct indices i,j(1≤i,j≤N,i≠j) and flip the characters Si,Sj.
Find the minimum number of operations required to sort the given string S. It is always possible to
sort any string under the given constraints.
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
• For each test case:
o The first line contains N denoting the length of string S.
o The second line contains the binary string S.
Output format
For each test case, print the minimum number of operations required to sort the given string S.
9. Swaps
Problem statement
You are given an array A of size N. You can apply the following operation any number of times
(possibly zero) on A.
• Choose any two distinct indices X (0<=X<N) and Y (0<=Y<N) such that Bitwise Xor
of A[X] and A[Y] is odd and swap A[X] and A[Y].
Find the lexicographically smallest array that can be obtained after performing the operation any
number of times (possibly zero) on A.
An array P is lexicographically smaller than its permutation Q if and only if, for the earliest index at
which P and Q differ, the element of P at that index is smaller than the element of Q at that index.
Example, P = [1, 12, 4, 7, 8] is lexicographically smaller than Q = [1, 12, 8, 4, 7].
Input Format:
DSA PROBLEMS
The first line contains an integer T, which denotes the number of test cases.
The first line of each test case contains an integer N, denoting the number of elements in the array A.
The second line of each test case contains N space-separated integers, denoting the elements of the
array A.
Output Format:
Problem statement
Bob Plays an exciting game known as "Product Game" In the Game, Bob has been provided an array
of numbers of size N along with Qqueries. For each query, he is provided with integers L and R and
he has to find the product of all elements in the array between this range inclusively. Bob's task is to
find the difference between the maximum and minimum solutions among all the queries.
Note:
• Since the value of product can be large, print the answer modulo 1000000007.
• Compute the answer to each query as PQ−1 modulo 1000000007, where Q−1 denotes the
multiplicative inverse of Q modulo 1000000007
• Compute overall answer modulo 1000000007.
Task:
Find the difference between the maximum and minimum solutions among all the queries.
Function Description:
Complete the function solve provided in the editor. This function takes the following two parameters
and returns the required answer:
T: Test Cases
N:Size of Array
A: Array of Numbers
Q: Number of queries
queries: 2-D Array of queries containing [L,R]
L:LowerIndex
R:UpperIndex
Input Format:
The first line contains an Integer T which is the number of the test cases.
Output Format:
Problem statement
You are given a string S that represents a number. This string consists of the following
characters only:
• 1
• 2
• 3
• Swap any two adjacent characters only if the absolute difference between the characters is 1
Your task is to determine the smallest number that can be formed by using the provided operation.
You can perform this operation any number of times (possibly zero).
Input format
• The first line contains an integer T that denotes the number of test cases.
• For each test case:
o The first line contains an integer that denotes the length of string S.
o The second line contains a string that denotes the string S.
Output format
For each test case, print a string representing the smallest number that can be formed in a new line.
Problem statement
You are given a grid of size N×M where the top left square has coordinate (1,1) and bottom right
square has coordinate (N,M).You are given a coordinate (X,Y) and you have rectangular bars of size
L×B. You are dropping these bars randomly at any place on the grid (the bar should be inside the
boundaries of the grid) one after the other, until one falls on (X,Y).Calculate the expected number of
bars required to do so.
Input format First line: T (number of test cases)For each test case: Six space-separated integers N, M,
L, B, X, and Y
Output formatPrint the expected number of bars correctly up to exactly 6 places after decimal.
Problem statement
You are given an array A of integers of size N.
A triplet (i, j, k) is said to be amazing if these two conditions hold:
1. i<j<k
2. Ai<Aj<Ak
The value of an amazing triplet (i, j, k)=Ai+(Aj×Ak).
DSA PROBLEMS
Find the maximum value among all possible values of amazing triplets.
Input format
Output format
For each test case, print the maximum value among all possible values of amazing triplets. If there are
no amazing triplets, then print -1.
Problem statement
Task
You have to tell the minimum length subarray whose sum is divisible by K. If there is no
such subarray, output -1.
Notes:
Example
Assumption
• N=3
• K=6
• arr = [1, 3, 3]
Approach
Subarray [1]: Sum = 1 Subarray [3]: Sum = 3 Subarray [3]: Sum = 3 Subarray [1, 3]: Sum = 4
Subarray [3, 3]: Sum = 6 (divisible by K) Subarray [1, 3, 3]: Sum = 7
Function Description
DSA PROBLEMS
Complete the Solution function provided in the editor. The function takes the following three
parameters and returns the minimum length subarray whose sum is divisible by K. If there is no
such subarray, it returns -1.
Input Format
Note: This is the input format that you must use to provide custom input (available above
the Compile and Test button).
• The first line contains an integer T denoting the number of test cases. T also denotes the
number of times you have to run the Solution function on a different set of inputs.
• For each test case:
o The first line of each test case contains two space-separated integers N and K.
o The second line of each test case contains N space-separated integers denoting the
array arr.
Output Format
For each test case, print the minimum length subarray whose sum is divisible by K (If there is no
such subarray, print -1) in a new line.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
You are given an integer N denoting an N×N matrix. Initially, each cell of the matrix is empty. You
are given K tasks. In each task, you are given a cell (i,j) where cell (i,j) represents the ith row and jth
column of the given matrix.
You have to perform each task sequentially in the given order. Each task is described in cell (i,j). For
each task, you have to place X in each cell of row i and each cell column j. After you complete each
task, you are required to print the number of empty cells in the matrix.
Input format
• The first line contains two space-separated integers N and K where N is the number of rows
and columns in the given matrix and K is the number of tasks respectively.
• Next K lines contain two space-separated integers i and j.
Output format
Print K space-separated integers denoting the number of empty cells in the matrix.
Problem statement
You can perform the following operation any number of times. Pick any two elements of array B and
swap them.
Task
You are required to find the minimum value of |A1-B1| + |A2-B2| + ... + |AN-BN|.
Notes
Example
Assumption
• N=2
• A = [1, 4]
• B = [4, 2]
Approach
Function description
Complete the solve function provided in the editor. This function takes the following 3 parameters and
returns the answer.
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
• The first line contains a single integer T that denotes the number of test cases. T also denotes
the number of times you have to run the solve function on a different set of inputs.
• For each test case :
o The first line contains an integer denoting N the size of arrays.
o The next line contains N space separated integers denoting array A.
o The next line contains N space separated integers denoting array B.
DSA PROBLEMS
Output format
For each test case, print an integer denoting the minimum answer in a new line.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
Two players A and B play a game turn by turn in which one player can have either of the following
moves in his turn:
Task
Predict the winner of the game if both play optimally and A to start's the game. The game may end in
a draw if the game may go on forever.
Note
• 'a', 'e', 'i', 'o' ,'u' are vowels and others are consonants in lowercase alphabets.
Example
Assumptions
• N=2
• String S = "bc"
Approach
In 1st turn A will remove a consonant either 'b' or 'c'. In the 2nd turn, B will remove the
remaining consonant left. In 3rd turn, nothing is left so A loses the game.
Function description
Complete the solve function provided in the editor. This function takes the following 2 parameters and
returns the winner of the game if both play optimally:
Input:
DSA PROBLEMS
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
• The first line contains an integer T denoting the number of test cases. T also denotes the
number of times you have to run the solve function on a different set of inputs.
• For each test case:
o The first line consists of a single integer denoting the value of N.
o The second line consists of the string S.
Output
For each test case, print a single character A or B denoting the winner of the game. If there is a draw
print D in a new line.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
We're given an array of n integers. We can choose any subarray and perform the following operation:
We have to find the longest subarray we can make elements equal to and also the cost of the subarray
should be less than or equal to k.
Input Format:
Output Format:
For each test case, in a single line, print an integer of maximum length of subarray whose elements
can be made equal in the given cost.
Problem statement
Given an array A of N positive integers. For each array element A[i] (1≤i≤N), find the number of
A[j] (i<j) such that gcd(A[i],A[j])=G.
Input format The first line consists of a single integer denoting T - the number of test cases. For each
test case: The first line consists of two single space-separated integers denoting the value of N, G.
The second line consists of N single space-separated positive integers denoting the array A.
DSA PROBLEMS
Output format Print N single space-separated integers where the ith integer denotes the number of
elements A[j](i<j), such that gcd(A[i],A[j])=G
Problem statement
For a string S and an array of characters P of size N. A string S' is generated by removing all
characters from S that is in the given array P without changing the order of characters.
Task
You are given a string T and a set of characters P. You have to find the string S for which T can be
generated. If no such string is possible, print -1.
Note:
1. String Concatenation is the operation of joining character strings end-to-end. For example, the
concatenation of "Hacker" and "Earth" is "HackerEarth".
Example
Assumption
• T = "abcdab"
• N=2
• P = {c, d}
Approach
You will remove all the characters from the array P = {c, d} from S to generate S'.
S' = "ab"
Function description
Complete the solve function provided in the editor. This function takes the following three parameters
and returns the answer.
• T: Represents a string.
• N: Represents an integer denoting the size of array P.
• P: Represents an array of characters of size N.
Input format
DSA PROBLEMS
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
Output format
This question has code snippets for C, CPP, Java, and Python.
Problem statement
• N: An integer
• K: An integer
• A: An array of non-negative integers
Task
You need to determine the maximum length of subsequence such that the bitwise xor of every
consecutive element is equal to K.
If there exists no such subsequence of length greater than or equal to 2, you should output 0.
Note:
• A subsequence is a sequence that can be derived from the given sequence by deleting zero or
more elements without changing the order of the remaining elements.
Example
Assumption
• N=3
• K=1
• A = [1, 0, 1]
Approach
The maximum length subsequence having xor of consecutive elements as 1 is [1, 0, 1].
Function description
Complete the solve function provided in the editor. This function takes the following 2 parameters and
returns the answer.
DSA PROBLEMS
• N: Represents the size of array A.
• K: Represents the given integer.
• A: Represents the array of a given integers
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
• The first line contains a single integer T that denotes the number of test cases. T also denotes
the number of times you have to run the solve function on a different set of inputs.
• For each test cases:
o The first line contains 2 space-separated integers N and K denoting the length of array
A and the required value of bitwise xor of every consecutive element in the
subsequence respectively.
o The next line contains N space-separated integers denoting the input array A.
Output format
For each test case, print the maximum length of the subsequence of A in a new line.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
You are given an integer N and a two-dimensional String array info of size N. Each row of 2D
array info is of size 2. info array represents the scoreboard of a Gully Cricket Match. The first element
contains the bowler's name who bowled and the second element represents the event (runs, wicket, or
wide) that took place on that ball.
1. There is no such thing as a "No Ball". Only a Wicket (W), runs (0,1,2,3,4,6), or a Wide (WD)
can take place in one ball.
2. There are only 5 players playing, so total number of distinct bowlers cannot exceed 5.
3. Each over is of 6 valid balls (note that Wide ball is not valid), and if and only if a bowler
complete 6 valid balls, any other player comes to bowl.
4. The game is infinte. After one player gets out, any other player who does not have to bowl
next ball comes out to bat in sequence.
You have to print "YES" or "NO" depending on whether the information in the info scoreboard is
according to the rules above or not.Note: The match could still be in progress while you were given
this information, so it is possible that the last over can have less than 6 balls.
Example
Assumption
• T=1
• N=2
• info = {{"bob", "1"}, {"bob", "2"}}
DSA PROBLEMS
Approach
Function description
Complete the solve function provided in the editor. This function takes the following two parameters
and returns the answer.
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
• The first line contains a single integer T which denotes the number of test cases. T also
specifies the number of times you have to run the solve function on a different set of inputs.
• The first line contains an integer N denoting the size of array info.
• The next N lines contain two space-separated strings denoting the array info.
Output format
For each test case, print the answer ("YES" or "NO" in a new line)
This question has code snippets for C, CPP, Java, and Python.
Problem statement
You are given a new speaker. The speaker contains three circular knobs and they are used to adjust
the following:
• Volume
• Bass
• Treble
Initially, the pointers on the knobs are in random angles and they are measured from the
positive x axis. You want to rotate these knobs in any direction in such a way that all three pointers
are in the same direction. In other words, the pointers of the knobs must make the same angle from the
positive x axis.
Note: The knobs can be rotated either in the clockwise or anticlockwise direction.
DSA PROBLEMS
Your task is to determine the minimum sum of the degrees that the pointers must be rotated such that
all the pointers are pointing in the same direction.
Input format
• The first line contains an integer T denoting the number of test cases.
• The next line contains three space-separated integral angles measured in degrees.
Output format
For each test case, print the minimum sum of the degrees that the pointers must be rotated such that
all the pointers are pointing in the same direction. Note: The answer to each test case must be in a
new line.
Problem statement
James visits a restaurant, looks at the menu, and realizes that there is no price on it. Since he wanted
to know the prices before he orders, he looked up the restaurant online and found n different versions
of the menu. He knew from experience that usually the menu which has the maximum number of
items that have the maximum price on that item between the menus is the most updated one and if
there are multiple menus with that condition the one with the maximum average price is the most
updated one. Help him find the most updated menu.
In other words, a price on an item is good if it is the maximum price on that item among all menus,
and a menu is the most updated one if it has the maximum number of good prices on it. If there are
multiple menus with the maximum number of good prices, the menu with the higher price average is
the most updated one.
Input format
• The first line contains integers n and m that denote the number of menus and the number of
items on each menu respectively.
• The next n line each contains m integers represented as Aij, the jth price on the ith menu.
Output format
Print a single number denoting the number of the most updated menu.
Problem statement
You are given a number N. Your task is to find if there exist two distinct natural numbers such that
their bitwise XOR is N and bitwise AND is 0. If such numbers exist, then print 1. Otherwise, print 0.
Input format
Output format
DSA PROBLEMS
For each test case, print the answer.
Problem statement
You have N boxes numbered 1 through N and K candies numbered 1 through K. You put the candies
in the boxes in the following order:
Find the index of the box where you put the K-th candy.
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
• Each test case consists of a single line containing two integers N, and K.
Output format
For each test case, print the index of the box where you put the K-th candy.
Constraints
1≤T≤1052≤N≤1091≤K≤109
Problem statement
You are given an array A containing 2⋅N inetegers. You want to obtain exactly N even integers in the
array. Is it possible to achieve the goal using the following operation any number of times(possibly
zero) ?
• Choose two distinct indices i,j(1≤i,j≤N,i≠j) such that Ai is an even integer, then
set Ai=Ai2,Aj=Aj⋅2
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
DSA PROBLEMS
• For each test case:
o The first line contains an integer N where 2⋅N denotes size of array A.
o The second line contains 2⋅N space-separated integers A1,A2,…,A2⋅N - denoting the
elements of A.
Output format
For each test case, print "YES" (without quotes) if it is possible to achieve the goal and "NO" (without
quotes) otherwise.
Problem statement
You are given array A, B each containing N inetegers. You have to create array C of N inetegers
where Ci=Ai&Bi (Here, & denotes the Bitwise AND operation). You can shuffle elements
of A, B however you want. Find maximum value of C1&C2&…&CN.
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
• For each test case:
o The first line contains an integer N denotes size of array A, B.
o The next line contains N space-separated integers A1,A2,…,AN - denoting the
elements of A.
o The next line contains N space-separated integers B1,B2,…,BN - denoting the
elements of B.
Output format
For each test case, print maximum value of C1&C2&…&CN in a separate line.
Problem statement
You are given a list S that contains strings. Note: Each string in the list consists of only lowercase
Latin letters.
The score of any string P is represented as follows:
• The sum of the number of strings in S in which P occurs as a substring and the number of
strings in S in which P occurs as a prefix.
Example
If S = ['abc', 'abab', 'aba'] and P = 'ab', then score of P = 3. This is because (P occurs in all the strings)
+ 3 (P occurs in 'abc', 'abab', and 'aba' as a prefix) = 6.
Note: If P occurs as a prefix in any of the strings, then it will also contribute to the substring.
Your task is to determine the maximum score possible of any string provided.
Input format
Output format
For each test case, print the maximum score possible in a new line.
Problem statement
You are given an array A of n integers. Alice has to put all the integers in some bags. The bags can
accommodate infinitely many integers. An integer Ai will go in the bag number x, where x is
the largest prime factor of Ai. After storing all the integers in the respective bags, the bags
automatically add up all the integers stored in it. You have to tell how many bags Alice has to use and
the number of the bag which has the highest valued integer stored in it. If more than one bags has the
same integer stored, print the bag with the minimum number.
Input Format
Output Format
For each test case, print two space-separated integers, the total number of bags Alice has to use and
the number of the bag which has the highest valued integer stored in it.
Problem statement
In a cricket tournament, n matches are supposed to be played between Bob and James.
You are given two arrays A and B. Array A represents the energy levels of Bob and array B
represents the energy levels of James. The size of both the arrays is the same as n.
Now, n matches will be played between Bob and James, and for the ith match the energy level at ith
index is compared of both. The winner of the ith match will be the one whose energy level at that
index is higher.
If Bob wins in the ith match, then the energy level difference (between the ith level of both) will be
added to his score else his score remains the same i.e., on losing, Bob's points will not decrease.
Bob wants to maximize his score by changing his energy levels, that is, Bob can make a permutation
of his array.
Input format
• The first line of each test case contains an integer n denoting the number of matches.
• Each of the next two lines contains n integers representing energy levels.
Output format
Problem statement
You are given two arrays each of size n, a and b consisting of the first n positive integers each exactly
once, that is, they are permutations.
Your task is to find the minimum time required to make both the arrays empty. The following two
types of operations can be performed any number of times each taking 1 second:
• In the first operation, you are allowed to rotate the first array clockwise.
• In the second operation, when the first element of both the arrays is the same, they are
removed from both the arrays and the process continues.
Input format
• The first line contains an integer n, denoting the size of the array.
• The second line contains the elements of array a.
• The third line contains the elements of array b.
Output format
Print the total time taken required to empty both the array.
Problem statement
You are given an array A which consists of N elements. Each element of array is either zero or one.
Your task is to convert the given array into a good array with a minimum cost.
Good array: In a good array, select any subarray of even length M and the sum of elements in the
subarray will be M/2.
In order to transform an array to good array, you can perform the following two operations as many
times as you want:
1. Choose any index 1≤i≤N and if it is currently 0, then convert it to 1 for X coins.
2. Choose any index 1≤i≤N and if it is currently 1, then convert it to 0 for Y coins.
Input format
Output format
For each test case, print one line denoting the minimum cost to transform array A into a good array.
Problem statement
DSA PROBLEMS
Given an array A of N integers and an integer K. You can perform the following operation any number
of times on the given array :
Task
Your task is to count minimum number of operations required such that following conditions are met:
If the above conditions cannot be met after any number of operations, return -1.
Note:
Example
Assumptions:
• N=4
• A = [1, 4, 4, 1]
• K=5
Approach:
Note, there can be other possible selections of x and i, at each step but the minimum number of
operations remains the same.
Function description
Complete the minUpdates function provided in the editor. This function takes the following
3 parameters and returns an integer.
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
• The first line contains a single integer T, which denotes the number of test cases. T also
specifies the number of times you have to run the minUpdates function on a different set of
inputs.
• For each test case:-
o First line contains an integer N.
o Next line contains N space-separated integers denoting the elements of array A.
o Next line contains an integer K.
Output Format
For each test case in a new line, print an integer denoting the minimum number of operations required
or print -1, if the conditions cannot be met.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
You are given an integer n and a string s of size n composed of lower case english letters.
• In one operation, you have to choose any character in the string s, then delete the first
character to the left of the chosen character that is equal to the chosen character (if there
exists) and delete the first character to the right of the chosen character that is equal to the
chosen character (if there exists).
• Note that in one operation, the length of the string s is reduced by a maximum of two
characters.
Task
Find the minimum number of operations that need to be performed to minimize the length of the
string s.
Note:
Example
Assumptions :
• n=4
• s = "abaa" (without quotes)
DSA PROBLEMS
Approach:
• Choose 3rd character in the string for 1st operation, this will delete the 1st character
and 4th character in string s, the string becomes "ba".
• The length of the string s can not be reduced further.
• Hence, minimum number of operations needed to reduce the length of the string s to a
minimum is 1.
Function Description
Complete the Minimum_Operations function provided in the editor. This function takes the following
2 parameters and returns the required answer:
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile
and Test button).
• The first line contains a single integer T, which denotes the number of test cases. T also
specifies the number of times you have to run the Minimum_Operations function on a
different set of inputs.
• For each test case:
o First line contains an integer n.
o Second line contains a string s.
Output format
For each test case in a new line, print the minimum number of operations required to minimize the
length of string s.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
You need to form a new string P that follows the following conditions:
Task
Example
DSA PROBLEMS
Assumptions
• N=3
• K=4
• S = bca
Approach
Function Description
Complete the solve function provided in the editor. This function takes the following 3 parameters and
returns the valid string:
Input Format
Note: This is the input format you must use to provide custom input (available above the Compile and
Test button).
• The first line contains a single integer T denoting the number of test cases.
• For each test case:
o The first line contains a single integer N denoting the size of string S.
o The second line contains a single integer K denoting the number of new characters
added in string S.
o The third line contains the string S.
Output Format
For each test case in a separate line, output a string P that satisfies all the given conditions.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
Problem:
A tree is called K−Good(K≥1) if all the nodes of the tree have exactly K children except the leaves.
For given count of leaves M(M≥2), find the number of possible values of K.
Input Format:
• The first line of the input contains a single integer T - the number of test cases. The
description of T test cases follows.
• The first and only line of each test case contains a single integer M.
DSA PROBLEMS
Output Format:
• For each test case, print the number of possible values of K for given count of leaves M.
Problem statement
You are given three integers N, A, and B. You have another integer X = 0. You can apply the
following move (1-indexed) any number of times:
Find the minimum number of moves required so that the value of X becomes greater than or equal to
N or print -1 if it is impossible to do so.
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
• Each test case consists of a single line containing three integers N, A, and B.
Output format For each test case, print the minimum number of moves required so that the value of X
becomes greater than or equal to N or print -1 otherwise.
Problem statement
The weight of an array is defined as the difference between the maximum and minimum element of
the array. For example, the weight of the array [3, 7, 1, 2] is (7 - 1) = 6, the weight of the array [2] is
(2 - 2) = 0, the weight of an empty array is 0. You are given an array A of length N. You have to
divide the elements of A into two subsequences S1, and S2 (one of S1, S2 can be empty) such that:
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
• For each test case:
o The first line contains N denoting the size of array A.
o The second line contains N space-separated integers A[1], A[2], ....., A[N] - denoting
the elements of A.
Output format For each test case, print the maximum possible sum of weights of the two
subsequences.
DSA PROBLEMS
40. Maximum Inequality
Problem statement
Input format
• The first line contains T denoting the number of test cases. The description of T test cases is
as follows:
• For each test case:
o The first line contains N denoting the size of string S.
o The second line contains the string S.
Output formatFor each test case, print the maximum possible value of F(S1)+F(S2) in a separate line.
.
Problem statement
Pawan has a finite number of friends, each friend has a unique non-negative number associated with
him. (0, 1, 2..) Pawan plans to host a party and has to send notifications to his friends. A friend attends
the party if he receives at least 1 notification from Pawan. To send notifications, Pawan buys data
plans Pawan can send notifications using data plan X in the following way: Let's say Data plan X
costs Y. Then a friend associated with the number i will receive a notification if and only if the ith bit
in Y is set. Now Pawan wonders if he can cut costs. Let him know the maximum cost that he can cut
by removing at most 1 data plan and still being able to invite all the friends he could invite earlier
Input
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤100) — the
number of test cases. Description of the test cases follows.
The first line of each test case contains one integer N - the number of data plans
The second line of each test case contains N integers a1,a2,…,an — the cost of data plans array.
It is guaranteed that the sum of N over all test cases does not exceed 105
Output
For each test case, print a line containing a single integer – the maximum possible money that Pawan
can save (print 0 if no data plan can be removed)
DSA PROBLEMS
Problem statement
A disease has spread in your country that does not have any vaccines. The Health Department of your
country is working to create its vaccine. The limitation to create vaccines is the cost that is required to
create an effective vaccine.
The scientists of the department have created 9 samples that are numbered from 1 to 9. Each vaccine
is associated with a cost. The cost is denoted by an array X where Xi denotes the cost of the ith
sample. The Health Department has been assigned N units of money to spend. This amount of money
must be used to create stocks of vaccines that can be distributed among different hospitals that are
situated at different locations in your country.
You are required to find the largest number of stocks possible that can be formed by using the N units
of money. If it is not possible to form such a stock of vaccines, then print −1.
Example
• Largest number of stocks that can be formed is 777. Altough, 9, 776 can also be formed
but 777 is largest.
Function Description
Complete the Largest_Number function provided in the editor. The function takes the following 2
parameters and returns the largest number of stocks.
Input format
Output format
Print the largest number of stocks that can be formed by using the N units of money. If it is not
possible to form such a stock, then print −1. For each test case, print the answer in a new line.
Problem statement
You are given an integer K and a string S that contains lowercase English alphabets of length N,
where N is always a multiple of K. You are required to convert the given string to a special string.
DSA PROBLEMS
A special string is a string that follows a repeating pattern of K characters till the total length of
the string is N. For example, if S=abcdef and K=2, then the special string can be ababab or bcbcbc or
any other string which follows the given criteria.
In order to convert the string, you can perform an operation on the string. Considering lowercase
English alphabet set as circular, you can rotate the character of the string on either side in one
operation. For example, for a, you can rotate it to b or z. You can perform this operation any number
of times on any character of the string. Performing this operation once costs you 1 unit.
Your task is to convert the given string to special string in the minimum cost.
Input format
Output format
For each test case, print the minimum cost in a new line.
Problem statement
Count all numbers from the range L to R (inclusive) that have exactly one zero in its binary
representation.
Input Format: The only line of the input contains two integers L and R.
Problem statement
You are given an integer array A of length N. For each K=1 to N, find the sum of all subarrays of
array A of length K. Since these sums can be very large, output the sums modulo 998244353.
An array B is a subarray of an array A if B can be obtained from A by deletion of several (possibly,
zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In
particular, an array is a subarray of itself.
Input format:
Output format:
For each test case(in a separate line), output N integers, where ith integer denotes the
sum(modulo 998244353) of all subarrays of array A of length i.
Problem statement
DSA PROBLEMS
You are given an array arr of length N, representing money with N customers. The i-th customer has
arri rupees. There is another array cost of length M, representing the cost of M items in a store. The j-
th item costs costj rupees. The store offers some discount money d which any customer can use to buy
an item. Each customer can buy only one item and cannot use his money to buy an item for any other
customer.
Task
Determine the maximum number of customers who can buy an item and the minimum total sum of
customers’ money spent to achieve maximum customers with an item.
Notes
Example
Assumptions
• N=3
• M=2
• d = 100
• arr = [30, 40, 50]
• cost = [80, 110]
Approach
• 2nd customer buys 1st item worth 80 rupees using 40 rupees of discount money.
• Now the discount money left is 100 - 40 = 60.
• 3rd customer buys 2nd item worth 110 rupees using 60 rupees of discount money.
• The total number of customers who can buy an item is 2 with their total sum of money spent
equals 90.
Function Description
Complete the function maxCustomers provided in the editor. The function takes the following
parameters and returns an array consisting of 2 integers, the maximum number of customers with
items, and the minimum total customers' money spent:
Input format
DSA PROBLEMS
Note: This is the input format you must use to provide custom input (available above the Compile and
Test button).
• The first line of the input contains three integers N, M, and d, denoting the number of
customers, the number of items, and the discount money.
• The second line of the input contains N integers arr1, arr2, …, arrN, where arri is the money
with the i-th customer.
• The third line of the input contains M integers cost1, cost2, …, costM, where costj is the price
of the j-th item.
Output format
Print two space-separated numbers, the maximum number of customers who can buy an item and the
minimum total customers' money needed for buying the maximum number of items.
This question has code snippets for C, CPP, Java, and Python.
Problem statement
You are given a number. You want to compute the sum of all possible resulting numbers after
applying the magic operation. In a magic operation, you pick a non-empty subsequence from this
number, then take these digits out of the number. The remaining digits then fill in the gap, and the
number after the magic operation is the integer that is left.
For eg: Let's say the number is 12345. One of the subsequences of this number is 24. So after
removing this subsequence, the resulting number is 135. Leading zeros are allowed in the resulting
number (number after applying the magic operation). When all digits are removed, the number is
considered to be 0.
Input Format:
• The first line contains one integer T − the number of test cases. Then T test cases follow.
• The first and only line of each test case contains a single integer N.
Output Format:
For each test case output in a seperate line: The sum of all possible resulting numbers after applying
the magic operation modulo (109+7).
Problem statement
Given two positive integers m and n which are the height and width of a 0-indexed 2D-array board.
Find the number of Knight Traversals. A Knight Traversal includes a knight (of a chess game) starts
at (0,0) of the board and visits every cell of the board exactly once. The starting cell (0,0) is
considered visited and you shouldn't visit it again.
DSA PROBLEMS
Note that a knight can move from cell (r1, c1) to cell (r2, c2) if 0 <= r2 <= m - 1 and 0 <= c2 <= n - 1
and min(abs(r1 - r2), abs(c1 - c2)) = 1 and max(abs(r1 - r2), abs(c1 - c2)) = 2.
Input constraints:
1 <= m, n <= 5
Input format:
Output format:
Problem statement
Find the number of square grids having only positive integers in an m * n grid of integers.
Input constraints:
Input format:
Output format:
A number indicating the number of square grids having only positive integers.
Problem statement
Your college professor has given you a task to pass the semester. The task is as follows:
DSA PROBLEMS
You are given 2 integers L and R which denotes a range. Find a number in between(L and R
inclusive) which has the minimum number of set bits count. If there are multiple such numbers, print
the minimum number. Note: 14 can be written as 1110 in binary and its set bit count is 3.
Input
Output
A single integer, denoting the number with minimum set bit count.
Problem statement
You have given a n×n matrix containing empty spaces or walls. You have a team of 2 players where
the position of each player is given. There are 2 gold mines on the map. Your aim is to pick gold from
both these mines.
You are required to find the minimum time required to pick gold from both gold mines. In one
second, they all simultaneously can move in any of the four directions and any player can pick any
number of gold including 0.
Input format
• The first line contains t denoting the total number of test cases.
• The first line of each test case contains an integer n denoting the number of rows and columns
in the matrix.
• The next n lines of each test case contain n characters denoting the rows of the matrix.
o . denotes an empty cell
o # # denotes a wall
o ^ ^ denotes a player
o ∗ denotes a gold mine
Output format
• The first line contains "Yes" (without quotes) if it is possible to collect both the mines or
"No" (without quotes) if you are unable to pick both the mines.
• The second line contains the minimum time required to pick both the gold mines.
52.Problem statement
Bob sells burgers with three different combinations, B1, B2 and B3. There are total X, Y, and Z
burgers with combinations, B1, B2, and B3 respectively. Each burger has an integer value called
deliciousness as follows: