You are on page 1of 11

Problem No 1:

Write a program that takes an positive integer as an input and displays the binary equivalent of the
number.
Hint: You should be able to achieve this by using a single loop.

Input
321

Output
101000001

Constraints:
 N is an integer within the range [0, 524287]

Problem No 2:

A natural number N is called a perfect number if the sum of its proper divisors is equal to
itself. Write a program that takes a number N and displays 1 if N is perfect number else 0.

Read: Perfect Number

Input:
6

Output:
1

Explanation:6's proper positive divisors are 1,2 and 3.


Sum of divisors = 1 + 2 + 3 = 6.

Problem No 3:

Given an integer N, Check if Number N is Binary or not if N is Binary then print 1 else 0.

Input:
1011
where:
 First line represents value of N.

Output:
1

Assumptions:
 N can be in the range 1 to 100000.

Problem No 4:
Given an integer N, display the sum of the series 1 + 22 + 333 + 4444... up to N terms.

Input:
3

where:
 First line represents the value of N.
Output:
356

Problem No 5:
Given number N as input, find the sum of the series 1/1!+2/2!+3/3!+4/4!+5/5!+.....N/N! using
a function.

Write a function that accepts number N. The function should return a sum of the given series
up to Nth term.

Input:
5

where:
 First line represents a value N

Output:
2.708

Problem No 6:
Given a number N as input, find whether N is a perfect square or not using a function.

Write a function that accepts an integer N. The function should return 1 if N is perfect square
otherwise 0.

Input:
64

where:
 First line represents a value of N

Output:
1

Explanation: 42=64

Problem No 7:
Given a string S, check whether all the characters in the string are hexadecimal or not.

Input:
abc123

where:
 First line represents the input string S

Output:
Yes

Explanation: All characters in the string are hexadecimal.

Input:
@bz123

Output:
No
Explanation: Characters '@' and 'z' are not hexadecimal.

Assumptions:
 Length of the string S can be 0 to 10000

Problem No 8:
Given an integer N as input, find whether it is even or not using bitwise operator.

Input:
50

where:
 First line represents the integer N.

Output:
Yes

Assumption:
 Value of N can be in the range 0 to 10000.

Problem No 9:
Given two integers N1 and N2 as input, find whether they are equal or not using bitwise
operator.

Input:
5
5

where:
 First line represents the integer N1.
 Second line represents the integer N2.

Output:
Yes

Assumption:
 Value of N1 and N2 can be in the range -10000 to 10000.

Problem No 10:
Given an expression string S, write a program to examine whether the pairs and the orders of
“{“, ”}”, ”(“, ”)”, ”[“, ”]” are correct in exp.

Input :
[()]{}{[()()]()}

where:
 First line represents the input string S

Output:
Yes

Input:
[(])

Output:
No
Assumptions:
 Length of the string S can be 0 to 10000
 String S contains only brackets
Problem No 11:
Write a program takes N integers and stores them in an array Arr and prints the values in
reverse order.

Input
5
15820

Where,
 The first line of input represents the size of an array.
 The second line contains array elements separated by a single space.

Output
02851

Assume that,
 N is an integer within the range [0 to 100].

Problem No 12:
Given an array of integers, find the sum of its elements.

Input
5
12345

Where,
 The first line of input represents the size of the array.
 The second line contains the array's elements, separated here by a single space.

Output
15

Here the given input array is [1 2 3 4 5]


Sum of elements of array = 1 + 2 + 3 + 4 + 5 = 15.

Problem No 13:
Given an array of N integers, find the sum of all the distinct elements of the array.
Elements of the array would be in the range of 1 to N.

Input:
9
512467367

where:
 First line represents the number of elements in the array.
 Second line represents the elements in the array.

Output:
28

Explanation: The distinct elements in the array are 1, 2, 3, 4, 5, 6, and 7,


