├── !DOC! ├── 1.png ├── 22.png ├── 333.png ├── Easy#1.png ├── Easy#2.png ├── Easy#3.png ├── Hard#1.png ├── Hard#2.png ├── Hard#3.png ├── isEasy.png ├── isHard.png ├── GFG-Logo.png ├── Medium#1.png ├── Medium#2.png ├── Medium#3.png ├── isMedium.png ├── GFG.Appreciation.Letter.jpg ├── GIF--Shinchan-vIxKKPtpfnL1K.gif ├── GIF--Pika-Wiggles-aoydQ5HRJUAbm.gif ├── 30-Days-of-Code-By-GeeksforGeeks.png └── GIF--Happy-Powerpuff-Girls-Qakyyrk1IKwuK8YtQ6.gif ├── Day<01> ├── D01.png ├── Solution.py ├── Solution.cpp ├── Solution.java ├── MySolution.java └── ReadME.MD ├── Day<02> ├── D02.png ├── Solution.py ├── MySolution.java ├── Solution.java ├── Solution.cpp └── ReadME.MD ├── Day<03> ├── D03.png ├── Solution.py ├── Solution.cpp ├── Solution.java ├── MySolution.java └── ReadME.MD ├── Day<04> ├── D04.png ├── Solution.py ├── Solution.cpp ├── Solution.java └── MySolution.java ├── Day<05> ├── D05.png ├── MySolution.java ├── Solution.py ├── Solution.cpp └── Solution.java ├── Day<06> ├── D06.png ├── Solution.py ├── Solution.java ├── MySolution.java ├── Solution.cpp └── ReadME.MD ├── Day<07> ├── D07.png ├── Solution.py ├── Solution.cpp ├── Solution.java └── MySolution.java ├── Day<08> ├── D08.png ├── Solution.py ├── MySolution.java ├── Solution.java └── Solution.cpp ├── Day<09> ├── D09.png ├── Solution.cpp ├── Solution.py ├── MySolution.java └── Solution.java ├── Day<10> ├── D10.png ├── Solution.py ├── MySolution.java ├── Solution.java ├── Solution.cpp └── ReadME.MD ├── Day<11> ├── D11.png ├── MySolution.java ├── Solution.py ├── Solution.cpp ├── Solution.java └── ReadME.MD ├── Day<12> ├── D12.png ├── Solution.py ├── Solution.cpp ├── Solution.java ├── MySolution.java └── ReadME.MD ├── Day<13> ├── D13.png ├── Solution.cpp ├── Solution.py ├── Solution.java └── MySolution.java ├── Day<14> ├── D14.png ├── Solution.py ├── Solution.cpp ├── Solution.java └── MySolution.java ├── Day<15> ├── D15.png ├── Solution.py ├── MySolution.java ├── Solution.cpp ├── Solution.java └── ReadME.MD ├── Day<16> ├── D16.png ├── Solution.py ├── MySolution.java ├── Solution.cpp ├── Solution.java └── ReadME.MD ├── Day<17> ├── D17.png ├── Solution.py ├── Solution.cpp ├── MySolution.java └── Solution.java ├── Day<18> ├── D18.png ├── Solution.py ├── MySolution.java ├── Solution.cpp ├── Solution.java └── ReadME.MD ├── Day<19> ├── D19.png ├── Solution.py ├── MySolution.java ├── Solution.cpp └── Solution.java ├── Day<20> ├── D20.png └── D20.a.png ├── Day<21> ├── D21.png ├── D21.a.png ├── D21.b.png ├── Solution.py ├── MySolution.java └── Solution.cpp ├── Day<22> ├── D22.png ├── D22.a.png └── D22.b.png ├── Day<23> ├── D23.png ├── Solution.py ├── Solution.cpp ├── MySolution.java └── Solution.java ├── Day<24> ├── D24.png ├── Solution.cpp ├── MySolution.java └── Solution.java ├── Day<25> ├── D25.png ├── Solution.py ├── Solution.cpp ├── MySolution.java └── Solution.java ├── Day<26> ├── D26.png ├── D26_Exp.png └── Solution.cpp ├── Day<27> ├── D27.png ├── Solution.py ├── Solution.cpp ├── Solution.java ├── MySolution.java └── ReadME.MD ├── Day<28> ├── D28.png ├── Solution.py ├── Solution.cpp ├── MySolution.java └── Solution.java ├── Day<29> ├── D29.png ├── MySolution.java ├── Solution.py ├── Solution.cpp └── Solution.java └── Day<30> ├── D30.png ├── Solution.py ├── MySolution.java ├── Solution.cpp └── Solution.java /!DOC!/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/1.png -------------------------------------------------------------------------------- /!DOC!/22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/22.png -------------------------------------------------------------------------------- /!DOC!/333.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/333.png -------------------------------------------------------------------------------- /!DOC!/Easy#1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Easy#1.png -------------------------------------------------------------------------------- /!DOC!/Easy#2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Easy#2.png -------------------------------------------------------------------------------- /!DOC!/Easy#3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Easy#3.png -------------------------------------------------------------------------------- /!DOC!/Hard#1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Hard#1.png -------------------------------------------------------------------------------- /!DOC!/Hard#2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Hard#2.png -------------------------------------------------------------------------------- /!DOC!/Hard#3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Hard#3.png -------------------------------------------------------------------------------- /!DOC!/isEasy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/isEasy.png -------------------------------------------------------------------------------- /!DOC!/isHard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/isHard.png -------------------------------------------------------------------------------- /Day<01>/D01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<01>/D01.png -------------------------------------------------------------------------------- /Day<02>/D02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<02>/D02.png -------------------------------------------------------------------------------- /Day<03>/D03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<03>/D03.png -------------------------------------------------------------------------------- /Day<04>/D04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<04>/D04.png -------------------------------------------------------------------------------- /Day<05>/D05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<05>/D05.png -------------------------------------------------------------------------------- /Day<06>/D06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<06>/D06.png -------------------------------------------------------------------------------- /Day<07>/D07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<07>/D07.png -------------------------------------------------------------------------------- /Day<08>/D08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<08>/D08.png -------------------------------------------------------------------------------- /Day<09>/D09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<09>/D09.png -------------------------------------------------------------------------------- /Day<10>/D10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<10>/D10.png -------------------------------------------------------------------------------- /Day<11>/D11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<11>/D11.png -------------------------------------------------------------------------------- /Day<12>/D12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<12>/D12.png -------------------------------------------------------------------------------- /Day<13>/D13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<13>/D13.png -------------------------------------------------------------------------------- /Day<14>/D14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<14>/D14.png -------------------------------------------------------------------------------- /Day<15>/D15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<15>/D15.png -------------------------------------------------------------------------------- /Day<16>/D16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<16>/D16.png -------------------------------------------------------------------------------- /Day<17>/D17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<17>/D17.png -------------------------------------------------------------------------------- /Day<18>/D18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<18>/D18.png -------------------------------------------------------------------------------- /Day<19>/D19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<19>/D19.png -------------------------------------------------------------------------------- /Day<20>/D20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<20>/D20.png -------------------------------------------------------------------------------- /Day<21>/D21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<21>/D21.png -------------------------------------------------------------------------------- /Day<22>/D22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<22>/D22.png -------------------------------------------------------------------------------- /Day<23>/D23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<23>/D23.png -------------------------------------------------------------------------------- /Day<24>/D24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<24>/D24.png -------------------------------------------------------------------------------- /Day<25>/D25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<25>/D25.png -------------------------------------------------------------------------------- /Day<26>/D26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<26>/D26.png -------------------------------------------------------------------------------- /Day<27>/D27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<27>/D27.png -------------------------------------------------------------------------------- /Day<28>/D28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<28>/D28.png -------------------------------------------------------------------------------- /Day<29>/D29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<29>/D29.png -------------------------------------------------------------------------------- /Day<30>/D30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<30>/D30.png -------------------------------------------------------------------------------- /!DOC!/GFG-Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/GFG-Logo.png -------------------------------------------------------------------------------- /!DOC!/Medium#1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Medium#1.png -------------------------------------------------------------------------------- /!DOC!/Medium#2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Medium#2.png -------------------------------------------------------------------------------- /!DOC!/Medium#3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/Medium#3.png -------------------------------------------------------------------------------- /!DOC!/isMedium.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/isMedium.png -------------------------------------------------------------------------------- /Day<20>/D20.a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<20>/D20.a.png -------------------------------------------------------------------------------- /Day<21>/D21.a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<21>/D21.a.png -------------------------------------------------------------------------------- /Day<21>/D21.b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<21>/D21.b.png -------------------------------------------------------------------------------- /Day<22>/D22.a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<22>/D22.a.png -------------------------------------------------------------------------------- /Day<22>/D22.b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<22>/D22.b.png -------------------------------------------------------------------------------- /Day<26>/D26_Exp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/Day<26>/D26_Exp.png -------------------------------------------------------------------------------- /!DOC!/GFG.Appreciation.Letter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/GFG.Appreciation.Letter.jpg -------------------------------------------------------------------------------- /!DOC!/GIF--Shinchan-vIxKKPtpfnL1K.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/GIF--Shinchan-vIxKKPtpfnL1K.gif -------------------------------------------------------------------------------- /!DOC!/GIF--Pika-Wiggles-aoydQ5HRJUAbm.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/GIF--Pika-Wiggles-aoydQ5HRJUAbm.gif -------------------------------------------------------------------------------- /!DOC!/30-Days-of-Code-By-GeeksforGeeks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/30-Days-of-Code-By-GeeksforGeeks.png -------------------------------------------------------------------------------- /!DOC!/GIF--Happy-Powerpuff-Girls-Qakyyrk1IKwuK8YtQ6.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guru-shreyansh/GeeksforGeeks-30-Days-of-Code/HEAD/!DOC!/GIF--Happy-Powerpuff-Girls-Qakyyrk1IKwuK8YtQ6.gif -------------------------------------------------------------------------------- /Day<02>/Solution.py: -------------------------------------------------------------------------------- 1 | # function to change 'N' from base10 to base9 2 | class Solution: 3 | def findNth(self, N): 4 | # taking remainders by 9 5 | # dividing 'N' by 9 till N become 0 6 | s = "" 7 | while (N): 8 | s = str(N%9) + s 9 | N = N//9 10 | return int(s) 11 | 12 | t = int(input()) 13 | for i in range(0,t): 14 | n = int(input()) 15 | obj = Solution() 16 | s = obj.findNth(n) 17 | print(s) 18 | -------------------------------------------------------------------------------- /Day<01>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prank(self, a, n): 3 | for i in range(n): 4 | a[i] = a[i] + (a[a[i]] % n) * n 5 | 6 | for i in range(n): 7 | a[i] = a[i] // n 8 | 9 | if __name__ == '__main__': 10 | t = int(input()) 11 | for _ in range(t): 12 | n = int(input()) 13 | a = list(map(int, input().strip().split())) 14 | obj = Solution() 15 | obj.prank(a, n) 16 | for i in a: 17 | print(i, end=" ") 18 | print() 19 | -------------------------------------------------------------------------------- /Day<03>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestpositive(self, array, n): 3 | array.sort() 4 | result = 1 5 | for i in range(n): 6 | if array[i] <= result: 7 | result = result + array[i] 8 | return result 9 | 10 | if __name__ == '__main__': 11 | t = int(input()) 12 | for _ in range(t): 13 | n = int(input()) 14 | array = list(map(int, input().strip().split())) 15 | ob = Solution() 16 | print(ob.smallestpositive(array,n)) 17 | -------------------------------------------------------------------------------- /Day<10>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def repeatedStringMatch(self, A,B): 3 | S=A 4 | ret=1 5 | 6 | while len(S)/Solution.py: -------------------------------------------------------------------------------- 1 | from bisect import bisect_left as lower_bound 2 | class Solution: 3 | def ValidPair(self, a, n): 4 | a = sorted(a) 5 | ans = 0 6 | for i in range(n): 7 | if (a[i] <= 0): 8 | continue 9 | # search for first element >= (-a[i] + 1) 10 | j = lower_bound(a,-a[i] + 1) 11 | ans += i - j 12 | return ans 13 | 14 | if __name__ == '__main__': 15 | t = int(input()) 16 | for _ in range(t): 17 | n = int(input()) 18 | array = list(map(int, input().strip().split())) 19 | obj = Solution() 20 | ans = obj.ValidPair(array, n) 21 | print(ans) 22 | -------------------------------------------------------------------------------- /Day<17>/Solution.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | class Solution: 4 | def help_classmate(self, arr, n): 5 | stack = deque() 6 | ret = [-1 for _ in range(n)] 7 | 8 | for i in range(n): 9 | while len(stack) and arr[i] < arr[stack[-1]]: 10 | ret[ stack.pop() ] = arr[i] 11 | stack.append(i) 12 | 13 | return ret 14 | 15 | if __name__=="__main__": 16 | for _ in range(int(input())): 17 | n = int(input()) 18 | arr = [ int(x) for x in input().split() ] 19 | obj = Solution() 20 | result = obj.help_classmate(arr,n) 21 | for i in result: 22 | print(i,end=' ') 23 | print() 24 | -------------------------------------------------------------------------------- /Day<02>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main (String[] args) 7 | { 8 | Scanner ob = new Scanner(System.in); 9 | int t = ob.nextInt(); 10 | while (t-->0) 11 | { 12 | long n = ob.nextLong(); 13 | Solution ab = new Solution(); 14 | long k = ab.findNth(n); 15 | System.out.println(k); 16 | } 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | long findNth(long N) 23 | { 24 | StringBuilder sss = new StringBuilder(""); 25 | while ( 0 < N ) 26 | { 27 | sss.append(N % 9); 28 | N /= 9; 29 | } 30 | return Long.valueOf(sss.reverse().toString()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Day<14>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def digitSum(self, n): 4 | sum = 0 5 | while (n): 6 | sum += (n % 10) 7 | n //= 10 8 | return sum 9 | 10 | def RulingPair(self, arr, n): 11 | map = dict() 12 | ans = -1 13 | for i in range(n): 14 | dSum = self.digitSum(arr[i]) 15 | 16 | if (dSum in map): 17 | ans = max(ans, map[dSum] + arr[i]) 18 | map[dSum] = max(map.get(dSum, 0) , arr[i]) 19 | return ans 20 | 21 | if __name__ == '__main__': 22 | t = int(input()) 23 | for _ in range(t): 24 | n = int(input()) 25 | arr = list(map(int, input().strip().split())) 26 | obj = Solution(); 27 | print(obj.RulingPair(arr,n)) 28 | -------------------------------------------------------------------------------- /Day<02>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main (String[] args) 7 | { 8 | Scanner ob = new Scanner(System.in); 9 | int t = ob.nextInt(); 10 | while (t-->0) 11 | { 12 | long n = ob.nextLong(); 13 | Solution ab = new Solution(); 14 | long k = ab.findNth(n); 15 | System.out.println(k); 16 | } 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | long findNth(long N) 23 | { 24 | String s = ""; 25 | // taking remainders by 9 till 'N' become 0 26 | while ( N!=0 ) 27 | { 28 | s = (char)( N%9 + '0' ) + s; 29 | N = N/9; 30 | } 31 | return Long.parseLong(s); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Day<12>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumBitDiff(self, arr, n): 3 | ans = 0 # Initialize result 4 | 5 | # traverse over all bits 6 | for i in range(0, 32): 7 | 8 | # count number of elements with i'th bit set 9 | count = 0 10 | for j in range(0,n): 11 | if ( (arr[j] & (1 << i)) ): 12 | count+=1 13 | 14 | # Add "count * (n - count) * 2" to the answer 15 | ans += (count * (n - count) * 2); 16 | 17 | return ans 18 | 19 | 20 | if __name__=="__main__": 21 | t=int(input()) 22 | for _ in range(t): 23 | n=int(input()) 24 | arr=[int(x) for x in input().strip().split()] 25 | obj = Solution(); 26 | print(obj.sumBitDiff(arr,n)) 27 | -------------------------------------------------------------------------------- /Day<01>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | void prank(long long a[], int n) 8 | { 9 | long long N = n; 10 | for (int i=0; i> t; 24 | while (t--) 25 | { 26 | int n; 27 | cin >> n; 28 | long long a[n]; 29 | for (int i=0; i> a[i]; 31 | Solution ob; 32 | ob.prank(a,n); 33 | 34 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | long long smallestpositive(vector array, int n) 8 | { 9 | sort(array.begin(),array.end()); 10 | long long result = 1; 11 | for (int i = 0; i < n && array[i] <= result; i++) 12 | result = result + array[i]; 13 | return result; 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | int t; 20 | cin >> t; 21 | while (t--) 22 | { 23 | int n; 24 | cin >> n; 25 | vector array(n); 26 | for (int i=0; i> array[i]; 29 | } 30 | Solution ob; 31 | cout << ob.smallestpositive(array,n) << "\n"; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Day<07>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | long long ValidPair(int a[], int n) 8 | { 9 | sort(a, a+n); 10 | long long ans = 0; 11 | for (int i = 0; i < n; ++i) 12 | { 13 | if (a[i] <= 0) 14 | continue; 15 | // search for first element >= (-a[i] + 1) 16 | int j = lower_bound(a, a + n, -a[i] + 1) - a; 17 | ans += (long long )(i - j); 18 | } 19 | return ans; 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | int t; 26 | cin>>t; 27 | while (t--) 28 | { 29 | int n; 30 | cin>>n; 31 | int array[n]; 32 | for (int i = 0; i < n; ++i) 33 | cin>>array[i]; 34 | Solution obj; 35 | cout << obj.ValidPair(array, n) <<"\n"; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Day<19>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def candies(self, m, n): 3 | i = 0 4 | X = (m * n) - m - n 5 | 6 | queue = [] 7 | queue.append(X) 8 | set = {X} 9 | 10 | count = 0 11 | while (len(queue) > 0): 12 | curr = queue[0] 13 | queue.remove(queue[0]) 14 | 15 | count += 1 16 | key = curr-m 17 | if (key > 0 and key not in set): 18 | queue.append(key) 19 | set.add(key) 20 | 21 | key = curr-n 22 | if (key > 0 and key not in set): 23 | queue.append(key) 24 | set.add(key) 25 | return count 26 | 27 | if __name__ == '__main__': 28 | t = int(input()) 29 | for _ in range(t): 30 | arr = list(map(int, input().strip().split())) 31 | m = arr[0] 32 | n = arr[1] 33 | obj = Solution() 34 | print(obj.candies(m,n)) 35 | -------------------------------------------------------------------------------- /Day<02>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | // function to change 'N' from base10 to base9 8 | long long findNth(long long N) 9 | { 10 | long long base10num = N; 11 | long long base9num = 0; 12 | long long pos = 1; 13 | while ( base10num > 0 ) 14 | { 15 | // taking remainder with 9 16 | base9num += pos * (base10num % 9); 17 | // dividing number with 9 18 | base10num /= 9; 19 | // multiplying position with 10 20 | pos *= 10; 21 | } 22 | return base9num; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | int t; 29 | cin >> t; 30 | while (t--) 31 | { 32 | long long n , ans; 33 | cin >> n; 34 | Solution obj; 35 | ans = obj.findNth(n); 36 | cout << ans << endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Day<19>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int m = sc.nextInt(); 13 | int n = sc.nextInt(); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.candies(m,n)); 16 | t--; 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | static int candies(int m, int n) 24 | { // Any number > "LCM(m,n)-m-n" can be formed 25 | return (m-1)*(n-1)/2; // So we only need to check for numbers less 26 | } // than or equal to (mn - m - n) 27 | /** Chicken McNugget Problem / Two Stamp Problem / Frobenius Problem **/ 28 | } 29 | -------------------------------------------------------------------------------- /Day<03>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n = sc.nextInt(); 13 | long array[] = new long[n]; 14 | for (int i=0; i/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def build_bridges_util(self,str1, str2, i, j, dp): 3 | if i >= len(str1) or j >= len(str2): 4 | return 0 5 | if dp[i][j] != -1: 6 | return dp[i][j] 7 | 8 | if str1[i] == str2[j]: 9 | dp[i][j] = 1 + self.build_bridges_util(str1, str2, i + 1, j + 1, dp) 10 | return dp[i][j] 11 | 12 | dp[i][j] = max(self.build_bridges_util(str1, str2, i + 1, j, dp), self.build_bridges_util(str1, str2, i, j + 1, dp)) 13 | return dp[i][j] 14 | 15 | def build_bridges(self, str1, str2): 16 | n1, n2 = len(str1), len(str2) 17 | dp = [[-1 for i in range(n2 + 1)] for j in range(n1 + 1)] 18 | return self.build_bridges_util(str1, str2, 0, 0,dp) 19 | 20 | if __name__ == '__main__': 21 | T = int(input()) 22 | for _ in range(T): 23 | obj = Solution() 24 | str1, str2 = input().split() 25 | print(obj.build_bridges(str1, str2)) 26 | -------------------------------------------------------------------------------- /Day<14>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | private: 7 | int digitSum(int n) 8 | { 9 | int sum = 0; 10 | while (n) 11 | { 12 | sum += (n % 10); 13 | n /= 10; 14 | } 15 | return sum; 16 | } 17 | 18 | public: 19 | int RulingPair(vector arr, int n) 20 | { 21 | unordered_map map; 22 | int ans = -1; 23 | 24 | for (int i = 0; i < n; i++) 25 | { 26 | int dSum = digitSum(arr[i]); 27 | if (map[dSum] != 0) 28 | { 29 | ans = max(ans, map[dSum] + arr[i]); 30 | } 31 | mp[dSum] = max(map[dSum], arr[i]); 32 | } 33 | return ans; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | int t; 40 | cin>>t; 41 | while (t--) 42 | { 43 | int n; 44 | cin>>n; 45 | vector arr(n); 46 | for (int i = 0; i < n; ++i) 47 | cin>>arr[i]; 48 | Solution obj; 49 | cout << obj.RulingPair(arr,n)<<"\n"; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Day<12>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | long long sumBitDiff(int arr[], int n) 8 | { 9 | long long ans = 0; // Initialize result 10 | // traverse over all bits 11 | for (int i = 0; i < 32; i++) 12 | { 13 | // count number of elements with i'th bit set 14 | int count = 0; 15 | for (int j = 0; j < n; j++) 16 | if ( (arr[j] & (1 << i)) ) 17 | count++; 18 | 19 | // Add "count * (n - count) * 2" to the answer 20 | ans += (count * (n - count) * 2); 21 | } 22 | return ans; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while (t--) 31 | { 32 | int n; 33 | cin>>n; 34 | int arr[n]; 35 | for (int i = 0; i < n; i++) 36 | cin >> arr[i]; 37 | Solution obj; 38 | cout << obj.sumBitDiff(arr, n) << "\n"; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Day<17>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | vector help_classmate(vector arr, int n) 8 | { 9 | stack s; 10 | vector result(n,-1); 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (s.empty()) 15 | { 16 | s.push(i); 17 | continue; 18 | } 19 | while (s.empty() == false && arr[s.top()] > arr[i]) 20 | { 21 | result[s.top()] = arr[i]; 22 | s.pop(); 23 | } 24 | s.push(i); 25 | } 26 | return result; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | int t; 33 | cin>>t; 34 | while (t--) 35 | { 36 | int n; 37 | cin>>n; 38 | vector array(n); 39 | for (int i = 0; i < n; ++i) 40 | { 41 | cin>>array[i]; 42 | } 43 | Solution obj; 44 | vector result = obj.help_classmate(array,n); 45 | for (int i = 0; i < n; ++i) 46 | { 47 | cout</Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def catchThieves(self, arr, n, k): 3 | i = 0 4 | l = 0 5 | r = 0 6 | result = 0 7 | thief = [] 8 | police = [] 9 | 10 | while i < n: 11 | if arr[i] == 'P': 12 | police.append(i) 13 | elif arr[i] == 'T': 14 | thief.append(i) 15 | i += 1 16 | 17 | # track lowest current indices of thief: thief[l], police: police[r] 18 | while l < len(thief) and r < len(police): 19 | 20 | # can be caught 21 | if (abs( thief[l] - police[r] ) <= k): 22 | result += 1 23 | l += 1 24 | r += 1 25 | 26 | # increment the minimum index 27 | elif thief[l] < police[r]: 28 | l += 1 29 | else: 30 | r += 1 31 | return result 32 | 33 | if __name__=='__main__': 34 | t = int(input()) 35 | for _ in range(t): 36 | line1 = list(map(int, input().strip().split())) 37 | n = line1[0] 38 | k = line1[1] 39 | arr = list(input().strip().split()) 40 | obj = Solution() 41 | print(obj.catchThieves(arr, n, k)) 42 | -------------------------------------------------------------------------------- /Day<16>/Solution.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | class Solution: 4 | def Reduced_String(self, k, s): 5 | if k == 1: 6 | ret = '' 7 | return ret 8 | 9 | q = deque() 10 | for c in s: 11 | # entries in deque will consist of a char 12 | # and an integer representing how many times it is repeating 13 | if len(q) and q[-1][0]==c: 14 | q[-1][1] += 1 # incrementing count 15 | if q[-1][1]==k: 16 | q.pop() # popping if count is k 17 | else: 18 | q.append( [c,1] ) # adding new character 19 | 20 | ret='' 21 | while len(q): # generating output string 22 | ret += q[0][0] * q[0][1] 23 | q.popleft() 24 | 25 | return ret 26 | 27 | if __name__=="__main__": 28 | t=int(input()) 29 | for _ in range(t): 30 | k=int(input()) 31 | s=input().strip() 32 | obj = Solution() 33 | print(obj.Reduced_String(k,s)) 34 | -------------------------------------------------------------------------------- /Day<03>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n = sc.nextInt(); 13 | long array[] = new long[n]; 14 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int candies(int m, int n) 9 | { 10 | int X = (m * n) - m - n; 11 | unordered_set hash_set; 12 | queue queue; 13 | queue.push(X); 14 | hash_set.insert(X); 15 | 16 | int count = 0; 17 | while (queue.size() > 0) 18 | { 19 | int curr = queue.front(); 20 | queue.pop(); 21 | count++; 22 | 23 | int key = curr-m; 24 | if (key > 0 && (hash_set.find(key) == hash_set.end())) 25 | { 26 | queue.push(key); 27 | hash_set.insert(key); 28 | } 29 | 30 | key = curr-n; 31 | if (key > 0 && (hash_set.find(key) == hash_set.end())) 32 | { 33 | queue.push(key); 34 | hash_set.insert(key); 35 | } 36 | } 37 | return count; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while (t--) 46 | { 47 | int m,n; 48 | cin>>m>>n; 49 | Solution obj; 50 | cout << obj.candies(m,n)</Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def min_sprinklers(self, gallery, n): 3 | sprinklers = [] 4 | for i in range(n): 5 | if -1/Solution.py: -------------------------------------------------------------------------------- 1 | from math import inf 2 | 3 | class Solution: 4 | def recreationalSpot(self, arr, n): 5 | l=[] 6 | if len(arr)<=2: 7 | return False 8 | if len(arr)==3: 9 | if arr[0]temp[j]: 25 | while l and l[-1]<=temp[j]: 26 | l.pop() 27 | 28 | if l and l[-1]/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 9 | int t = Integer.parseInt(read.readLine()); 10 | 11 | while (t-- > 0) 12 | { 13 | int n = Integer.parseInt(read.readLine()); 14 | String input_line[] = read.readLine().trim().split("\\s+"); 15 | long a[] = new long[n]; 16 | for (int i=0; i/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | class GURU 6 | { 7 | public static void main (String[] args) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while (t-->0) 12 | { 13 | int n = sc.nextInt(); 14 | int arr[]= new int[n]; 15 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool recreationalSpot(int arr[], int n) 8 | { 9 | if (n < 3) 10 | return false; 11 | stack stack; 12 | int temp[n]; 13 | temp[0] = arr[0]; 14 | for (int i = 1; i = 0; j--) 18 | { 19 | if (arr[j] > temp[j]) 20 | { 21 | while (!stack.empty() && stack.top() <= temp[j]) 22 | stack.pop(); 23 | if (!stack.empty() && stack.top() < arr[j]) 24 | return true; 25 | stack.push(arr[j]); 26 | } 27 | } 28 | return false; 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | int t; 35 | cin>>t; 36 | while (t--) 37 | { 38 | int n; 39 | cin>>n; 40 | int arr[n]; 41 | for (int i=0; i>arr[i]; 43 | Solution ob; 44 | if (ob.recreationalSpot(arr, n)) 45 | cout<<"True"</MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | String str1 = sc.next(); 13 | String str2 = sc.next(); 14 | Sol obj = new Sol(); 15 | System.out.println(obj.build_bridges(str1,str2)); 16 | } 17 | } 18 | } 19 | 20 | class Sol 21 | { 22 | public static int build_bridges(String str1 , String str2) 23 | { 24 | return longestCommonSubsequence(str1 , str2); 25 | } 26 | public static int longestCommonSubsequence(String S1, String S2) 27 | { 28 | char[] C1 = S1.toCharArray(); 29 | char[] C2 = S2.toCharArray(); 30 | 31 | int[][] _Dp = new int[C1.length + 1][C2.length + 1]; 32 | for (int i=0; i/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) throws Exception 7 | { 8 | BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 9 | int t = Integer.parseInt(read.readLine()); 10 | 11 | while (t-- > 0) 12 | { 13 | int n = Integer.parseInt(read.readLine()); 14 | String input_line[] = read.readLine().trim().split("\\s+"); 15 | long a[] = new long[n]; 16 | for (int i=0; i/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def Maximum_Sum(self, mat, N, K): 3 | sum_matrix = [[0 for _ in range(N) ] for _ in range(N)] 4 | 5 | for r in range(N): 6 | for c in range(N): 7 | sum_matrix[r][c] = mat[r][c] 8 | 9 | if r>0: 10 | sum_matrix[r][c] += sum_matrix[r-1][c] 11 | 12 | if c>0: 13 | sum_matrix[r][c] += sum_matrix[r][c-1] 14 | 15 | if r>0 and c>0: 16 | sum_matrix[r][c] -= sum_matrix[r-1][c-1] 17 | 18 | ret=0 19 | for r in range(K-1,N): 20 | for c in range(K-1,N): 21 | value = sum_matrix[r][c] 22 | 23 | if r>K-1: 24 | value -= sum_matrix[r-K][c] 25 | 26 | if c>K-1: 27 | value -= sum_matrix[r][c-K] 28 | 29 | if r>K-1 and c>K-1: 30 | value += sum_matrix[r-K][c-K] 31 | 32 | ret = max(ret,value) 33 | 34 | return ret 35 | -------------------------------------------------------------------------------- /Day<08>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxCandy(self, height, n): 3 | # To store the maximum water so far 4 | maximum = 0; 5 | 6 | # Both the pointers are pointing at the first and the last buildings respectively 7 | i = 0 8 | j = n - 1 9 | 10 | # While the water can be stored between the currently chosen buildings 11 | while (i < j): 12 | # Update maximum water so far and increment i 13 | if (height[i] < height[j]): 14 | maximum = max(maximum, (j - i - 1) * height[i]); 15 | i += 1; 16 | 17 | # Update maximum water so far and decrement j 18 | elif (height[j] < height[i]): 19 | maximum = max(maximum, (j - i - 1) * height[j]); 20 | j -= 1; 21 | 22 | # Any of the pointers can be updated (or both) 23 | else: 24 | maximum = max(maximum, (j - i - 1) * height[i]); 25 | i += 1; 26 | j -= 1; 27 | 28 | return maximum; 29 | 30 | if __name__ == '__main__': 31 | t = int(input()) 32 | for _ in range(t): 33 | n = int(input()) 34 | arr = list(map(int, input().strip().split())) 35 | obj = Solution() 36 | print(obj.maxCandy(arr,n)) 37 | -------------------------------------------------------------------------------- /Day<23>/Solution.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | class Solution: 4 | def Kclosest(self, arr, n, x, k): 5 | max_heap = [ ( -1*abs(x-arr[i]) , -1*arr[i] ) for i in range(k) ] 6 | heapq.heapify(max_heap) 7 | # python heapq generates min heap it can be used as max_heap by multiplying elements by -1 8 | 9 | for i in range(k,n): 10 | dist = -1*max_heap[0][0] 11 | hno = -1*max_heap[0][1] 12 | if abs(arr[i]-x)/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class GURU 5 | { 6 | public static void main (String[] args) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0 11 | { 12 | int n = sc.nextInt(); 13 | int array[] = new int[n]; 14 | for (int i = 0; i < n; ++i) 15 | { 16 | array[i] = sc.nextInt(); 17 | } 18 | Solution ob = new Solution(); 19 | int ans[] = ob.help_classmate(array,n); 20 | 21 | for (int i=0; i/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Solution 5 | { 6 | static int maxCandy(int height[], int n) 7 | { 8 | if ( n < 3 ) return 0; 9 | int start = 0; 10 | int end = n-1; 11 | int currArea = 0; 12 | int maxFound = ( n-2 )*( Math.min(height[0] , height[n-1]) ); 13 | 14 | while ( start < end ) 15 | { 16 | currArea = Math.min(height[start] , height[end]) * (end-start-1); 17 | maxFound = Math.max(currArea , maxFound); 18 | 19 | if ( height[start] < height[end] ) 20 | ++start; 21 | else 22 | --end; 23 | } 24 | return maxFound; 25 | } 26 | } 27 | 28 | class GURU 29 | { 30 | public static void main(String[] args) 31 | { 32 | Scanner sc = new Scanner(System.in); 33 | int t = sc.nextInt(); 34 | while (t > 0) 35 | { 36 | int n = sc.nextInt(); 37 | int height[] = new int[n]; 38 | for (int i = 0; i < n; ++i) 39 | { 40 | height[i] = sc.nextInt(); 41 | } 42 | Solution ob = new Solution(); 43 | System.out.println(ob.maxCandy(height,n)); 44 | t--; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Day<10>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = Integer.parseInt(sc.nextLine()); 10 | while (t > 0) 11 | { 12 | String A = sc.nextLine(); 13 | String B = sc.nextLine(); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.repeatedStringMatch(A,B)); 16 | t--; 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | static int repeatedStringMatch(String A, String B) 24 | { 25 | if ( A.indexOf(B) != -1 ) 26 | return 1; 27 | 28 | StringBuilder aaa = new StringBuilder(""); 29 | StringBuilder bbb = new StringBuilder(""); 30 | 31 | int length = B.length()+A.length(); 32 | int repeated = 0; 33 | 34 | while ( (aaa.indexOf(B) == -1) && (aaa.length() < length) ) 35 | { 36 | aaa.append(A); 37 | ++repeated; 38 | } 39 | 40 | if ( aaa.indexOf(B) == -1 ) 41 | return -1; 42 | return repeated; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Day<12>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | int n = sc.nextInt(); 13 | int arr[] = new int[n]; 14 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int transfigure (string A, string B) 8 | { 9 | if (A.length () != B.length ()) 10 | { 11 | return -1; 12 | } 13 | 14 | unordered_map m; 15 | int n = A.length (); 16 | 17 | for (int i = 0; i < n; i++) 18 | m[A[i]]++; 19 | for (int i = 0; i < n; i++) 20 | if (m[B[i]]--); 21 | 22 | for (auto i : m) 23 | { 24 | if (i.second != 0) 25 | { 26 | return -1; 27 | } 28 | } 29 | int i = n - 1, j = n - 1; 30 | int res = 0; 31 | while (i >= 0 and j >= 0) 32 | { 33 | while (i >= 0 and A[i] != B[j]) 34 | { 35 | i--; 36 | res++; 37 | } 38 | i--; 39 | j--; 40 | } 41 | return res; 42 | } 43 | }; 44 | 45 | int main() 46 | { 47 | int t; cin >> t; 48 | while (t--) 49 | { 50 | string A, B; 51 | cin >> A >> B; 52 | Solution obj; 53 | cout << obj.transfigure (A, B) << endl; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Day<27>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int catchThieves(char arr[], int n, int k) 8 | { 9 | int result = 0; 10 | vector thief; 11 | vector police; 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | if (arr[i] == 'P') 16 | police.push_back(i); 17 | else if (arr[i] == 'T') 18 | thief.push_back(i); 19 | } 20 | 21 | // track lowest current indices of thief: thi[l], police: pol[r] 22 | int l = 0, r = 0; 23 | while (l < thief.size() && r < police.size()) 24 | { 25 | // can be caught 26 | if (abs(thief[l] - police[r]) <= k) 27 | { 28 | result++; 29 | l++; 30 | r++; 31 | } 32 | // increment the minimum index 33 | else if (thief[l] < police[r]) 34 | l++; 35 | else 36 | r++; 37 | } 38 | return result; 39 | } 40 | }; 41 | 42 | int main() 43 | { 44 | int t; 45 | cin>>t; 46 | while (t--) 47 | { 48 | int n, k; 49 | cin>>n >> k; 50 | char arr[n]; 51 | for (int i = 0; i < n; ++i) 52 | cin>>arr[i]; 53 | Solution obj; 54 | cout</MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.math.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = Integer.parseInt(sc.next()); 10 | while (t-- > 0) 11 | { 12 | int k = Integer.parseInt(sc.next()); 13 | String s = sc.next(); 14 | Solution T = new Solution(); 15 | System.out.println(T.reduced_String(k, s)); 16 | } 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | public static String reduced_String(int k, String s) 23 | { 24 | if (k==1) return ""; 25 | StringBuilder output = new StringBuilder(s); 26 | int count[] = new int[output.length()]; 27 | 28 | for (int i=0; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class GfG 5 | { 6 | public static void main (String[] args) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n = sc.nextInt(); 13 | int array[] = new int[n]; 14 | for (int i = 0; i < n; ++i) 15 | { 16 | array[i] = sc.nextInt(); 17 | } 18 | Solution ob = new Solution(); 19 | int ans[] = ob.help_classmate(array,n); 20 | 21 | for (int i=0; i s = new Stack(); 34 | int result[] = new int[n]; 35 | Arrays.fill(result, -1); 36 | 37 | for (int i = 0; i < n; i++) 38 | { 39 | if (s.empty()) 40 | { 41 | s.push(i); 42 | continue; 43 | } 44 | while (s.empty() == false && arr[s.peek()] > arr[i]) 45 | { 46 | result[s.peek()] = arr[i]; 47 | s.pop(); 48 | } 49 | s.push(i); 50 | } 51 | return result; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Day<15>/Solution.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | def countPairs(head,x): 4 | if head==None or head.nxt==None or x<2: 5 | return 0 6 | dq = deque() 7 | walk = head 8 | while walk: 9 | dq.append(walk.val) 10 | walk=walk.nxt 11 | 12 | ret=0 13 | l=dq.popleft() 14 | r=dq.pop() 15 | while(1): 16 | if l+r==x: 17 | ret+=1 18 | if len(dq)==0: 19 | return ret 20 | if l+r>x: 21 | l=dq.popleft() 22 | else: 23 | r=dq.pop() 24 | 25 | def countTriplets(head,x): 26 | ret = 0 27 | while head != None: 28 | ret += countPairs(head.nxt, x-head.val) 29 | head = head.nxt 30 | return ret 31 | 32 | class Node: 33 | def __init__(self,x): 34 | self.val=x 35 | self.nxt=None 36 | 37 | if __name__ == '__main__': 38 | t=int(input()) 39 | for _ in range(t): 40 | line = input().strip().split() 41 | n = int(line[0]) 42 | x = int(line[1]) 43 | line = input().strip().split() 44 | 45 | head = Node(int(line[0])) 46 | tail = head 47 | for i in range(1,n): 48 | tail.nxt = Node(int(line[i])) 49 | tail = tail.nxt 50 | 51 | print(countTriplets(head,x)) 52 | -------------------------------------------------------------------------------- /Day<12>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | int n = sc.nextInt(); 13 | int arr[] = new int[n]; 14 | for (int i=0; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = Integer.parseInt(sc.nextLine()); 10 | while (t > 0) 11 | { 12 | String A = sc.nextLine(); 13 | String B = sc.nextLine(); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.repeatedStringMatch(A,B)); 16 | t--; 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | static boolean issubstring(String str2, String rep1) 24 | { 25 | int M = str2.length(); 26 | int N = rep1.length(); 27 | 28 | for (int i = 0; i <= N-M; i++) 29 | { 30 | int j; 31 | for (j = 0; j < M; j++) 32 | if (rep1.charAt(i + j) != str2.charAt(j)) 33 | break; 34 | if (j == M) 35 | return true; 36 | } 37 | return false; 38 | } 39 | 40 | static int repeatedStringMatch(String A, String B) 41 | { 42 | int ans = 1; 43 | String S = A; 44 | 45 | while (S.length() < B.length()) 46 | { 47 | S += A; 48 | ans++; 49 | } 50 | if (issubstring(B, S)) return ans; 51 | if (issubstring(B, S + A)) 52 | return ans + 1; 53 | return -1; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Day<10>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | private: 7 | bool isSubString(string s, string sub) 8 | { 9 | int count = 0; 10 | for (int i = 0; i < s.length(); i++) 11 | { 12 | if (s[i] == sub[count]) 13 | count++; 14 | else 15 | i-=count; 16 | count = 0; 17 | if (count == sub.length()) 18 | return true; 19 | } 20 | return false; 21 | } 22 | 23 | public: 24 | int repeatedStringMatch(string A, string B) 25 | { 26 | string temp = A; 27 | int count = ceil(float(B.length()) / float(A.length())); 28 | 29 | for (int i=1; i>t; 48 | while (t--) 49 | { 50 | string A,B; 51 | cin>>A; 52 | cin>>B; 53 | Solution obj; 54 | cout</Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | 11 | while (0 < t) 12 | { 13 | int n = sc.nextInt(); 14 | int array[] = new int[n]; 15 | for (int i=0; i map = new HashMap(); 40 | 41 | for (int i = 0; i < 1000 ; i++) 42 | { 43 | map.put(i, 0); 44 | } 45 | int ans = -1; 46 | for (int i = 0; i < n; i++) 47 | { 48 | int dSum = digitSum(arr[i]); 49 | if ((int)map.get(dSum) != 0) 50 | { 51 | ans = Math.max(ans, (int)map.get(dSum) + arr[i]); 52 | } 53 | mp.put(dSum,Math.max((int)map.get(dSum), arr[i])); 54 | } 55 | return ans; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Day<09>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def transfigure(self, A, B): 3 | m = len(A) 4 | n = len(B) 5 | 6 | # Check whether conversion is possible or not 7 | if n != m: 8 | return -1 9 | 10 | count = {} 11 | keys = count.keys() 12 | 13 | # count characters in A 14 | for i in A: 15 | if i in keys: 16 | count[i] += 1 17 | else: 18 | count[i] = 1 19 | 20 | # subtract count for every char in B 21 | for i in B: 22 | if i in keys: 23 | count[i] -= 1 24 | else: 25 | count[i] = 1 26 | 27 | # Check if all counts become 0 28 | for i in keys: 29 | if count[i]: 30 | return -1 31 | 32 | # Calculate the number of operations required 33 | res = 0 34 | i = n-1 35 | j = n-1 36 | while i >= 0: 37 | # if there is a mismatch, then keep incrementing 38 | # result 'res' until B[j] is not found in A[0..i] 39 | while i>= 0 and A[i] != B[j]: 40 | i -= 1 41 | res += 1 42 | 43 | # if A[i] and B[j] match 44 | if i >= 0: 45 | i -= 1 46 | j -= 1 47 | 48 | return res 49 | -------------------------------------------------------------------------------- /Day<07>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n = sc.nextInt(); 13 | int array[] = new int[n]; 14 | for (int i=0; i>>1; 33 | 34 | if (x <= a[m]) 35 | r = m; 36 | else 37 | l = m; 38 | } 39 | return r; 40 | } 41 | 42 | static long ValidPair(int a[], int n) 43 | { 44 | Arrays.sort(a); 45 | long ans = 0; 46 | for (int i = 0; i < n; ++i) 47 | { 48 | if (a[i] <= 0) 49 | continue; 50 | // search for first element >= (-a[i] + 1) 51 | int j = lowerBound(a, -a[i] + 1); 52 | ans += (i - j); 53 | } 54 | return ans; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Day<30>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int build_bridges_util(int i , int j , string str1 , string str2 , int n , int m , int dp[105][105]) 8 | { 9 | if (n <= i or m <= j) 10 | return 0; 11 | 12 | int ans = dp[i][j]; 13 | if (ans != -1) 14 | return ans; 15 | 16 | ans = 0; 17 | if (str1[i]==str2[j]) 18 | ans = 1 + build_bridges_util(i+1, j+1, str1, str2,n, m, dp); 19 | 20 | ans = max(ans, build_bridges_util(i+1, j, str1, str2, n, m, dp)); 21 | ans = max(ans, build_bridges_util(i, j+1, str1, str2, n, m, dp)); 22 | 23 | return dp[i][j] = ans; 24 | } 25 | 26 | int build_bridges(string str1, string str2) 27 | { 28 | int dp[105][105]; 29 | memset(dp, -1, sizeof dp); 30 | int n = str1.size(); 31 | int m = str2.size(); 32 | return build_bridges_util(0,0,str1,str2,n,m,dp); 33 | } 34 | }; 35 | 36 | int main() 37 | { 38 | ios_base::sync_with_stdio(0); 39 | cin.tie(NULL); 40 | cout.tie(NULL); 41 | 42 | int t; 43 | cin >> t; 44 | while (t--) 45 | { 46 | string str1,str2; 47 | cin >> str1 >> str2; 48 | Solution obj; 49 | cout</MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | String str = sc.next(); 13 | 14 | Sol obj = new Sol(); 15 | System.out.println(obj.maxFrequency(str)); 16 | } 17 | } 18 | } 19 | 20 | class Sol 21 | { 22 | int maxFrequency(String S) 23 | { 24 | String prefix = "", suffix = ""; 25 | int N = S.length(); 26 | int start = 0, end = N-1; 27 | int maxFreq = 1; 28 | int freq = 0; 29 | 30 | while ( start <= end ) 31 | { 32 | prefix = S.substring(0 , start+1); // prefix + C[start]; 33 | suffix = S.substring(end); // C[end] + suffix; 34 | if ( prefix.equals(suffix) ) 35 | { 36 | freq = ( N - S.replaceAll(prefix,"").length() ) / prefix.length(); 37 | if ( freq < maxFreq ) // If new Frequency is < Max Frequency 38 | return maxFreq; // found so far we can't get 39 | maxFreq = Math.max( freq , maxFreq ); // a higher count 40 | } 41 | --end; 42 | ++start; 43 | } 44 | return maxFreq; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Day<19>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int m = sc.nextInt(); 13 | int n = sc.nextInt(); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.candies(m,n)); 16 | t--; 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | static int candies(int m, int n) 24 | { 25 | int X = (m * n) - m - n; 26 | HashSet hash_set = new HashSet<>(); 27 | Queue queue = new LinkedList<>(); 28 | queue.add(X); 29 | hash_set.add(X); 30 | 31 | int count = 0; 32 | while (!queue.isEmpty()) 33 | { 34 | int curr = queue.poll(); 35 | count++; 36 | 37 | int key = curr-m; 38 | if (key > 0 && hash_set.contains(key)==false ) 39 | { 40 | queue.add(key); 41 | hash_set.add(key); 42 | } 43 | 44 | key = curr - n; 45 | if (key > 0 && hash_set.contains(key)==false) 46 | { 47 | queue.add(key); 48 | hash_set.add(key); 49 | } 50 | } 51 | return count; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Day<27>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Solution 5 | { 6 | static int catchThieves(char arr[], int n, int k) 7 | { 8 | int result = 0; 9 | ArrayList thief = new ArrayList(); 10 | ArrayList police = new ArrayList(); 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (arr[i] == 'P') 15 | police.add(i); 16 | else if (arr[i] == 'T') 17 | thief.add(i); 18 | } 19 | 20 | // track lowest current indices of thief: thi[l], police: pol[r] 21 | int l = 0, r = 0; 22 | while (l < thief.size() && r < police.size()) 23 | { 24 | // can be caught 25 | if (Math.abs(thief.get(l) - police.get(r)) <= k) 26 | { 27 | result++; 28 | l++; 29 | r++; 30 | } 31 | else if (thief.get(l) < police.get(r)) 32 | l++; 33 | else 34 | r++; 35 | } 36 | return result; 37 | } 38 | } 39 | 40 | class GfG 41 | { 42 | public static void main(String args[]) throws IOException 43 | { 44 | Scanner sc = new Scanner(System.in); 45 | int t = sc.nextInt(); 46 | 47 | while (0 < t) 48 | { 49 | int n = sc.nextInt(); 50 | int k = sc.nextInt(); 51 | char arr[] = new char[n]; 52 | for (int i=0; i/Solution.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | dr = [-1, 0, 0, 1] 4 | dc = [ 0,-1, 1, 0] 5 | 6 | class Solution: 7 | def findDistance(self, matrix, m, n): 8 | ret = [ [-1 for _ in range(n)] for _ in range(m) ] 9 | q = deque() 10 | 11 | for i in range(m): 12 | for j in range(n): 13 | if matrix[i][j] == 'B': 14 | ret[i][j] = 0 15 | q.append((i,j)) 16 | 17 | while len(q): 18 | r=q[0][0] 19 | c=q[0][1] 20 | q.popleft() 21 | 22 | for i in range(4): 23 | nextr=r+dr[i] 24 | nextc=c+dc[i] 25 | if 0<=nextr and nextr/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | class GfG 6 | { 7 | public static void main (String[] args) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while (t-->0) 12 | { 13 | int n = sc.nextInt(); 14 | int arr[]= new int[n]; 15 | for (int i=0; i stack = new Stack < > (); 33 | int[] temp = new int[n]; 34 | temp[0] = arr[0]; 35 | for (int i = 1; i = 0; j--) 39 | { 40 | if (arr[j] > temp[j]) 41 | { 42 | while (!stack.empty() && stack.peek() <= temp[j]) 43 | stack.pop(); 44 | if (!stack.empty() && stack.peek() < arr[j]) 45 | return true; 46 | stack.push(arr[j]); 47 | } 48 | } 49 | return false; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Day<24>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | vector TopK(vector& array, int k) 8 | { 9 | int i,j=0; 10 | int arr[10002]; 11 | for (i=0; i<10002; i++) 12 | arr[i] = 0; 13 | for (i=0; ires[100002]; 17 | 18 | for (i=0; i<10002; i++) 19 | { 20 | if (arr[i]!=0) 21 | res[arr[i]].push_back(i); 22 | } 23 | 24 | vectorans; 25 | for (i=100001; 0<=i; i--) 26 | { 27 | for (int p=res[i].size()-1; 0<=p; p--) 28 | { 29 | if (j!=k) 30 | ans.push_back(res[i][p]); 31 | else 32 | break; 33 | j++; 34 | } 35 | } 36 | return ans; 37 | } 38 | }; 39 | 40 | int main() 41 | { 42 | int t; 43 | cin>>t; 44 | while (t--) 45 | { 46 | int n,k; 47 | cin>>n; 48 | 49 | vectorarray(n); 50 | 51 | for (int i=0; i>array[i]; 53 | cin>>k; 54 | 55 | Solution obj; 56 | vector result = obj.TopK(array,k); 57 | 58 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int min_sprinklers(int gallery[], int n) 8 | { 9 | vector> sprinklers; 10 | for (int i=0; i ( i-gallery[i], i+gallery[i] ) ); 13 | 14 | sort(sprinklers.begin(), sprinklers.end()); 15 | 16 | int target=0, sprinklers_on=0, i=0; 17 | while (target < n) 18 | { 19 | if (i==sprinklers.size() || target>t; 43 | while (t--) 44 | { 45 | int n; 46 | cin>>n; 47 | int gallery[n]; 48 | for (int i=0; i> gallery[i]; 50 | Solution obj; 51 | cout << obj.min_sprinklers(gallery,n) << endl; 52 | } 53 | return 1; 54 | } 55 | -------------------------------------------------------------------------------- /Day<09>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | class GURU 4 | { 5 | public static void main(String args[])throws IOException 6 | { 7 | BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); 8 | int t = Integer.parseInt(br.readLine()); 9 | while (t-->0) 10 | { 11 | String arr[] = br.readLine().split(" "); 12 | String A = arr[0]; 13 | String B = arr[1]; 14 | Solution obj = new Solution(); 15 | System.out.println(obj.transfigure (A, B)); 16 | } 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | int transfigure(String A, String B) 23 | { 24 | int i = 0, j = 0; 25 | int len = A.length(); 26 | if ( len != B.length() ) return -1; 27 | 28 | char[] aaa = new StringBuilder(A).reverse().toString().toCharArray(); 29 | char[] bbb = new StringBuilder(B).reverse().toString().toCharArray(); 30 | int[] freq = new int[128]; 31 | 32 | for (i = 0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | string Reduced_String(int k,string s) 9 | { 10 | if (k == 1) 11 | { 12 | string ans = ""; 13 | return ans; 14 | } 15 | 16 | stack> st; 17 | string ans=""; 18 | for (int i=0; i>t; 56 | while (t--) 57 | { 58 | int k; 59 | cin>>k; 60 | string s; 61 | cin>>s; 62 | Solution obj; 63 | cout</MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Solution 5 | { 6 | static int catchThieves(char arr[], int n, int k) 7 | { 8 | int max = 0; 9 | boolean[] police = new boolean[n]; // Storing indices of Police 10 | Arrays.fill(police , false); 11 | 12 | for (int i=0; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Solution 5 | { 6 | static int maxCandy(int height[], int n) 7 | { 8 | // To store the maximum water so far 9 | int max = 0; 10 | 11 | // Both the pointers are pointing at the first and the last buildings respectively 12 | int i = 0, j = n - 1; 13 | 14 | // While the water can be stored between the currently chosen buildings 15 | while (i < j) 16 | { 17 | // Update maximum water so far and increment i 18 | if (height[i] < height[j]) 19 | { 20 | max = Math.max(max, (j - i - 1) * height[i]); 21 | i++; 22 | } 23 | // Update maximum water so far and decrement j 24 | else if (height[j] < height[i]) 25 | { 26 | max = Math.max(max, (j - i - 1) * height[j]); 27 | j--; 28 | } 29 | // Any of the pointers can be updated (or both) 30 | else 31 | { 32 | max = Math.max(max, (j - i - 1) * height[i]); 33 | i++; 34 | j--; 35 | } 36 | } 37 | return max; 38 | } 39 | } 40 | 41 | class GfG 42 | { 43 | public static void main(String[] args) 44 | { 45 | Scanner sc = new Scanner(System.in); 46 | int t = sc.nextInt(); 47 | while (t > 0) 48 | { 49 | int n = sc.nextInt(); 50 | int height[] = new int[n]; 51 | for (int i = 0; i < n; ++i) 52 | { 53 | height[i] = sc.nextInt(); 54 | } 55 | Solution ob = new Solution(); 56 | System.out.println(ob.maxCandy(height,n)); 57 | t--; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Day<30>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | String str1 = sc.next(); 13 | String str2 = sc.next(); 14 | Sol obj = new Sol(); 15 | System.out.println(obj.build_bridges(str1,str2)); 16 | } 17 | } 18 | } 19 | 20 | class Sol 21 | { 22 | public static int build_bridges(String str1 , String str2) 23 | { 24 | int n = str1.length(); 25 | int m = str2.length(); 26 | int dp[][] = new int[105][105]; 27 | 28 | for (int i=0; i<105; i++) 29 | for (int j=0;j<105; j++) 30 | dp[i][j] = -1; 31 | 32 | return build_bridges_util(0,0,str1,str2,n,m,dp); 33 | } 34 | public static int build_bridges_util(int i , int j , String str1 , String str2 , int n , int m , int dp[][]) 35 | { 36 | if (n <= i || m <= j) 37 | return 0; 38 | int ans = dp[i][j]; 39 | if (ans != -1) 40 | return ans; 41 | 42 | ans = 0; 43 | if (str1.charAt(i)==str2.charAt(j)) 44 | ans = 1 + build_bridges_util(i+1, j+1 , str1 , str2 , n , m , dp); 45 | 46 | ans = Math.max(ans, build_bridges_util(i+1, j , str1 , str2 , n , m , dp)); 47 | ans = Math.max(ans, build_bridges_util(i, j+1 , str1 , str2 , n , m , dp)); 48 | 49 | dp[i][j] = ans; 50 | return ans; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Day<05>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class GURU 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | int t = sc.nextInt(); 9 | while (t > 0) 10 | { 11 | int n = sc.nextInt(); 12 | int m = sc.nextInt(); 13 | int k = sc.nextInt(); 14 | int arr[][] = new int[n][m]; 15 | for (int i=0; i= columnStart; --i, ++k) 50 | if ( k==K ) 51 | return arr[rowEnd - 1][i]; 52 | rowEnd--; 53 | } 54 | if (columnStart < columnEnd) 55 | { 56 | for (i = rowEnd - 1; i >= rowStart; --i, ++k) 57 | if ( k==K ) 58 | return arr[i][columnStart]; 59 | columnStart++; 60 | } 61 | } 62 | return -1; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Day<08>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int maxCandy(int height[], int n) 8 | { 9 | // To store the maximum water so far 10 | int maximum = 0; 11 | // Both the pointers are pointing at the first and the last buildings respectively 12 | int i = 0, j = n - 1; 13 | 14 | // While the water can be stored between the currently chosen buildings 15 | while (i < j) 16 | { 17 | // Update maximum water so far and increment i 18 | if (height[i] < height[j]) 19 | { 20 | maximum = max(maximum, (j - i - 1) * height[i]); 21 | i++; 22 | } 23 | // Update maximum water so far and decrement j 24 | else if (height[j] < height[i]) 25 | { 26 | maximum = max(maximum, (j - i - 1) * height[j]); 27 | j--; 28 | } 29 | // Any of the pointers can be updated (or both) 30 | else 31 | { 32 | maximum = max(maximum, (j - i - 1) * height[i]); 33 | i++; 34 | j--; 35 | } 36 | } 37 | return maximum; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while (t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int height[n]; 50 | for (int i = 0; i < n; ++i) 51 | { 52 | cin>>height[i]; 53 | } 54 | Solution obj; 55 | cout << obj.maxCandy(height,n)<<"\n"; 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Day<04>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def find_min(self, a, n, k): 3 | s = 0 4 | complete_pairs=0 5 | 6 | for i in range(0,n): 7 | # complete_pairs should hold the value of max pairs that can be picked from the drawer 8 | complete_pairs += a[i] // 2; 9 | 10 | # sum holds the value of maximum pairs that can be picked without exhausting any colour 11 | if ( a[i] % 2 == 0 ): 12 | s += ( a[i] - 2 )//2; 13 | else: 14 | s += ( a[i] - 1 )//2; 15 | 16 | # returning -1 if required pairs is more than available pairs 17 | if (k > complete_pairs): 18 | return -1; 19 | # if k is lesser than or equal to sum, worst case after picking k-1 pairs is 2*(k-1) socks representing k-1 pairs 20 | # along with n socks, each of different colour one more pick after this will certainly complete kth pair 21 | if (k<=s): 22 | return 2*(k-1) + n + 1; 23 | 24 | # if however, k>sum, the worst case is as described below 'sum' pairs are picked without exhausting any colour (2*sum picks) 25 | # n socks are picked, all of different colours (n picks) now, no colour has more than one sock left in drawer 26 | # this implies, each new pick will complete a pair therefore (k-sum) more picks required 27 | return 2*s + n + (k-s); 28 | 29 | if __name__ == '__main__': 30 | t = int(input()) 31 | for _ in range(t): 32 | n = int(input()) 33 | a = list(map(int,input().split())) 34 | k = int(input()) 35 | obj = Solution() 36 | print(obj.find_min(a,n,k)) 37 | -------------------------------------------------------------------------------- /Day<14>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | 11 | while (0 < t) 12 | { 13 | int n = sc.nextInt(); 14 | int array[] = new int[n]; 15 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int find_min(int a[], int n, int k) 8 | { 9 | int sum = 0, complete_pairs = 0; 10 | for (int i = 0; i < n; i++) 11 | { 12 | complete_pairs += a[i] / 2; 13 | // complete_pairs should hold the value of max pairs that can be picked from the drawer 14 | if (a[i] % 2 == 0) 15 | sum += (a[i] - 2) / 2; 16 | else 17 | sum += (a[i] - 1) / 2; 18 | // sum holds the value of maximum pairs that can be picked without exhausting any colour 19 | } 20 | if (k > complete_pairs) return -1; 21 | // returning -1 if required pairs is more than available pairs 22 | if (k <= sum) return 2 * (k - 1) + n + 1; 23 | // if k is lesser than or equal to sum, worst case after picking k-1 pairs is 2*(k-1) socks representing k-1 pairs 24 | // along with n socks, each of different colour one more pick after this will certainly complete kth pair 25 | 26 | return 2 * sum + n + (k - sum); 27 | // if however, k>sum, the worst case is as described below 'sum' pairs are picked without exhausting any colour (2*sum picks) 28 | // n socks are picked, all of different colours (n picks) now, no colour has more than one sock left in drawer 29 | // this implies, each new pick will complete a pair therefore (k-sum) more picks required 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | int t; 36 | cin >> t; 37 | while (t--) 38 | { 39 | int n, k; 40 | cin >> n; 41 | int a[n]; 42 | for (int i=0; i> a[i]; 43 | cin >> k; 44 | Solution obj; 45 | cout << obj.find_min(a, n, k) << endl; 46 | } 47 | return 1; 48 | } 49 | -------------------------------------------------------------------------------- /Day<15>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int a) 7 | { 8 | data = a; 9 | next = null; 10 | } 11 | } 12 | 13 | class LinkedList 14 | { 15 | public static void main(String[] args) 16 | { 17 | Scanner sc=new Scanner(System.in); 18 | int t=sc.nextInt(); 19 | while (t-->0) 20 | { 21 | int n=sc.nextInt(); 22 | int x=sc.nextInt(); 23 | Node head=null; 24 | for (int i=0; i visited = new HashSet(); 56 | 57 | while (firstPtr != null) 58 | { 59 | Node secondPtr = firstPtr.next; 60 | while (secondPtr != null) 61 | { 62 | if ( visited.contains(x - (firstPtr.data + secondPtr.data)) ) 63 | ++triplets; 64 | 65 | secondPtr = secondPtr.next; 66 | } 67 | 68 | visited.add(firstPtr.data); 69 | firstPtr = firstPtr.next; 70 | } 71 | return triplets; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Day<09>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[])throws IOException 7 | { 8 | BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); 9 | int t = Integer.parseInt(br.readLine()); 10 | while (t-->0) 11 | { 12 | String arr[] = br.readLine().split(" "); 13 | String A = arr[0]; 14 | String B = arr[1]; 15 | Solution obj = new Solution(); 16 | System.out.println(obj.transfigure (A, B)); 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | int transfigure (String A, String B) 24 | { 25 | if (A.length () != B.length ()) 26 | { 27 | return -1; 28 | } 29 | 30 | HashMap m = new HashMap(); 31 | int n = A.length (); 32 | for (int i = 0; i < n; i++) 33 | { 34 | if (m.containsKey(A.charAt(i))) 35 | m.put(A.charAt(i), m.get(A.charAt(i)) + 1); 36 | else 37 | m.put(A.charAt(i),1); 38 | } 39 | for (int i = 0; i < n; i++) 40 | { 41 | if (m.containsKey(B.charAt(i))) 42 | m.put(B.charAt(i),m.get(B.charAt(i)) - 1 ); 43 | } 44 | for (Map.Entry entry : m.entrySet()) 45 | { 46 | if (entry.getValue() != 0) 47 | return -1; 48 | } 49 | 50 | int i = n - 1, j = n - 1; 51 | int res = 0; 52 | while (i >= 0 && j >= 0) 53 | { 54 | while (i >= 0 && A.charAt(i) != B.charAt(j)) 55 | { 56 | i--; 57 | res++; 58 | } 59 | i--; 60 | j--; 61 | } 62 | return res; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Day<29>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def z_function(self, s): 3 | n = len(s) 4 | z = [0] * n 5 | 6 | l = r = 0 7 | for i in range(1, n): 8 | if i <= r: 9 | z[i] = min(r - i + 1, z[i - l]) 10 | 11 | while i + z[i] < n and s[z[i]] == s[i + z[i]]: 12 | z[i] += 1 13 | 14 | if i + z[i] - 1 > r: 15 | l = i 16 | r = i + z[i] - 1 17 | return z 18 | 19 | def update(self, idx, val, bit, n): 20 | if idx == 0: 21 | return 22 | while idx <= n: 23 | bit[idx] += val 24 | idx += (idx & - idx) 25 | 26 | def pref(self, idx, bit): 27 | ans = 0 28 | while idx > 0: 29 | ans += bit[idx] 30 | idx -= (idx & - idx) 31 | return ans 32 | 33 | def maxFrequency(self, s): 34 | n = len(s) 35 | z = self.z_function(s) 36 | bit = [0] * (n + 5) 37 | 38 | for i in range(1, n): 39 | self.update(z[i], 1, bit, n) 40 | 41 | m = defaultdict(int) 42 | for i in range(n - 1, 0, -1): 43 | if z[i] != n - i: 44 | continue 45 | m[z[i]] += ((self.pref(n, bit)) - self.pref(z[i] - 1, bit) + 1) 46 | 47 | ans = 1 48 | for val in m.values(): 49 | ans = max(ans, val) 50 | 51 | return ans 52 | 53 | from collections import defaultdict 54 | 55 | if __name__ == '__main__': 56 | T = int(input(): 57 | 58 | for _ in range(T): 59 | Str = input() 60 | obj = Solution() 61 | 62 | print(obj.maxFrequency(Str)) 63 | -------------------------------------------------------------------------------- /Day<05>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findK(self, a, n, m, k): 3 | top = 0 4 | left = 0 5 | right = m-1; 6 | bottom = n-1; 7 | dir = 0; 8 | count = 0; 9 | while (top<=bottom and left<=right): 10 | if (dir==0): 11 | for i in range(left, right+1): 12 | k -= 1 13 | if k == 0: 14 | return (a[top][i]) 15 | top += 1 16 | dir=1 17 | 18 | if (dir==1): 19 | for i in range(top, bottom+1): 20 | k -= 1 21 | if k == 0: 22 | return (a[i][right]) 23 | right -= 1 24 | dir=2 25 | if (dir==2): 26 | for i in range(right, left - 1, -1): 27 | k -= 1 28 | if k == 0: 29 | return (a[bottom][i]) 30 | bottom -= 1 31 | dir=3 32 | if (dir==3): 33 | for i in range(bottom, top - 1, -1): 34 | k -= 1 35 | if k == 0: 36 | return (a[i][left]) 37 | left += 1 38 | dir=0 39 | 40 | if __name__=='__main__': 41 | t = int(input()) 42 | for i in range(t): 43 | n = list(map(int, input().strip().split())) 44 | arr = list(map(int, input().strip().split())) 45 | matrix = [[0 for i in range(n[1])]for j in range(n[0])] 46 | c = 0 47 | for i in range(n[0]): 48 | for j in range(n[1]): 49 | matrix[i][j] = arr[c] 50 | c += 1 51 | obj = Solution() 52 | print(obj.findK(matrix, n[0], n[1], n[2])) 53 | -------------------------------------------------------------------------------- /Day<06>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main (String[] args) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = Integer.parseInt(sc.next()); 10 | while (t-- > 0) 11 | { 12 | int n = Integer.parseInt(sc.next()); 13 | int a[][] = new int[n][n]; 14 | 15 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool checktree(int preorder[], int inorder[], int postorder[], int N) 8 | { 9 | // if the array lengths are 0 then all of them are obviously equal 10 | if (N == 0) return 1; 11 | 12 | // if array lengths are 1, then check if all of them are equal 13 | if (N == 1) 14 | return (preorder[0] == inorder[0]) && (inorder[0] == postorder[0]); 15 | 16 | // search for first element of preorder in inorder array 17 | int idx = -1; 18 | for (int i = 0; i < N; ++i) 19 | if (inorder[i] == preorder[0]) 20 | { 21 | idx = i; 22 | break; 23 | } 24 | 25 | if (idx == -1) return 0; 26 | 27 | // matching element in postorder array 28 | if( preorder[0] != postorder[N-1] ) return false; 29 | 30 | // check for the left subtree 31 | int ret1 = checktree(preorder + 1, inorder, postorder, idx); 32 | 33 | // check for the right subtree 34 | int ret2 = checktree(preorder + idx + 1, inorder + idx + 1, postorder + idx, N - idx - 1); 35 | 36 | // return 1 only if both of them are correct else 0 37 | return (ret1 && ret2); 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int preorder[n]; 50 | int inorder[n]; 51 | int postorder[n]; 52 | 53 | for (int i = 0; i < n; ++i) 54 | cin>>preorder[i]; 55 | 56 | for (int i = 0; i < n; ++i) 57 | cin>>inorder[i]; 58 | 59 | for (int i = 0; i < n; ++i) 60 | cin>>postorder[i]; 61 | 62 | Solution obj; 63 | if (obj.checktree(preorder, inorder, postorder, n)) 64 | cout << "Yes\n"; 65 | else 66 | cout<<"No\n"; 67 | } 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Day<06>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main (String[] args) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = Integer.parseInt(sc.next()); 10 | while (t-- > 0) 11 | { 12 | int n = Integer.parseInt(sc.next()); 13 | int a[][] = new int[n][n]; 14 | 15 | for (int i=0; i/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | int n = sc.nextInt(); 13 | ArrayList arr = new ArrayList(); 14 | ArrayList res = new ArrayList(); 15 | for (int i=0; iTopK(ArrayList array, int k) 37 | { 38 | Map fMap = new HashMap<>(); 39 | for (Integer num : array) 40 | { 41 | fMap.put(num, fMap.getOrDefault(num, 0) + 1); 42 | } 43 | 44 | Queue> heap = new PriorityQueue<>((a,b) 45 | -> a.getValue().equals(b.getValue()) ? Integer.compare(b.getKey(), a.getKey()) 46 | : Integer.compare(b.getValue(), a.getValue())); 47 | for (Map.Entry key : fMap.entrySet()) 48 | { 49 | heap.offer( key ); 50 | } 51 | 52 | ArrayList ans = new ArrayList<>(); 53 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | struct Info 7 | { 8 | int distance; 9 | int houseno; 10 | Info(int x,int y) 11 | { 12 | distance = x; 13 | houseno = y; 14 | } 15 | }; 16 | 17 | struct Compare 18 | { 19 | bool operator() (Info const& p1,Info const& p2) 20 | { 21 | if (p1.distance == p2.distance) 22 | return p1.houseno < p2.houseno; 23 | else 24 | return p1.distance < p2.distance; 25 | } 26 | }; 27 | 28 | public: 29 | vector Kclosest(vectorarr, int n, int x, int k) 30 | { 31 | vector result; 32 | priority_queue , Compare> pq; 33 | 34 | for (int i = 0; i < k; i++) 35 | { 36 | Info obj(abs(arr[i] - x) , arr[i]); 37 | pq.push(obj); 38 | } 39 | 40 | for (int i = k; i < n; i++) 41 | { 42 | int diff = abs(arr[i] - x); 43 | if (pq.top().distance < diff) 44 | continue; 45 | 46 | if (diff == pq.top().distance && pq.top().houseno < arr[i]) 47 | continue; 48 | 49 | pq.pop(); 50 | Info obj(abs(arr[i] - x) , arr[i]); 51 | pq.push(obj); 52 | } 53 | while (pq.empty() == false) 54 | { 55 | result.push_back(pq.top().houseno); 56 | pq.pop(); 57 | } 58 | sort(result.begin(),result.end()); 59 | return result; 60 | } 61 | }; 62 | 63 | int main() 64 | { 65 | int t; 66 | cin>>t; 67 | while (t--) 68 | { 69 | int n,x,k; 70 | cin>>n>>x>>k; 71 | vector array(n); 72 | for (int i = 0; i < n; ++i) 73 | cin>>array[i]; 74 | 75 | Solution obj; 76 | vector result = obj.Kclosest(array, n, x, k); 77 | for (int i = 0; i < result.size(); ++i) 78 | { 79 | cout</Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int Maximum_Sum(vector> &mat,int N,int K) 8 | { 9 | int stripSum[N][N]; 10 | for (int j=0; j max_sum) 31 | { 32 | max_sum = sum; 33 | pos = &(mat[i][0]); 34 | } 35 | 36 | for (int j=1; j max_sum) 40 | { 41 | max_sum = sum; 42 | pos = &(mat[i][j]); 43 | } 44 | } 45 | } 46 | return max_sum; 47 | } 48 | }; 49 | 50 | int main() 51 | { 52 | int t; 53 | cin >> t; 54 | while (t--) 55 | { 56 | int N; 57 | cin>>N; 58 | vector> mat(N,vector(N,0)); 59 | 60 | for (int i=0; i>mat[i][j]; 65 | } 66 | } 67 | 68 | int k; 69 | cin>>k; 70 | Solution obj; 71 | cout << obj.Maximum_Sum(mat,N,k)<<"\n"; 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Day<29>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(0); 7 | cin.tie(NULL); 8 | cout.tie(NULL); 9 | int t; 10 | cin >> t; 11 | while (t--) 12 | { 13 | string str; 14 | cin >> str; 15 | Solution obj; 16 | cout << obj.maxFrequency(str) << "\n"; 17 | } 18 | return 0; 19 | } 20 | 21 | class Solution 22 | { 23 | public: 24 | vector z_function(string s) 25 | { 26 | int n = s.size(); 27 | vector z(n); 28 | for (int i = 1, l = 0, r = 0; i < n; i++) 29 | { 30 | if (i <= r) 31 | z[i] = min(r - i + 1, z[i - l]); 32 | 33 | while (i + z[i] < n && s[z[i]] == s[i + z[i]]) 34 | z[i]++; 35 | 36 | if (r < i + z[i] - 1) 37 | l = i, r = i + z[i] - 1; 38 | } 39 | return z; 40 | } 41 | 42 | void update(int idx, int val, vector &bit, int n) 43 | { 44 | if (idx == 0) 45 | return; 46 | while (idx <= n) 47 | { 48 | bit[idx] += val; 49 | idx += (idx&-idx); 50 | } 51 | } 52 | 53 | int pref(int idx, vector &bit) 54 | { 55 | int ans = 0; 56 | while (0 < idx) 57 | { 58 | ans += bit[idx]; 59 | idx -= (idx&-idx); 60 | } 61 | return ans; 62 | } 63 | 64 | int maxFrequency(string str) 65 | { 66 | int n = str.size(); 67 | vector z = z_function(str); 68 | vector bit(n+5, 0); 69 | 70 | for (int i = 1; i < n; i++) 71 | update(z[i], 1, bit, n); 72 | 73 | map m; 74 | for (int i = n-1; 1 <= i; i--) 75 | { 76 | if (z[i] != (n-i)) 77 | continue; 78 | m[z[i]] += (pref(n, bit) - pref(z[i]-1, bit) + 1); 79 | } 80 | int ans = 1; 81 | for (auto it:m) 82 | ans = max(ans, it.second); 83 | return ans; 84 | } 85 | }; 86 | -------------------------------------------------------------------------------- /Day<28>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class GURU 4 | { 5 | public static void main (String[] args) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | int t = Integer.parseInt(sc.next()); 9 | while (t-- > 0) 10 | { 11 | int n = Integer.parseInt(sc.next()); 12 | int gallery[] = new int[n]; 13 | for (int i=0; i> sprinklers = new ArrayList>(); 26 | for (int i=0; i row = new ArrayList(); 29 | if ( gallery[i] != -1 ) 30 | { 31 | row.add( Math.max(0 , i-gallery[i]) ); 32 | row.add( Math.min(i+gallery[i] , n-1) ); 33 | sprinklers.add( row ); 34 | } 35 | } 36 | sprinklers.sort( (l1, l2) -> l1.get(0).compareTo( l2.get(0) ) ); 37 | // System.out.println( sprinklers ); 38 | 39 | int curr = 0, ON = 0, i = 0; 40 | int maxRange = 0; 41 | while ( curr < n ) 42 | { 43 | if ( sprinklers.size()<=i || curr < sprinklers.get(i).get(0) ) 44 | return -1; 45 | maxRange = sprinklers.get(i).get(1); 46 | while ( i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define endl '\n' 4 | 5 | class Solution 6 | { 7 | public: 8 | int minTime(vector> &dependency, int duration[], int n, int m) 9 | { 10 | vector v[n+1]; 11 | int vis[n+1]={0},deg[n+1]={0},req[n+1]={0},ans=0; 12 | for (auto i:dependency) 13 | { 14 | v[i.first].push_back(i.second); 15 | deg[i.second]++; 16 | } 17 | vector q; 18 | for (int i=0; i> t; 55 | while (t--) 56 | { 57 | int n, m, x, y; 58 | cin >> n >> m; 59 | int duration[n + 5]; 60 | vector> dependency; 61 | for (int i = 0; i < n; i++) cin >> duration[i]; 62 | for (int i = 0; i < m; i++) 63 | { 64 | cin >> x >> y; 65 | dependency.push_back({x, y}); 66 | } 67 | Solution obj; 68 | cout << obj.minTime(dependency, duration, n, m) << endl; 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Day<28>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class GfG 4 | { 5 | public static void main (String[] args) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | int t = Integer.parseInt(sc.next()); 9 | while (t-- > 0) 10 | { 11 | int n = Integer.parseInt(sc.next()); 12 | int gallery[] = new int[n]; 13 | for (int i=0; i sprinklers = new ArrayList<>(); 37 | for (int i=0; i() 42 | { 43 | @Override 44 | public int compare(Pair p1,Pair p2) 45 | { 46 | return p1.x - p2.x; 47 | } 48 | }); 49 | 50 | int target=0, sprinklers_on=0, i=0; 51 | while (target < n) 52 | { 53 | if (i==sprinklers.size() || target/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.math.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = Integer.parseInt(sc.next()); 10 | while (t-- > 0) 11 | { 12 | int k = Integer.parseInt(sc.next()); 13 | String s = sc.next(); 14 | Solution T = new Solution(); 15 | System.out.println(T.reduced_String(k, s)); 16 | } 17 | } 18 | } 19 | 20 | class Solution 21 | { 22 | public static String reduced_String(int k, String s) 23 | { 24 | if (k == 1) 25 | { 26 | String ans = ""; 27 | return ans; 28 | } 29 | Stack st = new Stack(); 30 | int l = s.length(); 31 | int ctr = 0; 32 | for (int i=0; i 0) 60 | { 61 | char c = st.peek().c; 62 | int cnt = st.peek().ctr; 63 | while (cnt-- > 0) 64 | ans = c + ans; 65 | st.pop(); 66 | } 67 | return ans; 68 | } 69 | } 70 | class Pair 71 | { 72 | char c; 73 | int ctr; 74 | Pair(char c,int ctr) 75 | { 76 | this.c = c; 77 | this.ctr = ctr; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Day<07>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n = sc.nextInt(); 13 | int array[] = new int[n]; 14 | for (int i=0; i=1) 33 | if (allNonPositive) return 0; // If all elements are -ve or 0 (<=0) 34 | 35 | Arrays.sort(arr); 36 | int j=0, key=0, end=n-1; 37 | long validPairs = 0L; 38 | 39 | for (i=1; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | int n = sc.nextInt(); 13 | ArrayList arr = new ArrayList(); 14 | ArrayList res = new ArrayList(); 15 | for (int i=0; iTopK(ArrayList array, int k) 37 | { 38 | int i,j=0; 39 | int arr[] = new int[10002]; 40 | for (i=0; i<10002; i++) 41 | arr[i] = 0; 42 | 43 | for (i=0; ires[] = new ArrayList[100002]; 47 | for (i=0; i<100002; i++) 48 | { 49 | ArrayListtemp = new ArrayList(); 50 | res[i]=temp; 51 | } 52 | for (i=0; i<10002; i++) 53 | if (arr[i]!=0) 54 | res[arr[i]].add(i); 55 | 56 | ArrayListans = new ArrayList(); 57 | for (i=100001; 0<=i; i--) 58 | { 59 | for (int p=res[i].size()-1; 0<=p; p--) 60 | { 61 | if (j!=k) 62 | ans.add(res[i].get(p)); 63 | else 64 | break; 65 | j++; 66 | } 67 | } 68 | return ans; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Day<15>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* Link list node */ 5 | struct Node 6 | { 7 | int data; 8 | Node* next; 9 | Node(int x) 10 | { 11 | data = x; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | void push(struct Node** head_ref, int new_data) 17 | { 18 | struct Node* new_node = new Node(new_data); 19 | 20 | new_node->next = (*head_ref); 21 | (*head_ref) = new_node; 22 | } 23 | int countTriplets(struct Node* head, int x); 24 | 25 | int main() 26 | { 27 | int t; 28 | cin>>t; 29 | while(t--) 30 | { 31 | int n , x ,i , num; 32 | struct Node *head = NULL; 33 | cin>>n>>x; 34 | for (i=0; i>num; 37 | push(&head,num); 38 | } 39 | 40 | /* Check the count function */ 41 | cout < um; 53 | 54 | // insert the tuple in 'um' 55 | for (ptr = head; ptr != NULL; ptr = ptr->next) 56 | um[ptr->data] = ptr; 57 | 58 | // generate all possible pairs 59 | for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next) 60 | for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next) 61 | { 62 | // p_sum - sum of elements in the current pair 63 | int p_sum = ptr1->data + ptr2->data; 64 | // if 'x-p_sum' is present in 'um' and either of the two nodes 65 | // are not equal to the 'um[x-p_sum]' node 66 | if (um.find(x - p_sum) != um.end() && um[x - p_sum] != ptr1 && um[x - p_sum] != ptr2) 67 | // increment count 68 | count++; 69 | } 70 | // required count of triplets 71 | // division by 3 as each triplet is counted 3 times 72 | return (count / 3); 73 | } 74 | -------------------------------------------------------------------------------- /Day<04>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n, k, ans; 13 | n = sc.nextInt(); 14 | int[] a = new int[n]; 15 | for (int i=0; i complete_pairs) return -1; 43 | 44 | // if k is lesser than or equal to sum, worst case after picking k-1 pairs is 2*(k-1) socks representing k-1 pairs 45 | // along with n socks, each of different colour one more pick after this will certainly complete kth pair 46 | if (k <= sum) return 2 * (k - 1) + n + 1; 47 | 48 | // if however, k>sum, the worst case is as described below 'sum' pairs are picked without exhausting any colour (2*sum picks) 49 | // n socks are picked, all of different colours (n picks) now, no colour has more than one sock left in drawer 50 | // this implies, each new pick will complete a pair therefore (k-sum) more picks required 51 | return 2 * sum + n + (k - sum); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Day<23>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.lang.*; 4 | 5 | class Solution 6 | { 7 | public ArrayList Kclosest(int arr[], int n, int x, int k) 8 | { 9 | List> list = new ArrayList>(); 10 | for (int i=0; i row = new ArrayList(); 13 | row.add( Math.abs(arr[i]-x) ); 14 | row.add( arr[i] ); 15 | list.add(row); 16 | } 17 | list.sort( (l1, l2) -> l1.get(0).compareTo( l2.get(0) ) ); 18 | 19 | List last = List.of(100000000, 100000000); 20 | list.add( last ); 21 | 22 | ArrayList result = new ArrayList(); 23 | for (int i=0; i row = new ArrayList(); 28 | row.add( list.get(i).get(1) ); 29 | while ( list.get(i).get(0)==list.get(i+1).get(0) ) 30 | { 31 | row.add( list.get(i+1).get(1) ); ++i; 32 | } 33 | Collections.sort( row ); 34 | result.addAll(row); 35 | continue; 36 | } 37 | result.add( list.get(i).get(1) ); 38 | } 39 | Collections.sort( result ); 40 | for (int i=k; i 0) 53 | { 54 | int n = sc.nextInt(); 55 | int x = sc.nextInt(); 56 | int k = sc.nextInt(); 57 | int arr[] = new int[n]; 58 | for(int i=0; i ans = ob.Kclosest(arr,n,x,k); 64 | for (int i=0; i/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int findK(vector> &a, int n, int m, int k) 8 | { 9 | int top = 0; 10 | int left = 0; 11 | int right = m-1; 12 | int bottom = n-1; 13 | int dir = 0; 14 | int count = 0; 15 | 16 | while (top<=bottom and left<=right) 17 | { 18 | if (dir==0) 19 | { 20 | for (int i=left; i<=right; i++) 21 | { 22 | k--; 23 | if (!k) 24 | return (a[top][i]); 25 | } 26 | top++; 27 | dir=1; 28 | } 29 | if (dir==1) 30 | { 31 | for (int i=top; i<=bottom; i++) 32 | { 33 | k--; 34 | if (!k) 35 | return (a[i][right]); 36 | } 37 | right--; 38 | dir=2; 39 | } 40 | if (dir==2) 41 | { 42 | for (int i=right; i>=left; i--) 43 | { 44 | k--; 45 | if (!k) 46 | return (a[bottom][i]); 47 | } 48 | bottom--; 49 | dir=3; 50 | } 51 | if (dir==3) 52 | { 53 | for (int i=bottom; i>=top; i--) 54 | { 55 | k--; 56 | if (!k) 57 | return (a[i][left]); 58 | } 59 | left++; 60 | dir=0; 61 | } 62 | } 63 | } 64 | }; 65 | 66 | int main() 67 | { 68 | int T; 69 | cin>>T; 70 | while (T--) 71 | { 72 | int n,m; 73 | int k=0; 74 | cin>>n>>m>>k; 75 | vector> a(n, vector(m, 0)); 76 | 77 | for (int i=0; i>a[i][j]; 82 | } 83 | } 84 | Solution obj; 85 | cout << obj.findK(a, n, m, k) << "\n"; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Day<13>/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checktree(self, preorder, inorder, postorder, N): 3 | 4 | # if the array lengths are 0, then all of them are obviously equal 5 | if N == 0: 6 | return 1 7 | 8 | # if array lengths are 1, then check if all of them are equal 9 | if N == 1: 10 | return (preorder[0] == inorder[0]) and (inorder[0] == postorder[0]); 11 | 12 | # search for first element of preorder in inorder array 13 | idx = -1; 14 | 15 | for i in range(N): 16 | if inorder[i] == preorder[0]: 17 | idx = i 18 | break 19 | 20 | if idx == -1: 21 | return 0; 22 | 23 | # matching element in postorder array 24 | if preorder[0] != postorder[N-1]: 25 | return 0; 26 | 27 | # check for the left subtree 28 | ret1 = self.checktree(preorder[1:], inorder, postorder, idx); 29 | 30 | # check for the right subtree 31 | ret2 = self.checktree(preorder[idx + 1:], inorder[idx + 1:], postorder[idx:], N-idx-1); 32 | 33 | # return 1 only if both of them are correct else 0 34 | return (ret1 and ret2) 35 | 36 | class Solution: 37 | def checktree(self, preorder, inorder, postorder, N): 38 | 39 | # if the array lengths are 0, then all of them are obviously equal 40 | if N == 0: 41 | return 1 42 | 43 | # if array lengths are 1, then check if all of them are equal 44 | if N == 1: 45 | return (preorder[0] == inorder[0]) and (inorder[0] == postorder[0]); 46 | 47 | # search for first element of preorder in inorder array 48 | idx = -1; 49 | 50 | for i in range(N): 51 | if inorder[i] == preorder[0]: 52 | idx = i 53 | break 54 | 55 | if idx == -1: 56 | return 0; 57 | 58 | # matching element in postorder array 59 | if preorder[0] != postorder[N-1]: 60 | return 0; 61 | 62 | # check for the left subtree 63 | ret1 = self.checktree(preorder[1:], inorder, postorder, idx); 64 | 65 | # check for the right subtree 66 | ret2 = self.checktree(preorder[idx + 1:], inorder[idx + 1:], postorder[idx:], N-idx-1); 67 | 68 | # return 1 only if both of them are correct else 0 69 | return (ret1 and ret2) 70 | -------------------------------------------------------------------------------- /Day<15>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int a) 7 | { 8 | data = a; 9 | next = null; 10 | } 11 | } 12 | 13 | class LinkedList 14 | { 15 | public static void main(String[] args) 16 | { 17 | Scanner sc=new Scanner(System.in); 18 | int t=sc.nextInt(); 19 | while (t-->0) 20 | { 21 | int n=sc.nextInt(); 22 | int x=sc.nextInt(); 23 | Node head=null; 24 | for (int i=0; i um = new HashMap(); 58 | 59 | // insert the tuple in 'um' 60 | for (ptr = head; ptr != null; ptr = ptr.next) 61 | um.put(ptr.data, ptr); 62 | 63 | // generate all possible pairs 64 | for (ptr1 = head; ptr1 != null; ptr1 = ptr1.next) 65 | for (ptr2 = ptr1.next; ptr2 != null; ptr2 = ptr2.next) 66 | { 67 | // p_sum - sum of elements in the current pair 68 | int p_sum = ptr1.data + ptr2.data; 69 | 70 | // if 'x-p_sum' is present in 'um' and either of the two nodes are not equal to the 'um[x-p_sum]' node 71 | if (um.containsKey(x - p_sum) && um.get(x - p_sum) != ptr1 && um.get(x - p_sum) != ptr2) 72 | // increment count 73 | count++; 74 | } 75 | // required count of triplets division by 3 as each triplet is counted 3 times 76 | return (count / 3); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Day<11>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class GURU 5 | { 6 | public static void main(String[] args) throws IOException 7 | { 8 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 | 10 | int tc = Integer.parseInt(br.readLine()); 11 | while (tc --> 0) 12 | { 13 | String s = br.readLine().trim(); 14 | Solution sln = new Solution(); 15 | String ans = sln.compress(s); 16 | System.out.println(ans); 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | public String Compress(String s) 24 | { 25 | String sub = s.substring( 0,1 ); 26 | StringBuilder encrypted = new StringBuilder(sub); 27 | 28 | int i = 1; 29 | while ( i pi = new ArrayList(n); 56 | for (i=0; i=(i+1)/2 && (i+1)%(2*(i+1-pi.get(i)))==0 ) 73 | { 74 | secC.append( "*" ); 75 | i /= 2; 76 | i++; 77 | } 78 | else 79 | secC.append(s.charAt(i)); 80 | } 81 | else 82 | secC.append(s.charAt(i)); 83 | } 84 | return secC.reverse().toString(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Day<05>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class GfG 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | int t = sc.nextInt(); 9 | while (t > 0) 10 | { 11 | int n = sc.nextInt(); 12 | int m = sc.nextInt(); 13 | int k = sc.nextInt(); 14 | int arr[][] = new int[n][m]; 15 | for (int i=0; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GfG 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t-->0) 11 | { 12 | String str = sc.next(); 13 | 14 | Sol obj = new Sol(); 15 | System.out.println(obj.maxFrequency(str)); 16 | } 17 | } 18 | } 19 | 20 | class Sol 21 | { 22 | ArrayList z_function(String s) 23 | { 24 | int n = s.length(); 25 | ArrayList z = new ArrayList(n); 26 | for (int i=0; i bit, int n) 47 | { 48 | if (idx == 0) 49 | return; 50 | while (idx <= n) 51 | { 52 | bit.set(idx , bit.get(idx) + val); 53 | idx += (idx&-idx); 54 | } 55 | } 56 | 57 | int pref(int idx, ArrayList bit) 58 | { 59 | int ans = 0; 60 | while (0 < idx) 61 | { 62 | ans += bit.get(idx); 63 | idx -= (idx&-idx); 64 | } 65 | return ans; 66 | } 67 | 68 | int maxFrequency(String str) 69 | { 70 | int n = str.length(); 71 | ArrayList z = z_function(str); 72 | 73 | ArrayList bit = new ArrayList(n + 5); 74 | for (int i=0; i m = new TreeMap(); 81 | for (int i = n-1; 1 <= i; i--) 82 | { 83 | if (z.get(i) != (n-i)) 84 | continue; 85 | if (m.containsKey(z.get(i))) 86 | { 87 | m.put(z.get(i), m.get(z.get(i)) + (pref(n, bit) - pref(z.get(i) - 1, bit) + 1)); 88 | } 89 | else 90 | m.put(z.get(i) , (pref(n, bit) - pref(z.get(i) - 1, bit) + 1)); 91 | } 92 | int ans = 1; 93 | for (Map.Entry entry : m.entrySet()) 94 | ans = Math.max(ans, entry.getValue()); 95 | return ans; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Day<13>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | class GfG 6 | { 7 | public static void main(String args[]) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while (t > 0) 12 | { 13 | int n = sc.nextInt(); 14 | int[] preorder = new int[n]; 15 | int[] inorder = new int[n]; 16 | int[] postorder = new int[n]; 17 | for (int i=0; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.lang.*; 4 | 5 | class Info 6 | { 7 | int distance; 8 | int houseno; 9 | Info(int x,int y) 10 | { 11 | distance = x; 12 | houseno = y; 13 | } 14 | } 15 | 16 | class Compare implements Comparator 17 | { 18 | public int compare (Info p1,Info p2) 19 | { 20 | if (p1.distance == p2.distance) 21 | { 22 | if (p1.houseno < p2.houseno) 23 | return +1; 24 | if (p1.houseno > p2.houseno) 25 | return -1; 26 | return 0; 27 | } 28 | else 29 | { 30 | if (p1.distance < p2.distance) 31 | return +1; 32 | if (p1.distance > p2.distance) 33 | return -1; 34 | return 0; 35 | } 36 | } 37 | } 38 | 39 | class Solution 40 | { 41 | public ArrayList Kclosest(int arr[], int n, int x, int k) 42 | { 43 | ArrayList result= new ArrayList(); 44 | PriorityQueue pq = new PriorityQueue(k, new Compare()); 45 | 46 | for (int i = 0; i < k; i++) 47 | { 48 | Info obj = new Info(Math.abs(arr[i] - x) , arr[i]); 49 | pq.add(obj); 50 | } 51 | 52 | for (int i = k; i < n; i++) 53 | { 54 | int diff = Math.abs(arr[i] - x); 55 | if (pq.peek().distance < diff) 56 | continue; 57 | 58 | if (diff == pq.peek().distance && pq.peek().houseno < arr[i]) 59 | continue; 60 | 61 | pq.remove(); 62 | Info obj = new Info(Math.abs(arr[i] - x) , arr[i]); 63 | pq.add(obj); 64 | } 65 | while (0 < pq.size()) 66 | { 67 | result.add(pq.peek().houseno); 68 | pq.remove(); 69 | } 70 | Collections.sort(result); 71 | return result; 72 | } 73 | } 74 | 75 | 76 | class GfG 77 | { 78 | public static void main(String args[]) throws IOException 79 | { 80 | Scanner sc = new Scanner(System.in); 81 | int t = sc.nextInt(); 82 | while (t > 0) 83 | { 84 | int n = sc.nextInt(); 85 | int x = sc.nextInt(); 86 | int k = sc.nextInt(); 87 | int arr[] = new int[n]; 88 | for(int i=0; i ans = ob.Kclosest(arr,n,x,k); 94 | for (int i=0; i/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Nth Natural Number

