Sei sulla pagina 1di 17

SET – 7

(Mathematical Based)

1) Lucky Numbers
Lucky numbers are subset of integers. Rather than going into much theory, let us see the process of arriving
at lucky numbers,
Take the set of integers
1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,……
First, delete every second number, we get following reduced set.
1,3,5,7,9,11,13,15,17,19,…………
Now, delete every third number, we get
1, 3, 7, 9, 13, 15, 19,….….
Continue this process indefinitely……
Any number that does NOT get deleted due to above process is called “lucky”.
Therefore, set of lucky numbers is 1, 3, 7, 13………
Now, given an integer ‘n’, write a function to say whether this number is lucky or not.

2) Factorial of a large number


Factorial of 100 has 158 digits. It is not possible to store these many digits even if we use long long int.

3) Check if a number can be expressed as x^y (x raised


to power y)
Given a positive integer n, find if it can be expressed as xy where y > 1 and x > 0. x and y both are integers.

Examples:
Input: n = 8

Output: true

8 can be expressed as 23

Input: n = 49
Output: true
49 can be expressed as 72

Input: n = 48
Output: false
48 can't be expressed as xy

4) Write Your own function to find Square root of an


integer
Given an integer x, find square root of it. If x is not a perfect square, then return floor(√x).
Examples:

Input: x = 4

Output: 2

Input: x = 11

Output: 3

5) Multiply two integers without using multiplication,


division and bitwise operators, and no loops

6) Write you own Power without using


multiplication (*) and division (/) operators

7) Average of a stream of numbers


Given a stream of numbers, print average (or mean) of the stream at every point. For example, let us
consider the stream as 10, 20, 30, 40, 50, 60, …

Average of 1 numbers is 10.00

Average of 2 numbers is 15.00

Average of 3 numbers is 20.00

Average of 4 numbers is 25.00

Average of 5 numbers is 30.00

Average of 6 numbers is 35.00

..................
9) Check whether a given point lies inside a triangle
or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within
the triangle or not.
For example, consider the following program, the function should return true for P(10, 15) and false for
P'(30, 15)

B(10,30)

/ \

/ \

/ \

/ P \ P'

/ \

A(0,0) ----------- C(20,0)

10) Sieve of Eratosthenes


Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13,
17, 19″.
The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is
smaller than 10 million or so.

11) Find day of the week for a given date


Write a function that calculates the day of the week for any particular date in the past or future. A typical
application is to calculate the day of the week on which someone was born or some other special event
occurred.
Input
30/8/2010

Output
Monday
12) Measure one liter using two vessels and infinite
water supply
There are two vessels of capacities ‘a’ and ‘b’ respectively. We have infinite water supply. Give an efficient
algorithm to make exactly 1 litre of water in one of the vessels. You can throw all the water from any vessel
any point of time. Assume that ‘a’ and ‘b’ are Coprimes.

13) Efficient program to print all prime factors of a


given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input
number is 12,then output should be “2 2 3″. And if the input number is 315, then output should be “3 3 5 7″

15) How to check if a given point lies inside or


outside a polygon?
Point polygon1[] = {{0, 0}, {10, 0}, {10, 10}, {0, 10}};

Point p = {20, 20};

Output

No

p = {5, 5};
output
Yes

16) Write a program to print all permutations of a given string


17) How to check if a given number is Fibonacci
number?
Given a number ‘n’, how to check if n is a Fibonacci number.

18) Program for nth Catalan Number


Catalan numbers are a sequence of natural numbers that occurs in many interesting counting problems
like following.
1) Count the number of expressions containing n pairs of parentheses which are correctly matched. For n
= 3, possible expressions are ((())), ()(()), ()()(), (())(), (()()).
2) Count the number of possible Binary Search Trees with n keys
3) Count the number of full binary trees (A rooted binary tree is full if every vertex has either two children
or no children) with n+1 leaves.

The first few Catalan numbers for n = 0, 1, 2, 3, … are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …

19) Count trailing zeroes in factorial of a number


Given an integer n, write a function that returns count of trailing zeroes in n!.
Examples:
Input: n = 5
Output: 1
Factorial of 5 is 20 which has one trailing 0.

Input: n = 20
Output: 4
Factorial of 20 is 2432902008176640000 which has
4 trailing zeroes.

Input: n = 100
Output: 24

20) Find the smallest number whose digits multiply to


a given number n
Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ‘n’. The result
‘p’ should have minimum two digits.
Examples:

Input: n = 36

Output: p = 49

// Note that 4*9 = 36 and 49 is the smallest such number

Input: n = 100

Output: p = 455

// Note that 4*5*5 = 100 and 455 is the smallest such number

Input: n = 1

Output:p = 11

// Note that 1*1 = 1

Input: n = 13

