Sie sind auf Seite 1von 28

ARRAY DIVISION

Write a program to get an array in ascending order and a number as input and do the
following.
By removing a single element from the given input array, form a resultant array that
should be divisible by the given number. The new sum of the result array should be the
maximum possible sum obtained after removing the element from the original array. Display
the new array and the number that was removed. If no such number exists in the array then
display the original array itself.
Sample Input 1:
Elements of the Array: {1, 2, 3, 4, 5}
Number: 4
Sample Output 1:
Elements of the Output Array = {1, 2, 4, 5}
Elements Removed: 3
Explanation:
Sum of the initial array = 15.
Multiples of the number 4, that is less than 15 = {0, 4, 8, 12}
If the element '3' is removed from the input array then the new sum of the result array
will be 12. The result array's sum is also divisible by 4.
Sample Input 2:
Elements of the Array: {1, 2, 7}
Number: 6
Sample Output 2:
Elements of the Output Array = {1, 2, 7}
Elements Removed: Explanation:
Sum of the initial array = 10.
A new array cannot be formed with multiples of 6 by removing any element in the
original array. Hence it is printed as is.

VALIDATE PATTERN
Write a program to get a string containing parenthesis '(', ')' (open and closed braces)
and other characters. Check whether the brackets in the statement are matching. If the
parenthesis in the string matches to form a valid pattern (an open brace should have a
matching closed brace following it), print the same string as output. If the parenthesis does
not match, then remove the unmatched / unwanted parenthesis and print the result string.
Note that the input string will also contain characters enclosed within the open and closed
braces.
Note: There will not be any spaces in between the characters / symbols (It is given in
these examples for clarity only).
Sample Input 1:
(O)
Sample Output 1:
(O)
Sample Input 2:
((a)(bb)(c))
Sample Output 2:
((a)(bb)(c))
Sample Input 3:
))((a))
Sample Output 3:
((a))
Explanation:
The first two closing braces are not required.
Sample Input 4:
(good)((bad)(better)(wow)
Sample Output 4:
(good)(bad)(better)(wow)
Sample Input 5:
((good)((bad)(better)(wow))
Sample Output 5:
(good)((bad)(better)(wow))

B.BHUVANESWARAN / AP (SS) / CSE / REC - 2

LUCKY NUMBER
A number is considered lucky if it contains either 3 or 4 or 3 and 4 both in it. Write a
program to print the nth lucky number. Example, 1st lucky number is 3, and 2nd lucky
number is 4 and 3rd lucky number is 33 and 4th lucky number is 34 and so on. Note that 13,
40 etc., are not lucky as they have other numbers in it.
The program should accept a number 'n' as input and display the nth lucky number as
output.
Sample Input 1:
3
Sample Output 1:
33
Explanation:
Here the lucky numbers are 3, 4, 33, 34., and the 3rd lucky number is 33.
Sample Input 2:
34
Sample Output 2:
33344

B.BHUVANESWARAN / AP (SS) / CSE / REC - 3

POWER OF 2
Write a program to find the minimum number of powers of 2 (sum of all those
numbers) that are required to form the given input number and print those numbers.
Constraint: Input vary from 1 to 1024
Example: To make a sum of 10, 2 power 3 (8) and 2 power 1 (2) are required.
Sample Input 1:
10
Sample Output 1:
Count of Numbers required: 2
The Powers of 2 are: 8, 2
Sample Input 2:
12
Sample Output 2:
Count of Numbers required: 2
The Powers of 2 are: 8, 4
Sample Input 3:
20
Sample Output 3:
Count of Numbers required: 2
The Powers of 2 are: 16, 4

B.BHUVANESWARAN / AP (SS) / CSE / REC - 4

VALIDATE STATEMENT
Write a program to validate whether the given statement is valid or not. The statement
can contain the characters a-z, numbers 0-9, the operator symbols +, -, *, / and parenthesis '('
and ')' only.
A valid statement would be like: (a+b)*(c+(d-e/f))
Following statements are sample invalid statements:
1) (a+) or
2) (a**b) or 3) (a+b/4etc.,
Sample Input 1:
(a+b+c)
Sample Output 1:
Valid
Sample Input 2:
a+b*(c-d)
Sample Output 2:
Valid
Sample Input 3:
ab+c
Sample Output 3:
Invalid
Sample Input 4:
(a+b))
Sample Output 4:
Invalid
Sample Input 5:
(+a)
Sample Output 5:
Invalid
B.BHUVANESWARAN / AP (SS) / CSE / REC - 5