Hence their sum: 1+2+3+4+5+6+7 = 28, so the output is 28.
Problem No 14:
Given an array of integers, find another integer such that, if all the elements of the array are
subtracted individually from that number, then the sum of all the differences should add to 0. If
any such integer exists, print its value otherwise print '-1'.

Input:
3
123

where:
 First line represents the number of elements in the array.
 Second line represents the elements in the array.

Output:
2

Explanation: Substracting all the elements of arrays from 2, The sum of difference is: 1 + 0 + (-
1) = 0.

Assumptions:
 Array can be of size 1 to 1000.
 Array elements can be in the range -1000 to 1000.

Problem No 15:
Given a list, find the sum of numbers excluding largest and smallest one. If there are multiple
smallest value, exclude just one, and likewise for the largest value.

Write a function solution that accepts a list. The function should return the sum of the list.

Input
9
436892159

Where,
 First line represents the size of the list, N.
 The second line represents list elements separated by single space. All the elements of
the list should be on a single line.

Output
37

For the given list largest and smallest values are 9 and 1 respectively. Also, 9 is repeated two
times hence excluding just one 9.
Sum of remaining values = (4 + 3 + 6 + 8 + 2 + 5 + 9) = 37

Problem No 16:

Given a number N as input, find whether N is a perfect square or not using a function.

Write a function isPerfectSquare that accepts an integer N and returns 1 if N is perfect square
otherwise 0.

Input:
64
where:
 First line represents a value of N

Output:
1

Explanation: 42=64

Problem No 17:
Given an array of positive integers A, check the array for the odd occurrence(s) of at least one
element.

Display 1 if an array has odd occurrences of at least one element otherwise 0.

Input
12
123451234567

Where,
 First line contains size of array N.
 Second line contains array elements.

Output
1

Here for the given array, all elements of array repeated an even number of times except 6 and
7. Hence output is 1.

Problem No 18:
Given an array A, push all the zeros of the array to the front. The order of all other elements
should remain same.

Input
10
2 0 3 4 0 4 0 10 9 0

where,
 First line represents size of an array N.
 Second line represents array elements.

Output
0 0 0 0 2 3 4 4 10 9

Problem No 19:
Given an array of integers A and an integer S, determines whether there exist two elements in
the array whose sum is exactly equal to S or not.

Display 1 a pair is found in an array with matching sum S else 0.

Input
6
5
1 -2 3 8 7

Where,
 First line represents integer S.
 Second line represents the size of an array.
 Third line represents array elements separated by single space.

Output
1

For the given array, A[1] + A[3] = -2 + 8 = 6 which is equal to the given number S=6.

Problem No 20:
Given a list of integers, find the Majority Element.

Write a function solution that accepts a list L. The function should return the element if it is a
Majority Element in the list otherwise 0.

Input
5
12122

Where,
 First line of input represents the size of the list .
 Second line contains list of elements separated by single space. All the elements of the
list should be on a single line.

Output
2

Here for the given array element '2' is appears 3 times in the array of size 5.

Problem No 21:
Given an array of integers, display the elements of array which are not multiples of 5

Input:
6
2 3 4 11 22 320

where:
 First line represents the number of elements in the array.
 Second line represents the elements in the array.

Output:
2 3 4 11 22

Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it
is removed.

Assumptions:
 Array can be of size from 1 to 1000.
 Array element can be in the range -1000 to 1000.

Problem No 22:
Given an array of integers in random order. Segregate even numbers on the left side and odd
numbers on the right side of the array.

Input:
10
0 3 4 11 223 4 44 7 8 9
where:
 First line represents the number of elements in the array.
 Second line represents the elements in the array.

Output:
0 4 4 44 8 3 11 223 7 9

Explanation: Output displays the array which contains all the even numbers segregated to the
left and odd numbers to the right, according to their occurance

Assumptions:
 Array can be of size from 1 to 1000.
 Array elements can be in the range -1000 to 1000.

Problem No 23:
Given an array of integers, write a program to insert an element X at index K.

