0% found this document useful (0 votes)
5 views

coding QB

Uploaded by

vinupriyatpc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

coding QB

Uploaded by

vinupriyatpc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

JAI SHRIRAM ENGINEERING COLLEGE

TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

Approach 1 (Using Hashing): The idea behind the following approach is


The numbers will be in the range (1, N), an array of size N can be maintained to keep record of the
elements present in the given array

 Create a temp array temp[] of size n + 1 with all initial values as 0.


 Traverse the input array arr[], and do following for each arr[i]
 if(temp[arr[i]] == 0) temp[arr[i]] = 1
 Traverse temp[] and output the array element having value as 0 (This is the missing element).
Below is the implementation of the above approach:

#include <stdio.h>

void findMissing(int arr[], int N)

int temp[N + 1];

for (int i = 0; i <= N; i++) {

temp[i] = 0;

}
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

for (int i = 0; i < N; i++) {

temp[arr[i] - 1] = 1;

int ans;

for (int i = 0; i <= N; i++) {

if (temp[i] == 0)

ans = i + 1;

printf("%d", ans);

/* Driver code */

int main()

int arr[] = { 1, 3, 7, 5, 6, 2 };

int n = sizeof(arr) / sizeof(arr[0]);

findMissing(arr, n);
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

// This code is contributed by nikhilm2302

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.

Follow the steps mentioned below to implement the idea:


 Calculate the sum of the first N natural numbers as sumtotal= N*(N+1)/2.
 Traverse the array from start to end.
 Find the sum of all the array elements.
 Print the missing number as SumTotal – sum of array
Below is the implementation of the above approach:

#include <stdio.h>

// Function to find the missing number

int getMissingNo(int a[], int n)

int i, total;
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

total = (n + 1) * (n + 2) / 2;

for (i = 0; i < n; i++)

total -= a[i];

return total;

// Driver code

void main()

int arr[] = { 1, 2, 3, 5 };

int N = sizeof(arr) / sizeof(arr[0]);

// Function call

int miss = getMissingNo(arr, N);

printf("%d", miss);

Output
4
Time Complexity: O(N)
Auxiliary Space: O(1)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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]

Follow the steps below to solve the given problem.


 Initialize a temporary array(temp[n]) of length same as the original array
 Initialize an integer(k) to keep a track of the current index
 Store the elements from the position d to n-1 in the temporary array
 Now, store 0 to d-1 elements of the original array in the temporary array
 Lastly, copy back the temporary array to the original array
Below is the implementation of the above approach :

#include <bits/stdc++.h>

using namespace std;


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

// Function to rotate array

void Rotate(int arr[], int d, int n)

// Storing rotated version of array

int temp[n];

// Keeping track of the current index

// of temp[]

int k = 0;

// Storing the n - d elements of

// array arr[] to the front of temp[]

for (int i = d; i < n; i++) {

temp[k] = arr[i];

k++;

// Storing the first d elements of array arr[]


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

// into temp

for (int i = 0; i < d; i++) {

temp[k] = arr[i];

k++;

// Copying the elements of temp[] in arr[]

// to get the final rotated array

for (int i = 0; i < n; i++) {

arr[i] = temp[i];

// Function to print elements of array

void PrintTheArray(int arr[], int n)

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

// Driver code

int main()

int arr[] = { 1, 2, 3, 4, 5, 6, 7 };

int N = sizeof(arr) / sizeof(arr[0]);

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Examples :
Input : A[]={3, 3, 4, 2, 4, 4, 2, 4, 4}
Output : 4
Explanation: The frequency of 4 is 5 which is greater than the half of the size of the array size.
Input : A[] = {3, 3, 4, 2, 4, 4, 2, 4}
Output : No Majority Element
Explanation: There is no element whose frequency is greater than the half of the size of the array size.

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.

Follow the steps below to solve the given problem:


 Create a variable to store the max count, count = 0
 Traverse through the array from start to end.
 For every element in the array run another loop to find the count of similar elements in the given
array.
 If the count is greater than the max count update the max count and store the index in another
variable.
 If the maximum count is greater than half the size of the array, print the element. Else print there is
no majority element.

#include <stdio.h>

// Function to find Majority element


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

// in an array

void findMajority(int arr[], int n)

int maxCount = 0;

int index = -1; // sentinels

for (int i = 0; i < n; i++) {

int count = 0;

for (int j = 0; j < n; j++) {

if (arr[i] == arr[j])

count++;

// update maxCount if count of

// current element is greater

if (count > maxCount) {

maxCount = count;

index = i;

}
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

// if maxCount is greater than n/2

// return the corresponding element

if (maxCount > n / 2)

printf("%d\n", arr[index]);

else

printf("No Majority Element\n");

// Driver code

int main()

int arr[] = { 1, 1, 2, 1, 3, 5, 1 };

int n = sizeof(arr) / sizeof(arr[0]);

// Function calling
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

findMajority(arr, n);

return 0;

// This code is contributed by Vaibhav Saroj.

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.

// C program to find the row with maximum number of 1s.

#include<stdio.h>

#include<stdbool.h>
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

#define R 4

#define C 4

// Function that returns index of row

// with maximum number of 1s.

int rowWithMax1s(bool mat[R][C]) {

int indexOfRowWithMax1s = -1 ;

int maxCount = 0 ;

// Visit each row.

// Count number of 1s.

/* If count is more that the maxCount then update the maxCount

and store the index of current row in indexOfRowWithMax1s variable. */

for(int i = 0 ; i < R ; i++){

int count = 0 ;

for(int j = 0 ; j < C ; j++ ){

if(mat[i][j] == 1){

count++ ;
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

if(count > maxCount){

maxCount = count ;

indexOfRowWithMax1s = i ;

return indexOfRowWithMax1s ;

// Driver Code

int main()

bool mat[R][C] = { {0, 0, 0, 1},

{0, 1, 1, 1},

{1, 1, 1, 1},

{0, 0, 0, 0}};
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

int indexOfRowWithMax1s = rowWithMax1s(mat);

printf("Index of row with maximum 1s is %d",indexOfRowWithMax1s);

return 0;

// This code is contributed by Rohit_Dwivedi

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
otherwise skip to the next possible combination. The recursive approach has exponential complexity
due to its overlapping subproblem and optimal substructure property.

# Python3 implementation to maximize sum

# of atmost K elements in array by taking

# only corner elements

# Function to return maximum points

def maxPointCount(arr, K, start, end,

points, max_points):

if (K == 0):

return max_points

# Pick the start index

points_start = points + arr[start]

# Update maximum points if necessary

max_points = max(max_points, points_start)


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

# Pick the end index

points_end = points + arr[end]

# Update maximum points if necessary

max_points = max(max_points, points_end)

# Recursive call to get max value

return max(maxPointCount(arr, K - 1, start + 1, end,

points_start, max_points),

maxPointCount(arr, K - 1, start, end - 1,

points_end, max_points))

# Driver code

if __name__ == "__main__":

arr = [ -2, -1, -6, -3, 1 ]

N = len(arr)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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))

# This code is contributed by chitranayal

Output:
1

6.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

maximum possible number of monsters you can defeat?

(Question difficulty level: Hardest)

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.

Input Output Output Description


2 2  Initial experience level is 123 points.
123  Defeat the first monster having power of 78 and bonus of 10. Experience level is now 123+10
78  Defeat the second monster.
130
10
0
3 2  Initial experience level is 100 points.
100  Defeat the second monster having power of 100 and bonus of 1. Experience level is now 100+
101  Defeat the first monster having power of 101 and bonus of 100. Experience level is now 101+
100  The third monster can’t be defeated.
304
100
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Input Output Output Description
1
524

8.Problem Statement:

Your birthday is coming soon and one of your friends, Alex, is thinking about a gift for you. He knows

that you really like integer arrays with interesting properties.

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

written down on this paper?


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Since the answer can be really large, print it modulo 10000.

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.

Input Output Output Description


2 2 The required length is 1, so there are only two possible arrays: [1] and [2].
1
2 3 All possible arrays are [1, 1], [1, 2], [2, 2].
2 [2, 1] is invalid because 1 is not divisible by 2.
3 5 All possible arrays are [1, 1], [1, 2], [1, 3], [2, 2], [3, 3].
2
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

9.Problem Statement:

You have an array A of N integers A1 A2 .. An. Find the longest increasing subsequence Ai1 Ai2 .. Ak

(1 <= k <= N) that satisfies the following condition:

For every adjacent pair of numbers of the chosen subsequence Ai[x] and Ai[x+1] (1 < x < k), the

expression( Ai[x] & Ai[x+1] ) * 2 < ( Ai[x] | Ai[x+1] ) is true

Note: ‘&’ is the bitwise AND operation, ‘ | ‘ is the bit-wise OR operation

Input:

1. The first line contains an integer, N, denoting the number of elements in A.


2. Each line i of the N subsequent lines (where 0 ≤ i < N) contains an integer describing Ai.

Sample cases:

Input Output Output D


2 One possible subsequence is: 5 12
5

15

12

1
2 One possible subsequence is:
6

17
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Input Output Output D

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.

 Swap any two characters by paying a cost of A coins.


 Flip any character by paying a cost of B coins
 flipping a character means converting a ‘1’to a ‘0’or converting a ‘0’ to a ‘1’.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Initially, you have been given coins equal to the value defined in CASH. Your task is to minimize the

ugliness of the string by performing the above mentioned operations on it. Since the answer can be very

large, return the answer modulo 10^9+7.

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

 1 <= N <= 10^5


 1< len(S)<= 10^5
 1<=CASH <=10^5
 1<=A<=10^5
 1<=B<=10^5

Sample Input 1 :

1111

Sample Output 1 :

1
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Explanation:

3 flips can be used to create “0001” which represents 1.

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

“111011”=>”0111111″=>”001111″=>”000111″ the value represented is 7.

Sample Input 3:

111011

Sample Output 3:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
3

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Khaled has an array A of N elements. It is guaranteed that N is even. He wants to choose at most N/2

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:

 The first line contains an integer, N, denoting the number of elements in A.


 Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

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

number of elements in the subset is 1 which is less than N/2.


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Sample Input 2

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

number of elements in the subset is 1 which is less than N/2.


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

called Xor- sum as follows

 Xor-sum(x)=(x XOR A[1])+(x XOR A[2])+(x XOR A[3])+…………..+(x XOR A[N])

Can you find the integer x in the range [0,K] with the maximum Xor-sum (x) value?

Print only the value.

Input format

 The first line contains integer N denoting the number of elements in A.


 The next line contains an integer, k, denoting the maximum value of x.
 Each line i of the N subsequent lines(where 0<=i<=N) contains an integer describing Ai.

Constraints

 1<=N<=10^5
 0<=K<=10^9
 0<=A[i]<=10^9

Sample Input 1
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
1

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
4

Sample Output 3

46

Explanation:

Xor_sum(8)=(8^7)+(8^4) +(8^0)+(8^3)=46.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
13.Problem Statement :

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

reach a quotient that is less than K

The representation of M in the system based on K is formed by the final quotient (as first digit) and is

followed by the remainder of the previous divisionsFor example :

 If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number

 In decimal system = 172 in octal system.

 172 in base 8 = 1*8^2 + 7*8 + 2 = 122

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

in base 10= 333 in base 4

Given a number M in its decimal representation, your task is find the minimum base B such that in the

representation of M at base B all digits are the same.

Input Format
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
 The first line contains an integer, M, denoting the number given

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
 The next line contains an integer K denoting the largest number of obligations he could cancel

 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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
7

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

others also have to use the machines.

If performing all the exercises does not make you feel tired, return -1.

Input Format

E :: INTEGER

The first line contains an integer, E, denoting the Energy.

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

energy drained by i-th exercise.

A[i] :: 1 -> 10^5


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Sample Input 1:

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

distance is zero or negative display ” is an Invalid Input”.

[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;

System.out.printf(“You need a sum of Rs.%.2f to cover the trip”,cost);

Sample Input 1:

 Enter the no of liters to fill the tank

20

 Enter the distance covered

150

Sample Output 1:

 Liters/100KM

13.33
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
 Miles/gallons

17.64

Explanation:

 For 150 KM fuel consumption is 20 liters,


 Then for 100 KM fuel consumption would be (20/150)*100=13.33,
 Distance is given in KM, we have to convert it to miles (150*0.6214)=93.21,
 Fuel consumption is given in liters, we have to convert it to gallons (20*0.2642)=5.284,
 Then find (miles/gallons)=(93.21/5.284)=17.64

Sample Input 2:

 Enter the no of liters to fill the tank

-5

Sample Output 2:

 -5 is an Invalid Input
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Generate a bill for What Vohra has bought.

Sample Input 1:

 Enter the no of pizzas bought:10


 Enter the no of puffs bought:12
 Enter the no of cool drinks bought:5

Sample Output 1:

Bill Details

 No of pizzas:10
 No of puffs:12
 No of cooldrinks:5
 Total price=1290

ENJOY THE SHOW!!!


JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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.

[Assume the number of inputs should be always 4 ]

Sample Input 1:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
 Enter the digits:
65
66
67
68

Sample Output 1:

65-A

66-B

67-C

68-D

Sample Input 2:

 Enter the digits:


115
116
101
112

Sample Output 2:

115-s

116-t

101-e

112-p
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

Python
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

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:

 Enter the no of students placed in CSE:90


 Enter the no of students placed in ECE:45
 Enter the no of students placed in MECH:70

Sample Output 1:

 Highest placement

CSE
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)
Sample Input 2:

 Enter the no of students placed in CSE:55


 Enter the no of students placed in ECE:85
 Enter the no of students placed in MECH:85

Sample Output 2:

 Highest placement

ECE

MECH

Sample Input 3:

 Enter the no of students placed in CSE:0


 Enter the no of students placed in ECE:0
 Enter the no of students placed in MECH:0

Sample Output 3:

 None of the department has got the highest placement

Sample Input 4:

 Enter the no of students placed in CSE:10


 Enter the no of students placed in ECE:-50
 Enter the no of students placed in MECH:40

Sample Output 4:

 Input is Invalid
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

python
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660

Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai


Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE)

You might also like