PAIR NUMBER
Goel is the smartest boy in his college. In order to find the 2nd smartest person he
gives a puzzle. The task of the puzzle is simple. He gives a set of numbers which are pair
numbers.
A pair number is one which can be represented as (2, 2) In that he randomly picks a
number of his choice.
(NOTE: The choice number is not present in that array i.e., the choice number is
present only once in that array)
Your task is to find the choice number or the missing number.
Inputs:
T - Number of test cases 1 <= T <= 10
N - Number of elements in the array (always odd) 1 <= N <= 100000
Next line contains array elements
Output :
Single integer: Answer to the problem.
Sample Input:
1
7
1341943
Sample Output:
9
Explanation:
For test case 1: (1, 1) (3, 3) (4, 4) are pair numbers. Random number picked by Goel
is 9 pair number of 9 is missing. so 9 is the required answer.

B.BHUVANESWARAN / AP (SS) / CSE / REC - 6

PARTICLES
There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded
onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there
are more than N particles in a chamber, a reaction will cause 1 particle to move to the
immediate next chamber(if current chamber is 0, then to chamber number 1), and all the
particles in the current chamber will be be destroyed and same continues till no chamber has
number of particles greater than N. Given K,N and the total number of particles bombarded
(A), find the final distribution of particles in the K chambers. Particles are bombarded one at
a time. After one particle is bombarded, the set of reactions, as described, take place. After all
reactions are over, the next particle is bombarded. If a particle is going out from the last
chamber, it has nowhere to go and is lost.
Input
The input will consist of one line containing three numbers A,N and K separated by
spaces.
A will be between 0 and 1000000000 inclusive.
N will be between 0 and 100 inclusive.
K will be between 1 and 100 inclusive.
All chambers start off with zero particles initially.
Output
Consists of K numbers on one line followed by a newline. The first number is the
number of particles in chamber 0, the second number is the number of particles in chamber 1
and so on.
Example
Sample Input:
313
Sample Output:
110
Explanation
Total of 3 particles are bombarded. After particle 1 is bombarded, the chambers have
particle distribution as "1 0 0". After second particle is bombarded, number of particles in
chamber 0 becomes 2 which is greater than 1. So, num of particles in chamber 0 becomes 0
and in chamber 1 becomes 1. So now distribution is "0 1 0". After the 3rd particle is
bombarded, chamber 0 gets 1 particle and so distribution is "1 1 0" after all particles are
bombarded one by one.

B.BHUVANESWARAN / AP (SS) / CSE / REC - 7

COIN PROBLEM
Sanskar have some coins with him each has different values. He wants to find gifts to
his girlfriend for his valentines' day. Since the shopkeeper is too busy with his customers he
won't give back any change to him.
Find the number of ways he can give money to the shopkeeper for the gift without any
loss.
Input:
First line contain N.
Next line contain N integers denoting the value of each coin.
Next line contain the cost of the gift.
Output:
The total number of ways Sanskar can pay the shopkeeper.
Sample Input:
6
123345
6
Sample Output :
5
Explanation :
(1+2+3) (1+2+3) (1+5) (3+3) (2+4)

B.BHUVANESWARAN / AP (SS) / CSE / REC - 8

GCD
Your task is simple.
Find the GCD of 2 numbers.
Input:
x, y 1 <= x, y <= 1000000000
Output:
GCD of x, y. (required answer)
Example
Sample Input:
2
26
10 11
Sample Output:
2
1

B.BHUVANESWARAN / AP (SS) / CSE / REC - 9