Input:
5
10 20 30 40 50
15
1

where:
 First line represents the number of elements in the array.
 Second line represents the elements in the array.
 Third line represents element X to be inserted.
 Fourth line represents the index K, where the element is to be inserted.

Output:
10 15 20 30 40 50

Explanation: Output displays the elements of the array after inserting the element X=15 at
index K=1.

Problem No 24:
Given two integers N1 and N2, display the given number pattern as described in output.

Input:
5
5

where:
 First line represents the value of N1( number of rows ).
 Second line represents the value of N2( number of columns ).

Output:
12345
23456
34567
45678
56789

Problem No 25:
How can mutliplication of two posive integers be defined inductively?
Ans: Something like should do
multRecur(m,n) : 0 if m is zero
n if m is 1
n+multRecur(m-1, n) otherwise.

Create a function that accepts integers m and n. The function should return product of m and n
using recursion.

Input:
20
15

where:
 First line represents number m
 Second line represents number n

Output:
300
Problem No 26
Given an integer N, display the even number pattern as described in output.

Input:
5

where:
 First line represents the value of N( number of rows ).

Output:
2 4 6 8 10
4 6 8 10
6 8 10
8 10
10

Problem No 27:
There can be functions that don't return anything at all. Technically we say such function return
a void. In C a void is a small piece of data which doesn't store any information in it. We use
suck functions to printout stuff. For example, the following function takes an array and the size
of the array as inputs and produces nothing fruitful. Nevertheless as a nice side effect it prints
the elements of the array in a sequence:

void printArray(int arr[], int size){


for(int i = 0; i < size; i++){
printf("%d", arr[i]);
if(i != size - 1){
printf(", ");
}
printf("\n");
}
return;
}
----------------------------
Now, suppose you are given two positive integers N1 and N2. You can create a void
function printNums that recursively prints all the numbers between N1 and N2 in the order.
The funciton can be inductively defined as follows:

printNums(N1, N2) : if N1 > N2 return;


otherwise print N1 and return printNums(N1+1, N2);

Input:
16
25

where:
 First line represents a value of N1
 Second line represents number N2

Output:
16 17 18 19 20 21 22 23 24 25

Problem No 28
Given an integer N, find whether N is a Palindrome using recursion.

Write a function that accepts an integer N. The function should return 1 if N is a palindrome
else 0.

Input:
121

Output:
1

The reverse of 121 is also 121.

Assume that,
 N is an integer within the range [0 to 1000000000].

Problem No 29
Given string S, find the product of ASCII values of characters in the string S.

Input:
ab

where:
 First line represents string S.

Output:
9506

Problem No 30
Given string S containing only lowercase English alphabets, find the product of all the prime
frequencies of the characters in S.

Input:
abaccdcdbbbd

where:
 First line represents input string S

Output:
18
Explanation:
 Only characters a, c,d have prime frequencies and product of their
frequencies=2*3*3=18

Problem No 31
Given an array of N integers, find the largest gap between any two elements of the array.
In simple words, find max(|Ai-Aj|) where 0 ≤ i < N and 0 ≤ j < N.

Input:
4
3 6 7 10

where:
 First line represents the number of elements in the array.
 Second line represents the elements in the array.

Output:
7

Explanation: Here, the largest gap can be found between 3 and 10 which is 10 - 3 = 7, hence
the output 7.

Assumptions:
 Array element can be in the range -1000 to 1000.

Problem No 32
Given two integers N1 and N2, write a program to display a number based on following
conditions:
 0 if N1 is 5 and N2 is 2.
 1 if N1 is 5 and N2 is not 2.
 2 if N1 is not 5 and N2 is 2.
 3 if N1 is not 5 and N2 is not 2.

Use nested-if-else statement.

Input
5
4

Output
1

Here, N1 = 5 and N2 != 2. Hence, the result is 1 according to the given conditions.

You might also like