Algolution Set - 3
Algolution Set - 3
List of Programs
SET-3 Algolution
1)In the theory of numbers, square free numbers have a special place. A square free number
is one that is not divisible by a perfect square (other than 1). Thus 72 is divisible by 36 (a
perfect square), and is not a square free number, but 70 has factors 1, 2, 5, 7, 10, 14, 35 and
70. As none of these are perfect squares (other than 1), 70 is a square free number.
For some algorithms, it is important to find out the square free numbers that divide a number.
Note that 1 is not considered a square free number.
In this problem, you are asked to write a program to find the number of square free numbers
that divide a given number.
Input:
The only line of the input is a single integer N which is divisible by no prime number larger than
19
Output:
One line containing an integer that gives the number of square free numbers (not including 1)
Constraints:
N < 10^9
Difficulty Level:
Simple
Examples:
Example 1:
Input:
20
Output:
3
Explanation
N=20
If we list the numbers that divide 20, they are
1, 2, 4, 5, 10, 20
1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a perfect square. 2
and 5, being prime, are square free, and 10 is divisible by 1,2,5 and 10, none of which are
perfect squares. Hence the square free numbers that divide 20 are 2, 5, 10. Hence the result is
3.
Example 2:
Input:
72
Output:
3
Explanation:
N=72. The numbers that divide 72 are
1 is not considered square free. 4, 9 and 36 are perfect squares, and 8,12,18,24 and 72 are
divisible by one of them. Hence only 2, 3 and 6 are square free. (It is easily seen that none of
them are divisible by a perfect square). The result is 3.
___________________________________________________________________________
2) Given an array A of size N where, 1<= N <= 10^5. The task is to find the OR of all possible
sub-arrays of A and then the OR of all these results.
Examples:
Input : 1 4 6
Output : 7
All possible subarrays are
{1}, {1, 4}, {4, 6} and {1, 4, 6}
ORs of these subarrays are 1, 5, 6
and 7. OR of these ORs is 7.
Input : 10 100 1000
Output : 1006
(Note: This question can also be asked in this way: after finding ORs of subarrays just return all
distinct values of ORs)
__________________________________________________________________________
3)Given an array and an integer K, find the maximum for each and every contiguous subarray
of size K.
Examples :
Input: arr[] = {1, 2, 3, 1, 4, 5}, K = 3
Output: 3 3 4 5
Explanation: Maximum of 1, 2, 3 is 3
Maximum of 2, 3, 1 is 3
Maximum of 3, 1, 4 is 4
Maximum of 1, 4, 5 is 5
______________________________________________________________________
4)Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.
The following rules define a valid string:
Any left parenthesis '(' must have a corresponding right parenthesis ')'.
Any right parenthesis ')' must have a corresponding left parenthesis '('.
Left parenthesis '(' must go before the corresponding right parenthesis ')'.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "(())"
Output: true
Example 3:
Input: s = ")("
Output: false
Example 4:
Input: s = "()("
Output: false
Constraints:
1 <= s.length <= 104
s consists of parentheses only '()'.
___________________________________________________________________________
5)Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input
string is valid.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([])"
Output: true
Constraints:
1 <= s.length <= 104
s consists of parentheses only '()[]{}'.
Example 1:
Input: s =”()”
Output: true
Example 2:
Input: s = “()[]{}”
Output: true
Example 3:
Input: s = ”(]”
Output: false
____________________________________________________________________________
6)Cyclic Palindrome.
Description:
A string is said to be palindrome, if it reads the same from both the ends. Given a string S, you
are allowed to perform cyclic shifts. More formally, you can pick any one character from any end
(head or tail) and you can append that character at the other end. For example, if the string is
"abc", then if we do a shift using the character at head position then the string becomes "bca".
Similarly, if we do the shift using the character at the tail then the input string becomes "cab".
Your task is to find out the minimum number of shifts needed to make the given string, a
palindrome. In case, we can't convert the string to palindrome then print -1
Input Format:
First line starts with T i.e. number of test cases, and then T lines will follow each containing a
string "S".
Output Format:
Print the minimum number of cyclic shifts for each string if it can be made a palindrome, else -1.
Sample Input:
4
abbb
aaabb
aabb
abc
Sample Output:
-1
1
1
-1
Explanation:
For Test Case 2 (aaabb):
Shift the character at the tail to the head and the result will be "baaab", which is a palindrome.
This is an operation which requires minimum number of shifts to make the given string a
palindrome.
One way to convert the given string to palindrome is, shift the character at the head to the tail,
and the result will be "abba", which is a palindrome. Another way is to shift the character at the
tail to the head, and the result will be "baab", which is also a palindrome. Both require only one
shift.
___________________________________________________________________________
7)One person hands over the list of digits to Mr. String, But Mr. String understands only strings.
Within strings also he understands only vowels. Mr. String needs your help to find the total
number of pairs which add up to a certain digit D.The rules to calculate digit D are as follow :-
Take all digits and convert them into their textual representation.
Next, sum up the number of vowels i.e. {a, e, i, o, u} from all textual representations.
This sum is digit D
Now, once digit D is known, find out all unordered pairs of numbers in input whose sum is equal
to D. Refer example section for better understanding.
Constraints
1 <= N <= 100
1 <= value of each element in second line of input <= 100
Number 100, if and when it appears in input should be converted to textual representation as
hundred and not as one hundred. Hence number of vowels in number 100 should be 2 and not
4
Input
First line contains an integer N which represents number of elements to be processed as input
Second line contains N numbers separated by space
Output
Lower case representation of textual representation of number of pairs in input that sum up to
digit D
Note: – (If the count exceeds 100 print “greater 100”)
Examples
Input : 5
12345
Output : one
Input : 3
742
Output : zero
___________________________________________________________________________
8)Given an integer array of coins[ ] of size N representing different types of denominations and
an integer sum, the task is to count all combinations of coins to make a given value sum.
Note: Assume that you have an infinite supply of each type of coin.
Examples:
7)Given two strings text1 and text2, return the length of their longest common subsequence. If
there is no common subsequence, return 0.
A subsequence of a string is a new string generated from the original string with some
characters (can be none) deleted without changing the relative order of the remaining
characters.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
1 <= text1.length, text2.length <= 1000
text1 and text2 consist of only lowercase English characters.
_________________________________________________________________________
9) Given an m x n matrix, the task is to print all elements of the matrix in spiral form.
Examples:
Input: matrix = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16 }}
Output: 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
Explanation: The output is matrix in spiral format.
____________________________________________________________________
10)Given an array of n positive integers. We are required to write a program to print the
minimum product of k integers of the given array.
Examples:
Input : 198 76 544 123 154 675
k=2
Output : 9348
We get minimum product after multiplying
76 and 123.
Input : 11 8 5 7 5 100
k=4
Output : 1400
______________________________________________________________________
Input Format
The first line contains t, which represents number of strings. The next t lines contain strings.
Constraints
1<=t<=5
1<=x<=10000 where x denotes string length
Strings contain only lowercase english alphabets.
Output Format
For each string print Yes if there exists an Anagram that is a Palindrome, else print No, in a new
line
Sample Input:
3
daamm
rfeer
motor
Sample Output:
Yes
Yes
No
Explanation:
The string daamm has an anagram --> madam which is a palindrome. So print Yes.
The string rfeer has an anagram --> refer which is a palindrome. So print Yes.
The string motor has no anagram which is also a palindrome. So print No.
_______________________________________________________________________
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
1 <= n <= 231 - 1
______________________XXX_________________________