ANAGRAM
Two Strings S1, S2 are passed as input to the program. The program must print the
count of anagrams present in both the strings. Out of each pair of words in the anagram the
first word in the pair must be in the S1 and the second word in the pair must be in S2.
If two words have the same characters and the occurrence number of each character is
also identical respectively, they are anagrams.
Example:
silent & listen
Input Format:
The first line will contain the value of S1.
The second line will contain the value of S2.
Boundary Conditions:
Length of S1 and S2 is from 5 to 200.
Output Format:
The count of pair of anagrams in S1 and S2 based on the conditions mentioned.
Example Input / Output 1:
Input:
but i will not listen to him
water dropped into the silent tub
Output:
2
Explanation:
The anagram pairs are
but tub
listen silent

B.BHUVANESWARAN / AP (SS) / CSE / REC - 10

NUMBER COUNT
A set of N numbers (separated by one or more spaces) is passed as input to the
program. The program must identify the count of numbers where the number on the left is
twice the number on the right.
Input Format:
The first line will contain the N numbers separated by one or more spaces.
Boundary Conditions:
3<=N<=50
The value of the numbers can be from -99999999 to 99999999
Output Format:
The count of numbers where the sum of the numbers on the LHS is twice that of the
sum of numbers on the RHS.
Example Input / Output 1:
Input:
10 20 5 40 15 -2 30 -1 60
Output:
2
Explanation:
The numbers meeting the criteria are 20, -30
Example Input / Output 2:
Input:
5 90 10 2 5 -4 10 6 5 3
Output:
3
Explanation:
The numbers meeting the criteria are 2, 6, 5

B.BHUVANESWARAN / AP (SS) / CSE / REC - 11

SUM OF NON-EDGES
A set of numbers forming a matrix N*N is passes as input. The program has to print
the sum of numbers which are not along the edges.
Input Format:
The first line will contain the value of N
The next lines will contain N numbers each separated by one or more spaces
Boundary Conditions:
3<=N<=10
Value of a given number in the matrix is from 0 to 99999999
Output Format:
The sum of the numbers which are not along the edges.
Example Input / Output 1:
Input:
3
567
891
234
Output:
9
Explanation:
All numbers except 9 are along the edges. Hence the sum is 9 which is printed as
output.

B.BHUVANESWARAN / AP (SS) / CSE / REC - 12

PETROL
A man his driving car from home to office with X petrol. There are N numbers of
petrol bunks in the city with only few capacities and each petrol is located in different places.
For one km one liter will consume. So he fill up petrol in his petrol tank in each petrol bunks.
Output the remaining petrol if he has or tell him that he cannot travel if he is out of petrol.
Input Format:
The first line will contain the petrol in car.
The next line will contain petrol bunks named A to Z. separated by one or more
spaces.
The next line will contain distance from each petrol bunks.
The next line will contain capacities of each petrol bunk.
Sample Input:
2
ABC
153
642
Sample Output:
5

B.BHUVANESWARAN / AP (SS) / CSE / REC - 13

ARRAY GROUP
To find the number of groups in an array.
Explanation: To find the sum of the elements in the groups and that sum should be
divisible by input X and the groups should be limited to range with X numbers.
If X is 3, then the group should have only 2 elements and 3 elements from the array
whose sum is divisible by 3.
Input Format:
The first line will contain the array.
The next line will contain the value of X.
Sample Input:
397468
3
Sample Output:
4
Explanation:
3, 9
3, 6
9, 6
3, 9, 6

B.BHUVANESWARAN / AP (SS) / CSE / REC - 14

ALPHABET LETTER
To output the given string for the given input which is an integer.
Input: 1
Output: A
Input: 26
Output: Z
Input : 27
Output: AA
Input: 28:
Output: AB
Input: 1000
Output: ALL

B.BHUVANESWARAN / AP (SS) / CSE / REC - 15

ODD NUMBERS
To find the odd numbers in between the range.
Input:
2
15
Output:
3

11

13

B.BHUVANESWARAN / AP (SS) / CSE / REC - 16

FACTORS
To find the factors of the numbers given in an array and to sort the numbers in
descending order according to the factors present in it.
Input:
Given array :
8, 2, 3, 12, 16
Output:
12, 16, 8, 2, 3

B.BHUVANESWARAN / AP (SS) / CSE / REC - 17

