coding QB
coding QB
1.Given an array arr[] of size N-1 with integers in the range of [1, N], the task is to find the missing
number from the first N integers.
Note: There are no duplicates in the list.
Examples:
Input: arr[] = {1, 2, 4, 6, 3, 7, 8}, N = 8
Output: 5
Explanation: The missing number between 1 to 8 is 5
Input: arr[] = {1, 2, 3, 5}, N = 5
Output: 4
Explanation: The missing number between 1 to 5 is 4
#include <stdio.h>
temp[i] = 0;
}
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
temp[arr[i] - 1] = 1;
int ans;
if (temp[i] == 0)
ans = i + 1;
printf("%d", ans);
/* Driver code */
int main()
int arr[] = { 1, 3, 7, 5, 6, 2 };
findMissing(arr, n);
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Output
4
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2 (Using summation of first N natural numbers) : The idea behind the approach is to use
the summation of the first N numbers.
Find the sum of the numbers in the range [1, N] using the formula N * (N+1)/2. Now find the sum of
all the elements in the array and subtract it from the sum of the first N natural numbers. This will give
the value of the missing element.
#include <stdio.h>
int i, total;
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
total = (n + 1) * (n + 2) / 2;
total -= a[i];
return total;
// Driver code
void main()
int arr[] = { 1, 2, 3, 5 };
// Function call
printf("%d", miss);
Output
4
Time Complexity: O(N)
Auxiliary Space: O(1)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
2.Given an array of integers arr[] of size N and an integer, the task is to rotate the array elements to
the left by d positions.
Examples:
Input:
arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2
Output: 3 4 5 6 7 1 2
Input: arr[] = {3, 4, 5, 6, 7, 1, 2}, d=2
Output: 5 6 7 1 2 3 4
Approach 1 (Using temp array): This problem can be solved using the below idea:
After rotating d positions to the left, the first d elements become the last d elements of the array
First store the elements from index d to N-1 into the temp array.
Then store the first d elements of the original array into the temp array.
Copy back the elements of the temp array into the original array
Illustration:
Suppose the give array is arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2.
First Step:
=> Store the elements from 2nd index to the last.
=> temp[] = [3, 4, 5, 6, 7]
Second Step:
=> Now store the first 2 elements into the temp[] array.
=> temp[] = [3, 4, 5, 6, 7, 1, 2]
Third Steps:
=> Copy the elements of the temp[] array into the original array.
=> arr[] = temp[] So arr[] = [3, 4, 5, 6, 7, 1, 2]
#include <bits/stdc++.h>
int temp[n];
// of temp[]
int k = 0;
temp[k] = arr[i];
k++;
// into temp
temp[k] = arr[i];
k++;
arr[i] = temp[i];
// Driver code
int main()
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int d = 2;
// Function calling
Rotate(arr, d, N);
PrintTheArray(arr, N);
return 0;
Output
3456712
3.Find the majority element in the array. A majority element in an array A[] of size n is an element
that appears more than n/2 times (and hence there is at most one such element).
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Naive Approach:
The basic solution is to have two loops and keep track of the maximum count for all different
elements. If the maximum count becomes greater than n/2 then break the loops and return the element
having the maximum count. If the maximum count doesn’t become more than n/2 then the majority
element doesn’t exist.
Illustration:
arr[] = {3, 4, 3, 2, 4, 4, 4, 4}, n = 8
For i = 0:
count = 0
Loop over the array, whenever an element is equal to arr[i] (is 3), increment count
count of arr[i] is 2, which is less than n/2, hence it can’t be majority element.
For i = 1:
count = 0
Loop over the array, whenever an element is equal to arr[i] (is 4), increment count
count of arr[i] is 5, which is greater than n/2 (i.e 4), hence it will be majority element.
Hence, 4 is the majority element.
#include <stdio.h>
// in an array
int maxCount = 0;
int count = 0;
if (arr[i] == arr[j])
count++;
maxCount = count;
index = i;
}
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
if (maxCount > n / 2)
printf("%d\n", arr[index]);
else
// Driver code
int main()
int arr[] = { 1, 1, 2, 1, 3, 5, 1 };
// Function calling
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
findMajority(arr, n);
return 0;
Output
1
4.Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Example:
Input matrix : 0 1 1 1
0011
1 1 1 1 // this row has maximum 1s
0000
Output: 2
A simple method is to do a row-wise traversal of the matrix, count the number of 1s in each row, and
compare the count with the max. Finally, return the index of the row with a maximum of 1s. The time
complexity of this method is O(m*n) where m is the number of rows and n is the number of columns in
the matrix.
#include<stdio.h>
#include<stdbool.h>
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
#define R 4
#define C 4
int indexOfRowWithMax1s = -1 ;
int maxCount = 0 ;
int count = 0 ;
if(mat[i][j] == 1){
count++ ;
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
maxCount = count ;
indexOfRowWithMax1s = i ;
return indexOfRowWithMax1s ;
// Driver Code
int main()
{0, 1, 1, 1},
{1, 1, 1, 1},
{0, 0, 0, 0}};
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
return 0;
Output
Index of row with maximum 1s is 2
5.Given an array arr[] and an integer K, the task is to find and maximize the sum of at most
K elements in the Array by taking only corner elements.
A corner element is an element from the start of the array or from the end of the array.
Examples:
Input: N = 8, arr[] = {6, -1, 14, -15, 2, 1, 2, -5}, K = 4
Output: 19
Explanation:
Here the optimal choice is to pick three cards from the beginning. After that if we want to pick the next
card, our points will decrease. So maximum points is arr[0] + arr[1] + arr[2] = 19.
Input : N = 5, arr[] = {-2, -1, -6, -3, 1}, K = 2
Output : 1
Here optimal choice is to pick last card. So maximum possible points is arr[4] = 1. Any further
selection will reduce the value.
Naive Approach:
To solve the problem mentioned above we will use Recursion. As we can only take a start or end index
value hence initialize two variables and take at most K steps and return the maximum sum among all
the possible combinations. Update the maximum sum only if it is greater than the previous sum
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
points, max_points):
if (K == 0):
return max_points
points_start, max_points),
points_end, max_points))
# Driver code
if __name__ == "__main__":
N = len(arr)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
K=2
points = 0
max_points = 0
# Beginning index
start = 0
# end index
end = N - 1
print(maxPointCount(arr, K, start,
end, points,
max_points))
Output:
1
6.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
7.Problem Statement:
While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There
are n monsters you’ll need to defeat in this quest. Each monster i is described with two integer numbers
– poweri and bonusi. To defeat this monster, you’ll need at least poweri experience points. If you try
fighting this monster without having enough experience points, you lose immediately. You will also
gain bonusi experience points if you defeat this monster. You can defeat monsters in any order.
The quest turned out to be very hard – you try to defeat the monsters but keep losing repeatedly. Your
friend told you that this quest is impossible to complete. Knowing that, you’re interested, what is the
Input:
The first line contains an integer, n, denoting the number of monsters. The next line contains an
integer, e, denoting your initial experience.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, poweri, which
represents power of the corresponding monster.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonusi, which
represents bonus for defeating the corresponding monster.
8.Problem Statement:
Your birthday is coming soon and one of your friends, Alex, is thinking about a gift for you. He knows
He selected two numbers, N and K and decided to write down on paper all integer arrays of length K (in
form a[1], a[2], …, a[K]), where every number a[i] is in range from 1 to N, and, moreover, a[i+1] is
divisible by a[i] (where 1 < i <= K), and give you this paper as a birthday present.
Alex is very patient, so he managed to do this. Now you’re wondering, how many different arrays are
Input:
The first line contains an integer, n, denoting the maximum possible value in the arrays.
The next line contains an integer, k, denoting the length of the arrays.
9.Problem Statement:
You have an array A of N integers A1 A2 .. An. Find the longest increasing subsequence Ai1 Ai2 .. Ak
For every adjacent pair of numbers of the chosen subsequence Ai[x] and Ai[x+1] (1 < x < k), the
Input:
Sample cases:
15
12
1
2 One possible subsequence is:
6
17
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
15
10.Problem Statement :
You have been given a string S of length N. The given string is a binary string which consists of only 0’s
and ‘1’s. Ugliness of a string is defined as the decimal number that this binary string represents.
Example:
“101” represents 5.
“0000” represents 0.
“01010” represents 10.
There are two types of operations that can be performed on the given string.
ugliness of the string by performing the above mentioned operations on it. Since the answer can be very
Note:
You can perform an operation only if you have enough number of coins to perform it.
After every operation the number of coins get deducted by the cost for that operation.
Input Format
The first line contains an integer, N, denoting the number of character in the string
The next line contains a string, S, denoting the the binary string
The next line contains an integer, CASH, denoting the total number of coins present initially
Next will contains an integer, A, denoting the cost to swap two characters.
Then the next line contains an integer, B, denoting the cost to flip a character.
Constraints
Sample Input 1 :
1111
Sample Output 1 :
1
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Sample Input 2:
111011
Sample Output 2:
Explanation:
First swap 0 with the most significant 1, then use flip twice first on index one and then on index two
Sample Input 3:
111011
Sample Output 3:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Explanation:
Flip the 3 most significant characters to get “000011” : the value represented by this string is 3.N
11.Problem Statement :
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
elements from array A. It is not necessary to choose consecutive elements. Khaled is interested in XOR
of all the elements he chooses. Here, XOR denotes the bitwise XOR operation.
For example:
If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2 XOR 4 XOR 8)=14.
Khaled wants to maximize the XOR of all the elements he chooses. Your task is to help khaled to find the
max XOR of a subset that he can achieve by choosing at most N/2 elements?
Input format:
Constraints
1<=N<=120
1<=A[i]<=10^6
Sample Input 1
Sample Output 1
Explanation:
N=2, A=[1,2] khaled can choose the subset[2]. The xor of the elements in the subset is 2. And the
Sample Output 2
Explanation:
N=4, A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the subset is 7, and the
12.Problem Statement :
Wael is well-known for how much he loves the bitwise XOR operation, while kaito is well known for
how much he loves to sum numbers, so their friend Resli decided to make up a problem that would enjoy
both of them. Resil wrote down an array A of length N, an integer K and he defined a new function
Can you find the integer x in the range [0,K] with the maximum Xor-sum (x) value?
Input format
Constraints
1<=N<=10^5
0<=K<=10^9
0<=A[i]<=10^9
Sample Input 1
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
989898
Sample Output 1
989898
Explanation:
Xor_sum(0)=(0^989898)=989898
Sample Input 2
Sample Output 2
14
Explanation
Xor_sum(4)=(4^1)+(4^6)+(4^3)=14.
Sample Input 3
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Sample Output 3
46
Explanation:
Xor_sum(8)=(8^7)+(8^4) +(8^0)+(8^3)=46.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
One of the first lessons IT students learn is the representation of natural numbers in the binary number
system (base 2) This system uses only two digits, 0 and 1. In everyday life we use for convenience the
decimal system (base 10) which uses ten digits, from 0 to 9. In general, we could use any
numbering system.
Computer scientists often use systems based on 8 or 16. The numbering system based on K uses K digits
with a value from 0 to K-1. Suppose a natural number M is given, written in the decimal system To
convert it to the corresponding writing in the system based on K, we successively divide M by K until we
The representation of M in the system based on K is formed by the final quotient (as first digit) and is
If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number
You made the following observation in applying the above rule of converting natural numbers to another
numbering system
In some cases in the new representation all the digits of the number are the same. For example 63
Given a number M in its decimal representation, your task is find the minimum base B such that in the
Input Format
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Constraints
1 <= M = 10^12
Sample Input 1 :
41
Sample Output 1 :
40
Explanation :
Here 41 in base 40. will be 11 so it has all digits the same, and there is no smaller base satisfying the
requirements
Sample Input 2 :
34430
Sample Output 2 :
312
Explanation :
Here 34430 in base 312 will have all digits the same and there is no smaller base satisfying the
requirements
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
14.Problem Statement :
Andy wants to go on a vacation to de-stress himself. Therefore he decides to take a trip to an island. It is
given that he has as many consecutive days as possible to rest, but he can only make one trip to the
island. Suppose that the days are numbered from 1 to N. Andy has M obligations in his schedule, which
he has already undertaken and which correspond to some specific days. This means that ith obligation is
scheduled for day Di. Andy is willing to cancel at most k of his obligations in order to take more
holidays.
Your task is to find out the maximum days of vacation Andy can take by canceling at most K of his
obligations.
Input Format
The first line contains an integer N, denoting the total number of days
The next line contains an integer M denoting the total number of obligations.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Each line i of the M subsequent lines (where 0<=i<=M) contains an integer describing Di.
Constraints
1<=N<=10^6
1<=M<=2*10^6
1<=K<=2*10^6
1<=D[i]<=10^6
Sample Input 1:
10
Sample Output 1 :
Explanation:
Here he could cancel his 3rd and 4th obligation which makes vacation length 5.
Sample input 2:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Sample Output 2:
Explanation:
Here he could not cancel any obligation since K=0, so the vacation length is 3.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
15.Problem Statement :
Today you decided to go to the gym. You currently have energy equal to E units. There are N exercises in
the gym. Each of these exercises drains Ai amount of energy from your body.
You feel tired if your energy reaches 0 or below. Calculate the minimum number of exercises you have to
perform such that you become tired. Every unique exercise can only be performed at most 2 times as
If performing all the exercises does not make you feel tired, return -1.
Input Format
E :: INTEGER
E :: 1 -> 10^5
N :: INTEGER
The next line contains an integer, N, denoting the number of exercises. N :: 1 -> 10^5
A :: INTEGER ARRAY
Each line i of the N subsequent lines (where 0 ≤ i < N) contains an integer describing the amount of
Sample Output 1 :
Sample input 2:
10
Sample Output 2:
-1
Sample input 3:
Sample Output 3:
1
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
16.Problem Statement – Write a program to calculate the fuel consumption of your truck.The program
should ask the user to enter the quantity of diesel to fill up the tank and the distance covered till the tank
goes dry.Calculate the fuel consumption and display it in the format (liters per 100 kilometers).
Convert the same result to the U.S. style of miles per gallon and display the result. If the quantity or
[Note: The US approach of fuel consumption calculation (distance / fuel) is the inverse of the European
approach (fuel / distance ). Also note that 1 kilometer is 0.6214 miles, and 1 liter is 0.2642 gallons.]
The result should be with two decimal place.To get two decimal place refer the below-mentioned print
statement :
float cost=670.23;
Sample Input 1:
20
150
Sample Output 1:
Liters/100KM
13.33
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
17.64
Explanation:
Sample Input 2:
-5
Sample Output 2:
-5 is an Invalid Input
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
17.Problem Statement – Vohra went to a movie with his friends in a Wave theatre and during break
time he bought pizzas, puffs and cool drinks. Consider the following prices :
Rs.100/pizza
Rs.20/puffs
Rs.10/cooldrink
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Sample Input 1:
Sample Output 1:
Bill Details
No of pizzas:10
No of puffs:12
No of cooldrinks:5
Total price=1290
18.Problem Statement – Ritik wants a magic board, which displays a character for a corresponding
number for his science project. Help him to develop such an application.
For example when the digits 65,66,67,68 are entered, the alphabet ABCD are to be displayed.
Sample Input 1:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Sample Output 1:
65-A
66-B
67-C
68-D
Sample Input 2:
Sample Output 2:
115-s
116-t
101-e
112-p
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Python
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
19.Problem Statement – FOE college wants to recognize the department which has succeeded in getting
the maximum number of placements for this academic year. The departments that have participated in the
recruitment drive are CSE,ECE, MECH. Help the college find the department getting maximum
placements. Check for all the possible output given in the sample snapshot
Note : If any input is negative, the output should be “Input is Invalid”. If all department has equal
number of placements, the output should be “None of the department has got the highest placement”.
Sample Input 1:
Sample Output 1:
Highest placement
CSE
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Sample Output 2:
Highest placement
ECE
MECH
Sample Input 3:
Sample Output 3:
Sample Input 4:
Sample Output 4:
Input is Invalid
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
python
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660