3 | 4 | `Mathematics` 5 | `Number System` 6 | 7 | ________________________________________________________________________________________________________________________________________________________ 8 |

:heavy_check_mark: Day 2

9 |
Given a positive integer N. You have to find Nth natural number after removing all the numbers containing digit 9.
10 | 11 | * **Example 1**:
12 |   N = 8
13 |   Output: 8
14 |   Explanation: After removing natural numbers which contains digit 9, first 8 numbers are 1,2,3,4,5,6,7,8 and 8th number is 8.
15 | * **Example 2**:
16 |   N = 9
17 |   Output: 10
18 |   Explanation: After removing natural numbers which contains digit 9, first 9 numbers are 1,2,3,4,5,6,7,8,10 and 9th number is 10.
19 | 20 | **➔ Your Task**: 21 | You don't need to read input or print anything. Complete the function findNth() which accepts an integer N as input parameter and return the Nth number 22 | after removing all the numbers containing digit 9. 23 | 24 | 25 | 26 | 27 |
Expected Time Complexity Expected Auxiliary Space
O(logN) O(1)
28 | 29 | #### < Constraints > 30 | 1 ≤ ` N ` ≤ 10¹²
31 | 32 | ### ∑ HINTS ▸ 33 | - [x] _Observe carefully, base of the given sequence after changes._ 34 | ```java 35 | long findNth(long N) 36 | { 37 | StringBuilder sss = new StringBuilder(""); 38 | while ( 0 < N ) 39 | { 40 | sss.append(N % 9); 41 | N /= 9; 42 | } 43 | return Long.valueOf(sss.reverse().toString()); 44 | } 45 | @author : Shreyansh Kumar Singh 46 | ``` 47 |