Output: Not Possible


21) Calculate the angle between hour hand and
minute hand
This problem is known as Clock angle problem where we need to find angle between hands of an analog
clock at .
Examples:

Input: h = 12:00, m = 30.00

Output: 165 degree

Input: h = 3.00, m = 30.00

Output: 75 degree

22) Count Possible Decodings of a given Digit


Sequence
Let 1 represent ‘A’, 2 represents ‘B’, etc. Given a digit sequence, count the number of possible decodings
of the given digit sequence.
Examples:

Input: digits [] = "121"

Output: 3

// The possible decoding are "ABA", "AU", "LA"

Input: digits [] = "1234"

Output: 3

// The possible decoding are "ABCD", "LCD", "AWD"

23) Multiply two polynomials


Given two polynomials represented by two arrays, write a function that multiplies given two polynomials.

Input: A[] = {5, 0, 10, 6}

B[] = {1, 2, 4}

Output: prod[] = {5, 10, 30, 26, 52, 24}

The first input array represents "5 + 0x^1 + 10x^2 + 6x^3"

The second array represents "1 + 2x^1 + 4x^2"


And Output is "5 + 10x^1 + 30x^2 + 26x^3 + 52x^4 + 24x^5"

24) Find number of solutions of a linear equation of n


variables
Given a linear equation of n variables, find number of non-negative integer solutions of it. For example,let
the given equation be “x + 2y = 5″, solutions of this equation are “x = 1, y = 2″, “x = 5, y = 0″ and “x = 1. It
may be assumed that all coefficients in given equation are positive integers.
Example:

Input: coeff[] = {1, 2}, rhs = 5

Output: 3

The equation "x + 2y = 5" has 3 solutions.

(x=3,y=1), (x=1,y=2), (x=5,y=0)

Input: coeff[] = {2, 2, 3}, rhs = 4

Output: 3

The equation "2x + 2y + 3z = 4" has 3 solutions.

(x=0,y=2,z=0), (x=2,y=0,z=0), (x=1,y=1,z=0)

25) Find next greater number with same set of digits


Given a number n, find the smallest number that has same set of digits as n and is greater than n. If x is
the greatest possible number with its set of digits, then print “not possible”.
Examples:
For simplicity of implementation, we have considered input number as a string.

Input: n = "218765"

Output: "251678"

Input: n = "1234"

Output: "1243"

Input: n = "4321"

Output: "Not Possible"

Input: n = "534976"
Output: "536479"

27) Print squares of first n natural numbers without


using *, / and –
Given a natural number ‘n’, print squares of first n natural numbers without using *, / and -.

Input: n = 5

Output: 0 1 4 9 16

Input: n = 6

Output: 0 1 4 9 16 25

28) Find n’th number in a number system with only 3


and 4
Given a number system with only 3 and 4. Find the nth number in the number system. First few numbers
in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343,
3344, 3433, 3434, 3443, 3444, …

29) Tiling Problem


Given a “2 x n” board and tiles of size “2 x 1″, count the number of ways to tile the given board using the 2
x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically i.e., as 2 x 1 tile.
Examples:

Input n = 3

Output: 3

Explanation:

We need 3 tiles to tile the board of size 2 x 3.

We can tile the board using following ways

1) Place all 3 tiles vertically.

2) Place first tile vertically and remaining 2 tiles horizontally.

3) Place first 2 tiles horizontally and remaining tiles vertically

Input n = 4

Output: 5

Explanation:
For a 2 x 4 board, there are 5 ways

1) All 4 vertical

2) All 4 horizontal

3) First 2 vertical, remaining 2 horizontal

4) First 2 horizontal, remaining 2 vertical

5) Corner 2 vertical, middle 2 horizontal

30) Given p and n, find the largest x such that p^x divides n!
Given an integer n and a prime number p, find the largest x such that p x (p raised to power x) divides n!
(factorial)

31) Count factorial numbers in a given range


A number F is a factorial number if there exists some integer I >= 0 such that F = I! (that is, F is factorial
of I). Examples of factorial numbers are 1, 2, 6, 24, 120, ….
Write a program that takes as input two long integers ‘low’ and ‘high’ where 0 < low < high and finds count
of factorial numbers in the closed interval [low, high].

Examples:
Input: 0 1

Output: 1 //Reason: Only factorial number is 1

Input: 12 122

Output: 2 // Reason: factorial numbers are 24, 120

Input: 2 720

Output: 5 // Factorial numbers are: 2, 6, 24, 120, 720

32) How to check if given four points form a square


Given coordinates of four points in a plane, find if the four points form a square or not.
To check for square, we need to check for following.
a) All four sides formed by points are same.
b) The angle between any two sides is 90 degree. (This condition is required as Quadrilateral also has
same sides.

33) Program to add two polynomials


