Project 2
Arrays and String:
Examples:
Input:arr [ ]= { 1,4,20,3,10,5} sum=33
Output: sum found between indexes 2 and 4
Explanation: sum of elements between indices 2 and 4 is 20+3+10=33
Input:arr[ ]= { 1,4, 0,0,3,10,5} sum=7
Output: sum found between indexes 1and 4
Explanation: sum of elements between indices 1and 4 is 4+0+0+3=7
Constraints: 1< = N<= 10^7
1<=Ai<=1010
Permutation of string:
Examples:
Input: S= " ABC "
Output: "ABC ", "ACB ", " BAC ", " BCA ", " CBA ", " CAB "
Input: S= " XY"
Output: " XY", " YX"
Determine if two trees are identical:
Count Leaves in Binary tree:
Example:
Input: Given tree is
4
/ \
8 10
/ / \
7 5 1
/
3
Output: 3
Explanation: Three Leaves are 3, 5 and 1.
Constraints: 1< = N <= 10^4
Lowest Common Ancestor:
Example:
3
/ \
5 1
/ \ / \
6 2 0 8
/ \
7 4
Input: root = [ 3,5, 1, 6, 2,0,8 , null ,null,7, 4] , p= 5, q=1
Output: 3
Explanation: The lCA of node 5 and 1 is 3.
Sorting:
Example1:
Input : N =8
Value [ ]= { 1, 2, 2, 1, 0, 2, 2 }
Output: 0 1 1 2 2 2 2
Explanation: All the 0s are segregated to the left end of the linked list , 2s to the right end of the list, and 1s is between.
Example2 :
Input: N =4
Value [ ] = { 2, 2, 0, 1 }
Output: 0 1 2 2
Explanation: After arranging all the 0s, 1s and 2s in the given format, the output will be 0 1 2 2 .
Constraints: 1< = N <= 10^3
Shortest source to destination path:
Example1:
Input: N = 11
M= 4
A1[ ]={ 2,1,2,5,7,1,9,3,6,8,8}
A2[ ]= {2, 1, 8, 3}
Output: 2 2 11 8 8 3 5 6 7 9
Explanation: Array elements of A1 [ ]are sorted according to a2[] so 2 comes first then1, now we append remaining elements in sorted order.
Example2 : N= 11
M=4
A1[ ]={2,1,2,5,7,1,9,3,6,8,8}
A2[ ]={99,22,44,56}
Output: 1 1 2 2 3 5 6 7 8 8 9
Explanation:No A1[] elements are a2[] so we cannot sort a1[]according to a2[ ].
Circle of string:
Example1:
Input: N=3
A[ ]={" abc","bcd", "cdf"}
Output: 0
Explanation:these string can't from a circle because no string has ' d' at the starting index.
Example2:
Input: N =4
A[ ]={ "ab", "bc", " cd", "da"}
Output: 1
Explanation: these string can from a circle of string.
Constraints:
1<N<10^4
1<length of string <20
Dynamic programming:
Longest common subsequence:
Examples:
Input: s1= " AGGTAB", S2 = "GXTXAYB"
Output 4:
Explanation: the longest subsequence which is present in both string is " GTAB"
Input: s1 = " ABCDGH", S2 = " AEDFHR"
Output:3
Explanation: the longest subsequent which is present in both strings is " ADH".
String contains all characters of another string:
Input: string= this is a test string, pattern = tist
Output: t "str"
Explanation :t "str"contains all the characters of pattern .
Input: string = " geeksforgeeks, pattern= "ork"
Output: "ksfor"
Power of 2:
Input: n=1
Output:True
Explanation: 2^ 5 = 32
Constraints: 1<T<=100
0<N<=1018
Argument print
1 yes
98. NO
EX: Input:N = 4
Output:yes
Explanation: 2^ 2=4
N=7
Output: No
Argument
Input n=1
Output:yes
Another one
Input: 98
Output:no