⭐️ GURU-Shreyansh 48 | Problem ID: 704822🖱

49 | 52 | -------------------------------------------------------------------------------- /Day<25>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | struct QueueNode 7 | { 8 | int i, j, distance; 9 | }; 10 | private: 11 | bool isValid(int i, int j, int M, int N) 12 | { 13 | if ((i < 0 || i > M - 1) || (j < 0 || j > N - 1)) 14 | return false; 15 | return true; 16 | } 17 | 18 | bool isSafe(int i, int j, vector >& matrix, vector >& result) 19 | { 20 | if (matrix[i][j] != 'O' || result[i][j] != -1) 21 | return false; 22 | return true; 23 | } 24 | 25 | public: 26 | vector > findDistance(vector >& matrix, int M, int N) 27 | { 28 | vector > result(M); 29 | queue q; 30 | 31 | for (int i = 0; i < M; i++) 32 | { 33 | result[i].resize(N, -1); 34 | for (int j = 0; j < N; j++) 35 | { 36 | if (matrix[i][j] == 'B') 37 | { 38 | QueueNode node = {i, j, 0}; 39 | q.push(node); 40 | result[i][j] = 0; 41 | } 42 | } 43 | } 44 | 45 | while (!q.empty()) 46 | { 47 | QueueNode curr = q.front(); 48 | int x = curr.i, y = curr.j, dist = curr.distance; 49 | 50 | int row[] = { -1, 0, 1, 0}; 51 | int col[] = { 0, 1, 0, -1 }; 52 | 53 | for (int i = 0; i < 4; i++) 54 | { 55 | if (isValid(x + row[i], y + col[i], M, N) && isSafe(x + row[i], y + col[i], matrix, result)) 56 | { 57 | result[x + row[i]][y + col[i]] = dist + 1; 58 | QueueNode node = {x + row[i], y + col[i], dist + 1}; 59 | q.push(node); 60 | } 61 | } 62 | q.pop(); 63 | } 64 | return result; 65 | } 66 | }; 67 | 68 | int main() 69 | { 70 | int t; 71 | cin >> t; 72 | while (t--) 73 | { 74 | int M,N; 75 | cin>>M; 76 | cin>>N; 77 | vector > matrix(M); 78 | for (int i=0; i>matrix[i][j]; 83 | } 84 | vector>result; 85 | Solution obj; 86 | result = obj.findDistance(matrix, M,N); 87 | for (int i=0; i/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fillarray(self, s, a): 3 | a[0]=0 4 | for i in range(1,len(s)): 5 | series=a[i-1] 6 | while(series): 7 | if(s[series]==s[i]): 8 | a[i]=series+1 9 | break 10 | series=a[series-1] 11 | if(series==0): 12 | a[i]=(int(s[i]==s[0])) 13 | return a 14 | 15 | def compress(self, s): 16 | a=[0]*len(s) 17 | 18 | # ith element of array a stores the length of longest 19 | # proper suffix which is also a proper prefix 20 | # for substr s[0] to s[i] 21 | a = self.fillarray(s, a) 22 | #print(a) 23 | shortened=[] 24 | n=len(s) 25 | i=n-1 26 | 27 | # for even index, string length is odd hence it cannot be divided into two 28 | # so we simply push ith character in stack 29 | while(i>0): 30 | if(i%2==0): 31 | shortened.append(s[i]) 32 | i=i-1 33 | continue 34 | 35 | # star_here will be made TRUE if substring s[0] to s[i] 36 | # can be divided into identical halves 37 | star_here=False 38 | 39 | # suffix and substring length are also meant for substring s[0] to s[i] 40 | suffix=a[i] 41 | substrlen=i+1 42 | 43 | # these conditions, if true, imply that, substring can be divided into 2 identical halves 44 | if(suffix*2>=substrlen): 45 | if(substrlen%(substrlen-suffix)==0): 46 | if((substrlen/(substrlen-suffix))%2==0): 47 | star_here=True 48 | 49 | 50 | # adding * to stack and moving index as required 51 | if(star_here==True): 52 | shortened.append('*') 53 | i=(i//2)+1 54 | 55 | # else, simply adding character to stack 56 | else: 57 | shortened.append(s[i]) 58 | i=i-1 59 | ret="" 60 | ret=ret+s[0] 61 | n=len(shortened) 62 | 63 | # since we analysed input string from end to start removing elements from stack and pushing back to 64 | # output string will reverse them back to required order 65 | while(n): 66 | ret=ret+shortened[n-1] 67 | shortened.pop() 68 | n=n-1 69 | return ret 70 | 71 | 72 | t=int(input()) 73 | for _ in range(0,t): 74 | s=input() 75 | obj = Solution(); 76 | ans=obj.compress(s) 77 | print(ans) 78 | -------------------------------------------------------------------------------- /Day<21>/Solution.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #Node Class: 3 | class Node: 4 | def _init_(self,val): 5 | self.data = val 6 | self.left = None 7 | self.right = None 8 | ''' 9 | class Solution: 10 | def traverse(self, n): 11 | if not n: 12 | return (0,2) 13 | 14 | vaccineKitL , distL = self.traverse(n.left) 15 | vaccineKitR , distR = self.traverse(n.right) 16 | 17 | if max(distL,distR) == 3: 18 | return (vaccineKitL+vaccineKitR+1 , 1) 19 | 20 | return ( vaccineKitL+vaccineKitR , 1+min(distL,distR) ) 21 | 22 | def supplyVaccine(self, root): 23 | vaccineKit,dist = self.traverse(root) 24 | if dist==3: 25 | return vaccineKit+1 26 | return vaccineKit 27 | 28 | from collections import deque 29 | 30 | #Tree Node 31 | class Node: 32 | def __init__(self, val): 33 | self.right = None 34 | self.data = val 35 | self.left = None 36 | 37 | # Function to Build Tree 38 | def buildTree(s): 39 | # Corner Case 40 | if (len(s)==0 or s[0]=="N"): 41 | return None 42 | 43 | # Creating list of strings from input string after spliting by space 44 | ip=list(map(str,s.split())) 45 | 46 | # Create the root of the tree 47 | root=Node(int(ip[0])) 48 | size=0 49 | q=deque() 50 | 51 | # Push the root to the queue 52 | q.append(root) 53 | size=size+1 54 | 55 | # Starting from the second element 56 | i=1 57 | while (0/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GURU 5 | { 6 | public static void main(String args[]) throws IOException 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while (t > 0) 11 | { 12 | int n, k, ans; 13 | n = sc.nextInt(); 14 | int[] a = new int[n]; 15 | for (int i=0; ia[0] ) return -1; 31 | int maxPossiblePairs = 0; 32 | int picks = n; 33 | ArrayList odd = new ArrayList(); 34 | ArrayList even = new ArrayList(); 35 | ArrayList rest = new ArrayList(); 36 | for (int i=0; i k= "+k+" picks= "+picks); 64 | } 65 | for (i=0; i payload) 78 | { 79 | result[i][j] = payload; 80 | update(result, matrix, i - 1, j, payload + 1, M, N); 81 | update(result, matrix, i, j + 1, payload + 1, M, N); 82 | update(result, matrix, i + 1, j, payload + 1, M, N); 83 | update(result, matrix, i, j - 1, payload + 1, M, N); 84 | } 85 | } 86 | } 87 | 88 | static boolean isValid(char[][] matrix, int i, int j, int M, int N) 89 | { 90 | return (0 <= i && 0 <= j && i < M && j < N && matrix[i][j] == 'O'); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /Day<15>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Count Triplets

3 | 4 | `Linked List` 5 | `Mathematical` 6 | 7 | ________________________________________________________________________________________________________________________________________________________ 8 |

:heavy_check_mark: Day 15

9 |
Given a sorted linked list of distinct nodes (no two nodes have the same data) and an integer X. Count distinct triplets in the list that sum up to given integer X.
10 | 11 | * **Example 1**:
12 |   LinkedList: 1->2->4->5->6->8->9
13 |   X = 17
14 |   Output: 2
15 |   Explanation: Distinct triplets are (2, 6, 9) and (4, 5, 8) which have sum equal to X i.e 17.
16 | * **Example 2**:
17 |   LinkedList: 1->2->4->5->6->8->9,
18 |   X = 15
19 |   Output: 5
20 |   Explanation: (1, 5, 9), (1, 6, 8), (2, 4, 9), (2, 5, 8), (4, 5, 6) are the distinct triplets.
21 | 22 | **➔ Your Task**: 23 | You don't need to read input or print anything. Complete the function countTriplets() which takes a head reference and X as input parameters and returns the triplet count. 24 | 25 | 26 | 27 | 28 |
Expected Time Complexity Expected Auxiliary Space
O(N²) O(N)
29 | 30 | #### < Constraints > 31 | 1 ≤ ` Number of Nodes ` ≤ 10³
32 | 1 ≤ ` Node Value ` ≤ 10⁴ 33 | 34 | ### ∑ HINTS ▸ 35 | - [x] _Think how hashing will work here!_ 36 | ```java 37 | static int countTriplets(Node head, int x) 38 | { 39 | int triplets = 0; 40 | Node firstPtr = head; 41 | HashSet visited = new HashSet(); 42 | 43 | while (firstPtr != null) 44 | { 45 | Node secondPtr = firstPtr.next; 46 | while (secondPtr != null) 47 | { 48 | if ( visited.contains(x - (firstPtr.data + secondPtr.data)) ) 49 | ++triplets; 50 | 51 | secondPtr = secondPtr.next; 52 | } 53 | 54 | visited.add(firstPtr.data); 55 | firstPtr = firstPtr.next; 56 | } 57 | return triplets; 58 | } 59 | @author : Shreyansh Kumar Singh 60 | ``` 61 |

⭐️ GURU-Shreyansh 62 | Problem ID: 706071🖱

63 | 66 | -------------------------------------------------------------------------------- /Day<10>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Repeated String Match

3 | 4 | `Strings` 5 | 6 | ________________________________________________________________________________________________________________________________________________________ 7 |

:heavy_check_mark: Day 10

8 |
Given two strings A and B, find the minimum number of times A has to be repeated such that B becomes a substring of the repeated A. If B cannot be a substring of A no matter how many times it is repeated, return -1.
9 | 10 | * **Example 1**:
11 |   A = "abcd"
12 |   B = "cdabcdab"
13 |   Output: 3
14 |   Explanation: After repeating A three times, we get "abcdabcdabcd".
15 | * **Example 2**:
16 |   A = "aa"
17 |   B = "a"
18 |   Output: 1
19 |   Explanation: B is already a substring of A.
20 | 21 | **➔ Your Task**: 22 | You don't need to read input or print anything. Complete the function repeatedStringMatch() which takes strings A and B as input parameters and returns the minimum number of operations to complete the task. If it is not possible then return -1. 23 | 24 | 25 | 26 | 27 |
Expected Time Complexity Expected Auxiliary Space
O(|A| * |B|) O(1)
28 | 29 | #### < Constraints > 30 | 1 ≤ ` |A| ` , ` |B| ` ≤ 10⁵
31 | 32 | ### ∑ HINTS ▸ 33 | - [x] _Let count = B.length() / A.length(). Now check whether B is a substring of A*count or A*(count+1)_ 34 | ```java 35 | static int repeatedStringMatch(String A, String B) 36 | { 37 | if ( A.indexOf(B) != -1 ) 38 | return 1; 39 | 40 | StringBuilder aaa = new StringBuilder(""); 41 | StringBuilder bbb = new StringBuilder(""); 42 | 43 | int length = B.length()+A.length(); 44 | int repeated = 0; 45 | 46 | while ( (aaa.indexOf(B) == -1) && (aaa.length() < length) ) 47 | { 48 | aaa.append(A); 49 | ++repeated; 50 | } 51 | 52 | if ( aaa.indexOf(B) == -1 ) 53 | return -1; 54 | return repeated; 55 | } 56 | @author : Shreyansh Kumar Singh 57 | ``` 58 |

⭐️ GURU-Shreyansh 59 | Problem ID: 706066🖱

60 | 63 | -------------------------------------------------------------------------------- /Day<11>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | private: 7 | void fill_array(string s, int a[]) 8 | { 9 | a[0] = 0; 10 | for (int i=1 ; i shortened; 35 | 36 | for (int i=s.size()-1 ; i>0 ; i--) 37 | // we start checking string from last index 38 | { 39 | if (i%2==0) { shortened.push(s[i]); continue; } 40 | // for even index, string length is odd hence it cannot be divided into two so we simply push ith character in stack 41 | 42 | bool star_here=0; 43 | // star_here will be made TRUE if substring s[0] to s[i] can be divided into identical halves 44 | int suffix = a[i]; 45 | int substrlen = i+1; 46 | // suffix and substring length are also meant for substring s[0] to s[i] 47 | 48 | if (suffix*2 >= substrlen) 49 | if ( substrlen % (substrlen-suffix) == 0 ) 50 | if ( ( substrlen / (substrlen-suffix) ) % 2 == 0 ) 51 | // these conditions, if true, imply that, substring can be divided into 2 identical halves 52 | star_here = 1; 53 | 54 | if (star_here) 55 | shortened.push('*'); i = i/2 + 1; 56 | // adding * to stack and moving index as required 57 | else 58 | shortened.push(s[i]); 59 | // else, simply adding character to stack 60 | } 61 | 62 | string ret; 63 | ret.push_back( s[0] ); 64 | 65 | while( !shortened.empty() ) 66 | { 67 | // since we analysed input string from end to start removing elements from stack and pushing back to 68 | // output string will reverse them back to required order 69 | ret.push_back( shortened.top() ); 70 | shortened.pop(); 71 | } 72 | return ret; 73 | } 74 | }; 75 | 76 | int main() 77 | { 78 | int t; 79 | cin>>t; 80 | while (t--) 81 | { 82 | string s; 83 | cin>>s; 84 | Solution obj; 85 | cout<< obj.compress(s) << "\n"; 86 | } 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Day<18>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: 132 Geeky Buildings

3 | 4 | `Array` 5 | `Stack` 6 | 7 | ________________________________________________________________________________________________________________________________________________________ 8 |

:heavy_check_mark: Day 18

9 |
There are N buildings in Linear Land. They appear in a linear line one after the other and their heights are given in the array arr[]. Geek wants to select three buildings in Linear Land and remodel them as recreational spots. The third of the selected building must be taller than the first and shorter than the second. 10 | Can geek build the three-building recreational zone?
11 | 12 | * **Example 1**:
13 |   N = 6
14 |   arr[ ] = {4, 7, 11, 5, 13, 2}
15 |   Output: True
16 |   Explanation: [4, 7, 5] fits the condition.
17 | * **Example 2**:
18 |   N = 4
19 |   arr[] = {11, 11, 12, 9}
20 |   Output: False
21 |   Explanation: No 3 buildings fit the given condition.
22 | 23 | **➔ Your Task**: 24 | You don't need to read input or print anything. Complete the function recreationalSpot() which takes the array arr[] and its size N as input parameters and returns a boolean value based on whether his building selection was successful or not. 25 | Note: The generated output will be "True" or "False". 26 | 27 | 28 | 29 | 30 |
Expected Time Complexity Expected Auxiliary Space
O(N) O(N)
31 | 32 | #### < Constraints > 33 | 1 ≤ ` N ` ≤ 10⁴
34 | 1 ≤ ` arr[i] ` ≤ 10⁵ 35 | 36 | ### ∑ HINTS ▸ 37 | - [x] _Use a stack._ 38 | ```java 39 | static boolean recreationalSpot(int[] array, int N) 40 | { 41 | int M = Integer.MIN_VALUE; 42 | Stack stack = new Stack<>(); 43 | for (int i = N-1; 0 <= i; i--) 44 | { 45 | if (array[i] < M) 46 | return true; 47 | while (!stack.isEmpty() && stack.peek() < array[i]) 48 | M = stack.pop(); 49 | stack.push(array[i]); 50 | } 51 | return false; 52 | } 53 | @author : Shreyansh Kumar Singh 54 | ``` 55 |

⭐️ GURU-Shreyansh 56 | Problem ID: 706074🖱

57 | 60 | -------------------------------------------------------------------------------- /Day<03>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Smallest Positive Integer that can not be represented as Sum

3 | 4 | `Arrays` 5 | `Sorting` 6 | `Mathematical` 7 | 8 | ________________________________________________________________________________________________________________________________________________________ 9 |

:heavy_check_mark: Day 3

10 |
Given an array of size N, find the smallest positive integer value that cannot be represented as sum of some elements from the array.
11 | 12 | * **Example 1**:
13 |   N = 6
14 |   array[ ] = {1, 10, 3, 11, 6, 15}
15 |   Output: 2
16 |   Explanation: 2 is the smallest integer value that cannot be represented as sum of elements from the array.
17 | * **Example 2**:
18 |   N = 3
19 |   array[ ] = {1, 1, 1}
20 |   Output: 4
21 |   Explanation: 1 is present in the array. 2 can be created by combining two 1s. 3 can be created by combining three 1s. 4 is the smallest integer value that cannot be 22 | represented as sum of elements from the array.
23 | 24 | **➔ Your Task**: 25 | You dont need to read input or print anything. Complete the function smallestpositive() which takes the array and N as input parameters and returns 26 | the smallest positive integer value that cannot be represented as sum of some elements from the array. 27 | 28 | 29 | 30 | 31 |
Expected Time Complexity Expected Auxiliary Space
O(N * Log(N)) O(1)
32 | 33 | #### < Constraints > 34 | 1 ≤ ` N ` ≤ 10⁶
35 | 1 ≤ ` a[i] ` ≤ 10⁹ (Array may contain duplicates.) 36 | 37 | ### ∑ HINTS ▸ 38 | - [x] _Sort the array. Then traverse the sorted array from left to right and observe the numbers you are able to build with each addition._ 39 | ```java 40 | long smallestpositive(long[] array, int n) 41 | { 42 | Arrays.sort(array); 43 | long currNumber = 1L; // Minimum positive output 44 | for (int i=0; i ⭐️ GURU-Shreyansh 56 | Problem ID: 706045🖱

57 | 60 | -------------------------------------------------------------------------------- /Day<13>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | class GURU 6 | { 7 | public static void main(String args[]) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while (t > 0) 12 | { 13 | int n = sc.nextInt(); 14 | int[] preorder = new int[n]; 15 | int[] inorder = new int[n]; 16 | int[] postorder = new int[n]; 17 | for (int i=0; i list = new ArrayList<>(); 52 | postOrder(root,list); 53 | int myPost[] = new int[len]; 54 | for (int i=0; i list) 63 | { 64 | if (root==null) return; 65 | 66 | postOrder(root.left,list); 67 | postOrder(root.right,list); 68 | list.add(root.data); 69 | } 70 | static TreeNode buildTree(int[] pre, int in[], int l, int r) 71 | { 72 | if (r < l || pre.length <= index) 73 | return null; 74 | 75 | int idx = 0; 76 | TreeNode root = new TreeNode(pre[index++]); 77 | for (int i=0; i/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class GfG 5 | { 6 | public static void main(String[] args) throws IOException 7 | { 8 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 | 10 | int tc = Integer.parseInt(br.readLine()); 11 | while (tc--> 0) 12 | { 13 | String s = br.readLine().trim(); 14 | Solution sln = new Solution(); 15 | String ans = sln.compress(s); 16 | System.out.println(ans); 17 | } 18 | } 19 | } 20 | 21 | class Solution 22 | { 23 | void fillArray(String s, int[] a) 24 | { 25 | a[0] = 0; 26 | for (int i = 1; i shortened = new Stack(); 55 | 56 | // for even index, string length is odd hence it cannot be divided into two 57 | // so we simply push ith character in stack 58 | for (int i = s.length() - 1; 0 < i; i--) 59 | { 60 | if (i % 2 == 0) 61 | { 62 | shortened.push(s.charAt(i)); 63 | continue; 64 | } 65 | // star_here will be made TRUE if substring s[0] to s[i] can be divided into identical halves 66 | boolean star_here = false; 67 | 68 | // suffix and substring length are also meant for substring s[0] to s[i] 69 | int suffix = a[i]; 70 | int substrlen = i + 1; 71 | 72 | // these conditions, if true, imply that, substring can be divided into 2 identical halves 73 | if (substrlen <= suffix * 2) 74 | if (substrlen % (substrlen - suffix) == 0) 75 | if ((substrlen / (substrlen - suffix)) % 2 == 0) 76 | star_here = true; 77 | 78 | // adding * to stack and moving index as required 79 | if (star_here == true) 80 | { 81 | shortened.push('*'); 82 | i = i / 2 + 1; 83 | } 84 | // else, simply adding character to stack 85 | else 86 | shortened.push(s.charAt(i)); 87 | } 88 | 89 | StringBuilder ret = new StringBuilder(); 90 | ret.append(s.charAt(0)); 91 | 92 | // since we analysed input string from end to start removing elements from stack and pushing back to 93 | // output string will reverse them back to required order 94 | while (!shortened.empty()) 95 | { 96 | ret.append(shortened.peek()); 97 | shortened.pop(); 98 | } 99 | return ret.toString(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Day<25>/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.math.*; 3 | class Pair 4 | { 5 | int x,y; 6 | Pair(int x, int y) 7 | { 8 | this.x = x; 9 | this.y = y; 10 | } 11 | } 12 | 13 | class GfG 14 | { 15 | public static void main(String args[]) 16 | { 17 | Scanner sc = new Scanner(System.in); 18 | int t = Integer.parseInt(sc.next()); 19 | while (t-- > 0) 20 | { 21 | int m = Integer.parseInt(sc.next()); 22 | int n = Integer.parseInt(sc.next()); 23 | char mat[][] = new char[m][n]; 24 | for (int i=0; i q = new LinkedList(); 53 | for (int i=0; i 0) 71 | { 72 | Pair p = q.peek(); 73 | int i = p.x; 74 | int j = p.y; 75 | q.remove(); 76 | 77 | if (i + 1 < m && mat[i+1][j] == 'O' && ans[i+1][j] == -1) 78 | { 79 | ans[i+1][j] = 1 + ans[i][j]; 80 | q.add(new Pair(i+1,j)); 81 | } 82 | 83 | if (0 <= i - 1 && ans[i-1][j] == -1 && mat[i-1][j] == 'O') 84 | { 85 | ans[i-1][j] = 1 + ans[i][j]; 86 | q.add(new Pair(i-1,j)); 87 | } 88 | 89 | if (j + 1 < n && ans[i][j + 1] == -1 && mat[i][j + 1] == 'O') 90 | { 91 | ans[i][j + 1] = 1 + ans[i][j]; 92 | q.add(new Pair(i,j + 1)); 93 | } 94 | 95 | if (0 <= j - 1 && mat[i][j - 1] == 'O' && ans[i][j-1] == -1) 96 | { 97 | ans[i][j - 1] = 1 + ans[i][j]; 98 | q.add(new Pair(i,j - 1)); 99 | } 100 | } 101 | } 102 | return ans; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Day<21>/MySolution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Node 5 | { 6 | int data; 7 | Node left; 8 | Node right; 9 | Node(int data) 10 | { 11 | this.data = data; 12 | left=null; 13 | right=null; 14 | } 15 | } 16 | 17 | class GURU 18 | { 19 | static Node buildTree(String str) 20 | { 21 | if (str.length()==0 || str.charAt(0)=='N') 22 | return null; 23 | 24 | String ip[] = str.split(" "); 25 | Node root = new Node(Integer.parseInt(ip[0])); 26 | 27 | Queue queue = new LinkedList<>(); 28 | queue.add(root); 29 | 30 | int i = 1; 31 | while (0 < queue.size() && i < ip.length) 32 | { 33 | Node currNode = queue.peek(); 34 | queue.remove(); 35 | String currVal = ip[i]; 36 | if (!currVal.equals("N")) 37 | { 38 | currNode.left = new Node(Integer.parseInt(currVal)); 39 | queue.add(currNode.left); 40 | } 41 | i++; 42 | if (ip.length <= i) 43 | break; 44 | currVal = ip[i]; 45 | 46 | if (!currVal.equals("N")) 47 | { 48 | currNode.right = new Node(Integer.parseInt(currVal)); 49 | queue.add(currNode.right); 50 | } 51 | i++; 52 | } 53 | return root; 54 | } 55 | 56 | public static void main (String[] args) throws IOException 57 | { 58 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 59 | int t=Integer.parseInt(br.readLine()); 60 | 61 | while (t > 0) 62 | { 63 | String s = br.readLine(); 64 | Node root = buildTree(s); 65 | 66 | Solution g = new Solution(); 67 | System.out.println(g.supplyVaccine(root)); 68 | t--; 69 | } 70 | } 71 | } 72 | 73 | class Solution 74 | { 75 | public static int supplyVaccine(Node root) 76 | { 77 | result = 0; 78 | int out = (out = 0) + traverse(root); 79 | return (out!=0 ? result : ++result); 80 | } 81 | static int result; 82 | public static int traverse(Node node) 83 | { 84 | int n = 0; 85 | boolean zero = false , two = false; 86 | if (node.left == null && node.right == null) 87 | return 0; 88 | 89 | if (node.left != null) 90 | { 91 | n = traverse(node.left); 92 | if ( n==0 ) 93 | zero = true; 94 | if ( n==2 ) 95 | two = true; 96 | } 97 | 98 | if (node.right != null) 99 | { 100 | n = traverse(node.right); 101 | if ( n==0 ) 102 | zero = true; 103 | if ( n==2 ) 104 | two = true; 105 | } 106 | 107 | if ( zero ) 108 | { 109 | ++result; 110 | return 2; 111 | } 112 | 113 | if ( two ) 114 | return 1; 115 | return 0; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Day<12>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Bit Difference

3 | 4 | `Bit` 5 | `Bit Magic` 6 | `Bit Manipulation` 7 | 8 | ________________________________________________________________________________________________________________________________________________________ 9 |

:heavy_check_mark: Day 12

10 |
Given an integer array of size N . You have to find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. 11 | For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers).
12 | 13 | * **Example 1**:
14 |   N = 2
15 |   arr[ ] = {1, 2}
16 |   Output: 4
17 |   Explanation: All possible pairs are [(1,1) (1,2) (2,1) (2,2)] their respective Bit differences are 0 , 2 , 2 , 0.
18 | * **Example 2**:
19 |   N = 5
20 |   arr[ ] = {3, 5, 2, 5, 2}
21 |   Output: 36
22 |   Explanation: NaN
23 | 24 | **➔ Your Task**: 25 | You don't need to read input or print anything. Complete the function sumBitDiff() which takes the array a[] and size of array N as input parameters and returns the answer. 26 | 27 | 28 | 29 | 30 |
Expected Time Complexity Expected Auxiliary Space
O(N*log(N)) O(1)
31 | 32 | #### < Constraints > 33 | 1 ≤ ` N ` ≤ 10⁵
34 | 1 ≤ ` arr[i] ` ≤ 10⁸ 35 | 36 | ### ∑ HINTS ▸ 37 | - [x] _Find some mathematical formula using information of total number of set bits in all the numbers at a particular ith bit position._ 38 | ```java 39 | public static long sumBitDiff(int arr[], int n) 40 | { // O( NlogN ) Solution 41 | long even = 0L; 42 | long zeros = 0L; 43 | long diffSum = 0L; 44 | for ( int i=1; i<=32; i++ ) 45 | { 46 | even = 0L; 47 | zeros = 0L; 48 | for ( int j=0; j ⭐️ GURU-Shreyansh 65 | Problem ID: 706069🖱

66 | 69 | -------------------------------------------------------------------------------- /Day<21>/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | Node *left; 8 | Node *right; 9 | Node(int val) 10 | { 11 | data = val; 12 | left = right = NULL; 13 | } 14 | }; 15 | 16 | // Function to Build Tree 17 | Node *buildTree(string str) 18 | { 19 | // Corner Case 20 | if (str.length() == 0 || str[0] == 'N') 21 | return NULL; 22 | 23 | // Creating vector of strings from input 24 | // string after spliting by space 25 | vector ip; 26 | 27 | istringstream iss(str); 28 | for (string str; iss >> str;) 29 | ip.push_back(str); 30 | 31 | // Create the root of the tree 32 | Node *root = new Node(stoi(ip[0])); 33 | 34 | // Push the root to the queue 35 | queue queue; 36 | queue.push(root); 37 | 38 | // Starting from the second element 39 | int i = 1; 40 | while (!queue.empty() && i < ip.size()) 41 | { 42 | // Get and remove the front of the queue 43 | Node *currNode = queue.front(); 44 | queue.pop(); 45 | 46 | // Get the current Node's value from the string 47 | string currVal = ip[i]; 48 | // If the left child is not null 49 | if (currVal != "N") 50 | { 51 | // Create the left child for the current Node 52 | currNode->left = new Node(stoi(currVal)); 53 | // Push it to the queue 54 | queue.push(currNode->left); 55 | } 56 | // For the right child 57 | i++; 58 | if (ip.size() <= i) 59 | break; 60 | currVal = ip[i]; 61 | 62 | // If the right child is not null 63 | if (currVal != "N") 64 | { 65 | // Create the right child for the current Node 66 | currNode->right = new Node(stoi(currVal)); 67 | // Push it to the queue 68 | queue.push(currNode->right); 69 | } 70 | i++; 71 | } 72 | return root; 73 | } 74 | 75 | class Solution 76 | { 77 | int ans; 78 | int post(Node* node) 79 | { 80 | bool zero = false , two = false; 81 | if (node->left==NULL && node->right==NULL) 82 | return 0; 83 | 84 | if (node->left) 85 | { 86 | int get = post(node->left); 87 | if (get==0) 88 | zero = true; 89 | if (get==2) 90 | two = true; 91 | } 92 | 93 | if (node->right) 94 | { 95 | int get = post(node->right); 96 | if (get==0) 97 | zero = true; 98 | if (get==2) 99 | two = true; 100 | } 101 | 102 | if (zero==true) 103 | { 104 | ans++; 105 | return 2; 106 | } 107 | 108 | if (two==true) 109 | { 110 | return 1; 111 | } 112 | return 0; 113 | } 114 | public: 115 | int supplyVaccine(Node* root) 116 | { 117 | ans = 0; 118 | int result = post(root); 119 | if (result == 0) 120 | return (ans + 1); 121 | return ans; 122 | } 123 | }; 124 | -------------------------------------------------------------------------------- /Day<01>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Rearrange Geek and his Classmates

3 | 4 | `List` 5 | `Array` 6 | `Vector` 7 | 8 | ________________________________________________________________________________________________________________________________________________________ 9 |

:heavy_check_mark: Day 1

10 |
Geek and his classmates are playing a prank on their Computer Science teacher. They change places every time the teacher turns to look at the blackboard. 11 | Each of the N students in the class can be identified by a unique roll number X and each desk has a number i associated with it. Only one student can sit on one desk. 12 | Each time the teacher turns her back, a student with roll number X sitting on desk number i gets up and takes the place of the student with roll number i. 13 | If the current position of N students in the class is given to you in an array, such that i is the desk number and a[i] is the roll number of the student sitting on the desk, can you modify a[ ] to represent the next position of all the students ?
14 | 15 | * **Example 1**:
16 |   N = 6
17 |   a[ ] = {0, 5, 1, 2, 4, 3}
18 |   Output: 0 3 5 1 4 2
19 |   Explanation: After reshuffling, the modified position of all the students would be {0, 3, 5, 1, 4, 2}.
20 | * **Example 2**:
21 |   N = 5
22 |   a[ ] = {4, 3, 2, 1, 0}
23 |   Output: 0 1 2 3 4
24 |   Explanation: After reshuffling, the modified position of all the students would be {0, 1, 2, 3, 4}.
25 | 26 | **➔ Your Task**: 27 | You dont need to read input or print anything. Complete the function prank() which takes the array a[ ] and its size N as input parameters and modifies the array in-place to reflect the new arrangement. 28 | 29 | 30 | 31 | 32 |
Expected Time Complexity Expected Auxiliary Space
O(N) O(1)
33 | 34 | #### < Constraints > 35 | 1 ≤ ` N ` ≤ 10⁵
36 | 0 ≤ ` a[i] ` ≤ N-1 37 | 38 | ### ∑ HINTS ▸ 39 | - [x] _Use simple mathematics and modular arithmetic._ 40 | ```java 41 | void prank(long[] arr, int n) 42 | { 43 | long temp = 0; 44 | long[] result = new long[n]; 45 | for (int i=0; i

55 |

⭐️ GURU-Shreyansh 56 | Problem ID: 705801🖱

57 | 60 | -------------------------------------------------------------------------------- /Day<16>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Restrictive Candy Crush

3 | 4 | `Strings` 5 | `Stack` 6 | 7 | ________________________________________________________________________________________________________________________________________________________ 8 |

:heavy_check_mark: Day 16

9 |
Given a string S and an integer K, the task is to reduce the string by applying the following operation: 10 | Choose a group of K consecutive identical characters and remove them. The operation can be performed any number of times until it is no longer possible.
11 | 12 | * **Example 1**:
13 |   K = 2
14 |   S = "geeksforgeeks"
15 |   Output: gksforgks
16 |   Explanation: Modified String after each step: "geeksforgeeks" -> "gksforgks"
17 | * **Example 2**:
18 |   K = 2
19 |   S = "geegsforgeeeks"
20 |   Output: sforgeks
21 |   Explanation: Modified String after each step: "geegsforgeeeks" -> "ggsforgeks" -> "sforgeks"
22 | 23 | **➔ Your Task**: 24 | You don't need to read input or print anything. Complete the function Reduced_String() which takes integer K and string S as input parameters and returns the reduced string. 25 | 26 | 27 | 28 | 29 |
Expected Time Complexity Expected Auxiliary Space
O(|S|) O(|S|)
30 | 31 | #### < Constraints > 32 | 1 ≤ ` |S| ` ≤ 10⁵
33 | 1 ≤ ` K ` ≤ |S| 34 | 35 | ### ∑ HINTS ▸ 36 | - [x] _Iterate over the string and use a stack to keep count of appearaces of every character._ 37 | - [x] _Every element of stack will hold information for the character and its number of consecutive appearances._ 38 | - [x] _If count reaches k, pop the stack entry._ 39 | ```java 40 | public static String reduced_String(int k, String s) 41 | { 42 | if (k==1) return ""; 43 | StringBuilder output = new StringBuilder(s); 44 | int count[] = new int[output.length()]; 45 | 46 | for(int i=0; i ⭐️ GURU-Shreyansh 65 | Problem ID: 706072🖱

66 | 69 | -------------------------------------------------------------------------------- /Day<27>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Police and Thieves

3 | 4 | `Greedy` 5 | 6 | ________________________________________________________________________________________________________________________________________________________ 7 |

:heavy_check_mark: Day 27

8 |
Given an array of size N such that each element contains either a 'P' for policeman or a 'T' for thief. Find the maximum number of thieves that can be caught by the police. Keep in mind the following conditions : 9 | 1) Each policeman can catch only one thief. 10 | 2) A policeman cannot catch a thief who is more than K units away from him.
11 | 12 | * **Example 1**:
13 |   N = 5, K = 1
14 |   arr[ ] = {P, T, T, P, T}
15 |   Output: 2
16 |   Explanation: Maximum 2 thieves can be caught. First policeman catches first thief and second police man can catch either second or third thief.
17 | * **Example 2**:
18 |   N = 6, K = 2
19 |   arr[ ] = {T, T, P, P, T, P}
20 |   Output: 3
21 |   Explanation: Maximum 3 thieves can be caught.
22 | 23 | **➔ Your Task**: 24 | You dont need to read input or print anything. Complete the function catchThieves() that takes n, k and arr[ ] as input parameters and returns the maximum number of thieves that can be caught by the police. 25 | 26 | 27 | 28 | 29 |
Expected Time Complexity Expected Auxiliary Space
O(N) O(N)
30 | 31 | #### < Constraints > 32 | 1 ≤ ` N ` ≤ 10⁵
33 | 1 ≤ ` K ` ≤ 100 34 | ` arr[i] ` = 'P' or 'T' 35 | 36 | ### ∑ HINTS ▸ 37 | - [x] _Use Greedy approach._ 38 | ```java 39 | static int catchThieves(char arr[], int n, int k) 40 | { 41 | int max = 0; 42 | boolean[] police = new boolean[n]; // Storing indices of Police 43 | Arrays.fill(police , false); 44 | 45 | for (int i=0; i ⭐️ GURU-Shreyansh 71 | Problem ID: 705822🖱

72 | 75 | -------------------------------------------------------------------------------- /Day<06>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Coins of Geekland

3 | 4 | `Arrays` 5 | `Matrix` 6 | 7 | ________________________________________________________________________________________________________________________________________________________ 8 |

:heavy_check_mark: Day 6

9 |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. 10 | Note: Coins of the negative denomination are also possible at Geekland.
11 | 12 | * **Example 1**:
13 |   Input: N = 5, K = 3
14 |   mat[[ ]] = {1, 1, 1, 1, 1} 15 | {2, 2, 2, 2, 2} 16 | {3, 8, 6, 7, 3} 17 | {4, 4, 4, 4, 4} 18 | {5, 5, 5, 5, 5}
19 |   Output: 48
20 |   Explanation: {8, 6, 7} 21 | {4, 4, 4} 22 | {5, 5, 5} has the maximum sum
23 | * **Example 2**:
24 |   N = 1, K = 1
25 |   mat[[ ]] = {{4}}
26 |   Output: 4
27 |   Explanation: NaN
28 | 29 | **➔ Your Task**: 30 | You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. 31 | 32 | 33 | 34 | 35 |
Expected Time Complexity Expected Auxiliary Space
O(N²) O(N²)
36 | 37 | #### < Constraints > 38 | 1 ≤ ` K ` ≤ ` N ` ≤ 10³
39 | -5\*10⁵ ≤ ` mat[i][j] ` ≤ 5\*10⁵ 40 | 41 | ### ∑ HINTS ▸ 42 | - [x] _Apply 2d prefix sum_ 43 | ```java 44 | public int Maximum_Sum(int mat[][], int n, int k) 45 | { 46 | int[][] preSUM = new int[n-k+1][n]; // Array to store sum of all vertical 47 | int i=0, j=0; // sub-matrices of size 1*K 48 | for (j=0; j ⭐️ GURU-Shreyansh 75 | Problem ID: 706062🖱

76 | 79 | -------------------------------------------------------------------------------- /Day<11>/ReadME.MD: -------------------------------------------------------------------------------- 1 | 2 |

:people_holding_hands: Secret Cipher

3 | 4 | `Strings` 5 | `Stack` 6 | 7 | ________________________________________________________________________________________________________________________________________________________ 8 |

:heavy_check_mark: Day 11

9 |
Geek wants to send an encrypted message in the form of string S to his friend Keeg along with instructions on how to decipher the message. To decipher the message, his friend needs to iterate over the message string from left to right, if he finds a '*', he must remove it and add all the letters read so far to the string. He must keep on doing this till he gets rid of all the '*'. 10 | Can you help Geek encrypt his message string S? 11 | 12 | Note: If the string can be encrypted in multiple ways, find the smallest encrypted string.
13 | 14 | * **Example 1**:
15 |   S = "ababcababcd"
16 |   Output: ab*c*d
17 |   Explanation: We can encrypt the string in following way : "ababcababcd" -> "ababc*d" -> "ab*c*d"
18 | * **Example 2**:
19 |   S = "zzzzzzz"
20 |   Output: z*z*z
21 |   Explanation: The string can be encrypted in 2 ways: "z*z*z" and "z\*\*zzz". Out of the two "z*z*z" is smaller in length.
22 | 23 | **➔ Your Task**: 24 | You don't need to read input or print anything. Complete the function secretCipher() which takes the message string S as input parameter and returns the shortest possible encrypted string. 25 | 26 | 27 | 28 | 29 |
Expected Time Complexity Expected Auxiliary Space
O(N) O(N)
30 | 31 | #### < Constraints > 32 | 1 ≤ ` |S| ` ≤ 10⁵
33 | 34 | ### ∑ HINTS ▸ 35 | - [x] _Find the longest proper prefix which is also a proper suffix for each prefix of the string._ 36 | ```java 37 | public String compress(String s) 38 | { 39 | int i=0, n = s.length(); 40 | ArrayList sub = new ArrayList<>(n); 41 | for (i=0; i ⭐️ GURU-Shreyansh 74 | Problem Id: 706068🖱

75 | 78 | --------------------------------------------------------------------------------