Given two polynomials represented by two arrays, write a function that adds given two polynomials.
Input: A[] = {5, 0, 10, 6}
B[] = {1, 2, 4}

Output: sum[] = {5, 10, 30, 26, 52, 24}

The first input array represents "5 + 0x^1 + 10x^2 + 6x^3"

The second array represents "1 + 2x^1 + 4x^2"

And Output is "6 + 2x^1 + 14x^2 + 6x^3"

34) Compute sum of digits in all numbers from 1 to n


Given a number x, find sum of digits in all numbers from 1 to n.
Examples:

Input: n = 5

Output: Sum of digits in numbers from 1 to 5 = 15

Input: n = 12

Output: Sum of digits in numbers from 1 to 12 = 51

Input: n = 328

Output: Sum of digits in numbers from 1 to 328 = 3241

35) Count Distinct Non-Negative Integer Pairs (x, y)


that Satisfy the Inequality x*x + y*y < n
Given a positive number n, count all distinct Non-Negative Integer pairs (x, y) that satisfy the inequality x*x
+ y*y < n.
Examples:

Input: n = 5

Output: 6

The pairs are (0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (0, 2)

Input: n = 6

Output: 8

The pairs are (0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (0, 2),

(1, 2), (2, 1)


36) Count ways to reach the n’th stair
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1
stair or 2 stairs at a time. Count the number of ways, the person can reach the top.

Input: n = 1

Output: 1

There is only one way to climb 1 stair

Input: n = 2

Output: 2

There are two ways: (1, 1) and (2)

Input: n = 4

Output: 5

(1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)

37) Print all pairs of anagrams in a given array of


strings
Given an array of strings, find all anagram pairs in the given array.
Example:
Input: arr[] = {"geeksquiz", "geeksforgeeks", "abcd",

"forgeeksgeeks", "zuiqkeegs"};

Output: (geeksforgeeks, forgeeksgeeks), (geeksquiz, zuiqkeegs)

38) Check if characters of a given string can be


rearranged to form a palindrome
Given a string, Check if characters of the given string can be rearranged to form a palindrome.
For example characters of “geeksogeeks” can be rearranged to form a palindrome “geeksoskeeg”, but
characters of “geeksforgeeks” cannot be rearranged to form a palindrome.

39) Program to evaluate simple expressions


You are given a string that represent an expression of digits and operands. E.g. 1+2*3, 1-2+4. You need
to evaluate the string or the expression. NO BODMAS is followed. If the expression is of incorrect syntax
return -1.
Test cases:
a) 1+2*3 will be evaluated to 9.
b) 4-2+6*3 will be evaluated to 24.
c) 1++2 will be evaluated to -1(INVALID).
Also, in the string spaces can occur. For that case we need to ignore the spaces. Like : - 1*2 -1 is equals
to 1.

40) Nuts & Bolts Problem (Lock & Key problem)


Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between
nuts and bolts. Match nuts and bolts efficiently.
char nuts[] = {'@', '#', '$', '%', '^', '&'};
char bolts[] = {'$', '%', '&', '^', '@', '#'};

Output:

Matched nuts and bolts are :

# $ % & @ ^

# $ % & @ ^

41) Given n appointments, find all conflicting


appointments
Given n appointments, find all conflicting appointments.
Examples:

Input: appointments [] = { {1, 5} {3, 7}, {2, 6}, {10, 15}, {5, 6}, {4, 100}}

Output: Following are conflicting intervals

[3,7] Conflicts with [1,5]

[2,6] Conflicts with [1,5]

[5,6] Conflicts with [3,7]

[4,100] Conflicts with [1,5]

An appointment is conflicting, if it conflicts with any of the previous appointments in array.

42) Find number of days between two given dates


Given two dates, find total number of days between them. The count of days must be calculated in O(1)
time and O(1) auxiliary space.

Input: dt1 = {10, 2, 2014}

dt2 = {10, 3, 2015}

Output: 393

dt1 represents "10-Feb-2014" and dt2 represents "10-Mar-2015"


The difference is 365 + 28

Input: dt1 = {10, 2, 2000}

dt2 = {10, 3, 2000}

Output: 29

Note that 2000 is a leap year

Input: dt1 = {10, 2, 2000}

dt2 = {10, 2, 2000}

Output: 0

Both dates are same

Input: dt1 = {1, 2, 2000};

dt2 = {1, 2, 2004};

Output: 1461

Number of days is 365*4 + 1

43) Find Index of 0 to be replaced with 1 to get longest


continuous sequence of 1s in a binary array
Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous
sequence of 1s.Expected time complexity is O(n) and auxiliary space is O(1).

Example:

Input:

arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1}

Output:

Index 9

