├── 2024 Month └── 2024 Month ├── GeekForGeek ├── Readme.md ├── Binary representation.java ├── Solution7.java ├── Shortest_path_from_1_to_n.java ├── Determine_if_Two_Trees_are_Identical.java ├── Euler_circuit_and_Path.java ├── Move all zeroes to end of array.java ├── Reverse_a_Doubly_Linked_List.java ├── Shuffle_integers.java ├── Solution1.java ├── Find the String.java ├── Shortest Common Supersequence.java ├── Frequencies of Limited Range Array Elements.java ├── Check if string is rotated by two places.java ├── Solution8.java ├── Predict the Column.java ├── Check_whether_BST_contains_Dead_End.java ├── Sum of XOR of all pairs.java ├── Print Matrix in snake Pattern.java ├── Check_if_strings_arerotations_ofeach_otheror_not.java ├── Pascal_Triangle.java ├── Solution4.java ├── Number following a pattern.java ├── Intersection_of_two_sorted_Linked_lists.java ├── Solution12.java ├── Solution5.java ├── Pythagorean Triplet.java ├── Find Transition Point.java ├── GFG.java ├── Minimum distance between two numbers.java ├── Print_Pattern.java ├── Solution6.java ├── Binary_Tree_to_CDLL.java ├── Solution2.java ├── Sum of upper and lower triangles.java ├── Top K Frequent Elements in Array.java ├── Symmetric_Tree.java ├── Solution11.java ├── Better String.java ├── Solution9.java ├── Isomorphic Strings.java ├── Letters Collection.java ├── Solution10.java ├── K Sum Paths.java ├── AVL_Tree_Insertion.java ├── Solution3.java └── Solution.java ├── Modified_Game_of_Nim.java ├── Binary representation.java ├── Array.java ├── Check if strings are rotations of each other or not.java ├── Check_if_a_string_is_repetition_of_its_substring_of_k-length.java ├── Rightmost_different_bit.java ├── Solution7.java ├── So.java ├── Game_of_XOR.java ├── String's_Count.java ├── Shortest_path_from_1_to_n.java ├── Solution14.java ├── Widest_Vertical_Area_Between_Two_Points_Containing_No _Points.java ├── Consecutive_1's_not_allowed.java ├── Determine_if_Two_Trees_are_Identical.java ├── Inorder_Traversal_and_BST.java ├── Painting_the_Fence.java ├── Euler_circuit_and_Path.java ├── Move all zeroes to end of array.java ├── Subarray_with_0_sum.java ├── Max-Sum-Subarray-of-size-K.java ├── Reverse_a_Doubly_Linked_List.java ├── Solution1.java ├── Shuffle_integers.java ├── Find the String.java ├── Max_Sum_without_Adjacents.java ├── Shortest Common Supersequence.java ├── Frequencies of Limited Range Array Elements.java ├── k_Occurences.java ├── Check if string is rotated by two places.java ├── Transform-to-prime.java ├── Solution8.java ├── Calculate-Money-in-Leetcode-Bank.java ├── Predict the Column.java ├── Check_whether_BST_contains_Dead_End.java ├── Number-of-subarrays-with-maximum-values-in-given-range.java ├── Maximum_Score_After_Splitting_a_String.java ├── Sum of XOR of all pairs.java ├── Gold-Mine-Problem.java ├── Print Matrix in snake Pattern.java ├── Minimize-the-Heights.java ├── Pascal_Triangle.java ├── Number following a pattern.java ├── Intersection_of_two_sorted_Linked_lists.java ├── Candy.java ├── Solution12.java ├── Smith-Number.java ├── Solution.java ├── GFG.java ├── Pythagorean Triplet.java ├── Find Transition Point.java ├── Minimum distance between two numbers.java ├── Print_Pattern.java ├── Anti Diagonal Traversal of Matrix.java ├── Solution6.java ├── Buy-Maximum-Stocks-if-i-stocks-can-be-bought-on-i-th-day.c++ ├── Wildcard_string_matching.java ├── winner_of_elective.java ├── Decode_Ways.java ├── Solution2.java ├── Binary_Tree_to_CDLL.java ├── Solution10.java ├── Sum of upper and lower triangles.java ├── Top K Frequent Elements in Array.java ├── Symmetric_Tree.java ├── Solution11.java ├── Brothers_From_Different_Roots.java ├── Better String.java ├── Solution9.java ├── Isomorphic Strings.java ├── Letters Collection.java ├── Construct-String-from-Binary-Tree.java ├── Determinant_of_a_Matrix.java ├── Reach_the_Nth_point.java ├── K Sum Paths.java ├── Arithmetic_Subarrays.java ├── Unique Length-3 Palindromic Subsequences.java ├── AVL_Tree_Insertion.java ├── Sum-string.java ├── Largest-rectangular-sub-matrix-whose-sum-is-0.java ├── Solution3.java └── README.md /2024 Month/2024 Month: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /GeekForGeek/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Modified_Game_of_Nim.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int findWinner(int n, int arr[]) { 3 | int xor = 0; 4 | for (int a : arr) 5 | xor ^= a; 6 | return (n % 2 != 0 && xor != 0) ? 2 : 1; 7 | } 8 | } -------------------------------------------------------------------------------- /Binary representation.java: -------------------------------------------------------------------------------- 1 | class Binary representation 2 | { 3 | // Function to check if Kth bit is set or not. 4 | static boolean checkKthBit(int n, int k) { 5 | // Your code here 6 | return (n = n >> k) % 2 != 0; 7 | } 8 | 9 | } -------------------------------------------------------------------------------- /GeekForGeek/Binary representation.java: -------------------------------------------------------------------------------- 1 | class Binary representation 2 | { 3 | // Function to check if Kth bit is set or not. 4 | static boolean checkKthBit(int n, int k) { 5 | // Your code here 6 | return (n = n >> k) % 2 != 0; 7 | } 8 | 9 | } -------------------------------------------------------------------------------- /Array.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Array { 4 | 5 | int sumOfDependencies(ArrayList> adj, int V) { 6 | // code here 7 | int count = 0; 8 | for (int i = 0; i < adj.size(); i++) { 9 | count += adj.get(i).size(); 10 | } 11 | return count; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Check if strings are rotations of each other or not.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | 4 | public static boolean areRotations(String s1, String s2) { 5 | // Your code here 6 | if (s1.length() != s2.length()) { 7 | return false; 8 | } 9 | s1 += s1; 10 | return s1.contains(s2); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Check_if_a_string_is_repetition_of_its_substring_of_k-length.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int kSubstrConcat(int n, String s, int k) 4 | { 5 | if(n%k!=0) return 0; 6 | HashSet set=new HashSet<>(); 7 | for(int i=0;i>=1; 13 | } 14 | return c; 15 | } 16 | } -------------------------------------------------------------------------------- /Solution7.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int isPossible(int N, int arr[]) { 3 | 4 | int sum=0; 5 | 6 | for(int i:arr){ 7 | while(i>0){ 8 | int rem=i%10; 9 | i/=10; 10 | sum+=rem; 11 | } 12 | } 13 | return sum%3==0?1:0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /GeekForGeek/Solution7.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int isPossible(int N, int arr[]) { 3 | 4 | int sum=0; 5 | 6 | for(int i:arr){ 7 | while(i>0){ 8 | int rem=i%10; 9 | i/=10; 10 | sum+=rem; 11 | } 12 | } 13 | return sum%3==0?1:0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /So.java: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public int minOperation(int n) 4 | { 5 | //code here. 6 | if(n<=2) return n; 7 | int curr=n,count=0; 8 | while(curr>0){ 9 | if(curr%2==0) curr/=2; 10 | else curr--; 11 | count++; 12 | } 13 | return count; 14 | } 15 | } { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Game_of_XOR.java: -------------------------------------------------------------------------------- 1 | public class Game_of_XOR { 2 | static int gameOfXor(int N, int[] A) { 3 | // code here 4 | int ans = 0; 5 | for (int i = 0; i < N; i++) { 6 | int count = (i + 1) * (N - i); 7 | if (count % 2 != 0) { 8 | ans = ans ^ (A[i]); 9 | } 10 | } 11 | return ans; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /String's_Count.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static long countStr(long n) { 3 | // code here 4 | long case1 = 1; 5 | long case2 = n; 6 | long case3 = n; 7 | long case4 = n * (n - 1); 8 | long case5 = (n * (n - 1)) / 2; 9 | long case6 = (n * (n - 1) * (n - 2)) / 2; 10 | 11 | return case1 + case2 + case3 + case4 + case5 + case6; 12 | } 13 | } -------------------------------------------------------------------------------- /Shortest_path_from_1_to_n.java: -------------------------------------------------------------------------------- 1 | public class Shortest_path_from_1_to_n { 2 | 3 | public int minStep(int n) { 4 | // code here 5 | int ans = 0; 6 | while (n > 1) { 7 | if (n % 3 == 0) { 8 | n = n / 3; 9 | } 10 | 11 | else 12 | n--; 13 | ans++; 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /GeekForGeek/Shortest_path_from_1_to_n.java: -------------------------------------------------------------------------------- 1 | public class Shortest_path_from_1_to_n { 2 | 3 | public int minStep(int n) { 4 | // code here 5 | int ans = 0; 6 | while (n > 1) { 7 | if (n % 3 == 0) { 8 | n = n / 3; 9 | } 10 | 11 | else 12 | n--; 13 | ans++; 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Solution14.java: -------------------------------------------------------------------------------- 1 | public class Solution14 { 2 | 3 | { 4 | public int is_bleak(int n) 5 | { 6 | // Code here 7 | int num=String.valueOf(Math.log(n)).length() + 1; 8 | for(int i=n-num;i Integer.compare(a[0], b[0])); 6 | int diff = 0; 7 | for (int i = 1; i < points.length; i++) { 8 | diff = Math.max(diff, points[i][0] - points[i - 1][0]); 9 | } 10 | return diff; 11 | } 12 | } -------------------------------------------------------------------------------- /Consecutive_1's_not_allowed.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | long countStrings(int n) { 3 | // code here 4 | int MOD = 1000000007; 5 | long zero = 1; 6 | long one = 1; 7 | 8 | for (int i = 2; i <= n; i++) { 9 | long nZero = (zero + one) % MOD; 10 | long nOne = zero; 11 | 12 | zero = nZero; 13 | one = nOne; 14 | } 15 | return (int) (zero + one) % MOD; 16 | } 17 | } -------------------------------------------------------------------------------- /Determine_if_Two_Trees_are_Identical.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to check if two trees are identical. 3 | boolean isIdentical(Node root1, Node root2) { 4 | // Code Here 5 | if (root1 == null || root2 == null) 6 | return root1 == root2; 7 | if (root1.data != root2.data) 8 | return false; 9 | 10 | return isIdentical(root1.left, root2.left) && isIdentical(root1.right, root2.right); 11 | 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /GeekForGeek/Determine_if_Two_Trees_are_Identical.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to check if two trees are identical. 3 | boolean isIdentical(Node root1, Node root2) { 4 | // Code Here 5 | if (root1 == null || root2 == null) 6 | return root1 == root2; 7 | if (root1.data != root2.data) 8 | return false; 9 | 10 | return isIdentical(root1.left, root2.left) && isIdentical(root1.right, root2.right); 11 | 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /Inorder_Traversal_and_BST.java: -------------------------------------------------------------------------------- 1 | public class Inorder_Traversal_and_BST { 2 | static int isRepresentingBST(int arr[], int N) { 3 | // code here 4 | if (N == 1 || N == 0) { 5 | return 1; 6 | } 7 | for (int i = 0; i < N - 1; i++) { 8 | if (arr[i] > arr[i + 1]) { 9 | return 0; 10 | } 11 | } 12 | if (arr[N - 2] > arr[N - 1]) { 13 | return 0; 14 | } 15 | return 1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Painting_the_Fence.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | long countWays(int n, int k) { 4 | // code here. 5 | long mod = 1000000007; 6 | if (n == 1) { 7 | return k; 8 | } 9 | 10 | long same = 0; 11 | long diff = k; 12 | 13 | for (int i = 2; i <= n; i++) { 14 | long prev = same; 15 | same = diff; 16 | diff = (prev + same) * (k - 1) % mod; 17 | } 18 | return (same + diff) % mod; 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /Euler_circuit_and_Path.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class Euler_circuit_and_Path { 4 | public int isEulerCircuit(int V, List[] adj) { 5 | int e = 0, od = 0; 6 | for (int i = 0; i < V; i++) { 7 | if (adj[i].size() % 2 == 0) { 8 | e++; 9 | } else 10 | od++; 11 | } 12 | if (e == V) 13 | return 2; 14 | if (od > 0 && od == 2) 15 | return 1; 16 | return 0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Move all zeroes to end of array.java: -------------------------------------------------------------------------------- 1 | public class Move 2 | all zeroes 3 | to end 4 | of array 5 | { 6 | class Solution { 7 | void pushZerosToEnd(int[] arr, int n) { 8 | // code here 9 | int i=0,j=0; 10 | while(j set = new HashSet<>(); 6 | set.add(0); 7 | 8 | for (int i = 0; i < n; i++) { 9 | sum = sum + arr[i]; 10 | if (set.contains(sum)) 11 | return true; 12 | set.add(sum); 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /GeekForGeek/Euler_circuit_and_Path.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class Euler_circuit_and_Path { 4 | public int isEulerCircuit(int V, List[] adj) { 5 | int e = 0, od = 0; 6 | for (int i = 0; i < V; i++) { 7 | if (adj[i].size() % 2 == 0) { 8 | e++; 9 | } else 10 | od++; 11 | } 12 | if (e == V) 13 | return 2; 14 | if (od > 0 && od == 2) 15 | return 1; 16 | return 0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /GeekForGeek/Move all zeroes to end of array.java: -------------------------------------------------------------------------------- 1 | public class Move 2 | all zeroes 3 | to end 4 | of array 5 | { 6 | class Solution { 7 | void pushZerosToEnd(int[] arr, int n) { 8 | // code here 9 | int i=0,j=0; 10 | while(j Arr, int N) { 3 | if (N < K) { 4 | return -1; 5 | } 6 | long res = 0; 7 | for (int i = 0; i < K; i++) { 8 | res += Arr.get(i); 9 | } 10 | long currsum = res; 11 | for (int i = K; i < N; i++) { 12 | currsum += Arr.get(i) - Arr.get(i - K); 13 | res = Math.max(res, currsum); 14 | } 15 | return res; 16 | } 17 | } -------------------------------------------------------------------------------- /Reverse_a_Doubly_Linked_List.java: -------------------------------------------------------------------------------- 1 | import org.w3c.dom.Node; 2 | 3 | public class Reverse_a_Doubly_Linked_List { 4 | public static Node reverseDLL(Node head) { 5 | // Your code here 6 | Node temp = head; 7 | Node prev = null; 8 | Node next = null; 9 | while (temp != null) { 10 | next = temp.next; 11 | temp.next = prev; 12 | temp.prev = next; 13 | prev = temp; 14 | temp = next; 15 | } 16 | return prev; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Solution1.java: -------------------------------------------------------------------------------- 1 | class Solution1 { 2 | public static int floor(Node root, int x) { 3 | // Code here 4 | int a, b; 5 | if (root == null) { 6 | return -1; 7 | } else { 8 | if (root.data == x) 9 | return x; 10 | else if (root.data > x) 11 | return floor(root.left, x); 12 | else { 13 | a = root.data; 14 | b = floor(root.right, x); 15 | return a > b ? a : b; 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Shuffle_integers.java: -------------------------------------------------------------------------------- 1 | public class Shuffle_integers { 2 | void shuffleArray(long arr[], int n) { 3 | // Your code goes here 4 | 5 | solve(arr, n / 2 - 1, n - 1, n); 6 | 7 | } 8 | 9 | void solve(long arr[], int i, int j, int count) { 10 | if (count <= 0) { 11 | return; 12 | } 13 | 14 | long a = arr[i]; 15 | long b = arr[j]; 16 | 17 | solve(arr, i - 1, j - 1, count - 2); 18 | 19 | arr[count - 2] = a; 20 | arr[count - 1] = b; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /GeekForGeek/Reverse_a_Doubly_Linked_List.java: -------------------------------------------------------------------------------- 1 | import org.w3c.dom.Node; 2 | 3 | public class Reverse_a_Doubly_Linked_List { 4 | public static Node reverseDLL(Node head) { 5 | // Your code here 6 | Node temp = head; 7 | Node prev = null; 8 | Node next = null; 9 | while (temp != null) { 10 | next = temp.next; 11 | temp.next = prev; 12 | temp.prev = next; 13 | prev = temp; 14 | temp = next; 15 | } 16 | return prev; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /GeekForGeek/Shuffle_integers.java: -------------------------------------------------------------------------------- 1 | public class Shuffle_integers { 2 | void shuffleArray(long arr[], int n) { 3 | // Your code goes here 4 | 5 | solve(arr, n / 2 - 1, n - 1, n); 6 | 7 | } 8 | 9 | void solve(long arr[], int i, int j, int count) { 10 | if (count <= 0) { 11 | return; 12 | } 13 | 14 | long a = arr[i]; 15 | long b = arr[j]; 16 | 17 | solve(arr, i - 1, j - 1, count - 2); 18 | 19 | arr[count - 2] = a; 20 | arr[count - 1] = b; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Find the String.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner scanner = new Scanner(System.in); 7 | 8 | System.out.print("Enter the value of n: "); 9 | int n = scanner.nextInt(); 10 | 11 | System.out.print("Enter the value of k: "); 12 | int k = scanner.nextInt(); 13 | 14 | Solution solution = new Solution(); 15 | String result = solution.findString(n, k); 16 | 17 | System.out.println("Result: " + result); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /GeekForGeek/Solution1.java: -------------------------------------------------------------------------------- 1 | class Solution1 { 2 | public static int floor(Node root, int x) { 3 | // Code here 4 | int a, b; 5 | if (root == null) { 6 | return -1; 7 | } else { 8 | if (root.data == x) 9 | return x; 10 | else if (root.data > x) 11 | return floor(root.left, x); 12 | else { 13 | a = root.data; 14 | b = floor(root.right, x); 15 | return a > b ? a : b; 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Max_Sum_without_Adjacents.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | int findMaxSum(int arr[], int n) { 4 | 5 | // code here 6 | 7 | int take = arr[0], notTake = 0; 8 | 9 | 10 | 11 | for(int i=1; i map = new HashMap<>(); 7 | for (int i = 0; i < n; i++) { 8 | map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); 9 | } 10 | for (int i = 0; i < n; i++) { 11 | if (map.containsKey(i + 1)) { 12 | arr[i] = map.get(i + 1); 13 | } else { 14 | arr[i] = 0; 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /GeekForGeek/Shortest Common Supersequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int shortestCommonSupersequence(String x, String y, int n, int m) { 3 | int[][] dp = new int[n + 1][m + 1]; 4 | 5 | for (int i = 1; i <= n; i++) { 6 | for (int j = 1; j <= m; j++) { 7 | if (x.charAt(i - 1) == y.charAt(j - 1)) 8 | dp[i][j] = 1 + dp[i - 1][j - 1]; 9 | else 10 | dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); 11 | } 12 | } 13 | 14 | return n + m - dp[n][m]; 15 | } 16 | } -------------------------------------------------------------------------------- /k_Occurences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to find all elements in array that appear more than n/k times. 3 | public int countOccurence(int[] arr, int n, int k) { 4 | k = n / k; // Updated value of k 5 | int ans = 0; 6 | Arrays.sort(arr); 7 | 8 | for (int i = 1; i < n; i++) { 9 | int count = 0; 10 | while (i < n && arr[i] == arr[i - 1]) { 11 | count++; 12 | i++; 13 | } 14 | if (count >= k) 15 | ans++; 16 | } 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /GeekForGeek/Frequencies of Limited Range Array Elements.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | 4 | public static void frequencyCount(int arr[], int n, int P) { 5 | // code here 6 | HashMap map = new HashMap<>(); 7 | for (int i = 0; i < n; i++) { 8 | map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); 9 | } 10 | for (int i = 0; i < n; i++) { 11 | if (map.containsKey(i + 1)) { 12 | arr[i] = map.get(i + 1); 13 | } else { 14 | arr[i] = 0; 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Check if string is rotated by two places.java: -------------------------------------------------------------------------------- 1 | public class Check if 2 | string is 3 | rotated by 4 | two places 5 | { 6 | 7 | public static boolean isRotated(String str1, String str2) 8 | 9 | { 10 | 11 | if (str1.length() != str2.length()) 12 | return false; 13 | 14 | String s = str1 + str1; 15 | 16 | String su = s.substring(2, str1.length() + 2); 17 | 18 | String su1 = s.substring(str1.length() - 2, s.length() - 2); 19 | 20 | if (str2.equals(su) || str2.equals(su1)) 21 | return true; 22 | 23 | return false; 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Transform-to-prime.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public int minNumber(int arr[], int N) { 4 | // O(n) time O(1) space 5 | int sum = 0; 6 | for (int i : arr) 7 | sum += i; 8 | 9 | // O(nlogn) time O(1) space 10 | int temp = sum; 11 | while (!isPrime(temp)) 12 | temp++; 13 | 14 | return temp - sum; 15 | } 16 | 17 | // O(logn) time no space 18 | public boolean isPrime(int n) { 19 | for (int i = 2; i <= Math.sqrt(n); i++) 20 | if (n % i == 0) 21 | return false; 22 | return true; 23 | } 24 | } -------------------------------------------------------------------------------- /GeekForGeek/Check if string is rotated by two places.java: -------------------------------------------------------------------------------- 1 | public class Check if 2 | string is 3 | rotated by 4 | two places 5 | { 6 | 7 | public static boolean isRotated(String str1, String str2) 8 | 9 | { 10 | 11 | if (str1.length() != str2.length()) 12 | return false; 13 | 14 | String s = str1 + str1; 15 | 16 | String su = s.substring(2, str1.length() + 2); 17 | 18 | String su1 = s.substring(str1.length() - 2, s.length() - 2); 19 | 20 | if (str2.equals(su) || str2.equals(su1)) 21 | return true; 22 | 23 | return false; 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Solution8.java: -------------------------------------------------------------------------------- 1 | public class Solution8 { 2 | class Solution { 3 | static long sumOfDivisors(int N) { 4 | // code here 5 | // 1*4 = 4 6 | // 2*2 = 4 7 | // 3*1 = 3 8 | // 4*1 = 4 9 | long ans = 0; 10 | for (int i = 1; i <= N; i++) { 11 | ans += i * (N / i); 12 | } 13 | return ans; 14 | } 15 | 16 | public static void main(String[] args) { 17 | sumOfDivisors(0); 18 | char[] ans; 19 | System.out.println(long ans); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Calculate-Money-in-Leetcode-Bank.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int totalMoney(int n) { 3 | int week_count = n / 7; 4 | int remaining_days = n % 7; 5 | 6 | int total = ((week_count * (week_count - 1)) / 2) * 7; // Contribution from complete weeks 7 | total += week_count * 28; // Contribution from complete weeks (additional on Mondays) 8 | total += ((remaining_days * (remaining_days + 1)) / 2) + (week_count * remaining_days); // Contribution from 9 | // remaining days 10 | 11 | return total; 12 | } 13 | } -------------------------------------------------------------------------------- /Predict the Column.java: -------------------------------------------------------------------------------- 1 | Predict the column{ 2 | int columnWithMaxZeros(int arr[][], int N) 3 | { 4 | // code here 5 | int count=0; 6 | int data=-1; 7 | int max=-1; 8 | for(int i=0;i0 && count>data){ 18 | data=count; 19 | max=i; 20 | } 21 | count=0; 22 | } 23 | return max; 24 | } 25 | } -------------------------------------------------------------------------------- /GeekForGeek/Solution8.java: -------------------------------------------------------------------------------- 1 | public class Solution8 { 2 | class Solution { 3 | static long sumOfDivisors(int N) { 4 | // code here 5 | // 1*4 = 4 6 | // 2*2 = 4 7 | // 3*1 = 3 8 | // 4*1 = 4 9 | long ans = 0; 10 | for (int i = 1; i <= N; i++) { 11 | ans += i * (N / i); 12 | } 13 | return ans; 14 | } 15 | 16 | public static void main(String[] args) { 17 | sumOfDivisors(0); 18 | char[] ans; 19 | System.out.println(long ans); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /GeekForGeek/Predict the Column.java: -------------------------------------------------------------------------------- 1 | Predict the column{ 2 | int columnWithMaxZeros(int arr[][], int N) 3 | { 4 | // code here 5 | int count=0; 6 | int data=-1; 7 | int max=-1; 8 | for(int i=0;i0 && count>data){ 18 | data=count; 19 | max=i; 20 | } 21 | count=0; 22 | } 23 | return max; 24 | } 25 | } -------------------------------------------------------------------------------- /Check_whether_BST_contains_Dead_End.java: -------------------------------------------------------------------------------- 1 | import org.w3c.dom.Node; 2 | 3 | public class Check_whether_BST_contains_Dead_End { 4 | public static boolean isDeadEnd(Node root) { 5 | return isDeadEndHelper(root, 1, Integer.MAX_VALUE); 6 | } 7 | 8 | private static boolean isDeadEndHelper(Node node, int min, int max) { 9 | if (node == null) 10 | return false; 11 | if (min == max) 12 | return true; 13 | boolean leftDeadEnd = isDeadEndHelper(node.left, min, node.data - 1); 14 | boolean rightDeadEnd = isDeadEndHelper(node.right, node.data + 1, max); 15 | return leftDeadEnd || rightDeadEnd; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /GeekForGeek/Check_whether_BST_contains_Dead_End.java: -------------------------------------------------------------------------------- 1 | import org.w3c.dom.Node; 2 | 3 | public class Check_whether_BST_contains_Dead_End { 4 | public static boolean isDeadEnd(Node root) { 5 | return isDeadEndHelper(root, 1, Integer.MAX_VALUE); 6 | } 7 | 8 | private static boolean isDeadEndHelper(Node node, int min, int max) { 9 | if (node == null) 10 | return false; 11 | if (min == max) 12 | return true; 13 | boolean leftDeadEnd = isDeadEndHelper(node.left, min, node.data - 1); 14 | boolean rightDeadEnd = isDeadEndHelper(node.right, node.data + 1, max); 15 | return leftDeadEnd || rightDeadEnd; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Number-of-subarrays-with-maximum-values-in-given-range.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static long countLR(int arr[], int n, int x) { 3 | long count = 0; 4 | long sum = 0; 5 | for (int i = 0; i < n; i++) { 6 | if (arr[i] > x) { 7 | sum += (count * (count + 1)) / 2; 8 | count = 0; 9 | } else { 10 | count++; 11 | 12 | } 13 | } 14 | sum += (count * (count + 1)) / 2; 15 | return sum; 16 | } 17 | 18 | static long countSubarrays(int a[], int n, int L, int R) { 19 | // Complete the function 20 | return countLR(a, n, R) - countLR(a, n, L - 1); 21 | } 22 | } -------------------------------------------------------------------------------- /Maximum_Score_After_Splitting_a_String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxScore(String s) { 3 | int length = s.length(); 4 | int ones = 0; 5 | int tmpScore = s.charAt(0) == '0' ? 1 : 0; 6 | int score = tmpScore; 7 | for (int i = 1; i < length - 1; i++) { 8 | if (s.charAt(i) == '0') { 9 | tmpScore += 1; 10 | } else { 11 | ones++; 12 | tmpScore -= 1; 13 | } 14 | 15 | if (tmpScore > score) { 16 | score = tmpScore; 17 | } 18 | } 19 | ones += (s.charAt(length - 1) == '1' ? 1 : 0); 20 | 21 | return ones + score; 22 | } 23 | } -------------------------------------------------------------------------------- /Sum of XOR of all pairs.java: -------------------------------------------------------------------------------- 1 | class Sum 2 | of XOR 3 | of all pairs{ 4 | 5 | // Function for finding maximum and value pair 6 | public long sumXOR(int arr[], int n) { 7 | long sum = 0; 8 | for (int i = 0; i < 32; i++) { 9 | long set_bits = 0, unset_bits = 0; 10 | for (int j = 0; j < n; j++) { 11 | if ((arr[j] & (1 << i)) != 0) 12 | set_bits++; 13 | else 14 | unset_bits++; 15 | } 16 | 17 | long possible_combinations = set_bits * unset_bits; 18 | sum += possible_combinations * (1 << i); 19 | } 20 | 21 | return sum; 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /GeekForGeek/Sum of XOR of all pairs.java: -------------------------------------------------------------------------------- 1 | class Sum 2 | of XOR 3 | of all pairs{ 4 | 5 | // Function for finding maximum and value pair 6 | public long sumXOR(int arr[], int n) { 7 | long sum = 0; 8 | for (int i = 0; i < 32; i++) { 9 | long set_bits = 0, unset_bits = 0; 10 | for (int j = 0; j < n; j++) { 11 | if ((arr[j] & (1 << i)) != 0) 12 | set_bits++; 13 | else 14 | unset_bits++; 15 | } 16 | 17 | long possible_combinations = set_bits * unset_bits; 18 | sum += possible_combinations * (1 << i); 19 | } 20 | 21 | return sum; 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Gold-Mine-Problem.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int maxGold(int n, int m, int M[][]) 3 | { 4 | int dp[][] = new int[n+1][m+1]; 5 | for(int j = m-1; j>=0; j--){ 6 | for(int i = n-1; i>=0; i--){ 7 | if(i-1 >= 0) 8 | dp[i][j] = M[i][j] + Math.max(dp[i-1][j+1], Math.max(dp[i][j + 1], dp[i+1][j+1])); 9 | else 10 | dp[i][j] = M[i][j] + Math.max(dp[i][j + 1], dp[i+1][j+1]); 11 | 12 | } 13 | } 14 | 15 | int ans = dp[0][0]; 16 | for(int i = 1; i snakePattern(int matrix[][]) 5 | { 6 | // code here 7 | ArrayList snakePattern = new ArrayList<>(); 8 | int toAdd = 1; 9 | int col = 0; 10 | for(int row = 0; row < matrix.length; row++) { 11 | while(col != -1 && col != matrix[row].length) { 12 | snakePattern.add(matrix[row][col]); 13 | col += toAdd; 14 | } 15 | 16 | toAdd *= -1; 17 | col += toAdd; 18 | } 19 | 20 | return snakePattern; 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Minimize-the-Heights.java: -------------------------------------------------------------------------------- 1 | public class Minimize-the-Heights 2 | { 3 | 4 | int getMinDiff(int[] arr, int n, int k) { 5 | Arrays.sort(arr); 6 | int min=arr[0]; //minimum element at index 0 7 | int max= arr[n-1]; //maximum at last index 8 | int ans=max-min; //difference 9 | //@author: pankaj meharchandani 10 | for(int i=1 ; i snakePattern(int matrix[][]) 5 | { 6 | // code here 7 | ArrayList snakePattern = new ArrayList<>(); 8 | int toAdd = 1; 9 | int col = 0; 10 | for(int row = 0; row < matrix.length; row++) { 11 | while(col != -1 && col != matrix[row].length) { 12 | snakePattern.add(matrix[row][col]); 13 | col += toAdd; 14 | } 15 | 16 | toAdd *= -1; 17 | col += toAdd; 18 | } 19 | 20 | return snakePattern; 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Pascal_Triangle.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Pascal_Triangle { 4 | 5 | ArrayList nthRowOfPascalTriangle(int n) { 6 | ArrayList pre = new ArrayList<>(); 7 | long mod = 1000000007; 8 | for (int i = 0; i < n; i++) { 9 | ArrayList current = new ArrayList<>(); 10 | for (int j = 0; j < i + 1; j++) { 11 | if (j == i | j == 0) { 12 | current.add(1l); 13 | } else { 14 | long num = (pre.get(j) + pre.get(j - 1)) % mod; 15 | current.add(num); 16 | } 17 | } 18 | pre = current; 19 | } 20 | return pre; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /GeekForGeek/Check_if_strings_arerotations_ofeach_otheror_not.java: -------------------------------------------------------------------------------- 1 | public class Check_if_strings_arerotations_ofeach_otheror_not { 2 | 3 | public static boolean areRotations(String s1, String s2) { 4 | if (s1.length() != s2.length()) { 5 | return false; 6 | } 7 | s1 += s1; 8 | return s1.contains(s2); 9 | } 10 | 11 | public static void main(String[] args) { 12 | // Example usage: 13 | String s1 = "abcde"; 14 | String s2 = "deabc"; 15 | 16 | if (areRotations(s1, s2)) { 17 | System.out.println(s2 + " is a rotation of " + s1); 18 | } else { 19 | System.out.println(s2 + " is not a rotation of " + s1); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Number following a pattern.java: -------------------------------------------------------------------------------- 1 | public class Number 2 | following a pattern 3 | { 4 | 5 | static String printMinNumberForPattern(String S) { 6 | // code here 7 | String ans = ""; 8 | Stack st = new Stack<>(); 9 | int num = 1; 10 | for (int i = 0; i < S.length(); i++) { 11 | st.push(num); 12 | num++; 13 | if (S.charAt(i) == 'I') { 14 | while (!st.isEmpty()) { 15 | ans += Integer.toString(st.pop()); 16 | } 17 | } 18 | } 19 | st.push(num); 20 | while (!st.isEmpty()) { 21 | ans += Integer.toString(st.pop()); 22 | } 23 | return ans; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /GeekForGeek/Pascal_Triangle.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Pascal_Triangle { 4 | 5 | ArrayList nthRowOfPascalTriangle(int n) { 6 | ArrayList pre = new ArrayList<>(); 7 | long mod = 1000000007; 8 | for (int i = 0; i < n; i++) { 9 | ArrayList current = new ArrayList<>(); 10 | for (int j = 0; j < i + 1; j++) { 11 | if (j == i | j == 0) { 12 | current.add(1l); 13 | } else { 14 | long num = (pre.get(j) + pre.get(j - 1)) % mod; 15 | current.add(num); 16 | } 17 | } 18 | pre = current; 19 | } 20 | return pre; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /GeekForGeek/Solution4.java: -------------------------------------------------------------------------------- 1 | class Solution4{ 2 | static ArrayList> transitiveClosure(int n, int graph[][]) { 3 | for (int via = 0; via < n; via++) { 4 | for (int i = 0; i < n; i++) { 5 | for (int j = 0; j < n; j++) { 6 | if (graph[i][j] == 0 && graph[i][via] == 1 && graph[via][j] == 1) { 7 | graph[i][j] = 1; 8 | } 9 | if (i == j) { 10 | graph[i][j] = 1; 11 | } 12 | } 13 | } 14 | } 15 | ArrayList> ans = new ArrayList>(); 16 | for (int i = 0; i < n; i++) { 17 | ArrayList row = new ArrayList<>(); 18 | for (int j = 0; j < n; j++) { 19 | row.add(graph[i][j]); 20 | } 21 | ans.add(row); 22 | } 23 | return ans; 24 | } 25 | } -------------------------------------------------------------------------------- /GeekForGeek/Number following a pattern.java: -------------------------------------------------------------------------------- 1 | public class Number 2 | following a pattern 3 | { 4 | 5 | static String printMinNumberForPattern(String S) { 6 | // code here 7 | String ans = ""; 8 | Stack st = new Stack<>(); 9 | int num = 1; 10 | for (int i = 0; i < S.length(); i++) { 11 | st.push(num); 12 | num++; 13 | if (S.charAt(i) == 'I') { 14 | while (!st.isEmpty()) { 15 | ans += Integer.toString(st.pop()); 16 | } 17 | } 18 | } 19 | st.push(num); 20 | while (!st.isEmpty()) { 21 | ans += Integer.toString(st.pop()); 22 | } 23 | return ans; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Intersection_of_two_sorted_Linked_lists.java: -------------------------------------------------------------------------------- 1 | class Intersection_of_two_sorted_Linked_lists { 2 | public static Node findIntersection(Node head1, Node head2) { 3 | // code here. 4 | Node demmy = new Node(-1); 5 | Node curr = demmy; 6 | while (head1 != null && head2 != null) { 7 | if (head1.data == head2.data) { 8 | Node temp = new Node(head1.data); 9 | curr.next = temp; 10 | curr = curr.next; 11 | head1 = head1.next; 12 | head2 = head2.next; 13 | 14 | } else if (head1.data < head2.data) 15 | head1 = head1.next; 16 | else 17 | head2 = head2.next; 18 | } 19 | return demmy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /Candy.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int minCandy(int N, int ratings[]) { 3 | int[] nums = new int[ratings.length]; 4 | for (int i = 0; i < nums.length; i++) { 5 | nums[i] = 1; 6 | } 7 | for (int i = 1; i < nums.length; i++) { 8 | if (ratings[i] > ratings[i - 1]) { 9 | nums[i] = nums[i - 1] + 1; 10 | } 11 | } 12 | for (int i = nums.length - 1; i >= 1; i--) { 13 | if (ratings[i - 1] > ratings[i] && nums[i - 1] <= nums[i]) { 14 | nums[i - 1] = nums[i] + 1; 15 | } 16 | } 17 | int sum = 0; 18 | for (int i = 0; i < nums.length; i++) { 19 | sum = sum + nums[i]; 20 | } 21 | return sum; 22 | } 23 | } -------------------------------------------------------------------------------- /GeekForGeek/Intersection_of_two_sorted_Linked_lists.java: -------------------------------------------------------------------------------- 1 | class Intersection_of_two_sorted_Linked_lists { 2 | public static Node findIntersection(Node head1, Node head2) { 3 | // code here. 4 | Node demmy = new Node(-1); 5 | Node curr = demmy; 6 | while (head1 != null && head2 != null) { 7 | if (head1.data == head2.data) { 8 | Node temp = new Node(head1.data); 9 | curr.next = temp; 10 | curr = curr.next; 11 | head1 = head1.next; 12 | head2 = head2.next; 13 | 14 | } else if (head1.data < head2.data) 15 | head1 = head1.next; 16 | else 17 | head2 = head2.next; 18 | } 19 | return demmy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /Solution12.java: -------------------------------------------------------------------------------- 1 | class Solution12 { 2 | static int minimumNumberOfDeletions(String S) { 3 | 4 | // code here 5 | Integer[][] dp = new Integer[S.length()][S.length()]; 6 | return S.length() - lps(S, 0, S.length() - 1, dp); 7 | } 8 | 9 | public static int lps(String s, int start, int end, Integer[][] dp) { 10 | if (start > end) 11 | return 0; 12 | if (start == end) 13 | return 1; 14 | if (dp[start][end] == null) { 15 | if (s.charAt(start) == s.charAt(end)) 16 | dp[start][end] = 2 + lps(s, start + 1, end - 1, dp); 17 | else 18 | dp[start][end] = Math.max(lps(s, start + 1, end, dp), lps(s, start, end - 1, dp)); 19 | } 20 | return dp[start][end]; 21 | } 22 | } -------------------------------------------------------------------------------- /GeekForGeek/Solution12.java: -------------------------------------------------------------------------------- 1 | class Solution12 { 2 | static int minimumNumberOfDeletions(String S) { 3 | 4 | // code here 5 | Integer[][] dp = new Integer[S.length()][S.length()]; 6 | return S.length() - lps(S, 0, S.length() - 1, dp); 7 | } 8 | 9 | public static int lps(String s, int start, int end, Integer[][] dp) { 10 | if (start > end) 11 | return 0; 12 | if (start == end) 13 | return 1; 14 | if (dp[start][end] == null) { 15 | if (s.charAt(start) == s.charAt(end)) 16 | dp[start][end] = 2 + lps(s, start + 1, end - 1, dp); 17 | else 18 | dp[start][end] = Math.max(lps(s, start + 1, end, dp), lps(s, start, end - 1, dp)); 19 | } 20 | return dp[start][end]; 21 | } 22 | } -------------------------------------------------------------------------------- /Smith-Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int smithNum(int n) { 3 | // code here 4 | int sum = 0; 5 | int k = n; 6 | int temp = digitsum(n); 7 | 8 | // finding prime factor of number 9 | for (int i = 2; i <= Math.sqrt(n); i++) { 10 | while (n % i == 0) { 11 | sum += digitsum(i); 12 | n /= i; 13 | } 14 | } 15 | if (n > 1 && n != k) { 16 | sum += digitsum(n); 17 | } 18 | return sum == temp ? 1 : 0; 19 | } 20 | 21 | // finding addition of number 22 | static int digitsum(int n) { 23 | int temp = n; 24 | int ans = 0; 25 | while (temp != 0) { 26 | ans += temp % 10; 27 | temp /= 10; 28 | } 29 | return ans; 30 | } 31 | } -------------------------------------------------------------------------------- /Solution.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int knapSack(int N, int W, int val[], int wt[]) { 3 | Integer[][] memo = new Integer[N + 1][W + 1]; 4 | return solve(N, W, val, wt, memo); 5 | } 6 | 7 | static int solve(int N, int W, int[] val, int[] wt, Integer[][] memo) { 8 | if (N == 0) { 9 | return 0; 10 | } 11 | 12 | if (W < 0) { 13 | return 0; 14 | } 15 | 16 | if (memo[N][W] != null) { 17 | return memo[N][W]; 18 | } 19 | 20 | int notPick = solve(N - 1, W, val, wt, memo); 21 | int pick = 0; 22 | if (wt[N - 1] <= W) { 23 | pick = val[N - 1] + solve(N, W - wt[N - 1], val, wt, memo); 24 | } 25 | 26 | return memo[N][W] = Math.max(notPick, pick); 27 | } 28 | } -------------------------------------------------------------------------------- /GeekForGeek/Solution5.java: -------------------------------------------------------------------------------- 1 | class Solution5 { 2 | 3 | List eventualSafeNodes(int V, List> adj) { 4 | List safeNodes = new ArrayList<>(); 5 | int[] state = new int[V]; 6 | for (int i = 0; i < V; i++) { 7 | if (isSafeNode(i, adj, state)) 8 | safeNodes.add(i); 9 | } 10 | Collections.sort(safeNodes); 11 | return safeNodes; 12 | } 13 | 14 | private boolean isSafeNode(int node, List> adj, int[] state) { 15 | if (state[node] != 0) 16 | return state[node] == 2; 17 | state[node] = 1; 18 | for (int neighbour : adj.get(node)) { 19 | if (!isSafeNode(neighbour, adj, state)) 20 | return false; 21 | } 22 | state[node] = 2; 23 | return true; 24 | } 25 | } -------------------------------------------------------------------------------- /GFG.java: -------------------------------------------------------------------------------- 1 | class GfG { 2 | Node buildBalancedTree(Node root) { 3 | // Add your code here. 4 | ArrayList list = new ArrayList<>(); 5 | inOrder(root, list); 6 | Node res = BalancedBst(list, 0, list.size() - 1); 7 | return res; 8 | } 9 | 10 | void inOrder(Node root, ArrayList list) { 11 | if (root == null) 12 | return; 13 | inOrder(root.left, list); 14 | list.add(root.data); 15 | inOrder(root.right, list); 16 | } 17 | 18 | Node BalancedBst(ArrayList list, int l, int r) { 19 | if (l > r) { 20 | return null; 21 | } 22 | int mid = (l + r) / 2; 23 | Node root = new Node(list.get(mid)); 24 | root.left = BalancedBst(list, l, mid - 1); 25 | root.right = BalancedBst(list, mid + 1, r); 26 | return root; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Pythagorean Triplet.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | boolean checkTriplet(int[] arr, int n) { 3 | // code here 4 | 5 | Set sqr = new HashSet<>(); 6 | long[] sqrArr = new long[n]; 7 | 8 | // store all squares in a set and Array 9 | for (int i = 0; i < n; i++) { 10 | long val = (long) Math.pow(arr[i], 2); 11 | sqr.add(val); 12 | sqrArr[i] = val; 13 | } 14 | 15 | // iterate array and check if the sum exists in set 16 | for (int i = 0; i < n - 1; i++) { 17 | for (int j = i + 1; j < n; j++) { 18 | long num1 = sqrArr[i]; 19 | long num2 = sqrArr[j]; 20 | if (sqr.contains(num1 + num2)) { 21 | return true; 22 | } 23 | } 24 | } 25 | 26 | return false; // didn't got match 27 | } 28 | } -------------------------------------------------------------------------------- /Find Transition Point.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | int transitionPoint(int arr[], int n) { 4 | 5 | // code here 6 | 7 | if (arr[0] == 1) { 8 | 9 | return 0; 10 | 11 | } 12 | 13 | int low = 0; 14 | 15 | int high = n - 1; 16 | 17 | while (low < high) { 18 | 19 | int mid = (low + high) / 2; 20 | 21 | if (arr[mid] == 0 && arr[mid + 1] == 1) { 22 | 23 | return mid + 1; 24 | 25 | } 26 | 27 | if (arr[mid] == 1 && arr[mid - 1] == 0) { 28 | 29 | return mid; 30 | 31 | } 32 | 33 | if (arr[mid] == 0) { 34 | 35 | low = mid + 1; 36 | 37 | } else { 38 | 39 | high = mid - 1; 40 | 41 | } 42 | 43 | } 44 | 45 | return -1; 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /GeekForGeek/Pythagorean Triplet.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | boolean checkTriplet(int[] arr, int n) { 3 | // code here 4 | 5 | Set sqr = new HashSet<>(); 6 | long[] sqrArr = new long[n]; 7 | 8 | // store all squares in a set and Array 9 | for (int i = 0; i < n; i++) { 10 | long val = (long) Math.pow(arr[i], 2); 11 | sqr.add(val); 12 | sqrArr[i] = val; 13 | } 14 | 15 | // iterate array and check if the sum exists in set 16 | for (int i = 0; i < n - 1; i++) { 17 | for (int j = i + 1; j < n; j++) { 18 | long num1 = sqrArr[i]; 19 | long num2 = sqrArr[j]; 20 | if (sqr.contains(num1 + num2)) { 21 | return true; 22 | } 23 | } 24 | } 25 | 26 | return false; // didn't got match 27 | } 28 | } -------------------------------------------------------------------------------- /Minimum distance between two numbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int minDist(int arr[], int n, int x, int y) { 3 | // code here 4 | // Greedy aproach to always get the latest index of both (x,y) 5 | if (x == y) 6 | return 0; 7 | 8 | int min = Integer.MAX_VALUE; 9 | int indX = -1; 10 | int indY = -1; 11 | 12 | for (int i = 0; i < n; i++) { 13 | 14 | if (arr[i] == y) { 15 | if (indX != -1) 16 | min = Math.min(min, Math.abs(i - indX)); 17 | indY = i; 18 | } else if (arr[i] == x) { 19 | if (indY != -1) 20 | min = Math.min(min, Math.abs(i - indY)); 21 | indX = i; 22 | } 23 | } 24 | if (indX != -1 && indY != -1) { 25 | return min; 26 | } 27 | return -1; 28 | } 29 | } -------------------------------------------------------------------------------- /GeekForGeek/Find Transition Point.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | int transitionPoint(int arr[], int n) { 4 | 5 | // code here 6 | 7 | if (arr[0] == 1) { 8 | 9 | return 0; 10 | 11 | } 12 | 13 | int low = 0; 14 | 15 | int high = n - 1; 16 | 17 | while (low < high) { 18 | 19 | int mid = (low + high) / 2; 20 | 21 | if (arr[mid] == 0 && arr[mid + 1] == 1) { 22 | 23 | return mid + 1; 24 | 25 | } 26 | 27 | if (arr[mid] == 1 && arr[mid - 1] == 0) { 28 | 29 | return mid; 30 | 31 | } 32 | 33 | if (arr[mid] == 0) { 34 | 35 | low = mid + 1; 36 | 37 | } else { 38 | 39 | high = mid - 1; 40 | 41 | } 42 | 43 | } 44 | 45 | return -1; 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /GeekForGeek/GFG.java: -------------------------------------------------------------------------------- 1 | class GfG { 2 | Node buildBalancedTree(Node root) { 3 | // Add your code here. 4 | ArrayList list = new ArrayList<>(); 5 | inOrder(root, list); 6 | Node res = BalancedBst(list, 0, list.size() - 1); 7 | return res; 8 | } 9 | 10 | void inOrder(Node root, ArrayList list) { 11 | if (root == null) 12 | return; 13 | inOrder(root.left, list); 14 | list.add(root.data); 15 | inOrder(root.right, list); 16 | } 17 | 18 | Node BalancedBst(ArrayList list, int l, int r) { 19 | if (l > r) { 20 | return null; 21 | } 22 | int mid = (l + r) / 2; 23 | Node root = new Node(list.get(mid)); 24 | root.left = BalancedBst(list, l, mid - 1); 25 | root.right = BalancedBst(list, mid + 1, r); 26 | return root; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /GeekForGeek/Minimum distance between two numbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int minDist(int arr[], int n, int x, int y) { 3 | // code here 4 | // Greedy aproach to always get the latest index of both (x,y) 5 | if (x == y) 6 | return 0; 7 | 8 | int min = Integer.MAX_VALUE; 9 | int indX = -1; 10 | int indY = -1; 11 | 12 | for (int i = 0; i < n; i++) { 13 | 14 | if (arr[i] == y) { 15 | if (indX != -1) 16 | min = Math.min(min, Math.abs(i - indX)); 17 | indY = i; 18 | } else if (arr[i] == x) { 19 | if (indY != -1) 20 | min = Math.min(min, Math.abs(i - indY)); 21 | indX = i; 22 | } 23 | } 24 | if (indX != -1 && indY != -1) { 25 | return min; 26 | } 27 | return -1; 28 | } 29 | } -------------------------------------------------------------------------------- /Print_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Print_Pattern { 5 | 6 | public List pattern(int N) { 7 | ArrayList arr = new ArrayList(); 8 | if (N <= 0) { 9 | arr.add(N); 10 | return arr; 11 | } 12 | return helper(false, N, arr); 13 | } 14 | 15 | public List helper(boolean bool, int N, List arr) { 16 | if (bool) { 17 | arr.add(N); 18 | if (N == arr.get(0)) { 19 | return arr; 20 | } 21 | N = N + 5; 22 | return helper(bool, N, arr); 23 | } else { 24 | arr.add(N); 25 | N = N - 5; 26 | if (N <= 0) { 27 | bool = true; 28 | } 29 | return helper(bool, N, arr); 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /GeekForGeek/Print_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class Print_Pattern { 5 | 6 | public List pattern(int N) { 7 | ArrayList arr = new ArrayList(); 8 | if (N <= 0) { 9 | arr.add(N); 10 | return arr; 11 | } 12 | return helper(false, N, arr); 13 | } 14 | 15 | public List helper(boolean bool, int N, List arr) { 16 | if (bool) { 17 | arr.add(N); 18 | if (N == arr.get(0)) { 19 | return arr; 20 | } 21 | N = N + 5; 22 | return helper(bool, N, arr); 23 | } else { 24 | arr.add(N); 25 | N = N - 5; 26 | if (N <= 0) { 27 | bool = true; 28 | } 29 | return helper(bool, N, arr); 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Anti Diagonal Traversal of Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] antiDiagonalPattern(int[][] matrix) { 3 | int n = matrix.length; 4 | int m = matrix[0].length; 5 | int row = 0, col = 0; 6 | int ans[] = new int[n * m]; 7 | int index = 0; 8 | for (int i = 0; i < m; i++) { 9 | row = 0; 10 | col = i; 11 | while (row < n && row >= 0 && col < m && col >= 0) { 12 | ans[index++] = matrix[row][col]; 13 | row++; 14 | col--; 15 | } 16 | } 17 | 18 | for (int i = 1; i < n; i++) { 19 | row = i; 20 | col = m - 1; 21 | while (row < n && row >= 0 && col < m && col >= 0) { 22 | ans[index++] = matrix[row][col]; 23 | row++; 24 | col--; 25 | } 26 | } 27 | return ans; 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /Solution6.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to find the level of node X. 3 | int nodeLevel(int V, ArrayList> adj, int X) { 4 | // code here 5 | if (X == 0) 6 | return 0; 7 | int level = 0; 8 | Queue q = new LinkedList<>(); 9 | boolean vis[] = new boolean[V]; 10 | q.add(0); 11 | vis[0] = true; 12 | while (!q.isEmpty()) { 13 | int n = q.size(); 14 | for (int i = 0; i < n; i++) { 15 | int curr = q.poll(); 16 | for (int j : adj.get(curr)) { 17 | if (curr == X) 18 | return level; 19 | if (!vis[j]) { 20 | q.add(j); 21 | vis[j] = true; 22 | } 23 | } 24 | } 25 | level++; 26 | } 27 | return -1; 28 | } 29 | } -------------------------------------------------------------------------------- /Buy-Maximum-Stocks-if-i-stocks-can-be-bought-on-i-th-day.c++: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int buyMaximumProducts(int n, int k, int price[]){ 4 | //Write your code here 5 | priority_queue,vector>,greater>> pq; 6 | for(int i=0;i0){ 12 | pair stock=pq.top(); 13 | pq.pop(); 14 | int stocksCanBeBought=moneyLeft/stock.first; 15 | if(stocksCanBeBought>stock.second){ 16 | stocksCanBeBought=stock.second; 17 | } 18 | if(stocksCanBeBought==0){ 19 | break; 20 | } 21 | count+=stocksCanBeBought; 22 | moneyLeft-=stock.first*stocksCanBeBought; 23 | } 24 | 25 | return count; 26 | } 27 | }; -------------------------------------------------------------------------------- /GeekForGeek/Solution6.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to find the level of node X. 3 | int nodeLevel(int V, ArrayList> adj, int X) { 4 | // code here 5 | if (X == 0) 6 | return 0; 7 | int level = 0; 8 | Queue q = new LinkedList<>(); 9 | boolean vis[] = new boolean[V]; 10 | q.add(0); 11 | vis[0] = true; 12 | while (!q.isEmpty()) { 13 | int n = q.size(); 14 | for (int i = 0; i < n; i++) { 15 | int curr = q.poll(); 16 | for (int j : adj.get(curr)) { 17 | if (curr == X) 18 | return level; 19 | if (!vis[j]) { 20 | q.add(j); 21 | vis[j] = true; 22 | } 23 | } 24 | } 25 | level++; 26 | } 27 | return -1; 28 | } 29 | } -------------------------------------------------------------------------------- /Wildcard_string_matching.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static boolean helper( int i,int j,String w,String p,Boolean dp[][]) { 3 | if(i==0 || j==0) { 4 | if(i!=j)return false; 5 | else if(w.charAt(i)!=p.charAt(j))return false; 6 | else return true; 7 | } 8 | 9 | if(dp[i][j]!=null) return dp[i][j]; 10 | 11 | 12 | if(w.charAt(i)==p.charAt(j)) { 13 | return dp[i][j]=helper(i-1,j-1,w,p,dp); 14 | }else if(w.charAt(i)=='*') { 15 | return dp[i][j]=helper(i,j-1,w,p,dp)||helper(i-1,j-1,w,p,dp); 16 | }else if(w.charAt(i)=='?'){ 17 | return dp[i][j]=helper(i-1,j-1,w,p,dp); 18 | }else return dp[i][j]=false; 19 | } 20 | 21 | static boolean match(String wild, String pattern) 22 | { 23 | wild="#"+wild; 24 | pattern="#"+pattern; 25 | int n=wild.length(); 26 | int m=pattern.length(); 27 | Boolean dp[][]=new Boolean[n][m]; 28 | return helper(n-1,m-1,wild, pattern,dp); 29 | } 30 | } -------------------------------------------------------------------------------- /winner_of_elective.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class winner_of_elective { 5 | 6 | public static String[] winner(String arr[], int n) { 7 | 8 | Map mapNameToVote = new HashMap<>(); 9 | int maxVote = 0; 10 | 11 | for (int i = 0; i < n; i++) { 12 | mapNameToVote.put(arr[i], mapNameToVote.getOrDefault(arr[i], 0) + 1); 13 | maxVote = Math.max(maxVote, mapNameToVote.get(arr[i])); 14 | } 15 | 16 | String name = ""; 17 | 18 | for (Map.Entry entry : mapNameToVote.entrySet()) { 19 | if (entry.getValue() == maxVote) { 20 | if (name.equals("") || entry.getKey().compareTo(name) < 0) { 21 | name = entry.getKey(); 22 | } 23 | } 24 | } 25 | 26 | String vote = String.valueOf(maxVote); 27 | String[] result = { name, vote }; 28 | return result; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Decode_Ways.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isValid(int code, int len) { 3 | if (len == 1) { 4 | return code >= 1 && code <= 26; 5 | } else { 6 | return code >= 10 && code <= 26; 7 | } 8 | } 9 | 10 | public int countOfDecoding(int i, String s, int[] dp) { 11 | if (i >= s.length()) { 12 | return 1; 13 | } 14 | if (dp[i] != -1) { 15 | return dp[i]; 16 | } 17 | dp[i] = 0; 18 | if (isValid(s.charAt(i) - '0', 1)) { 19 | dp[i] += countOfDecoding(i + 1, s, dp); 20 | } 21 | if (i < s.length() - 1 && isValid((s.charAt(i) - '0') * 10 + s.charAt(i + 1) - '0', 2)) { 22 | dp[i] += countOfDecoding(i + 2, s, dp); 23 | } 24 | return dp[i]; 25 | } 26 | 27 | public int numDecodings(String s) { 28 | int[] dp = new int[s.length()]; 29 | Arrays.fill(dp, -1); 30 | return countOfDecoding(0, s, dp); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Solution2.java: -------------------------------------------------------------------------------- 1 | class Solution2{ 2 | 3 | public static ArrayList findCommon(Node root1, Node root2) { 4 | ArrayList result = new ArrayList<>(); 5 | findCommonNodes(root1, root2, result); 6 | return result; 7 | } 8 | 9 | private static void findCommonNodes(Node curnode1, Node curnode2, ArrayList result) { 10 | if (curnode1 == null || curnode2 == null) { 11 | return; 12 | } 13 | 14 | findCommonNodes(curnode1.left, curnode2, result); 15 | if(isPresent(curnode2,curnode1.data)) 16 | result.add(curnode1.data); 17 | findCommonNodes(curnode1.right,curnode2,result); 18 | } 19 | private static boolean isPresent(Node curnode, int data) { 20 | if(curnode == null) 21 | return false; 22 | 23 | if(data < curnode.data){ 24 | return isPresent(curnode.left,data); 25 | }else if(data > curnode.data){ 26 | return isPresent(curnode.right,data); 27 | }else{ 28 | return true; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Binary_Tree_to_CDLL.java: -------------------------------------------------------------------------------- 1 | class Binary_Tree_to_CDLL { 2 | // Function to convert binary tree into circular doubly linked list. 3 | Node bTreeToClist(Node root) { 4 | // your code here 5 | Stack st = new Stack<>(); 6 | addToStack(root, st); 7 | Node head, cur; 8 | Node temp = st.pop(); 9 | addToStack(temp.right, st); 10 | head = temp; 11 | cur = head; 12 | while (!st.isEmpty()) { 13 | // System.out.println(st); 14 | temp = st.pop(); 15 | addToStack(temp.right, st); 16 | cur.right = temp; 17 | temp.left = cur; 18 | cur = cur.right; 19 | 20 | } 21 | cur.right = head; 22 | head.left = cur; 23 | return head; 24 | 25 | } 26 | 27 | static void addToStack(Node root, Stack st) { 28 | if (root == null) 29 | return; 30 | st.add(root); 31 | while (root.left != null) { 32 | st.add(root.left); 33 | root = root.left; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /GeekForGeek/Binary_Tree_to_CDLL.java: -------------------------------------------------------------------------------- 1 | class Binary_Tree_to_CDLL { 2 | // Function to convert binary tree into circular doubly linked list. 3 | Node bTreeToClist(Node root) { 4 | // your code here 5 | Stack st = new Stack<>(); 6 | addToStack(root, st); 7 | Node head, cur; 8 | Node temp = st.pop(); 9 | addToStack(temp.right, st); 10 | head = temp; 11 | cur = head; 12 | while (!st.isEmpty()) { 13 | // System.out.println(st); 14 | temp = st.pop(); 15 | addToStack(temp.right, st); 16 | cur.right = temp; 17 | temp.left = cur; 18 | cur = cur.right; 19 | 20 | } 21 | cur.right = head; 22 | head.left = cur; 23 | return head; 24 | 25 | } 26 | 27 | static void addToStack(Node root, Stack st) { 28 | if (root == null) 29 | return; 30 | st.add(root); 31 | while (root.left != null) { 32 | st.add(root.left); 33 | root = root.left; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /GeekForGeek/Solution2.java: -------------------------------------------------------------------------------- 1 | class Solution2{ 2 | 3 | public static ArrayList findCommon(Node root1, Node root2) { 4 | ArrayList result = new ArrayList<>(); 5 | findCommonNodes(root1, root2, result); 6 | return result; 7 | } 8 | 9 | private static void findCommonNodes(Node curnode1, Node curnode2, ArrayList result) { 10 | if (curnode1 == null || curnode2 == null) { 11 | return; 12 | } 13 | 14 | findCommonNodes(curnode1.left, curnode2, result); 15 | if(isPresent(curnode2,curnode1.data)) 16 | result.add(curnode1.data); 17 | findCommonNodes(curnode1.right,curnode2,result); 18 | } 19 | private static boolean isPresent(Node curnode, int data) { 20 | if(curnode == null) 21 | return false; 22 | 23 | if(data < curnode.data){ 24 | return isPresent(curnode.left,data); 25 | }else if(data > curnode.data){ 26 | return isPresent(curnode.right,data); 27 | }else{ 28 | return true; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Solution10.java: -------------------------------------------------------------------------------- 1 | public class Solution10 { 2 | class Solution { 3 | public int constrainedSubsetSum(int[] nums, int k) { 4 | int n = nums.length; 5 | int[] dp = new int[n]; 6 | int res = nums[0]; 7 | Queue queue = new PriorityQueue<>((a, b) -> dp[b] - dp[a]); // Declaring Max heap 8 | 9 | Arrays.fill(dp, Integer.MIN_VALUE); 10 | dp[0] = nums[0]; 11 | queue.offer(0); 12 | 13 | for (int j = 1; j < n; j++) { 14 | int i = Math.max(j - k, 0); // get the furthest index possible 15 | while (!queue.isEmpty() && queue.peek() < i) 16 | queue.poll(); // find the global max in the specified range for that particular j index 17 | int idx = queue.peek(); 18 | dp[j] = Math.max(dp[idx] + nums[j], nums[j]); 19 | res = Math.max(res, dp[j]); 20 | queue.offer(j); 21 | } 22 | return res; 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Sum of upper and lower triangles.java: -------------------------------------------------------------------------------- 1 | public class Sum 2 | of upper 3 | and lower triangles 4 | { 5 | 6 | 7 | //Function to return sum of upper and lower triangles of a matrix. 8 | static ArrayList sumTriangles(int matrix[][], int n) { 9 | // code here 10 | int m = matrix[0].length; 11 | int upperSum = 0; 12 | int lowerSum = 0; 13 | 14 | // Finding Upper traingle sum 15 | for (int i = 0; i < n; i++) { 16 | for (int j = 0; j < m; j++) { 17 | if (i <= j) { 18 | upperSum += matrix[i][j]; 19 | } 20 | } 21 | } 22 | // Finding Lower traingle sum 23 | for (int i = 0; i < n; i++) { 24 | for (int j = 0; j < m; j++) { 25 | if (i >= j) { 26 | lowerSum += matrix[i][j]; 27 | } 28 | } 29 | } 30 | // adding values in the Arraylist 31 | ArrayList al = new ArrayList<>(); 32 | al.add(upperSum); 33 | al.add(lowerSum); 34 | return al; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /GeekForGeek/Sum of upper and lower triangles.java: -------------------------------------------------------------------------------- 1 | public class Sum 2 | of upper 3 | and lower triangles 4 | { 5 | 6 | 7 | //Function to return sum of upper and lower triangles of a matrix. 8 | static ArrayList sumTriangles(int matrix[][], int n) { 9 | // code here 10 | int m = matrix[0].length; 11 | int upperSum = 0; 12 | int lowerSum = 0; 13 | 14 | // Finding Upper traingle sum 15 | for (int i = 0; i < n; i++) { 16 | for (int j = 0; j < m; j++) { 17 | if (i <= j) { 18 | upperSum += matrix[i][j]; 19 | } 20 | } 21 | } 22 | // Finding Lower traingle sum 23 | for (int i = 0; i < n; i++) { 24 | for (int j = 0; j < m; j++) { 25 | if (i >= j) { 26 | lowerSum += matrix[i][j]; 27 | } 28 | } 29 | } 30 | // adding values in the Arraylist 31 | ArrayList al = new ArrayList<>(); 32 | al.add(upperSum); 33 | al.add(lowerSum); 34 | return al; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Top K Frequent Elements in Array.java: -------------------------------------------------------------------------------- 1 | public class Top 2 | K Frequent 3 | Elements in Array 4 | { 5 | class Solution { 6 | public int[] topK(int[] nums, int k) { 7 | //map to store number and its frequency 8 | HashMap map = new HashMap<>(); 9 | for(int i: nums){ 10 | map.put(i, map.getOrDefault(i, 0) + 1); 11 | } 12 | 13 | // max heap to store the number according to their frequency 14 | PriorityQueue pq = new PriorityQueue<>((a, b) -> { 15 | //prioritize larger number, if frequencies are equal 16 | if(map.get(a) == map.get(b)){ 17 | return b-a; 18 | } 19 | 20 | return map.get(b) - map.get(a); 21 | }); 22 | 23 | for(int num: map.keySet()){ 24 | pq.add(num); 25 | } 26 | 27 | //gettting the first k values from the priority queue 28 | int output[] = new int[k]; 29 | for(int i = 0; i map = new HashMap<>(); 9 | for(int i: nums){ 10 | map.put(i, map.getOrDefault(i, 0) + 1); 11 | } 12 | 13 | // max heap to store the number according to their frequency 14 | PriorityQueue pq = new PriorityQueue<>((a, b) -> { 15 | //prioritize larger number, if frequencies are equal 16 | if(map.get(a) == map.get(b)){ 17 | return b-a; 18 | } 19 | 20 | return map.get(b) - map.get(a); 21 | }); 22 | 23 | for(int num: map.keySet()){ 24 | pq.add(num); 25 | } 26 | 27 | //gettting the first k values from the priority queue 28 | int output[] = new int[k]; 29 | for(int i = 0; i ll = preorder(root.left, new ArrayList<>()); 14 | List lr = postorder(root.right, new ArrayList<>()); 15 | return ll.equals(lr); 16 | } 17 | 18 | private static List preorder(Node root, List l1) { 19 | if (root == null) { 20 | return null; 21 | } 22 | l1.add(root.data); 23 | preorder(root.left, l1); 24 | preorder(root.right, l1); 25 | return l1; 26 | } 27 | 28 | private static List postorder(Node root, List l2) { 29 | if (root == null) { 30 | return null; 31 | } 32 | l2.add(root.data); 33 | postorder(root.right, l2); 34 | postorder(root.left, l2); 35 | return l2; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /GeekForGeek/Symmetric_Tree.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | import org.w3c.dom.Node; 5 | 6 | public class Symmetric_Tree { 7 | 8 | // return true/false denoting whether the tree is Symmetric or not 9 | public static boolean isSymmetric(Node root) { 10 | if (root == null || (root.left == null && root.right == null)) { 11 | return true; 12 | } 13 | List ll = preorder(root.left, new ArrayList<>()); 14 | List lr = postorder(root.right, new ArrayList<>()); 15 | return ll.equals(lr); 16 | } 17 | 18 | private static List preorder(Node root, List l1) { 19 | if (root == null) { 20 | return null; 21 | } 22 | l1.add(root.data); 23 | preorder(root.left, l1); 24 | preorder(root.right, l1); 25 | return l1; 26 | } 27 | 28 | private static List postorder(Node root, List l2) { 29 | if (root == null) { 30 | return null; 31 | } 32 | l2.add(root.data); 33 | postorder(root.right, l2); 34 | postorder(root.left, l2); 35 | return l2; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Solution11.java: -------------------------------------------------------------------------------- 1 | class Solution11 { 2 | 3 | static boolean isPalindrome(int i, int j, String s) { 4 | 5 | while (i < j) { 6 | 7 | if (s.charAt(i) != s.charAt(j)) 8 | 9 | return false; 10 | 11 | i++; 12 | j--; 13 | 14 | } 15 | 16 | return true; 17 | 18 | } 19 | 20 | static int solve(int idx, int n, String s, int[] dp) { 21 | 22 | if (idx == n) 23 | 24 | return 0; 25 | 26 | if (dp[idx] != -1) 27 | 28 | return dp[idx]; 29 | 30 | int mini = Integer.MAX_VALUE; 31 | 32 | for (int i = idx; i < n; i++) { 33 | 34 | if (isPalindrome(idx, i, s)) { 35 | 36 | mini = Math.min(mini, 1 + solve(i + 1, n, s, dp)); 37 | 38 | } 39 | 40 | } 41 | 42 | return dp[idx] = mini; 43 | 44 | } 45 | 46 | static int palindromicPartition(String str) { 47 | 48 | int n = str.length(); 49 | 50 | int[] dp = new int[n]; 51 | 52 | for (int i = 0; i < n; i++) { 53 | 54 | dp[i] = -1; 55 | 56 | } 57 | 58 | return solve(0, n, str, dp) - 1; 59 | 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /Brothers_From_Different_Roots.java: -------------------------------------------------------------------------------- 1 | public class Brothers_From_Different_Roots { 2 | public static void inorder(Node root, ArrayList list) { 3 | if (root == null) 4 | return; 5 | inorder(root.left, list); 6 | list.add(root.data); 7 | inorder(root.right, list); 8 | } 9 | 10 | public static int find(ArrayList l1, ArrayList l2, int x) { 11 | int count = 0; 12 | int i = 0; 13 | int j = l2.size() - 1; 14 | while (i < l1.size() && j >= 0) { 15 | if (l1.get(i) + l2.get(j) == x) { 16 | count++; 17 | i++; 18 | j--; 19 | } else if (l1.get(i) + l2.get(j) > x) 20 | j--; 21 | else 22 | i++; 23 | } 24 | return count; 25 | } 26 | 27 | public static int countPairs(Node root1, Node root2, int x) { 28 | // Code here 29 | ArrayList list1 = new ArrayList<>(); 30 | ArrayList list2 = new ArrayList<>(); 31 | inorder(root1, list1); 32 | inorder(root2, list2); 33 | int res = find(list1, list2, x); 34 | return res; 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /GeekForGeek/Solution11.java: -------------------------------------------------------------------------------- 1 | class Solution11 { 2 | 3 | static boolean isPalindrome(int i, int j, String s) { 4 | 5 | while (i < j) { 6 | 7 | if (s.charAt(i) != s.charAt(j)) 8 | 9 | return false; 10 | 11 | i++; 12 | j--; 13 | 14 | } 15 | 16 | return true; 17 | 18 | } 19 | 20 | static int solve(int idx, int n, String s, int[] dp) { 21 | 22 | if (idx == n) 23 | 24 | return 0; 25 | 26 | if (dp[idx] != -1) 27 | 28 | return dp[idx]; 29 | 30 | int mini = Integer.MAX_VALUE; 31 | 32 | for (int i = idx; i < n; i++) { 33 | 34 | if (isPalindrome(idx, i, s)) { 35 | 36 | mini = Math.min(mini, 1 + solve(i + 1, n, s, dp)); 37 | 38 | } 39 | 40 | } 41 | 42 | return dp[idx] = mini; 43 | 44 | } 45 | 46 | static int palindromicPartition(String str) { 47 | 48 | int n = str.length(); 49 | 50 | int[] dp = new int[n]; 51 | 52 | for (int i = 0; i < n; i++) { 53 | 54 | dp[i] = -1; 55 | 56 | } 57 | 58 | return solve(0, n, str, dp) - 1; 59 | 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /Better String.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | class Solution { 4 | public static String betterString(String str1, String str2) { 5 | // Code here 6 | long count1 = Distinctsub(str1); 7 | long count2 = Distinctsub(str2); 8 | if (count1 >= count2) { 9 | return str1; 10 | } else { 11 | return str2; 12 | } 13 | } 14 | 15 | public static long Distinctsub(String str) { 16 | int n = str.length(); 17 | long dp[] = new long[n + 1]; 18 | dp[0] = 1; 19 | HashMap hm = new HashMap<>(); 20 | for (int i = 1; i < dp.length; i++) { 21 | dp[i] = 2 * dp[i - 1]; 22 | char ch = str.charAt(i - 1); 23 | if (hm.containsKey(ch)) { 24 | int j = hm.get(ch); 25 | dp[i] = dp[i] - dp[j - 1]; 26 | } 27 | hm.put(ch, i); 28 | } 29 | return dp[n]; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // Example usage: 34 | String str1 = "abcabc"; 35 | String str2 = "abca"; 36 | String result = betterString(str1, str2); 37 | System.out.println("The better string is: " + result); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Solution9.java: -------------------------------------------------------------------------------- 1 | class Solution9 { 2 | private static final int MOD = 1000000007; 3 | 4 | private static long modInverse(long base) { 5 | return power(base, MOD - 2); 6 | } 7 | 8 | private static long power(long base, int exponent) { 9 | long result = 1; 10 | while (exponent > 0) { 11 | if (exponent % 2 == 1) { 12 | result = (result * base) % MOD; 13 | } 14 | base = (base * base) % MOD; 15 | exponent /= 2; 16 | } 17 | return result; 18 | } 19 | 20 | private static int nCk(int n, int k) { 21 | long numerator = 1; 22 | long denominator = 1; 23 | 24 | for (int i = 0; i < k; i++) { 25 | numerator = (numerator * (n - i)) % MOD; 26 | denominator = (denominator * (i + 1)) % MOD; 27 | } 28 | return (int) ((numerator * modInverse(denominator)) % MOD); 29 | } 30 | 31 | public static int numberOfPaths(int M, int N) { 32 | return nCk(M + N - 2, M - 1); 33 | } 34 | 35 | public static void main(String[] args) { 36 | int M = 10; 37 | int N = 100000000; 38 | int result = numberOfPaths(M, N); 39 | System.out.println(result); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /GeekForGeek/Better String.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | class Solution { 4 | public static String betterString(String str1, String str2) { 5 | // Code here 6 | long count1 = Distinctsub(str1); 7 | long count2 = Distinctsub(str2); 8 | if (count1 >= count2) { 9 | return str1; 10 | } else { 11 | return str2; 12 | } 13 | } 14 | 15 | public static long Distinctsub(String str) { 16 | int n = str.length(); 17 | long dp[] = new long[n + 1]; 18 | dp[0] = 1; 19 | HashMap hm = new HashMap<>(); 20 | for (int i = 1; i < dp.length; i++) { 21 | dp[i] = 2 * dp[i - 1]; 22 | char ch = str.charAt(i - 1); 23 | if (hm.containsKey(ch)) { 24 | int j = hm.get(ch); 25 | dp[i] = dp[i] - dp[j - 1]; 26 | } 27 | hm.put(ch, i); 28 | } 29 | return dp[n]; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // Example usage: 34 | String str1 = "abcabc"; 35 | String str2 = "abca"; 36 | String result = betterString(str1, str2); 37 | System.out.println("The better string is: " + result); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /GeekForGeek/Solution9.java: -------------------------------------------------------------------------------- 1 | class Solution9 { 2 | private static final int MOD = 1000000007; 3 | 4 | private static long modInverse(long base) { 5 | return power(base, MOD - 2); 6 | } 7 | 8 | private static long power(long base, int exponent) { 9 | long result = 1; 10 | while (exponent > 0) { 11 | if (exponent % 2 == 1) { 12 | result = (result * base) % MOD; 13 | } 14 | base = (base * base) % MOD; 15 | exponent /= 2; 16 | } 17 | return result; 18 | } 19 | 20 | private static int nCk(int n, int k) { 21 | long numerator = 1; 22 | long denominator = 1; 23 | 24 | for (int i = 0; i < k; i++) { 25 | numerator = (numerator * (n - i)) % MOD; 26 | denominator = (denominator * (i + 1)) % MOD; 27 | } 28 | return (int) ((numerator * modInverse(denominator)) % MOD); 29 | } 30 | 31 | public static int numberOfPaths(int M, int N) { 32 | return nCk(M + N - 2, M - 1); 33 | } 34 | 35 | public static void main(String[] args) { 36 | int M = 10; 37 | int N = 100000000; 38 | int result = numberOfPaths(M, N); 39 | System.out.println(result); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Isomorphic Strings.java: -------------------------------------------------------------------------------- 1 | public class Isomorphic Strings 2 | 3 | { 4 | 5 | //Function to check if two strings are isomorphic. 6 | 7 | public static boolean areIsomorphic(String str1, String str2) { 8 | 9 | if (str1.length() != str2.length()) { 10 | 11 | return false; 12 | 13 | } 14 | 15 | HashMap h = new HashMap(); 16 | 17 | HashMap k = new HashMap(); 18 | 19 | for (int i = 0; i < str1.length(); i++) { 20 | 21 | char c = str1.charAt(i); 22 | 23 | char d = str2.charAt(i); 24 | 25 | if (h.containsKey(c)) { 26 | 27 | if (h.get(c) != d) { 28 | 29 | return false; 30 | 31 | } 32 | 33 | } 34 | 35 | else { 36 | 37 | h.put(c, d); 38 | 39 | } 40 | 41 | if (k.containsKey(d)) { 42 | 43 | if (k.get(d) != c) { 44 | 45 | return false; 46 | 47 | } 48 | 49 | } 50 | 51 | else { 52 | 53 | k.put(d, c); 54 | 55 | } 56 | 57 | } 58 | 59 | return true; 60 | 61 | } 62 | 63 | }} 64 | -------------------------------------------------------------------------------- /GeekForGeek/Isomorphic Strings.java: -------------------------------------------------------------------------------- 1 | public class Isomorphic Strings 2 | 3 | { 4 | 5 | //Function to check if two strings are isomorphic. 6 | 7 | public static boolean areIsomorphic(String str1, String str2) { 8 | 9 | if (str1.length() != str2.length()) { 10 | 11 | return false; 12 | 13 | } 14 | 15 | HashMap h = new HashMap(); 16 | 17 | HashMap k = new HashMap(); 18 | 19 | for (int i = 0; i < str1.length(); i++) { 20 | 21 | char c = str1.charAt(i); 22 | 23 | char d = str2.charAt(i); 24 | 25 | if (h.containsKey(c)) { 26 | 27 | if (h.get(c) != d) { 28 | 29 | return false; 30 | 31 | } 32 | 33 | } 34 | 35 | else { 36 | 37 | h.put(c, d); 38 | 39 | } 40 | 41 | if (k.containsKey(d)) { 42 | 43 | if (k.get(d) != c) { 44 | 45 | return false; 46 | 47 | } 48 | 49 | } 50 | 51 | else { 52 | 53 | k.put(d, c); 54 | 55 | } 56 | 57 | } 58 | 59 | return true; 60 | 61 | } 62 | 63 | }} 64 | -------------------------------------------------------------------------------- /Letters Collection.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | static List matrixSum(int n, int m, int mat[][], int q, int queries[][]) 4 | 5 | { 6 | 7 | // code here 8 | 9 | List ans = new ArrayList<>(); 10 | 11 | for (int i = 0; i < q; i++) { 12 | 13 | int col = queries[i][2]; 14 | 15 | int row = queries[i][1]; 16 | 17 | int val = queries[i][0]; 18 | 19 | int sum = 0; 20 | 21 | int cs = col - val, rs = row - val + 1, re = row + val, ce = col + val; 22 | 23 | for (int j = cs; j <= ce && j < m; j++) { 24 | 25 | if (row - val >= 0 && j < m && j >= 0) { 26 | 27 | sum += mat[row - val][j]; 28 | 29 | } 30 | 31 | if (row + val < n && j < m && j >= 0) { 32 | 33 | sum += mat[row + val][j]; 34 | 35 | } 36 | 37 | } 38 | 39 | for (int j = rs; j < re && j < n; j++) { 40 | 41 | if (col - val >= 0 && j < n && j >= 0) { 42 | 43 | sum += mat[j][col - val]; 44 | 45 | } 46 | 47 | if (col + val < m && j < n && j >= 0) { 48 | 49 | sum += mat[j][col + val]; 50 | 51 | } 52 | 53 | } 54 | 55 | ans.add(sum); 56 | 57 | } 58 | 59 | return ans; 60 | 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /GeekForGeek/Letters Collection.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | static List matrixSum(int n, int m, int mat[][], int q, int queries[][]) 4 | 5 | { 6 | 7 | // code here 8 | 9 | List ans = new ArrayList<>(); 10 | 11 | for (int i = 0; i < q; i++) { 12 | 13 | int col = queries[i][2]; 14 | 15 | int row = queries[i][1]; 16 | 17 | int val = queries[i][0]; 18 | 19 | int sum = 0; 20 | 21 | int cs = col - val, rs = row - val + 1, re = row + val, ce = col + val; 22 | 23 | for (int j = cs; j <= ce && j < m; j++) { 24 | 25 | if (row - val >= 0 && j < m && j >= 0) { 26 | 27 | sum += mat[row - val][j]; 28 | 29 | } 30 | 31 | if (row + val < n && j < m && j >= 0) { 32 | 33 | sum += mat[row + val][j]; 34 | 35 | } 36 | 37 | } 38 | 39 | for (int j = rs; j < re && j < n; j++) { 40 | 41 | if (col - val >= 0 && j < n && j >= 0) { 42 | 43 | sum += mat[j][col - val]; 44 | 45 | } 46 | 47 | if (col + val < m && j < n && j >= 0) { 48 | 49 | sum += mat[j][col + val]; 50 | 51 | } 52 | 53 | } 54 | 55 | ans.add(sum); 56 | 57 | } 58 | 59 | return ans; 60 | 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /Construct-String-from-Binary-Tree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String tree2str(TreeNode root) { 3 | if (root == null) { 4 | return ""; 5 | } 6 | 7 | // Step 1: Start with an empty result string 8 | StringBuilder result = new StringBuilder(); 9 | 10 | // Step 2: Perform preorder traversal 11 | preorderTraversal(root, result); 12 | 13 | // Step 3: Return the final result string 14 | return result.toString(); 15 | } 16 | 17 | private void preorderTraversal(TreeNode node, StringBuilder result) { 18 | if (node == null) { 19 | return; 20 | } 21 | 22 | // Step 4: Append the current node's value to the result 23 | result.append(node.val); 24 | 25 | // Step 5: Check if the current node has a left child or a right child 26 | if (node.left != null || node.right != null) { 27 | 28 | // Step 6: If there is a left child, add empty parentheses for it 29 | result.append("("); 30 | preorderTraversal(node.left, result); 31 | result.append(")"); 32 | } 33 | 34 | // Step 7: If there is a right child, process it similarly 35 | if (node.right != null) { 36 | result.append("("); 37 | preorderTraversal(node.right, result); 38 | result.append(")"); 39 | } 40 | 41 | // Step 8: The recursion will handle all the child nodes 42 | } 43 | } -------------------------------------------------------------------------------- /Determinant_of_a_Matrix.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | 4 | { 5 | 6 | //Function for finding determinant of matrix. 7 | 8 | static int determinantOfMatrix(int matrix[][], int n) 9 | 10 | { 11 | 12 | // code here 13 | 14 | if(n==1)return matrix[0][0]; 15 | 16 | int ans=0; 17 | 18 | 19 | 20 | //Traversing on 0th row of matrix 21 | 22 | for(int i=0;i arr[prev]) { 24 | include = arr[curr] + solve(arr, size, curr + 1, curr, dp); 25 | } 26 | 27 | int exclude = solve(arr, size, curr + 1, prev, dp); 28 | 29 | return dp[curr][prev + 1] = Math.max(include, exclude); 30 | } 31 | 32 | public int Tabulation(int[] arr, int size) { 33 | int[] dp = new int[size]; 34 | int ans = Integer.MIN_VALUE; 35 | for (int idx = 0; idx < size; idx++) { 36 | dp[idx] = arr[idx]; 37 | } 38 | 39 | for (int idx1 = 0; idx1 < size; idx1++) { 40 | for (int idx2 = 0; idx2 < idx1; idx2++) { 41 | if (arr[idx1] > arr[idx2]) { 42 | dp[idx1] = Math.max(dp[idx2] + arr[idx1], dp[idx1]); 43 | } 44 | } 45 | ans = Math.max(ans, dp[idx1]); 46 | } 47 | 48 | return ans; 49 | } 50 | } -------------------------------------------------------------------------------- /K Sum Paths.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int mod = (int) (1e9 + 7); 3 | int cnt = 0; 4 | 5 | public void count(Node root, int k, ArrayList path) { 6 | 7 | if (root == null) 8 | return; 9 | 10 | path.add(root.data); 11 | 12 | int sum = 0; 13 | 14 | for (int i = path.size() - 1; i >= 0; i--) { 15 | sum += path.get(i); 16 | if (sum == k) { 17 | cnt++; 18 | } 19 | } 20 | 21 | count(root.left, k, path); 22 | count(root.right, k, path); 23 | 24 | path.remove(path.size() - 1); 25 | } 26 | 27 | public int sumK(Node root, int k) { 28 | 29 | ArrayList path = new ArrayList<>(); 30 | 31 | count(root, k, path); 32 | 33 | return cnt % mod; 34 | } 35 | 36 | } 37 | 38 | class Solution { 39 | int mod = (int) (1e9 + 7); 40 | int cnt = 0; 41 | 42 | public void count(Node root, int k, ArrayList path) { 43 | 44 | if (root == null) 45 | return; 46 | 47 | path.add(root.data); 48 | 49 | int sum = 0; 50 | 51 | for (int i = path.size() - 1; i >= 0; i--) { 52 | sum += path.get(i); 53 | if (sum == k) { 54 | cnt++; 55 | } 56 | } 57 | 58 | count(root.left, k, path); 59 | count(root.right, k, path); 60 | 61 | path.remove(path.size() - 1); 62 | } 63 | 64 | public int sumK(Node root, int k) { 65 | 66 | ArrayList path = new ArrayList<>(); 67 | 68 | count(root, k, path); 69 | 70 | return cnt % mod; 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /GeekForGeek/K Sum Paths.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int mod = (int) (1e9 + 7); 3 | int cnt = 0; 4 | 5 | public void count(Node root, int k, ArrayList path) { 6 | 7 | if (root == null) 8 | return; 9 | 10 | path.add(root.data); 11 | 12 | int sum = 0; 13 | 14 | for (int i = path.size() - 1; i >= 0; i--) { 15 | sum += path.get(i); 16 | if (sum == k) { 17 | cnt++; 18 | } 19 | } 20 | 21 | count(root.left, k, path); 22 | count(root.right, k, path); 23 | 24 | path.remove(path.size() - 1); 25 | } 26 | 27 | public int sumK(Node root, int k) { 28 | 29 | ArrayList path = new ArrayList<>(); 30 | 31 | count(root, k, path); 32 | 33 | return cnt % mod; 34 | } 35 | 36 | } 37 | 38 | class Solution { 39 | int mod = (int) (1e9 + 7); 40 | int cnt = 0; 41 | 42 | public void count(Node root, int k, ArrayList path) { 43 | 44 | if (root == null) 45 | return; 46 | 47 | path.add(root.data); 48 | 49 | int sum = 0; 50 | 51 | for (int i = path.size() - 1; i >= 0; i--) { 52 | sum += path.get(i); 53 | if (sum == k) { 54 | cnt++; 55 | } 56 | } 57 | 58 | count(root.left, k, path); 59 | count(root.right, k, path); 60 | 61 | path.remove(path.size() - 1); 62 | } 63 | 64 | public int sumK(Node root, int k) { 65 | 66 | ArrayList path = new ArrayList<>(); 67 | 68 | count(root, k, path); 69 | 70 | return cnt % mod; 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /Arithmetic_Subarrays.java: -------------------------------------------------------------------------------- 1 | public class Arithmetic_Subarrays { 2 | 3 | public List checkArithmeticSubarrays(int[] nums, int[] l, int[] r) { 4 | List result = new ArrayList<>(l.length); 5 | for (int i = 0; i < l.length; i++) { 6 | result.add(isArithmetic(nums, l[i], r[i])); 7 | } 8 | return result; 9 | } 10 | 11 | 12 | public boolean isArithmetic(int [] nums, int l, int r){ 13 | int max = Integer.MIN_VALUE; 14 | int min = Integer.MAX_VALUE; 15 | 16 | for (int i = l; i <= r; i++) { 17 | max = Math.max(nums[i],max); // max of nums 18 | min = Math.min(nums[i],min); // min of nums 19 | } 20 | 21 | int len = r -l +1; 22 | // if (max-min)/(length-1) yields remainder = 0 => AP 23 | // else => Not an AP 24 | 25 | if((max-min)%(len-1) !=0) 26 | return false; 27 | 28 | int diff = (max-min)/(len-1); 29 | if(diff == 0) 30 | return true; 31 | 32 | boolean [] visited = new boolean[len]; 33 | 34 | for (int i = l; i <= r; i++) { 35 | int val = nums[i]; 36 | if((val - min)% diff != 0) // checking remainder wrt min 37 | return false; 38 | else { 39 | int pos = (val - min) / diff; 40 | if(visited[pos]) // If node is visited, then it means repeatition of numbers 41 | return false; 42 | visited[pos] = true; // If node isn't visited yet, marked it visited 43 | } 44 | } 45 | return true; 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Unique Length-3 Palindromic Subsequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countPalindromicSubsequence(String inputString) { 3 | // Arrays to store the minimum and maximum occurrences of each character in the 4 | // input string 5 | int[] minExist = new int[26]; 6 | int[] maxExist = new int[26]; 7 | for (int i = 0; i < 26; i++) { 8 | minExist[i] = Integer.MAX_VALUE; 9 | maxExist[i] = Integer.MIN_VALUE; 10 | } 11 | 12 | // Populate minExist and maxExist arrays 13 | for (int i = 0; i < inputString.length(); i++) { 14 | int charIndex = inputString.charAt(i) - 'a'; 15 | minExist[charIndex] = Math.min(minExist[charIndex], i); 16 | maxExist[charIndex] = Math.max(maxExist[charIndex], i); 17 | } 18 | 19 | // Variable to store the final count of unique palindromic subsequences 20 | int uniqueCount = 0; 21 | 22 | // Iterate over each character in the alphabet 23 | for (int charIndex = 0; charIndex < 26; charIndex++) { 24 | // Check if the character has occurred in the input string 25 | if (minExist[charIndex] == Integer.MAX_VALUE || maxExist[charIndex] == Integer.MIN_VALUE) { 26 | continue; // No occurrences, move to the next character 27 | } 28 | 29 | // Set to store unique characters between the minimum and maximum occurrences 30 | HashSet uniqueCharsBetween = new HashSet<>(); 31 | 32 | // Iterate over the characters between the minimum and maximum occurrences 33 | for (int j = minExist[charIndex] + 1; j < maxExist[charIndex]; j++) { 34 | uniqueCharsBetween.add(inputString.charAt(j)); 35 | } 36 | 37 | // Add the count of unique characters between the occurrences to the final count 38 | uniqueCount += uniqueCharsBetween.size(); 39 | } 40 | 41 | // Return the total count of unique palindromic subsequences 42 | return uniqueCount; 43 | } 44 | } -------------------------------------------------------------------------------- /AVL_Tree_Insertion.java: -------------------------------------------------------------------------------- 1 | import org.w3c.dom.Node; 2 | 3 | public class AVL_Tree_Insertion { 4 | 5 | public Node insertToAVL(Node node, int data) { 6 | if (node == null) { 7 | return new Node(data); 8 | } 9 | if (node.data > data) { 10 | node.left = insertToAVL(node.left, data); 11 | } else if (node.data < data) { 12 | node.right = insertToAVL(node.right, data); 13 | } 14 | node.height = Math.max(height(node.left), height(node.right)) + 1; 15 | int diff = height(node.left) - height(node.right); 16 | if (diff > 1) { // Left subtree is higher 17 | if (data < node.left.data) { 18 | return rightRotate(node); 19 | } else if (data > node.left.data) { // LR rotation 20 | node.left = leftRotate(node.left); 21 | return rightRotate(node); 22 | } 23 | } else if (diff < -1) { // Right subtree is higher 24 | if (data < node.right.data) { // RL rotation 25 | node.right = rightRotate(node.right); 26 | return leftRotate(node); 27 | } else if (data > node.right.data) { 28 | return leftRotate(node); 29 | } 30 | } 31 | return node; 32 | } 33 | 34 | public Node leftRotate(Node a) { 35 | Node b = a.right; 36 | Node t2 = b.left; 37 | b.left = a; 38 | a.right = t2; 39 | a.height = Math.max(height(a.left), height(a.right)) + 1; 40 | b.height = Math.max(height(b.left), height(b.right)) + 1; 41 | return b; 42 | } 43 | 44 | public Node rightRotate(Node a) { 45 | Node b = a.left; 46 | Node t2 = b.right; 47 | b.right = a; 48 | a.left = t2; 49 | a.height = Math.max(height(a.left), height(a.right)) + 1; 50 | b.height = Math.max(height(b.left), height(b.right)) + 1; 51 | return b; 52 | } 53 | 54 | public int height(Node node) { 55 | if (node == null) { 56 | return 0; 57 | } else { 58 | return node.height; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /GeekForGeek/AVL_Tree_Insertion.java: -------------------------------------------------------------------------------- 1 | import org.w3c.dom.Node; 2 | 3 | public class AVL_Tree_Insertion { 4 | 5 | public Node insertToAVL(Node node, int data) { 6 | if (node == null) { 7 | return new Node(data); 8 | } 9 | if (node.data > data) { 10 | node.left = insertToAVL(node.left, data); 11 | } else if (node.data < data) { 12 | node.right = insertToAVL(node.right, data); 13 | } 14 | node.height = Math.max(height(node.left), height(node.right)) + 1; 15 | int diff = height(node.left) - height(node.right); 16 | if (diff > 1) { // Left subtree is higher 17 | if (data < node.left.data) { 18 | return rightRotate(node); 19 | } else if (data > node.left.data) { // LR rotation 20 | node.left = leftRotate(node.left); 21 | return rightRotate(node); 22 | } 23 | } else if (diff < -1) { // Right subtree is higher 24 | if (data < node.right.data) { // RL rotation 25 | node.right = rightRotate(node.right); 26 | return leftRotate(node); 27 | } else if (data > node.right.data) { 28 | return leftRotate(node); 29 | } 30 | } 31 | return node; 32 | } 33 | 34 | public Node leftRotate(Node a) { 35 | Node b = a.right; 36 | Node t2 = b.left; 37 | b.left = a; 38 | a.right = t2; 39 | a.height = Math.max(height(a.left), height(a.right)) + 1; 40 | b.height = Math.max(height(b.left), height(b.right)) + 1; 41 | return b; 42 | } 43 | 44 | public Node rightRotate(Node a) { 45 | Node b = a.left; 46 | Node t2 = b.right; 47 | b.right = a; 48 | a.left = t2; 49 | a.height = Math.max(height(a.left), height(a.right)) + 1; 50 | b.height = Math.max(height(b.left), height(b.right)) + 1; 51 | return b; 52 | } 53 | 54 | public int height(Node node) { 55 | if (node == null) { 56 | return 0; 57 | } else { 58 | return node.height; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Sum-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String sumStrings(String s1, String s2) { 3 | int i = s1.length() - 1, j = s2.length() - 1; 4 | StringBuilder ans = new StringBuilder(); 5 | int carry = 0; 6 | 7 | while (i >= 0 && j >= 0) { 8 | int x = Character.getNumericValue(s1.charAt(i)); 9 | int y = Character.getNumericValue(s2.charAt(j)); 10 | int res = x + y + carry; 11 | int rem = res % 10; 12 | carry = res / 10; 13 | ans.append(rem); 14 | i--; 15 | j--; 16 | } 17 | 18 | while (i >= 0) { 19 | int x = Character.getNumericValue(s1.charAt(i)); 20 | int res = x + carry; 21 | int rem = res % 10; 22 | carry = res / 10; 23 | ans.append(rem); 24 | i--; 25 | } 26 | 27 | while (j >= 0) { 28 | int y = Character.getNumericValue(s2.charAt(j)); 29 | int res = y + carry; 30 | int rem = res % 10; 31 | carry = res / 10; 32 | ans.append(rem); 33 | j--; 34 | } 35 | 36 | while (carry > 0) { 37 | int rem = carry % 10; 38 | ans.append(rem); 39 | carry /= 10; 40 | } 41 | 42 | return ans.reverse().toString(); 43 | } 44 | 45 | public boolean solve(String s, int pos, int l1, int l2) { 46 | String s1 = s.substring(pos, pos + l1); 47 | String s2 = s.substring(pos + l1, pos + l1 + l2); 48 | String s3 = sumStrings(s1, s2); 49 | int n = s3.length(); 50 | 51 | if (n > s.length() - pos - l1 - l2) { 52 | return false; 53 | } 54 | 55 | if (s.substring(pos + l1 + l2, pos + l1 + l2 + n).equals(s3)) { 56 | if (pos + l1 + l2 + n == s.length()) { 57 | return true; 58 | } 59 | return solve(s, pos + l1, l2, n); 60 | } 61 | 62 | return false; 63 | // @pankaj_meharchandai 64 | } 65 | 66 | public int isSumString(String s) { 67 | int n = s.length(); 68 | for (int i = 1; i < n; i++) { 69 | for (int j = 1; j < n - i; j++) { 70 | if (solve(s, 0, i, j)) { 71 | return 1; 72 | } 73 | } 74 | } 75 | return 0; 76 | } 77 | } -------------------------------------------------------------------------------- /Largest-rectangular-sub-matrix-whose-sum-is-0.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static ArrayList> sumZeroMatrix(int[][] a) { 3 | // code here 4 | int n = a.length, m = a[0].length; 5 | int c1 = 0, c2 = 0, r1 = 0, r2 = 0, maxArea = -1001; 6 | 7 | for (int i = 0; i < m; i++) { 8 | int arr[] = new int[n]; 9 | for (int j = i; j < m; j++) { 10 | for (int k = 0; k < n; k++) { 11 | arr[k] += a[k][j]; 12 | } 13 | int s[] = { 0 }; 14 | int e[] = { 0 }; 15 | int len = maxLen(arr, s, e); 16 | 17 | int area = (e[0] - s[0] + 1) * (j - i + 1); 18 | 19 | if (len > 0 && area > maxArea) { 20 | r1 = s[0]; 21 | r2 = e[0]; 22 | c1 = i; 23 | c2 = j; 24 | maxArea = area; 25 | } 26 | } 27 | } 28 | 29 | ArrayList> list = new ArrayList<>(); 30 | for (int i = r1; i <= r2; i++) { 31 | ArrayList ilist = new ArrayList<>(); 32 | for (int j = c1; j <= c2; j++) { 33 | ilist.add(a[i][j]); 34 | } 35 | list.add(ilist); 36 | } 37 | return list; 38 | } 39 | 40 | static int maxLen(int arr[], int s[], int e[]) { 41 | int sum = 0, mlen = 0; 42 | HashMap hm = new HashMap<>(); 43 | 44 | for (int i = 0; i < arr.length; i++) { 45 | sum += arr[i]; 46 | 47 | if (arr[i] == 0 && mlen == 0) { 48 | s[0] = i; 49 | e[0] = i; 50 | mlen = 1; 51 | } 52 | if (sum == 0) { 53 | if (mlen < i + 1) { 54 | s[0] = 0; 55 | e[0] = i; 56 | } 57 | mlen = i + 1; 58 | } 59 | if (hm.containsKey(sum)) { 60 | 61 | int plen = mlen; 62 | mlen = Math.max(mlen, i - hm.get(sum)); 63 | 64 | if (mlen > plen) { 65 | e[0] = i; 66 | s[0] = hm.get(sum) + 1; 67 | } 68 | } else 69 | hm.put(sum, i); 70 | } 71 | return mlen; 72 | 73 | } 74 | } -------------------------------------------------------------------------------- /Solution3.java: -------------------------------------------------------------------------------- 1 | class Solution3{ 2 | int par[],rank[]; 3 | public int largestIsland(int n,int[][] grid) 4 | { 5 | par = new int[n*n]; 6 | rank = new int[n*n]; 7 | 8 | for(int i=0;i set = new HashSet<>(); 37 | for(int i=0;i=0 && grid[i][j-1]==1) 49 | { 50 | int p = find(i*n+j-1); 51 | tot+=(set.add(p)?cnt[p]:0); 52 | } 53 | if(i+1=0 && grid[i-1][j]==1) 59 | { 60 | int p = find((i-1)*n+j); 61 | tot+=(set.add(p)?cnt[p]:0); 62 | } 63 | set.clear(); 64 | ans = Math.max(ans,tot); 65 | } 66 | } 67 | 68 | return Math.max(ans,0); 69 | } 70 | 71 | void dfs(int i,int j,int par_i,int par_j,int n,int grid[][],int vis[][]) 72 | { 73 | if(i<0 || j<0 || i==n || j==n || grid[i][j]==0 || vis[i][j]==1) return; 74 | 75 | vis[i][j] = 1; 76 | if(par_i!=-1) 77 | union(i*n+j,par_i*n+par_j); 78 | 79 | dfs(i,j+1,i,j,n,grid,vis); 80 | dfs(i+1,j,i,j,n,grid,vis); 81 | dfs(i-1,j,i,j,n,grid,vis); 82 | dfs(i,j-1,i,j,n,grid,vis); 83 | } 84 | 85 | int find(int node) 86 | { 87 | if(par[node]==node) 88 | return node; 89 | 90 | par[node] = find(par[node]); 91 | 92 | return par[node]; 93 | } 94 | 95 | void union(int u,int v) 96 | { 97 | int up = find(u); 98 | int vp = find(v); 99 | 100 | if(up==vp) return ; 101 | else if(rank[up] set = new HashSet<>(); 37 | for(int i=0;i=0 && grid[i][j-1]==1) 49 | { 50 | int p = find(i*n+j-1); 51 | tot+=(set.add(p)?cnt[p]:0); 52 | } 53 | if(i+1=0 && grid[i-1][j]==1) 59 | { 60 | int p = find((i-1)*n+j); 61 | tot+=(set.add(p)?cnt[p]:0); 62 | } 63 | set.clear(); 64 | ans = Math.max(ans,tot); 65 | } 66 | } 67 | 68 | return Math.max(ans,0); 69 | } 70 | 71 | void dfs(int i,int j,int par_i,int par_j,int n,int grid[][],int vis[][]) 72 | { 73 | if(i<0 || j<0 || i==n || j==n || grid[i][j]==0 || vis[i][j]==1) return; 74 | 75 | vis[i][j] = 1; 76 | if(par_i!=-1) 77 | union(i*n+j,par_i*n+par_j); 78 | 79 | dfs(i,j+1,i,j,n,grid,vis); 80 | dfs(i+1,j,i,j,n,grid,vis); 81 | dfs(i-1,j,i,j,n,grid,vis); 82 | dfs(i,j-1,i,j,n,grid,vis); 83 | } 84 | 85 | int find(int node) 86 | { 87 | if(par[node]==node) 88 | return node; 89 | 90 | par[node] = find(par[node]); 91 | 92 | return par[node]; 93 | } 94 | 95 | void union(int u,int v) 96 | { 97 | int up = find(u); 98 | int vp = find(v); 99 | 100 | if(up==vp) return ; 101 | else if(rank[up] set=new HashSet< >(); 9 | 10 | static { 11 | set.addAll(Arrays.asList("First Holidays" , "Big Milestones" , "Create your own", "Milestones in color", "Silver Ballons", "Pink Balloons", "Unicorn Balloons", "Ribbon Badges", "Circle Badges", "Animal numbers", "Cute animals", "Cute months ", "Floral numbers", "Starry numbers", "Activities", "Sleep", "Emotions", "Emoticons", "Endearments", "Love in Color", "Everyday in Color", "Badges & Banners", "Pop Art", "Fireworks", "Dividers", "Flags", "Hearts", "Love In Color", "Props", "Masks", "Stats", "This & That", "Gender Reveals", "Birth Announcements", "Measurements", "Announcements in Color", "Funny in Color", "Daddy", "Family", "Friends", "Friends & Family in Color", "Christmas", "Christmas Doodles", "Christmas in Color", "New Year", "New Year in Color", "Easter", "Easter in Color", "Halloween", "Halloween in Color", "Thanksgiving", "Thanksgiving in Color", "Mothers Day", "Mothers Day In Color", "Fathers Day", "Valentine’s Day", "Valentine’s Day in Color", "Happy Hanukkah", "Happy Ramadan", "Holidays in Color", "Birthday in Color", "Unicorn", "Party", "Summer", "Fall", "Winter", "School Bursts", "Gender Reveals", "Baby shower", "Pregnancy Milestones", "Bump Fruit Stickers", "Bump Fruit Badges", "Weekly Stickers", "Weekly Ribbon Badges", "Weekly Calendar Stickers", "Ribbon Due Dates", "Cute Due Dates", "Pregnancy in Color", "Gender Reveals", "Baby shower", "Pregnancy Milestones", "Bump Fruit Stickers", "Bump Fruit Badges", "Weekly Stickers", "Weekly Ribbon Badges", "Weekly Calendar Stickers", "Ribbon Due Dates", "Cute Due Dates", "Pregnancy in Color", "Gender Reveals", "Birth Announcements", "Measurements", "Announcements in Color", "Pink Balloons", "Unicorn Balloons", "Ribbon Badges", "Circle Badges", "Cute animals", "Cute months", "Floral numbers", "Starry numbers", "First Holidays", "Big Milestones", "Pregnancy Milestones", "Create Your Own", "Milestones in Color", "Activities", "Sleep", "Emotions", "Emoticons", "Endearments", "Love in Color", "Everyday in Color", "Badges & Banners", "Pop Art", "Fireworks", "Dividers", "Flags", "Hearts", "Love In Color", "Props", "Masks", "Stats", "This & That", "Daddy", "Family", "Friends", "Friends & Family in Color", "Christmas", "Christmas", "Christmas in Color", "New Year", "New Year in Color", "Easter", "Easter in Color", "Halloween", "Halloween in Color", "Thanksgiving", "Thanksgiving in Color", "Mothers Day", "Mothers Day In Color", "Fathers Day", "Valentine’s Day", "Valentine’s Day in Color", "Happy Hanukkah", "Happy Ramadan", "Holidays in Color", "Summer", "Fall", "Winter", "Funny in Color", "Birthday in Color", "Unicorn", "Party", "School Bursts" )); 12 | } 13 | 14 | public static void main(String[] args) { 15 | boolean cheack= set.contains("Animal Number"); 16 | 17 | 18 | private Set subtreeHashes = new HashSet<>(); 19 | private boolean duplicateSubtreeFound = false; 20 | 21 | String func(Node root) { 22 | if (root == null) 23 | return ""; 24 | 25 | String l = func(root.left); 26 | String r = func(root.right); 27 | 28 | String s = Integer.toString(root.data) + l + r; 29 | 30 | if (subtreeHashes.contains(s) && s.length() > 2) { 31 | duplicateSubtreeFound = true; 32 | } 33 | 34 | subtreeHashes.add(s); 35 | return s; 36 | } 37 | 38 | 39 | int dupSub(Node root) { 40 | // code here 41 | 42 | func(root); 43 | return duplicateSubtreeFound ? 1 : 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Cover image for GeeksForGeeks 2 | Logo 3 | GeeksForGeeks 4 | 5 | Is this your company? 6 | Overview 7 | 8 | 472 9 | Reviews 10 | 11 | -- 12 | Jobs 13 | 14 | 377 15 | Salaries 16 | 17 | 54 18 | Interviews 19 | 20 | 72 21 | Benefits 22 | 23 | 3 24 | Photos 25 | 26 | 105 27 | Diversity 28 | 29 | GeeksForGeeks Technical Content Writer Reviews 30 | 4.3 31 | 32 | 99% would recommend to a friend 33 | 34 | (164 total reviews) 35 | 36 | Companies can't alter or remove reviews. (Really!) See how Glassdoor protects users and content 37 | Close notice 38 | Ratings by category 39 | 4.4 40 | 41 | Work/Life Balance 42 | 43 | 4.3 44 | 45 | Culture and Values 46 | 47 | 4.2 48 | 49 | Diversity and Inclusion 50 | 51 | 4.1 52 | 53 | Senior Management 54 | 55 | 4.0 56 | 57 | Career Opportunities 58 | 59 | 4.0 60 | 61 | Compensation and Benefits 62 | 63 | Ratings distribution 64 | 5 stars 65 | 66 | 52% 67 | 68 | 4 stars 69 | 70 | 32% 71 | 72 | 3 stars 73 | 74 | 11% 75 | 76 | 2 stars 77 | 78 | 4% 79 | 80 | 1 star 81 | 82 | 1% 83 | 84 | What are your colleagues talking about? 85 | Find your exclusive Company Bowl™ on Community. Join the hottest conversation with your colleagues anonymously. 86 | Join the conversation 87 | Technical Content Writer 88 | 89 | Find Reviews 90 | Clear All 91 | 92 | Filter 93 | Found 164 of over 472 reviews 94 | Updated 24 Oct 2023 95 | 96 | Sort 97 | 5.0 98 | 24 Oct 2023 99 | 100 | Very good company with decent work life balance. 101 | Technical Content Writer 102 | Current Intern, less than 1 year 103 | Recommend 104 | CEO Approval 105 | Business Outlook 106 | Pros 107 | 108 | Flexible Work Hours: Company allows for flexible work hours, giving employees the freedom to set their schedules. Core hours ensure collaboration while accommodating personal preferences. Code Review Process: Code review guidelines are clear and well-documented. Automated tools catch common issues, saving time and ensuring quality. Reviewers are trained and provide constructive feedback. Regular peer review meetings enhance collaboration. Developer feedback loop helps improve the process continually. 109 | 110 | Cons 111 | 112 | Code Review Process: Review process can be slow, impacting efficiency. Consider streamlining procedures and setting clearer expectations for quicker reviews. 113 | 114 | Show More 115 | 116 | Helpful 117 | 118 | Share 119 | 5.0 120 | 7 Oct 2023 121 | 122 | gfg 123 | Technical Content Writer 124 | Current Employee, less than 1 year 125 | Noida 126 | Recommend 127 | CEO Approval 128 | Business Outlook 129 | Pros 130 | 131 | Enroll in GeeksforGeeks CIP course for technical interview preparation & coding practice. Learn interview preparation tips and Practice technical interview questions at GFG. Top Theoretical Content. 200+ coding problems. Data Structures -Advanced. Objective Questions. 132 | 133 | Cons 134 | 135 | Overwhelming Amount of Content: The sheer volume of content on GeeksforGeeks can be overwhelming, especially for beginners. It can be challenging to find specific information or navigate through the extensive library of articles. Quality Variability: While GeeksforGeeks has a lot of valuable content, the quality of articles and explanations can vary. Some articles may lack clarity or may not be up to date with the latest technologies and best practices. Limited Interactivity: GeeksforGeeks primarily provides text-based tutorials and coding examples. It may not be as interactive as some other online learning platforms that offer hands-on coding environments or interactive exercises. 136 | 137 | Show More 138 | 139 | Helpful 140 | 141 | Share 142 | Join the GeeksForGeeks team 143 | 4.0 144 | 16 Oct 2023 145 | 146 | Best 147 | Technical Content Writer 148 | Current Employee, less than 1 year 149 | Greater Noida 150 | Recommend 151 | CEO Approval 152 | Business Outlook 153 | Pros 154 | 155 | They give food along with the job. 156 | 157 | Cons 158 | 159 | The work gets boring after some time. 160 | 161 | 162 | Helpful 163 | 164 | Share 165 | 5.0 166 | 7 Oct 2023 167 | 168 | Technical content writer 169 | Technical Content Writer 170 | Current Employee, less than 1 year 171 | Guwahati 172 | Recommend 173 | CEO Approval 174 | Business Outlook 175 | Pros 176 | 177 | Work life balance.learn about the concept 178 | 179 | Cons 180 | 181 | There is no cons.You will love working there . 182 | 183 | 184 | Helpful 185 | 186 | Share 187 | 5.0 188 | 6 Sept 2023 189 | 190 | Best company 191 | Technical Content Writer 192 | Current Freelancer 193 | Recommend 194 | CEO Approval 195 | Business Outlook 196 | Pros 197 | 198 | Best team for technical Content Writer 199 | 200 | Cons 201 | 202 | Slow work to publish articles 203 | 204 | 205 | Helpful 206 | 207 | Share 208 | 4.0 209 | 16 Aug 2023 210 | 211 | above average 212 | Technical Content Writer 213 | Former Intern, less than 1 year 214 | Recommend 215 | CEO Approval 216 | Business Outlook 217 | Pros 218 | 219 | content writer job is helpful 220 | 221 | Cons 222 | 223 | really really less pay, try freelance 224 | 225 | 226 | Helpful 227 | 228 | Share 229 | 3.0 230 | 15 Aug 2023 231 | 232 | review 233 | Technical Content Writer 234 | Former Employee 235 | New Delhi 236 | Recommend 237 | CEO Approval 238 | Business Outlook 239 | Pros 240 | 241 | good for part time job as student 242 | 243 | Cons 244 | 245 | pay is less according to work 246 | 247 | 248 | Helpful 249 | 250 | Share 251 | 4.0 252 | 1 Aug 2023 253 | 254 | It is good place in online for writing articles and improving related skills with earning some money too. 255 | Content Writer 256 | Former Freelancer 257 | Hyderābād 258 | Recommend 259 | CEO Approval 260 | Business Outlook 261 | Pros 262 | 263 | Nice place to work online 264 | 265 | Cons 266 | 267 | Reviewing of articles gets delayed. 268 | 269 | 270 | Helpful 271 | 272 | Share 273 | 5.0 274 | 6 Aug 2023 275 | 276 | Good 277 | Junior Engineer/Technical Content Writer 278 | Current Intern, less than 1 year 279 | Recommend 280 | CEO Approval 281 | Business Outlook 282 | Pros 283 | 284 | You will get Paid good enough. 285 | 286 | Cons 287 | 288 | Lacks communication it should be improved 289 | 290 | 291 | Helpful 292 | 293 | Share 294 | 4.0 295 | 21 Jul 2023 296 | 297 | It was good experience with geeksforgeeks as a technical Content writer 298 | Technical Content Writer 299 | Former Intern 300 | Recommend 301 | CEO Approval 302 | Business Outlook 303 | Pros 304 | 305 | Salary was provided on the basis of content quality. 306 | 307 | Cons 308 | 309 | There was 10% of GST cut by the company. 310 | 311 | 312 | Helpful 313 | 314 | Share 315 | 316 | 12345 317 | Viewing 1 - 10 of 164 Reviews 318 | Reviews>Technical Content Writer>GeeksForGeeks 319 | Embed 320 | Popular Careers with GeeksForGeeks Job Seekers 321 | Product Designer Career 322 | JobsSalariesInterviews 323 | Mobile Engineer Career 324 | JobsSalariesInterviews 325 | Software Developer Career 326 | JobsSalariesInterviews 327 | Automation Engineer Career 328 | JobsSalariesInterviews 329 | Test Engineer Career 330 | JobsSalariesInterviews 331 | Technical Writer Career 332 | JobsSalariesInterviews 333 | Executive Assistant Career 334 | JobsSalariesInterviews 335 | Advisor Career 336 | JobsSalariesInterviews 337 | Content Writer Career 338 | JobsSalariesInterviews 339 | Front End Engineer Career 340 | JobsSalariesInterviews 341 | 342 | Work at GeeksForGeeks? Share Your Experiences 343 | GeeksForGeeks logo 344 | GeeksForGeeks 345 | ★★★★★ 346 | Click to Rate 347 | Start your review... 348 | or 349 | Add Salary 350 | Add Interview 351 | Add Benefits 352 | Reviews by Job Title 353 | Technical Content Writer(138) 354 | Content Writer(22) 355 | Technical Content Engineer(16) 356 | Software Engineer(14) 357 | Intern(11) 358 | 359 | Show more job titles 360 | Top Companies for "Compensation and Benefits" Near You 361 | Amazon 362 | Amazon 363 | Compensation & Benefits 364 | 3.8 365 | Accenture 366 | Accenture 367 | Compensation & Benefits 368 | 3.8 369 | Capgemini 370 | Capgemini 371 | Compensation & Benefits 372 | 3.5 373 | IBM 374 | IBM 375 | Compensation & Benefits 376 | 3.7 377 | GeeksForGeeks Photos 378 | + Add Photo 379 | GeeksForGeeks photo of: Employee Engagement Activity 380 | GeeksForGeeks photo of: Sandeep sir teaching students 381 | GeeksForGeeks photo of: Yash 382 | Have you worked here? 383 | 384 | See All Photos 385 | --------------------------------------------------------------------------------