NUMBER IN WORDS
To output the number in words (0-999)
Input:
234
Output:
Two hundred and Thirty Four

B.BHUVANESWARAN / AP (SS) / CSE / REC - 18

PATTERN PRINT
Print the given pattern:
Input:
N= 3, M=3
Output:
XXX
X0X
XXX
Input:
N=4 M=5
Output:
XXXX
X00X
X00X
X00X
XXXX
Input:
N=6 M=7
XXXXXX
X0000X
X0XX0X
X0XX0X
X0XX0X
X0000X
XXXXXX

B.BHUVANESWARAN / AP (SS) / CSE / REC - 19

GROUPS
To find the number of groups and output the groups:
Explanation: To find the sum of the elements in the groups and that sum should be
divisible by input X and the groups should be limited to range with X numbers.
If X is 3, then the group should have only 2 elements and 3 elements from the array
whose sum is divisible by 3.
Input:
Array: 3, 9, 7, 4, 6, 8
X: 3
Output:
3, 9
3, 6
9, 6
3, 9, 6
No of groups: 4

B.BHUVANESWARAN / AP (SS) / CSE / REC - 20

PRINT
Write a program to give the following output for the given input
Eg 1:
Input:
a1b10
Output:
abbbbbbbbbb
Eg: 2:
Input:
b3c6d15
Output:
bbbccccccddddddddddddddd
The number varies from 1 to 99.

B.BHUVANESWARAN / AP (SS) / CSE / REC - 21

ODD POSITIONS
Write a program to sort the elements in odd positions in descending order and
elements in ascending order
Eg 1:
Input:
13,2,4,15,12,10,5
Output: 13,2,12,10,5,15,4
Eg 2:
Input: 1,2,3,4,5,6,7,8,9
Output: 9,2,7,4,5,6,3,8,1

B.BHUVANESWARAN / AP (SS) / CSE / REC - 22

CHARACTER PATTERN
Write a program to print the following output for the given input. You can assume the
string is of odd length
Eg 1:
Input:
12345
Output:
1
5
2 4
3
2 4
1
5
Eg 2:
Input: PROGRAM
Output:
P
M
R
A
O R
G
O R
R
A
P
M

B.BHUVANESWARAN / AP (SS) / CSE / REC - 23

SUB STRING
Find if a String2 is substring of String1. If it is, return the index of the first
occurrence. else return -1.

Eg 1:
Input:
String 1: test123string
String 2: 123
Output: 4
Eg 2:
Input:
String 1: testing12
String 2: 1234
Output: -1

B.BHUVANESWARAN / AP (SS) / CSE / REC - 24

NO REPEAT
Given two sorted arrays, merge them such that the elements are not repeated
Eg 1:
Input:
Array 1: 2,4,5,6,7,9,10,13
Array 2: 2,3,4,5,6,7,8,9,11,15
Output:
Merged array: 2,3,4,5,6,7,8,9,10,11,13,15

B.BHUVANESWARAN / AP (SS) / CSE / REC - 25

RECURSION
Using Recursion reverse the string such as

Eg 1:
Input: one two three
Output: three two one
Eg 2:
Input: I love india
Output: india love I

B.BHUVANESWARAN / AP (SS) / CSE / REC - 26

MATRIX SEARCH
Save the string WELCOMETOZOHOCORPORATION in a two dimensional array
and search for substring like too in the two dimensional string both from left to right and
from top to bottom.
w
M
O
R
T

e
E
H
P
I

L
T
O
O
O

C
O
C
R
n

O
Z
O
A

And print the start and ending index as


Start index : <1,2>
End index: <3, 2>

B.BHUVANESWARAN / AP (SS) / CSE / REC - 27

ALTERNATE SORTING
Alternate sorting: Given an array of integers, rearrange the array in such a way that
the first element is first maximum and second element is first minimum.
Eg.)
Input : {1, 2, 3, 4, 5, 6, 7}
Output : {7, 1, 6, 2, 5, 3, 4}

B.BHUVANESWARAN / AP (SS) / CSE / REC - 28

Das könnte Ihnen auch gefallen