Assuming array index starts from 0, replacing 0 with 1 at index 9 causes

the maximum continuous sequence of 1s.

Input:

arr[] = {1, 1, 1, 1, 0}

Output:

Index 4
44) How to check if two given sets are disjoint?
Given two sets represented by two arrays, how to check if the given two sets are disjoint or not? It may be
assumed that the given arrays have no duplicates.

Input: set1[] = {12, 34, 11, 9, 3}

set2[] = {2, 1, 3, 5}

Output: Not Disjoint

3 is common in two sets.

Input: set1[] = {12, 34, 11, 9, 3}

set2[] = {7, 2, 1, 5}

Output: Yes, Disjoint

There is no common element in two sets.

45) Minimum Number of Platforms Required for a


Railway/Bus Station
Given arrival and departure times of all trains that reach a railway station, find the minimum number of
platforms required for the railway station so that no train waits.
We are given two arrays which represent arrival and departure times of trains that stop

Examples:

Input: arr[] = {9:00, 9:40, 9:50, 11:00, 15:00, 18:00}

dep[] = {9:10, 12:00, 11:20, 11:30, 19:00, 20:00}

Output: 3

There are at-most three trains at a time (time between 11:00 to 11:20)

46) Find the number of valid parentheses expressions


of given length
Given a number n find the number of valid parentheses expressions of that length.
Input: 2

Output: 1

There is only possible valid expression of length 2, "()"


Input: 4

Output: 2

Possible valid expression of length 4 are "(())" and "()()"

Input: 6

Output: 5

Possible valid expressions are ((())), ()(()), ()()(), (())() and (()())

47) Given a number, find the next smallest palindrome


Given a number, find the next smallest palindrome larger than this number. For example, if the input
number is “2 3 5 4 5″, the output should be “2 3 6 3 2″. And if the input number is “9 9 9″, the output
should be “1 0 0 1″.
The input is assumed to be an array. Every entry in array represents a digit in input number. Let the array
be ‘num[]’ and size of array be ‘n’

48) Intersection of n sets


Given n sets of integers of different sizes. Each set may contain duplicates also. How to find the intersection
of all the sets. If an element is present multiple times in all the sets, it should be added that many times in
the result.
For example, consider there are three sets {1,2,2,3,4} {2,2,3,5,6} {1,3,2,2,6}. The intersection of the given
sets should be {2,2,3}

49) Print all combinations of balanced parentheses


Write a function to generate all possible n pairs of balanced parentheses.
For example, if n=1
{}
for n=2
{}{}
{{}}

50) Find if two rectangles overlap


Given two rectangles, find if the given two rectangles overlap or not.
Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are
given following four coordinates.
l1: Top Left coordinate of first rectangle.
r1: Bottom Right coordinate of first rectangle.
l2: Top Left coordinate of second rectangle.
r2: Bottom Right coordinate of second rectangle.
We need to write a function bool doOverlap(l1, r1, l2, r2) that returns true if the two given rectangles
overlap.

51) Write a C program to find determinant / Inverse/ Transpose and Disjoint of a nxn
matrix.

52) Write a C Program to Convert a Digit into Text


Ex – i/p - 1795.50

o/p – one thousand seven hundred ninety five rupees and fifty paisa only.

53) Count number of binary strings without consecutive 1’s


Given a positive integer N, count all possible distinct binary strings of length N such that there are no
consecutive 1’s.
Examples:

Input: N = 2

Output: 3

// The 3 strings are 00, 01, 10

Input: N = 3

Output: 5

// The 5 strings are 000, 001, 010, 100, 101

54) Rearrange an array such that ‘arr[j]’ becomes ‘i’ if


‘arr[i]’ is ‘j’
Given an array of size n where all elements are in range from 0 to n-1, change contents of arr[] so that arr[i]
= j is changed to arr[j] = i.
Examples:

Example 1:
Input: arr[] = {1, 3, 0, 2};
Output: arr[] = {2, 0, 3, 1};
Explanation for the above output.
Since arr[0] is 1, arr[1] is changed to 0
Since arr[1] is 3, arr[3] is changed to 1
Since arr[2] is 0, arr[0] is changed to 2
Since arr[3] is 2, arr[2] is changed to 3

55) Find the smallest positive integer value that cannot


be represented as sum of any subset of a given array
Given a sorted array (sorted in non-decreasing order) of positive numbers, find the smallest positive integer
value that cannot be represented as sum of elements of any subset of given set.
Expected time complexity is O(n).
Examples:

Input: arr[] = {1, 3, 6, 10, 11, 15};

Output: 2

Input: arr[] = {1, 1, 1, 1};

Output: 5

Input: arr[] = {1, 1, 3, 4};

Output: 10

!!! All The Best !!!

Potrebbero piacerti anche