├── Arrays ├── DutchNationalFlag.java ├── Frequency_Array.java └── WaveArray.java ├── Binary Search ├── BinarySearch.java ├── InsertPosition.java ├── Peak_Element.java ├── SquareRoot.java ├── TransitionPoint.java └── rotation.java ├── CP_TEMPLATES ├── TEMPLATE1.java ├── TEMPLATE2.java ├── TEMPLATE3.java └── TEMPLATE4.java ├── CodeChef ├── ADADISH.java ├── ATTENDU.java ├── Arrays │ ├── CARVANS.java │ ├── ICPC16B.java │ └── MODEFREQ.java ├── BILLRD.java ├── CHEFSTR1.java ├── CHFHEIST.java ├── CHFM.java ├── CHPINTU.java ├── COCONUT.java ├── CONFLIP.java ├── CRDGAME.java ├── DECODEIT.java ├── DIVTHREE.java ├── ENC2020A.java ├── FCTRL.java ├── FEMA2.java ├── FLOW016.java ├── Hash │ └── TREE2.java ├── LADDU.java ├── LCH15JAB.java ├── MSNSADM1.java ├── MULTHREE.java ├── Oct-Long-Challenge │ ├── CHEFEZQ.java │ └── CVDRUN.java ├── PASSWD.java ├── RESTORE.java ├── SEDARR.java ├── SEGM01.java ├── SUMPOS.java ├── SmartPhone.java ├── Strings │ └── Lapindrome.java ├── TOWIN.java └── VACCINE1.java ├── Codeforces └── codeforces-ladders │ ├── Ladder A │ ├── 1061A.java │ ├── 1095A.java │ ├── 110A.java │ ├── 112A.java │ ├── 1173A.java │ ├── 1228.java │ ├── 1285.java │ ├── 1392A.java │ ├── 1397.java │ ├── 1401.java │ ├── 1409A.java │ ├── 1426A.java │ ├── 1433.java │ ├── 1436A.java │ ├── 1438.java │ ├── 1440A.java │ ├── 1447A.java │ ├── 1452A.java │ ├── 1465.java │ ├── 1466A.java │ ├── 1467A.java │ ├── 155A.java │ ├── 199A.java │ ├── 205A.java │ ├── 236A.java │ ├── 248A.java │ ├── 260A.java │ ├── 271A.java │ ├── 272A.java │ ├── 281A.java │ ├── 282A.java │ ├── 318A.java │ ├── 320A.java │ ├── 34A.java │ ├── 450A.java │ ├── 486A.java │ ├── 525A.java │ ├── 69A.java │ ├── 750A.java │ ├── 80A.java │ ├── 959(A).java │ ├── 959A.java │ └── watermelon(A).java │ ├── Ladder B │ ├── 1438B.java │ ├── 1454B.java │ ├── 1465B.java │ ├── 1466B.java │ ├── 266B.java │ └── 34B.java │ ├── Ladder C │ ├── 1452C.java │ └── 4C.java │ ├── Ladder D │ └── 1426D.java │ └── Ladder E │ └── 1426E.java ├── DSA CRACKER 450 ├── Array │ ├── Chocolate_distribution.java │ ├── Common_Elements.java │ ├── Count_Inversion.java │ ├── Count_pairs_with_given_sum.java │ ├── Duplicate_Number.java │ ├── Factorial_of_large_numbers.java │ ├── Kadane.java │ ├── KadanesAlgo.java │ ├── Max_product_subarray.java │ ├── Merge_2_sorted_arrays.java │ ├── Merge_Intervals.java │ ├── Min_no_of_jumps_to_reach_end.java │ ├── Minimize_The_Height.java │ ├── MoveAllNegativeToEnd.java │ ├── Next_Permutation.java │ ├── P1_reverseArray.java │ ├── Palindromic_array.java │ ├── RotateArray.java │ ├── Smallest_subarray_w_sum_less_than_x.java │ ├── Sort012.java │ ├── SubArray_with_0_sum.java │ ├── Subset_of_other_array.java │ ├── Three_way_partitioning.java │ ├── Trapping_Rainwater.java │ ├── Triple_sum_in_array.java │ ├── UnionOfArray.java │ ├── more_than_n\k_elements.java │ └── subarray_having_prod_less_than_k.java ├── Binary Trees │ ├── Balanced_Binary_tree.java │ ├── Boundary_Traversal.java │ ├── Diagonal_Traversal.java │ ├── Diameter_of_a_Binary_Tree.java │ ├── Height_of_binary_tree.java │ ├── Invert_Binary_Tree.java │ ├── Left_view_of_binary_tree.java │ ├── LevelOrderTraversal.java │ ├── Minimum_Depth_of_Binary_Tree.java │ ├── Mirror_Tree.java │ ├── ReverseLevelOrderTraversal.java │ └── Right_View.java ├── Dynamic Programming │ ├── CoinChange.java │ ├── Edit_Distance.java │ ├── Friends_pairing_problem.java │ ├── GoldMine_problem.java │ ├── Knapsack.java │ ├── Longest_Common_Subsequence.java │ ├── Partition_equal_subset_sum.java │ └── nCr.java ├── LinkedList │ ├── Check_Palindrome.java │ ├── Detect_loop_in_linkedlist.java │ ├── Intersection_point.java │ ├── Intersection_point_of_sorted_list.java │ ├── Remove_dulpicates_from_sorted_linkedlist.java │ ├── Remove_duplicates_from_unsorted_linkedlist.java │ ├── Reverse_a_linkedlist.java │ └── Reverse_in_a_group_of_k.java ├── Matrix │ ├── Median_in_rowWise_sorted_matrix.java │ ├── Rotate_matrix.java │ ├── Search_2D_matrix.java │ ├── Sorted_matrix.java │ └── Spiral_Traversal.java ├── Searching & Sorting │ ├── Aggressive_Cows.java │ ├── Allocate_min_number_of_pages.java │ ├── Arithmitic_numbers.java │ ├── Count_squares.java │ ├── Count_triplets_with_sum_smaller_than_x.java │ ├── Count_zero_sum_subarrays.java │ ├── EKOSPOJ.java │ ├── Find_Missing_and_repeating.java │ ├── First_and_last_occurrences_of_x.java │ ├── Majority_element.java │ ├── Merge_2_sorted_arrays.java │ ├── Middle_of_three.java │ ├── Minimum_swaps_to_sort.java │ ├── Painter's_Partition.java │ ├── Pair_of_given_difference.java │ ├── Product_array_puzzle.java │ ├── ROTI_PRATA.java │ ├── Sort_by_set_bit.java │ ├── Stickler_thief.java │ └── Value_equal_to_index_value.java ├── Stacks & Queues │ ├── Evaluation_of_Postfix_Expression.java │ ├── LRU_Cache.java │ ├── Max_Area_Histogram.java │ ├── NextGreaterElement.java │ ├── Queue_Reversal_using_Recursion.java │ ├── Reverse_a_stack_using_recursion.java │ ├── Stack_Permutation.java │ ├── Stack_Sort_using_Recursion.java │ ├── Stacks_using_2_Queues.java │ ├── TheCelebrityProblem.java │ └── ValidSubstring.java └── String │ ├── Count_palindromic_subsequence.java │ ├── Count_the_reversals.java │ ├── Edit_distance.java │ ├── Group_Anagrams.java │ ├── Isomorphic_strings.java │ ├── Longest_common_prefix.java │ ├── Longest_common_subsequence.java │ ├── Longest_palindromic_substring.java │ ├── Longest_repeating_subsequence.java │ ├── Minimum_swaps.java │ ├── Mobile_keyboard_equivalent.java │ ├── Next_permutation.java │ ├── Palindrome_string.java │ ├── Parenthesis_Checker.java │ ├── Rearrange_characters.java │ ├── Remove_consecutive_characters.java │ ├── Reverse_string.java │ ├── Roman_to_Integer.java │ ├── Smallest_dictinct_window.java │ ├── Split_binary_string.java │ ├── String_rotation.java │ ├── Transform_one_string_to_other.java │ ├── Wildcard_string_matching.java │ └── count_number_of_palindromic_substring.java ├── Dynamic Programming ├── CoinChange.java └── knapsack.java ├── Hash ├── Frequency.java ├── SubArray_with_0_sum.java ├── TwiceCounter.java ├── VitaliyAndPie.java ├── equalArray.java ├── groupAnagrams.java └── register.java ├── Linear Search ├── MaximunSum.java ├── ReastInPeace.java ├── segm01.java └── simpleSearch.java ├── LinkedList └── Detect_loop_in_linkedlist.java ├── Prefix-Sum Algo ├── BLONDIE.java ├── CSUMQ.java └── Equblirium_point.java ├── Queue └── Queue_reversal.java ├── README.md ├── Recursion └── NumberOfPaths.java ├── Replace_0_with_5.java ├── Sliding-Window ├── Distinct_elem_in_every_window.java ├── First_Neg_Integer_in_the _window.java ├── GreedyDD.java └── Max_Sum_Subarray_of_size_K.java ├── Stack ├── Reverse_first_k_elements_stack.java ├── Stack_designer.java ├── Stack_operation.java ├── Stack_using_Array.java └── String_reversal.java ├── StriverSDE ├── 2 Pointers │ ├── D7_P2_3Sum.java │ ├── D7_P3_Trapping_Rainwater.java │ ├── D7_P4_Remove_Duplicates_from_array.java │ └── D7_P5_Max_consecutive_ones.java ├── Arrays │ ├── D1_P1_Sort012.java │ ├── D1_P2_FindMissingAndRepeating.java │ ├── D1_P3_Merge_without_extra_space.java │ ├── D1_P4_MaxSubarray(KadaneAlgo).java │ ├── D1_P5_MergeIntervals.java │ ├── D1_P6_FindDuplicate.java │ ├── D2_P1_SetMatrix0.java │ ├── D2_P2_PascalTriangle.java │ ├── D2_P3_NextPermutation.java │ ├── D2_P4_CountInversion.java │ ├── D2_P5_BestTimeToBuyStocks.java │ ├── D2_P6_RotateArray.java │ ├── D3_P1_SearchMatrix.java │ ├── D3_P2_Power(x,n).java │ ├── D3_P3_MajorityElements.java │ ├── D3_P4_MajorityElement2.java │ ├── D3_P5_UniquePaths.java │ └── D3_P6_ReversePairs.java ├── Greedy │ ├── D8_P1_N_meetings_in_1_room.java │ ├── D8_P2_Minimun_platforms.java │ ├── D8_P3_Job_sequencing.java │ └── D8_P4_Fractional_knapsack.java ├── Hashing │ ├── D4_P1_2Sum.java │ ├── D4_P2_4Sum.java │ ├── D4_P3_LongestConsecutiveSeq.java │ ├── D4_P4_LargestSubarrayWith_0_Sum.java │ └── D4_P6_Longest_Substring_without_rep.java ├── Linked List │ ├── D5_P1_Reverse_linked_list.java │ ├── D5_P2_Middle_of_the_linked_list.java │ ├── D5_P3_Merge_2_sorted_list.java │ ├── D5_P4_Remove_nth_node_from_end.java │ ├── D5_P5_Delete_a_node.java │ ├── D5_P6_Add_two_numbers.java │ ├── D6_P1_Intersection_point_of_2_LinkedList.java │ └── D6_P4_Check_Palindrome.java └── Recursion │ └── D9_P1_Subset_sums.java └── two-pointer algo ├── Kth_small_element.java ├── sort.java └── triplet_with_zero_sum.java /Arrays/DutchNationalFlag.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/sort-colors/submissions/ 2 | 3 | // Sort 0,1,2 without using any sorting function 4 | 5 | class Solution { 6 | public void sortColors(int[] a) { 7 | 8 | // Dutch National Flag Algo 9 | int low=0, mid=0, hi=a.length-1,t; 10 | 11 | while(mid<=hi){ 12 | 13 | if(a[mid]==0){ 14 | t=a[mid]; 15 | a[mid] = a[low]; 16 | a[low] = t; 17 | low++; mid++; 18 | }else if(a[mid]==1){ 19 | mid++; 20 | }else{ 21 | t=a[hi]; 22 | a[hi] = a[mid]; 23 | a[mid] = t; 24 | hi--; 25 | } 26 | } 27 | for(int e:a){ 28 | System.out.print(e); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Arrays/Frequency_Array.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class solution 3 | { 4 | 5 | static void frequencyOfEach(int []arr, int n) 6 | { 7 | int max = Integer.MIN_VALUE; 8 | // finding maximum element in array 9 | for (int i = 0;imax) 12 | max = arr[i]; 13 | } 14 | 15 | 16 | // make hash array of size equal to maximum 17 | // element in array 18 | int []hash = new int[max + 1]; 19 | Arrays.fill(hash,0); 20 | 21 | /* Counting frequency of each element of array 22 | and storing it in hash*/ 23 | for (int i = 0; i < n; i++) { 24 | hash[arr[i]]++; 25 | } 26 | 27 | // printing frequency of elements 28 | for (int i = 0; i <= max; i++) { 29 | 30 | /* If hash[i] has stored any value 31 | i.e element has occurred atleat 32 | once in array */ 33 | if (hash[i] != 0) 34 | System.out.println(i+"-->"+hash[i]); 35 | } 36 | } 37 | 38 | public static void main(String args[]) 39 | { 40 | int []arr = { 5, 2, 2, 3, 5, 1, 1, 5, 3, 4 }; 41 | int n = arr.length; 42 | frequencyOfEach(arr, n); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Arrays/WaveArray.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1 2 | 3 | public static void convertToWave(int a[], int n){ 4 | if(n%2==0){ 5 | for(int i=0; i a) 4 | { 5 | int start = 0, end = a.size()-1; 6 | if(a.size() == 1)return a.get(0); // if size is 1 return that elem 7 | 8 | while(start<=end){ 9 | int mid = start+(end-start)/2; 10 | 11 | if(mid>0 && mida.get(mid-1) && a.get(mid)>a.get(mid+1)) 13 | return a.get(mid); 14 | else if(a.get(mid-1)>a.get(mid)) end = mid-1; 15 | else start = mid+1; 16 | } 17 | else if(mid==0){ 18 | if(a.get(0)>a.get(1))return a.get(0); 19 | else return a.get(1); 20 | } 21 | else if(mid==a.size()-1){ 22 | if(a.get(a.size()-1)>a.get(a.size()-2))return a.get(a.size()-1); 23 | else return a.get(a.size()-2); 24 | } 25 | } 26 | return -1; 27 | } 28 | -------------------------------------------------------------------------------- /Binary Search/SquareRoot.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/sqrtx/ 2 | 3 | class Solution { 4 | public int mySqrt(int a) { 5 | long x= a; 6 | long start=1, end=x, ans=0; 7 | if(x<=1)return (int)x; 8 | 9 | while(start<=end){ 10 | long mid = (start+end)/2; 11 | 12 | if(mid*mid==x)return (int)mid; 13 | else if(mid*mid=a[j]){ 30 | p = a[j]; 31 | c++; 32 | } 33 | } 34 | System.out.println(c); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CodeChef/Arrays/ICPC16B.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/problems/ICPC16B 2 | 3 | /* package codechef; // don't place package name! */ 4 | 5 | import java.util.*; 6 | import java.lang.*; 7 | import java.io.*; 8 | 9 | /* Name of the class has to be "Main" only if the class is public. */ 10 | class Codechef 11 | { 12 | public static void main (String[] args) throws java.lang.Exception 13 | { 14 | Scanner sc = new Scanner(System.in); 15 | int t=0; 16 | if(sc.hasNextInt()) 17 | t = sc.nextInt(); 18 | while(t--!=0){ 19 | int n = sc.nextInt(); 20 | int a[] = new int[n]; 21 | int zero=0, one=0, minus_one=0, other=0; 22 | for(int i=0; i 1) 30 | System.out.println("no"); 31 | else 32 | { 33 | if (other>0 && minus_one>0) 34 | System.out.println("no"); 35 | else if (minus_one > 1 && one == 0) 36 | System.out.println("no"); 37 | else 38 | System.out.println("yes"); 39 | } 40 | 41 | 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /CodeChef/Arrays/MODEFREQ.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/problems/MODEFREQ 2 | 3 | /* package codechef; // don't place package name! */ 4 | 5 | import java.util.*; 6 | import java.lang.*; 7 | import java.io.*; 8 | 9 | /* Name of the class has to be "Main" only if the class is public. */ 10 | class Codechef 11 | { 12 | public static void main (String[] args) throws java.lang.Exception 13 | { 14 | Scanner sc = new Scanner(System.in); 15 | int t=0; 16 | if(sc.hasNextInt()) 17 | t = sc.nextInt(); 18 | while(t--!=0){ 19 | int n = sc.nextInt(); 20 | int a[] = new int[n]; 21 | for(int i=0; imax){ 32 | max = fof[i]; 33 | ans=i; 34 | } 35 | } 36 | System.out.println(ans); 37 | 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CodeChef/CHEFSTR1.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/JULY20B/problems/CHEFSTR1 2 | 3 | /* package codechef; // don't place package name! */ 4 | 5 | import java.util.*; 6 | import java.lang.*; 7 | import java.io.*; 8 | 9 | /* Name of the class has to be "Main" only if the class is public. */ 10 | class Codechef 11 | { 12 | public static void main (String[] args) throws java.lang.Exception 13 | { 14 | Scanner s = new Scanner(System.in); 15 | int t=0; 16 | if(s.hasNextInt()) 17 | t = s.nextInt(); 18 | while (t--!=0) 19 | { 20 | int n = s.nextInt(); 21 | long sum=0l; 22 | int a[] = new int[n]; 23 | for (int i = 0; i < n; i++) { 24 | a[i] = s.nextInt(); 25 | } 26 | 27 | for (int j = 0; j < n-1; j++) { 28 | int x = (a[j]-a[j+1]); 29 | if(x<0)sum+=Math.abs(x+1); 30 | else sum+=Math.abs(x-1); 31 | } 32 | System.out.println(sum); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CodeChef/CHFM.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/JULY19B/problems/CHFM 2 | /* package codechef; // don't place package name! */ 3 | 4 | import java.util.*; 5 | import java.lang.*; 6 | import java.io.*; 7 | 8 | /* Name of the class has to be "Main" only if the class is public. */ 9 | class Codechef 10 | { 11 | public static void main (String[] args) throws java.lang.Exception 12 | { 13 | Scanner sc = new Scanner(System.in); 14 | int t=0; 15 | if(sc.hasNextInt()) 16 | t = sc.nextInt(); 17 | while(t--!=0){ 18 | int n = sc.nextInt(); 19 | long a[] = new long[n]; 20 | double s=0,mean; 21 | for(int i=0; ia[i]){ 35 | min = (int)a[i]; 36 | tmp=j; 37 | f=1; 38 | } 39 | } 40 | } 41 | if(f==1) 42 | System.out.println(tmp); 43 | else 44 | System.out.println("Impossible"); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CodeChef/CHPINTU.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/problems/CHPINTU 2 | 3 | /* package codechef; // don't place package name! */ 4 | 5 | import java.util.*; 6 | import java.lang.*; 7 | import java.io.*; 8 | 9 | /* Name of the class has to be "Main" only if the class is public. */ 10 | class Codechef 11 | { 12 | public static void main (String[] args) throws java.lang.Exception 13 | { 14 | Scanner sc = new Scanner(System.in); 15 | int t=0; 16 | if(sc.hasNextInt()) 17 | t = sc.nextInt(); 18 | while(t--!=0){ 19 | int n = sc.nextInt(); 20 | int m = sc.nextInt(); 21 | int f[] = new int[n]; 22 | int p[] = new int[n]; 23 | int arr[] = new int[m]; 24 | int min = Integer.MAX_VALUE; 25 | Arrays.fill(arr,-1); 26 | int i; 27 | for(i=0;imorty)chefcnt++; 33 | else if(morty>chef)mortycnt++; 34 | else{ 35 | chefcnt++; 36 | mortycnt++; 37 | } 38 | } 39 | if(chefcnt>mortycnt)System.out.println("0 "+chefcnt); 40 | else if(mortycnt>chefcnt)System.out.println("1 "+mortycnt); 41 | else System.out.println("2 "+chefcnt); 42 | } 43 | sc.close(); 44 | } 45 | public static long sumOfDigit(long a){ 46 | long s=0l; 47 | while(a!=0){ 48 | long d=a%10; 49 | s+=d; 50 | a/=10; 51 | } 52 | return s; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /CodeChef/FCTRL.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA01/problems/FCTRL 2 | /* package codechef; // don't place package name! */ 3 | 4 | 5 | import java.util.*; 6 | import java.lang.*; 7 | import java.io.*; 8 | 9 | /* Name of the class has to be "Main" only if the class is public. */ 10 | class Codechef 11 | { 12 | public static void main (String[] args) throws java.lang.Exception 13 | { 14 | // your code goes here 15 | Scanner sc = new Scanner(System.in); 16 | int t=0; 17 | if(sc.hasNextInt()) 18 | t = sc.nextInt(); 19 | while(t--!=0){ 20 | int x = sc.nextInt(); 21 | int c = 0; 22 | while(x>0){ 23 | c+=(x/5); 24 | x/=5; 25 | } 26 | System.out.println(c); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CodeChef/FEMA2.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/NOV20B/problems/FEMA2 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.io.*; 6 | 7 | /* Name of the class has to be "Main" only if the class is public. */ 8 | class Codechef 9 | { 10 | public static void main (String[] args) throws java.lang.Exception 11 | { 12 | Scanner sc = new Scanner(System.in); 13 | int t=0; 14 | if(sc.hasNextInt()) 15 | t = sc.nextInt(); 16 | while(t--!=0){ 17 | int n = sc.nextInt(); 18 | int k = sc.nextInt(); 19 | String s = sc.next().toUpperCase(); 20 | char c[] = s.toCharArray(); 21 | int i=0,j=0,mag=0,pow=0,sheet; 22 | while(i0){ 28 | mag++; 29 | i++; 30 | j++; 31 | }else 32 | if(i>j)j++; 33 | else i++; 34 | } 35 | else if(c[j]=='X'){ 36 | i=j; 37 | i++; 38 | j++; 39 | }else j++; 40 | } 41 | else if(c[i]=='X'){ 42 | j=i; 43 | i++; 44 | j++; 45 | } 46 | else i++; 47 | } 48 | System.out.println(mag); 49 | 50 | } 51 | } 52 | public static int count(int i, int j, char c[]){ 53 | int sheet=0; 54 | if(i0){ 18 | int n = sc.nextInt(); 19 | int a[] = new int[n]; 20 | for(int i=0; i set = new HashSet<>(); 24 | for(int e:a){ 25 | set.add(e); 26 | } 27 | if(set.contains(0)){ 28 | System.out.println(set.size()-1); 29 | }else{ 30 | System.out.println(set.size()); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CodeChef/LADDU.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/LRNDSA01/problems/LADDU 2 | /* package codechef; // don't place package name! */ 3 | 4 | import java.util.*; 5 | import java.lang.*; 6 | import java.io.*; 7 | 8 | /* Name of the class has to be "Main" only if the class is public. */ 9 | class Codechef 10 | { 11 | public static void main (String[] args) throws java.lang.Exception 12 | { 13 | // your code goes here 14 | 15 | Scanner sc = new Scanner(System.in); 16 | long t = sc.nextLong(); 17 | 18 | while(t--!=0) { 19 | long activites = sc.nextLong(); 20 | String origin = sc.next(); 21 | long laddus = 0; 22 | long j=0; 23 | for(; j < activites; j++) { 24 | String act = sc.next(); 25 | 26 | if(act.equals("CONTEST_WON")) { 27 | long rank = sc.nextLong(); 28 | if(rank <= 20) { 29 | laddus += (320 - rank); 30 | } else { 31 | laddus += 300; 32 | } 33 | } else if(act.equals("TOP_CONTRIBUTOR")) { 34 | laddus += 300; 35 | } else if(act.equals("BUG_FOUND")) { 36 | long x = sc.nextLong(); 37 | laddus += x; 38 | } else { 39 | laddus += 50; 40 | } 41 | 42 | 43 | } 44 | System.out.println(origin.equals("INDIAN")? (laddus/200):(laddus/400)); 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CodeChef/LCH15JAB.java: -------------------------------------------------------------------------------- 1 | //https://www.codechef.com/problems/LCH15JAB 2 | /* package codechef; // don't place package name! */ 3 | 4 | import java.util.*; 5 | import java.lang.*; 6 | import java.io.*; 7 | 8 | /* Name of the class has to be "Main" only if the class is public. */ 9 | class Codechef 10 | { 11 | public static void main (String[] args) throws java.lang.Exception 12 | { 13 | Scanner sc = new Scanner(System.in); 14 | int t=0; 15 | if(sc.hasNextInt()) 16 | t = sc.nextInt(); 17 | while(t--!=0){ 18 | // int n = sc.nextInt(); 19 | String s = sc.next(); 20 | int max=Integer.MIN_VALUE; 21 | if(s.length()%2!=0){ 22 | System.out.println("NO"); 23 | } 24 | else{ 25 | for(int i=0; ia = new ArrayList<>(); 19 | a = sieve(4000001); 20 | while(t--!=0){ 21 | long n = sc.nextLong(); 22 | int b[] = new int[(int)n]; 23 | ArrayListc = new ArrayList<>(); 24 | ArrayListd = new ArrayList<>(); 25 | for (int i=0;i sieve(int n) 45 | { 46 | boolean prime[] = new boolean[n+1]; 47 | ArrayListprime1 = new ArrayList<>(); 48 | for(int i=0;it) 26 | t = max; 27 | } 28 | System.out.println(t); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CodeChef/Strings/Lapindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | /* Name of the class has to be "Main" only if the class is public. */ 6 | class Codechef 7 | { 8 | public static void main (String[] args) throws java.lang.Exception 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | int t = sc.nextInt(); sc.nextLine(); 12 | while(t-->0){ 13 | String s = sc.nextLine(); 14 | String left,right=""; int flag=0; 15 | int l = s.length(); 16 | int div = l/2; 17 | left = s.substring(0,div); 18 | if(l%2!=0){ 19 | right = s.substring((div+1),l); 20 | }else{ 21 | right = s.substring(div,l); 22 | } 23 | char a[] = left.toCharArray(); 24 | Arrays.sort(a); 25 | char b[] = right.toCharArray(); 26 | Arrays.sort(b); 27 | for (int j=0;j=0;--i) 36 | { 37 | if((n-3-i)%2==0) 38 | sum2+=a[i]; 39 | else 40 | sum2-=a[i]; 41 | } 42 | if(sum2>sum1) 43 | System.out.println("second"); 44 | else if(sum1==sum2) 45 | System.out.println("draw"); 46 | else 47 | System.out.println("first"); 48 | } 49 | } 50 | } 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /Codeforces/codeforces-ladders/Ladder A/1095A.java: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/1095/problem/A 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | import java.io.BufferedReader; 6 | import java.io.IOException; 7 | import java.io.InputStreamReader; 8 | // import java.util.Scanner; 9 | import java.util.StringTokenizer; 10 | 11 | public class encrypt { 12 | static class FastReader { 13 | BufferedReader br; 14 | StringTokenizer st; 15 | 16 | public FastReader() { 17 | br = new BufferedReader(new InputStreamReader(System.in)); 18 | } 19 | 20 | String next() { 21 | while (st == null || !st.hasMoreElements()) { 22 | try { 23 | st = new StringTokenizer(br.readLine()); 24 | } catch (IOException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | return st.nextToken(); 29 | } 30 | 31 | int nextInt() { 32 | return Integer.parseInt(next()); 33 | } 34 | 35 | long nextLong() { 36 | return Long.parseLong(next()); 37 | } 38 | 39 | double nextDouble() { 40 | return Double.parseDouble(next()); 41 | } 42 | 43 | String nextLine() { 44 | String str = ""; 45 | try { 46 | str = br.readLine(); 47 | } catch (IOException e) { 48 | e.printStackTrace(); 49 | } 50 | return str; 51 | } 52 | } 53 | 54 | public static void main(String[] args) { 55 | FastReader s = new FastReader(); 56 | long t = 1; 57 | while (t-- != 0) { 58 | int n = s.nextInt(); 59 | String st = s.next(); 60 | String nst = ""; 61 | int p=1; 62 | for (int i = 0; i < n; i+=p) { 63 | nst+=st.charAt(i); 64 | p++; 65 | } 66 | System.out.println(nst); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Codeforces/codeforces-ladders/Ladder A/110A.java: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/problemset/problem/110/A 2 | 3 | import java.util.*; 4 | public class luckyNum { 5 | public static void main(String[] args) throws java.lang.Exception { 6 | Scanner sc= new Scanner(System.in); 7 | long n=0L; 8 | if(sc.hasNext()) 9 | n = sc.nextLong(); 10 | int c=0; 11 | while (n>0) { 12 | long d = n%10; 13 | if(d==4 || d==7){ 14 | c++; 15 | } 16 | n/=10; 17 | } 18 | if(c==4||c==7) 19 | System.out.println("YES"); 20 | else 21 | System.out.println("NO"); 22 | 23 | sc.close(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Codeforces/codeforces-ladders/Ladder A/1436A.java: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/1436/problem/A 2 | 3 | import java.util.*; 4 | public class reorder { 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | int t=0; 8 | if(sc.hasNext()) 9 | t = sc.nextInt(); 10 | while(t--!=0){ 11 | int n = sc.nextInt(); 12 | int m = sc.nextInt(); 13 | int a; 14 | int s=0; 15 | for(int i=1; i<=n; i++){ 16 | a = sc.nextInt(); 17 | s+=a; 18 | } 19 | if(s==m)System.out.println("YES"); 20 | else System.out.println("NO"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Codeforces/codeforces-ladders/Ladder A/155A.java: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/problemset/problem/155/A 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | public class i_love_username { 6 | 7 | public static void main(String[] args){ 8 | Scanner sc = new Scanner(System.in); 9 | int n=0; 10 | if(sc.hasNext()) 11 | n = sc.nextInt(); 12 | int a[] = new int[n]; 13 | int min = Integer.MAX_VALUE; 14 | int max = Integer.MIN_VALUE; 15 | int minc=0,maxc=0; 16 | for (int i = 0; i < a.length; i++) { 17 | a[i] = sc.nextInt(); 18 | } 19 | min=a[0]; max=a[0]; 20 | for (int i = 1; i < a.length; i++) { 21 | if(min>a[i]){ 22 | min=a[i]; 23 | minc++; 24 | } 25 | else if(max a, long n, long m) 6 | { 7 | // your code here 8 | Collections.sort(a); 9 | long ans = Long.MAX_VALUE; 10 | for(int i=0; i<=n-m; i++){ 11 | ans = Math.min( ans, (a.get(i+(int)m-1) - a.get(i)) ); 12 | } 13 | return ans; 14 | } 15 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Common_Elements.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/common-elements1132/1 2 | 3 | 4 | class Solution 5 | { 6 | ArrayList commonElements(int A[], int B[], int C[], int n1, int n2, int n3) 7 | { 8 | // code here 9 | ArrayList al = new ArrayList(); 10 | boolean ans1=false, ans2=false; 11 | int x = 0; 12 | for(int i=0; ilow){ 20 | mid = (low+high)/2; 21 | 22 | count += merge_sort(a,t,low,mid); 23 | count += merge_sort(a,t,mid+1,high); 24 | 25 | count += merge(a,t,low,mid+1,high); 26 | } 27 | return count; 28 | } 29 | public static long merge(long a[], long t[], long low, long mid, long high){ 30 | long count=0l; 31 | int i=(int)low; 32 | int j=(int)mid; 33 | int k=(int)low; 34 | while( (i<=mid-1) && (j<=high) ){ 35 | if(a[i]<=a[j]){ 36 | t[k++] = a[i++]; 37 | }else{ 38 | t[k++] = a[j++]; 39 | count += mid-i; 40 | } 41 | } 42 | while(i<=mid-1){ 43 | t[k++] = a[i++]; 44 | } 45 | while(j<=high){ 46 | t[k++] = a[j++]; 47 | } 48 | for(int x=(int)low; x<=(int)high; x++){ 49 | a[x] = t[x]; 50 | } 51 | return count; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Count_pairs_with_given_sum.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/1 2 | 3 | class Solution { 4 | int getPairsCount(int[] a, int n, int k) { 5 | // code here 6 | HashMap hm = new HashMap(); 7 | for(int i=0; ihm = new HashMap<>(); 6 | int res =0; 7 | for(int i=0; i factorial(int N){ 5 | //code here 6 | ArrayList ans = new ArrayList<>(); 7 | ans.add(1); 8 | for(int i=2; i<=N; i++){ 9 | int carry=0; 10 | for(int j=0; jmaxsum)maxsum = sum; 13 | if(sum<0)sum=0; 14 | } 15 | return maxsum; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/KadanesAlgo.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1 2 | 3 | class Solution{ 4 | int maxSubarraySum(int a[], int n){ 5 | 6 | // Your code here 7 | int sum=0,max = Integer.MIN_VALUE; 8 | for(int i=0; i max) max = sum; 11 | if(sum<0)sum=0; 12 | } 13 | return max; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Max_product_subarray.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/maximum-product-subarray3604/1# 2 | 3 | class Solution { 4 | long maxProduct(int[] a, int n) { 5 | // code here 6 | long current_max = a[0]; 7 | long current_min = a[0]; 8 | long ans = a[0]; 9 | for(int i=1; i=0 && jarr2[j]){ 10 | int temp=arr1[i]; 11 | arr1[i]=arr2[j]; 12 | arr2[j]=temp; 13 | } 14 | i--;j++; 15 | } 16 | Arrays.sort(arr1); 17 | Arrays.sort(arr2); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Merge_Intervals.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/merge-intervals/ 2 | 3 | class Solution { 4 | public int[][] merge(int[][] intervals) { 5 | Listres = new ArrayList<>(); 6 | if(intervals.length==0 || intervals==null){ 7 | return res.toArray(new int[0][]); 8 | } 9 | Arrays.sort(intervals, (a,b)->a[0]-b[0]); 10 | int start = intervals[0][0]; 11 | int end = intervals[0][1]; 12 | for(int []i: intervals){ 13 | if(i[0]<=end){ 14 | end = Math.max(end, i[1]); 15 | }else{ 16 | res.add(new int[]{start,end}); 17 | start = i[0]; 18 | end = i[1]; 19 | } 20 | } 21 | res.add(new int[]{start,end}); 22 | return res.toArray(new int[0][]); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Min_no_of_jumps_to_reach_end.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1 2 | 3 | class Solution{ 4 | static int minJumps(int[] a){ 5 | // your code here 6 | int n = a.length; 7 | if(n==0)return 0; 8 | if(a[0]==0)return -1; 9 | int maxrange = a[0], steps=a[0], jumps=1; 10 | for(int i=1; i=maxrange){ 17 | return -1; 18 | } 19 | steps = maxrange-i; 20 | } 21 | } 22 | return jumps; 23 | } 24 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Minimize_The_Height.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimize-the-heights3351/1 2 | 3 | 4 | class Solution { 5 | int getMinDiff(int[] a, int n, int k) { 6 | Arrays.sort(a); 7 | int ans = a[n-1]-a[0]; 8 | int smallest=0; 9 | int largest=0; 10 | for(int i=1; i=k){ 12 | largest = Math.max(a[i-1]+k, a[n-1]-k); 13 | smallest = Math.min(a[0]+k, a[i]-k); 14 | ans = Math.min(ans,largest-smallest ); 15 | } 16 | } 17 | return ans; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/MoveAllNegativeToEnd.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/move-all-negative-elements-to-end1813/1# 2 | 3 | class Solution { 4 | 5 | public void segregateElements(int a[], int n) 6 | { 7 | // Your code goes here 8 | int temp[] = new int[n]; 9 | int ind=0; 10 | for(int i=0; i=0){ 12 | temp[ind++] = a[i]; 13 | } 14 | } 15 | for(int i=0;i=0 && a[i]>=a[i+1])i--; // index which breaks descending order 1342 (index =1) 9 | int j = a.length-1; 10 | if(i>=0){ // if not entire descending 11 | while(a[j]<=a[i])j--; // index of right most first large digit 1342 (index =2) 12 | swap(a, i, j); 13 | } 14 | reverse(a, i+1, a.length-1); //reverse the rest 1432 -> 1423(ans) 15 | } 16 | 17 | public void swap(int []a, int i, int j){ 18 | int t = a[i]; 19 | a[i] = a[j]; 20 | a[j] = t; 21 | } 22 | 23 | public void reverse(int []a, int i, int j){ 24 | while(i=0; i--){ 10 | revstr += str.charAt(i); 11 | } 12 | return (revstr); 13 | } 14 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Palindromic_array.java: -------------------------------------------------------------------------------- 1 | class GfG 2 | { 3 | public static int palinArray(int[] a, int n) 4 | { 5 | //add code here. 6 | for(int i=0; ix && i hm = new HashMap(); 9 | int s=0; 10 | for(int i=0; ihm = new HashMap(); 6 | for(long e: a2){ 7 | if(hm.containsKey(e)){ 8 | hm.put(e, hm.get(e)+1); 9 | }else{ 10 | hm.put(e,1l); 11 | } 12 | } 13 | for(long e: a1){ 14 | if(hm.containsKey(e)){ 15 | hm.put(e, hm.get(e)-1); 16 | } 17 | } 18 | int flag = 0; 19 | for(long x: hm.values()){ 20 | if(x>=1){ 21 | flag=1; 22 | break; 23 | } 24 | } 25 | if(flag==1)return "No"; 26 | return "Yes"; 27 | } 28 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Three_way_partitioning.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/three-way-partitioning/1# 2 | 3 | 4 | /***************************************************************************************************** 5 | Concept used Dutch National Flag Algo because we need to sort the array according to three conditions 6 | 7 | 1) elem < a -----> should come first (we can assume this as 0's) 8 | 2) elem >= a && elem <= b -----> should come second (we can assume this as 1's) 9 | 3) elem > b -----> should comr third (we can assume this as 2's) 10 | 11 | *****************************************************************************************************/ 12 | 13 | 14 | 15 | class Solution{ 16 | //Function to partition the array around the range such 17 | //that array is divided into three parts. 18 | public void threeWayPartition(int arr[], int a, int b) 19 | { 20 | // code here 21 | int low = 0, mid=0, high = arr.length-1; 22 | while(mid<=high){ 23 | if(arr[mid]=a && arr[mid]<=b){ 30 | mid++; 31 | }else{ 32 | int t = arr[mid]; 33 | arr[mid] = arr[high]; 34 | arr[high] = t; 35 | high--; 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/Trapping_Rainwater.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/trapping-rain-water-1587115621/1 2 | 3 | class Solution{ 4 | 5 | static int trappingWater(int a[], int n) { 6 | 7 | // Your code here 8 | int l[] = new int[n]; 9 | int r[] = new int[n]; 10 | l[0] = a[0]; 11 | for(int i=1; i=0; i--){ 16 | r[i] = Math.max(r[i+1], a[i]); 17 | } 18 | int ans = 0; 19 | for(int i=0; i set = new HashSet<>(); 8 | for(int i=0; i hm = new HashMap(); 10 | for(int i=0; i(n/k))c++; 20 | } 21 | return c; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Array/subarray_having_prod_less_than_k.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-the-subarrays-having-product-less-than-k1708/1/# 2 | 3 | class Solution { 4 | 5 | public int countSubArrayProductLessThanK(long a[], long n, long k) 6 | { 7 | int ans=0; 8 | if(k<=1)return 0; 9 | long prod=1; int left=0,right=0; 10 | while(right=k){ 13 | prod/=a[left++]; 14 | } 15 | ans += right-left+1; 16 | right++; 17 | } 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Balanced_Binary_tree.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-for-balanced-tree/1 2 | 3 | class Tree 4 | { 5 | 6 | //Function to check whether a binary tree is balanced or not. 7 | boolean isBalanced(Node root) 8 | { 9 | // Your code here 10 | if(height(root)!=-1) 11 | return true; 12 | else 13 | return false; 14 | } 15 | int height(Node root){ 16 | if(root==null)return 0; 17 | int lh = height(root.left); 18 | if(lh==-1)return -1; 19 | int rh = height(root.right); 20 | if(rh==-1)return -1; 21 | if(Math.abs(lh-rh)>1)return -1; 22 | return 1+Math.max(lh,rh); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Boundary_Traversal.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/boundary-traversal-of-binary-tree/1 2 | 3 | class Solution 4 | { 5 | ArrayList boundary(Node node) 6 | { 7 | ArrayListans = new ArrayList<>(); 8 | if(isLeaf(node)==false)ans.add(node.data); 9 | addLeftBoundary(node, ans); 10 | addLeaves(node, ans); 11 | addRightBoundary(node, ans); 12 | return ans; 13 | } 14 | boolean isLeaf(Node root){ 15 | if(root.left==null && root.right==null)return true; 16 | else return false; 17 | } 18 | void addLeftBoundary(Node root, ArrayListres){ 19 | Node cur = root.left; 20 | while(cur!=null){ 21 | if(isLeaf(cur)==false)res.add(cur.data); 22 | if(cur.left!=null)cur = cur.left; 23 | else cur = cur.right; 24 | } 25 | } 26 | void addLeaves(Node root, ArrayListres){ 27 | if(isLeaf(root)){ 28 | res.add(root.data); 29 | return; 30 | } 31 | if(root.left!=null)addLeaves(root.left, res); 32 | if(root.right!=null)addLeaves(root.right, res); 33 | } 34 | void addRightBoundary(Node root, ArrayListres){ 35 | Node cur = root.right; 36 | ArrayListtemp = new ArrayList<>(); 37 | while(cur!=null){ 38 | if(isLeaf(cur)==false)temp.add(cur.data); 39 | if(cur.right!=null)cur = cur.right; 40 | else cur = cur.left; 41 | } 42 | for(int i=temp.size()-1; i>=0; i--){ 43 | res.add(temp.get(i)); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Diagonal_Traversal.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/diagonal-traversal-of-binary-tree/1/ 2 | 3 | class Tree 4 | { 5 | public ArrayList diagonal(Node root) 6 | { 7 | //add your code here. 8 | ArrayListans = new ArrayList<>(); 9 | Queueq = new LinkedList<>(); 10 | q.add(root); 11 | while(!q.isEmpty()){ 12 | Node temp = q.remove(); 13 | while(temp!=null){ 14 | if(temp.left!=null)q.add(temp.left); 15 | ans.add(temp.data); 16 | temp = temp.right; 17 | } 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Diameter_of_a_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/diameter-of-binary-tree/1 2 | 3 | class Solution { 4 | // Function to return the diameter of a Binary Tree. 5 | int max = 0; 6 | int diameter(Node root) { 7 | // Your code here 8 | maxDepth(root); 9 | return max+1; 10 | } 11 | public int maxDepth(Node root){ 12 | if(root==null)return 0; 13 | int lh = maxDepth(root.left); 14 | int rh = maxDepth(root.right); 15 | max = Math.max(max, (lh+rh)); 16 | return 1+Math.max(lh,rh); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Height_of_binary_tree.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1 2 | 3 | class Solution { 4 | //Function to find the height of a binary tree. 5 | int height(Node root) 6 | { 7 | // code here 8 | if(root==null)return 0; 9 | int lh = height(root.left); 10 | int rh = height(root.right); 11 | return 1+Math.max(lh, rh); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Invert_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/invert-binary-tree/ 2 | 3 | class Solution { 4 | public TreeNode invertTree(TreeNode root) { 5 | if(root==null)return root; 6 | TreeNode left = invertTree(root.left); 7 | TreeNode right = invertTree(root.right); 8 | root.left = right; 9 | root.right = left; 10 | return root; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Left_view_of_binary_tree.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/left-view-of-binary-tree/1 2 | 3 | class Tree 4 | { 5 | //Function to return list containing elements of left view of binary tree. 6 | ArrayList leftView(Node root) 7 | { 8 | // Your code here() 9 | ArrayList ans = new ArrayList<>(); 10 | printleft(root, ans, 0); 11 | return ans; 12 | 13 | } 14 | void printleft(Node root, ArrayList al, int level){ 15 | if(root == null)return; 16 | if(al.size()==level)al.add(root.data); 17 | printleft(root.left, al,level+1); 18 | printleft(root.right, al,level+1); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/LevelOrderTraversal.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/level-order-traversal/1 2 | 3 | class Solution 4 | { 5 | //Function to return the level order traversal of a tree. 6 | static ArrayList levelOrder(Node root) 7 | { 8 | // Your code here 9 | ArrayList inorder = new ArrayList<>(); 10 | Queue q = new LinkedList(); 11 | if(root==null) return inorder; 12 | q.offer(root); 13 | while(!q.isEmpty()){ 14 | root = q.poll(); 15 | inorder.add(root.data); 16 | if(root.left!=null) 17 | q.offer(root.left); 18 | if(root.right!=null) 19 | q.offer(root.right); 20 | } 21 | return inorder; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Minimum_Depth_of_Binary_Tree.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/minimum-depth-of-binary-tree/ 2 | 3 | class Solution { 4 | public int minDepth(TreeNode root) { 5 | if(root==null) return 0; 6 | if(root.left==null && root.right==null) return 1; 7 | if(root.left==null) return 1+minDepth(root.right); 8 | if(root.right==null) return 1+minDepth(root.left); 9 | int lh = minDepth(root.left); 10 | int rh = minDepth(root.right); 11 | return 1+Math.min(lh,rh); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Mirror_Tree.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/mirror-tree/1 2 | 3 | class Solution { 4 | // Function to convert a binary tree into its mirror tree. 5 | void mirror(Node node) { 6 | // Your code here 7 | if(node==null)return; 8 | Node t = node.left; 9 | node.left = node.right; 10 | node.right = t; 11 | mirror(node.left); 12 | mirror(node.right); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/ReverseLevelOrderTraversal.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-level-order-traversal/1# 2 | 3 | class Tree 4 | { 5 | public ArrayList reverseLevelOrder(Node root) 6 | { 7 | // code here 8 | ArrayListorg = new ArrayList<>(); 9 | ArrayListreverse = new ArrayList<>(); 10 | Queueq = new LinkedList(); 11 | if(root==null)return reverse; 12 | q.offer(root); 13 | while(!q.isEmpty()){ 14 | int level = q.size(); 15 | for(int i=0; i=0; i--){ 22 | reverse.add(org.get(i)); 23 | } 24 | return reverse; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Binary Trees/Right_View.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/right-view-of-binary-tree/1 2 | 3 | class Solution{ 4 | //Function to return list containing elements of right view of binary tree. 5 | ArrayList rightView(Node node) { 6 | //add code here. 7 | ArrayListans = new ArrayList<>(); 8 | right(node, ans, 0); 9 | return ans; 10 | } 11 | void right(Node root, ArrayListans, int level){ 12 | if(root==null)return; 13 | if(ans.size()==level) 14 | ans.add(root.data); 15 | right(root.right,ans,level+1); 16 | right(root.left,ans,level+1); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Dynamic Programming/CoinChange.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/coin-change2448/1 2 | 3 | class Solution 4 | { 5 | public long count(int a[], int n, int w) 6 | { 7 | long dp[][] = new long[n+1][w+1]; 8 | for(int i=0; ij){ 21 | dp[i][j] = dp[i-1][j]; 22 | } 23 | } 24 | } 25 | return(dp[n][w]); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Dynamic Programming/Edit_Distance.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/edit-distance3702/1 2 | 3 | class Solution { 4 | public int editDistance(String a, String b) { 5 | // Code here 6 | int m = a.length(); 7 | int n = b.length(); 8 | int editDP = editDP(a,b,m,n); 9 | return editDP; 10 | } 11 | public static int editDP(String a, String b, int m, int n){ 12 | int dp[][] = new int[m+1][n+1]; 13 | for(int i=0; i=0; c--){ 13 | for(int r=0; rn)return 0; 10 | else{ 11 | dp = new long[n+1][r+1]; 12 | return (int)helper(dp,n,r); 13 | } 14 | } 15 | static long helper(long dp[][], int n, int r){ 16 | if(r>n)return 0; 17 | if(r==0)return 1; 18 | if(n==r)return 1; 19 | if(dp[n][r]!=0)return dp[n][r]; 20 | else{ 21 | return dp[n][r] = (helper(dp, n-1, r-1)%mod+helper(dp, n-1,r)%mod)%mod; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DSA CRACKER 450/LinkedList/Check_Palindrome.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-linked-list-is-pallindrome/1 2 | 3 | class Solution 4 | { 5 | //Function to check whether the list is palindrome. 6 | boolean isPalindrome(Node head) 7 | { 8 | //Your code here 9 | if(head==null)return true; 10 | Node mid = middle(head); 11 | Node last = reverse(mid); 12 | Node curr = head; 13 | while(last!=null){ 14 | if(last.data!=curr.data) 15 | return false; 16 | last = last.next; 17 | curr = curr.next; 18 | } 19 | return true; 20 | } 21 | Node middle(Node head){ 22 | Node slow = head; 23 | Node fast = head; 24 | while(fast!=null && fast.next!=null){ 25 | slow = slow.next; 26 | fast = fast.next.next; 27 | } 28 | return slow; 29 | } 30 | Node reverse(Node head){ 31 | Node curr = head; 32 | Node prev = null; 33 | while(curr!=null){ 34 | Node temp = curr.next; 35 | curr.next = prev; 36 | prev = curr; 37 | curr = temp; 38 | } 39 | return prev; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /DSA CRACKER 450/LinkedList/Detect_loop_in_linkedlist.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/detect-loop-in-linked-list/1# 2 | 3 | class Solution { 4 | //Function to check if the linked list has a loop. 5 | public static boolean detectLoop(Node head){ 6 | // Add code here 7 | Node slow = head; 8 | Node fast = head; 9 | while(fast!=null && fast.next!=null){ 10 | slow = slow.next; 11 | fast = fast.next.next; 12 | if(slow==fast){ 13 | return true; 14 | } 15 | } 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DSA CRACKER 450/LinkedList/Intersection_point.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/1 2 | 3 | class Intersect 4 | { 5 | //Function to find intersection point in Y shaped Linked Lists. 6 | int intersectPoint(Node head1, Node head2) 7 | { 8 | // code here 9 | Node a = head1; 10 | Node b = head2; 11 | if(a==null || b==null)return -1; 12 | while(a!=b){ 13 | a = a==null? head2: a.next; 14 | b = b==null? head1: b.next; 15 | } 16 | return a.data; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DSA CRACKER 450/LinkedList/Intersection_point_of_sorted_list.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/intersection-of-two-sorted-linked-lists/1 2 | 3 | class Sol 4 | { 5 | public static Node findIntersection(Node head1, Node head2) 6 | { 7 | // code here. 8 | Node a = head1; 9 | Node b = head2; 10 | Node head = new Node(0); 11 | Node res = head; 12 | while(a!=null && b!=null){ 13 | if(a.data!=b.data){ 14 | if(a.datahs = new HashSet<>(); 10 | Node curr=head; 11 | Node prev=null; 12 | while(curr!=null){ 13 | if(hs.contains(curr.data)){ 14 | prev.next = curr.next; 15 | }else{ 16 | hs.add(curr.data); 17 | prev = curr; 18 | } 19 | curr = curr.next; 20 | } 21 | return head; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DSA CRACKER 450/LinkedList/Reverse_a_linkedlist.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-a-linked-list/1 2 | 3 | class Solution 4 | { 5 | //Function to reverse a linked list. 6 | Node reverseList(Node head) 7 | { 8 | // code here 9 | if(head==null || head.next==null)return head; 10 | Node prev = null; 11 | Node curr = head; 12 | Node forw = null; 13 | while(curr!=null){ 14 | forw = curr.next; 15 | curr.next = prev; 16 | prev = curr; 17 | curr = forw; 18 | } 19 | return prev; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DSA CRACKER 450/LinkedList/Reverse_in_a_group_of_k.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-a-linked-list-in-groups-of-given-size/1 2 | 3 | class Solution 4 | { 5 | public static Node reverse(Node head, int k) 6 | { 7 | //Your code here 8 | Node nextt = null; 9 | Node prev = null; 10 | Node curr = head; 11 | int c=0; 12 | while(curr!=null && c=0){ 6 | if(m[i][j]==target) return true; 7 | else if(m[i][j]>target){ 8 | j--; 9 | }else{ 10 | i++; 11 | } 12 | } 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Matrix/Sorted_matrix.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sorted-matrix2333/1 2 | 3 | class Solution { 4 | int[][] sortedMatrix(int n, int a[][]) { 5 | // code here 6 | ArrayListal = new ArrayList<>(); 7 | for(int i=0; i spirallyTraverse(int a[][], int r, int c) 7 | { 8 | // code here 9 | ArrayList ans = new ArrayList<>(); 10 | int k = 0, l=0; 11 | while(k=l; i--){ 29 | ans.add(a[r-1][i]); 30 | } 31 | r--; 32 | } 33 | 34 | // Print the first rem col 35 | if(l=k; i--){ 37 | ans.add(a[i][l]); 38 | } 39 | l++; 40 | } 41 | } 42 | return ans; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Aggressive_Cows.java: -------------------------------------------------------------------------------- 1 | // https://www.spoj.com/problems/AGGRCOW/ 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | 6 | class Main 7 | { 8 | public static void main (String[] args) throws java.lang.Exception 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | int t = sc.nextInt(); 12 | while(t--!=0){ 13 | int n = sc.nextInt(); 14 | int cows = sc.nextInt(); 15 | int a[] = new int[n]; 16 | for(int i=0; i=dist){ 39 | cow1 = a[i]; 40 | count++; 41 | } 42 | if(cows==count)return true; 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Allocate_min_number_of_pages.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages0937/1# 2 | 3 | class Solution 4 | { 5 | //Function to find minimum number of pages. 6 | public static int findPages(int[]a,int n,int m) 7 | { 8 | if(n mid){ 35 | student++; 36 | pages = a[i]; 37 | } 38 | else{ 39 | pages+=a[i]; 40 | } 41 | } 42 | return (student<=m); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Arithmitic_numbers.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/arithmetic-number2815/1 2 | 3 | class Solution{ 4 | static int inSequence(int A, int B, int C){ 5 | // code here 6 | if(C==0){ 7 | if(B==A)return 1; 8 | return 0; 9 | } 10 | int temp=(B-A)/C + 1; 11 | double d = (double)(B-A)/C+1; 12 | int check = (int) d; 13 | if(temp>0 && (check==d)) 14 | { 15 | return 1; 16 | } 17 | return 0; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Count_squares.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-squares3649/1 2 | 3 | class Solution { 4 | static int countSquares(int a) { 5 | long x= a-1; 6 | long start=1, end=x, ans=0; 7 | if(x<=1)return (int)0; 8 | 9 | while(start<=end){ 10 | long mid = (start+end)/2; 11 | 12 | if(mid*mid==x)return (int)mid; 13 | else if(mid*mid= sum)j--; 13 | else{ 14 | c += j-l; 15 | l++; 16 | } 17 | } 18 | } 19 | return c; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Count_zero_sum_subarrays.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/zero-sum-subarrays1825/1# 2 | 3 | class Solution{ 4 | //Function to count subarrays with sum equal to 0. 5 | public static long findSubarray(long[] a ,int n) 6 | { 7 | //Your code here 8 | HashMap hm = new HashMap<>(); 9 | long prefix=0, count=0; 10 | for(int i=0; i find(long a[], int n, int x) 6 | { 7 | // code here 8 | ArrayList ans = new ArrayList<>(); 9 | int start = 0, end = n-1; 10 | long first=-1, last=-1; 11 | 12 | // This Binary Search will find the first Occurrence of x 13 | while(start<=end){ 14 | int mid = start+(end-start)/2; 15 | if(x==a[mid]){ 16 | first = mid; 17 | end = mid-1; 18 | }else if(x>a[mid]){ 19 | start = mid+1; 20 | }else{ 21 | end = mid-1; 22 | } 23 | } 24 | 25 | // This Binary Search will find the last Occurrence of x 26 | start = 0; end = n-1; 27 | while(start<=end){ 28 | int mid = start+(end-start)/2; 29 | if(x==a[mid]){ 30 | last = mid; 31 | start = mid+1; 32 | }else if(x>a[mid]){ 33 | start = mid+1; 34 | }else{ 35 | end = mid-1; 36 | } 37 | } 38 | ans.add(first); 39 | ans.add(last); 40 | return ans; 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Majority_element.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/majority-element-1587115620/1 2 | 3 | class Solution 4 | { 5 | static int majorityElement(int a[], int size) 6 | { 7 | // MOORE'S VOTING ALGORITHM 8 | int elem=-1, count=0; 9 | for(int e: a){ 10 | if(count==0){ 11 | elem = e; 12 | } 13 | if(elem == e){ 14 | count++; 15 | }else { 16 | count--; 17 | } 18 | } 19 | 20 | // counting elem in array 21 | int c=0; 22 | for(int x: a){ 23 | if(elem==x)c++; 24 | } 25 | if(c>size/2)return elem; 26 | else return -1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Merge_2_sorted_arrays.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays5135/1 2 | 3 | class Solution { 4 | 5 | public void merge(int arr1[], int arr2[], int n, int m) { 6 | // code here 7 | int i=n-1, j=0; 8 | while(i>=0 && jarr2[j]){ 10 | int temp=arr1[i]; 11 | arr1[i]=arr2[j]; 12 | arr2[j]=temp; 13 | } 14 | i--;j++; 15 | } 16 | Arrays.sort(arr1); 17 | Arrays.sort(arr2); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Middle_of_three.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/middle-of-three2926/1 2 | 3 | class Solution{ 4 | int middle(int A, int B, int C){ 5 | //code here 6 | if(B>=A && B<=C || B<=A && B>=C)return B; 7 | else if(A>=B && A<=C || A<=B && A>=C)return A; 8 | else return C; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Minimum_swaps_to_sort.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimum-swaps/1# 2 | 3 | class Solution 4 | { 5 | //Function to find the minimum number of swaps required to sort the array. 6 | public int minSwaps(int a[]) 7 | { 8 | // Code here 9 | int n = a.length; 10 | HashMaphm = new HashMap<>(); 11 | for(int i=0; i0){ 29 | ans+=cycle-1; 30 | } 31 | } 32 | return ans; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Painter's_Partition.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/the-painters-partition-problem1535/1 2 | 3 | /* SOLUTION IS SAME AS ALLOCATE MINIMUM NUMBER OF PAGES */ 4 | 5 | class Solution{ 6 | static long minTime(int[] a,int n,int k){ 7 | long max = Long.MIN_VALUE, s=0; 8 | for(int i=0; imid){ 29 | painter++; 30 | board = a[i]; 31 | }else{ 32 | board+=a[i]; 33 | } 34 | } 35 | return (painter<=k); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Pair_of_given_difference.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/find-pair-given-difference1559/1 2 | 3 | class Solution 4 | { 5 | public boolean findPair(int arr[], int l, int n) 6 | { 7 | //code here. 8 | Arrays.sort(arr); 9 | int i=0, j=1; 10 | while(i n) 13 | i++; 14 | else j++; 15 | } 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Product_array_puzzle.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/product-array-puzzle4525/1 2 | 3 | class Solution 4 | { 5 | public static long[] productExceptSelf(int a[], int n) 6 | { 7 | // code here 8 | long p[] = new long[n]; 9 | long prod=1; 10 | Arrays.fill(p,1); 11 | for(int i=0; i=0; i--){ 17 | p[i]*=prod; 18 | prod*=a[i]; 19 | } 20 | return p; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Sort_by_set_bit.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/sort-by-set-bit-count1153/1 2 | 3 | class Compute 4 | { 5 | static void sortBySetBitCount(Integer arr[], int n) 6 | { 7 | // Your code goes here 8 | Arrays.sort(arr, (a,b) -> Integer.compare(Integer.bitCount(b), Integer.bitCount(a))); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Searching & Sorting/Stickler_thief.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/stickler-theif-1587115621/1# 2 | 3 | class Solution 4 | { 5 | //Function to find the maximum money the thief can get. 6 | public int FindMaxSum(int a[], int n) 7 | { 8 | // Your code here 9 | if(n==1)return a[0]; 10 | int dp[] = new int[n]; 11 | dp[0] = a[0]; 12 | dp[1] = Math.max(a[0], a[1]); 13 | for(int i=2; i valueEqualToIndex(int a[], int n) { 5 | // code here 6 | ArrayList ans = new ArrayList<>(); 7 | for(int i=0; is = new Stack<>(); 10 | for(int i=0; ihm = new HashMap<>(); 7 | int capacity; 8 | public LRUCache(int capacity) { 9 | this.capacity = capacity; 10 | head.next = tail; 11 | tail.prev = head; 12 | } 13 | 14 | public int get(int key) { 15 | if(hm.containsKey(key)){ 16 | Node node = hm.get(key); 17 | remove(node); 18 | insert(node); 19 | return node.value; 20 | }else{ 21 | return -1; 22 | } 23 | } 24 | 25 | public void set(int key, int value) { 26 | if(hm.containsKey(key)){ 27 | remove(hm.get(key)); 28 | } 29 | if(hm.size()==capacity){ 30 | remove(tail.prev); 31 | } 32 | insert(new Node(key, value)); 33 | } 34 | 35 | private void remove(Node node){ 36 | hm.remove(node.key); 37 | node.prev.next = node.next; 38 | node.next.prev = node.prev; 39 | } 40 | 41 | private void insert(Node node){ 42 | hm.put(node.key,node); 43 | Node headNext = head.next; 44 | head.next = node; 45 | node.prev = head; 46 | headNext.prev = node; 47 | node.next = headNext; 48 | } 49 | 50 | class Node{ 51 | int key, value; 52 | Node next, prev; 53 | Node(int _key, int _value){ 54 | key = _key; 55 | value = _value; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Stacks & Queues/Max_Area_Histogram.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram-1587115620/1 2 | 3 | class Solution 4 | { 5 | public static long getMaxArea(long a[], long n) 6 | { 7 | Stack s = new Stack<>(); 8 | s.push(-1); 9 | long maxarea = a[0]; 10 | long right_small[] = new long[a.length]; 11 | long left_small[] = new long[a.length]; 12 | for(int i=0; i<(int)n; i++){ 13 | left_small[i] = -1; 14 | right_small[i] = n; 15 | } 16 | int i=0; 17 | while(i0&&a[i]==a[(i-1)]){ 23 | left_small[i] = left_small[(int)(i-1)]; 24 | }else{ 25 | left_small[i] = s.peek(); 26 | } 27 | s.push(i); 28 | i++; 29 | } 30 | for(int j=0; j<(int)n; j++){ 31 | maxarea = Math.max(maxarea, a[j]*(right_small[j]-left_small[j]-1)); 32 | } 33 | return maxarea; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Stacks & Queues/NextGreaterElement.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/next-larger-element-1587115620/1 2 | /* 3 | [1,3,2,4] 4 | <---- Traverse from back and check for the condition that is the current elem is greater or smaller if a[i]>s.peek() push it to stack 5 | else pop() till you get the next greater in the stack. 6 | */ 7 | 8 | class Solution 9 | { 10 | //Function to find the next greater element for each element of the array. 11 | public static long[] nextLargerElement(long[] a, int n) 12 | { 13 | // Your code here 14 | Stacks = new Stack<>(); 15 | ArrayListans = new ArrayList<>(); 16 | long l[] = new long[n]; 17 | // s.push(a[n-1]); 18 | // ans.add(-1l); 19 | for(int i=n-1; i>=0; i--){ 20 | if(s.size()==0){ 21 | ans.add(-1l); 22 | }else if(s.size()>0 && s.peek()>a[i]){ 23 | ans.add(s.peek()); 24 | }else if(s.size()>0 && s.peek()<=a[i]){ 25 | while(s.size()>0 && s.peek()<=a[i]){ 26 | s.pop(); 27 | } 28 | if(s.size()==0){ 29 | ans.add(-1l); 30 | }else{ 31 | ans.add(s.peek()); 32 | } 33 | } 34 | s.push(a[i]); 35 | } 36 | int ind=0; 37 | for(int i=ans.size()-1; i>=0; i--){ 38 | l[ind++] = ans.get(i); 39 | } 40 | return l; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Stacks & Queues/Queue_Reversal_using_Recursion.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/queue-reversal/1 2 | 3 | class GfG{ 4 | //Function to reverse the queue. 5 | public Queue rev(Queue q){ 6 | //add code here. 7 | if(q.isEmpty()){ 8 | return q; 9 | }else{ 10 | int front = q.peek(); 11 | q.poll(); 12 | q = rev(q); 13 | q.offer(front); 14 | return q; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Stacks & Queues/Reverse_a_stack_using_recursion.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/reverse-a-stack/1 2 | 3 | class Solution 4 | { 5 | static void insert_bottom(Stacks, int n){ 6 | if(s.isEmpty()){ 7 | s.push(n); 8 | return; 9 | } 10 | int top = s.pop(); 11 | insert_bottom(s,n); 12 | 13 | s.push(top); 14 | } 15 | static void reverse(Stack s) 16 | { 17 | // add your code here 18 | if(s.isEmpty()) 19 | return; 20 | int top = s.pop(); 21 | reverse(s); 22 | insert_bottom(s,top); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Stacks & Queues/Stack_Permutation.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/stack-permutations/1 2 | 3 | class Solution { 4 | public static int isStackPermutation(int N, int[] A, int[] B) { 5 | // code here 6 | Stack stack = new Stack<>(); 7 | Queue queue = new LinkedList<>(); 8 | 9 | for(int i=0;i sort(Stack s) 5 | { 6 | //add code here 7 | if(!s.isEmpty()){ 8 | int x = s.pop(); 9 | sort(s); 10 | sortinsert(s,x); 11 | } 12 | return s; 13 | } 14 | public void sortinsert(Stacks, int x){ 15 | if(s.isEmpty() || s.peek() q1 = new LinkedList(); 6 | Queue q2 = new LinkedList(); 7 | 8 | //Function to push an element into stack using two queues. 9 | void push(int a) 10 | { 11 | // Your code here 12 | q2.offer(a); 13 | while(!q1.isEmpty()){ 14 | q2.offer(q1.peek()); 15 | q1.poll(); 16 | } 17 | Queueq = q1; 18 | q1 = q2; 19 | q2 = q; 20 | } 21 | 22 | //Function to pop an element from stack using two queues. 23 | int pop() 24 | { 25 | // Your code here 26 | if(q1.isEmpty()){ 27 | return -1; 28 | } 29 | return q1.poll(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /DSA CRACKER 450/Stacks & Queues/TheCelebrityProblem.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/the-celebrity-problem/1 2 | 3 | class Solution 4 | { 5 | boolean knows(int m[][], int a,int b){ 6 | if(m[a][b]==1)return true; 7 | else return false; 8 | } 9 | 10 | int celebrity(int m[][], int n) 11 | { 12 | // code here 13 | Stacks = new Stack<>(); 14 | for(int i=0; i1){ 18 | int a = s.peek(); 19 | s.pop(); 20 | 21 | int b = s.peek(); 22 | s.pop(); 23 | 24 | if(knows(m,a,b)==true) // if a knows b the push b in stack 25 | s.push(b); 26 | 27 | else 28 | s.push(a); 29 | } 30 | 31 | int ans = s.peek(); 32 | int countzero=0,countone=0; 33 | for(int i=0; is = new Stack<>(); 8 | s.push(-1); 9 | for(int i=0; ij)return 0l; 14 | else if(i == j)return 1l; 15 | else if(dp[i][j]!=-1l)return dp[i][j]%1000000007; 16 | else if(s.charAt(i)== s.charAt(j)) return dp[i][j] = (long)solve(dp, i+1, j, s) + (long)solve(dp, i, j-1, s)+1l; 17 | else return dp[i][j] = (long)solve(dp, i+1, j, s) + (long)solve(dp, i, j-1, s)-(long)solve(dp, i+1, j-1, s); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Count_the_reversals.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/count-the-reversals0401/1 2 | 3 | class Sol 4 | { 5 | int countRev (String s) 6 | { 7 | // your code here 8 | Stackst = new Stack<>(); 9 | int l = s.length(); 10 | 11 | if(l%2!=0) 12 | return -1; // if length is odd 13 | 14 | // looping to get rid of the balanced brackets pair 15 | for(int i=0; i> Anagrams(String[] s) { 5 | 6 | // Your Code here 7 | Map> map = new HashMap<>(); 8 | String org=""; 9 | for(String e : s){ 10 | org = e; 11 | char c[] = e.toCharArray(); 12 | Arrays.sort(c); 13 | String sorted = Arrays.toString(c); 14 | if(!map.containsKey(sorted)){ 15 | map.put(sorted, new LinkedList()); 16 | } 17 | map.get(sorted).add(org); 18 | } 19 | return new LinkedList<>(map.values()); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Isomorphic_strings.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/isomorphic-strings-1587115620/1# 2 | 3 | class Solution 4 | { 5 | //Function to check if two strings are isomorphic. 6 | public static boolean areIsomorphic(String s1,String s2) 7 | { 8 | // Your code here 9 | int n = s1.length(); 10 | int m = s2.length(); 11 | if(n!=m)return false; 12 | int a1[] = new int[256]; 13 | int a2[] = new int[256]; 14 | for(int i=0; i=0 && high < len && s.charAt(low)==s.charAt(high)){ 21 | --low; 22 | ++high; 23 | } 24 | ++low; --high; 25 | while(s.charAt(low)==s.charAt(high) && high-low+1>maxlength){ 26 | start = low; 27 | maxlength = high-low+1; 28 | } 29 | 30 | // for odd length of string 31 | low = i-1; 32 | high = i+1; 33 | while(low>=0 && high < len && s.charAt(low)==s.charAt(high)){ 34 | --low; 35 | ++high; 36 | } 37 | ++low; --high; 38 | while(s.charAt(low)==s.charAt(high) && high-low+1>maxlength){ 39 | start = low; 40 | maxlength = high-low+1; 41 | } 42 | } 43 | return s.substring(start, start+maxlength); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Longest_repeating_subsequence.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/longest-repeating-subsequence2004/1 2 | 3 | class Solution 4 | { 5 | public int LongestRepeatingSubsequence(String s) 6 | { 7 | // code here 8 | int n = s.length(); 9 | int dp[][] = new int[n+1][n+1]; 10 | for(int i=0; i catch 18 | dp[i][j] = 1+dp[i-1][j-1]; 19 | }else{ 20 | dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); 21 | } 22 | } 23 | } 24 | return dp[n][n]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Minimum_swaps.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimum-swaps-for-bracket-balancing2704/1 2 | 3 | class Solution{ 4 | static int minimumNumberOfSwaps(String s){ 5 | int op=0, cl=0, swap=0, imbalance=0; 6 | for(int i=0; i0){ 11 | swap+=imbalance; 12 | imbalance--; 13 | } 14 | }else if(c==']'){ 15 | cl++; 16 | imbalance = cl-op; 17 | } 18 | } 19 | return swap; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Mobile_keyboard_equivalent.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/convert-a-sentence-into-its-equivalent-mobile-numeric-keypad-sequence0547/1 2 | 3 | class Solution 4 | { 5 | String printSequence(String S) 6 | { 7 | // code here 8 | S = S.toUpperCase(); 9 | String keys[] = {"2","22","222","3","33","333","4","44","444","5","55","555","6","66","666", 10 | "7","77","777","7777","8","88","888","9","99","999","9999"}; 11 | String ans = ""; 12 | for(int i=0; i nextPermutation(int n, int a[]){ 5 | // code here 6 | ArrayListal = new ArrayList<>(); 7 | if(n<=0 || a==null)return al; 8 | 9 | int i= n-2; 10 | while(i>=0 && a[i]>=a[i+1])i--; 11 | int j= n-1; 12 | if(i>=0){ 13 | while(a[j]<=a[i]){ 14 | j--; 15 | } 16 | swap(a, i, j); 17 | } 18 | reverse(a, i+1, n-1); 19 | for(int e:a){ 20 | al.add(e); 21 | } 22 | return al; 23 | } 24 | 25 | public static void swap(int a[], int i, int j){ 26 | int t = a[i]; 27 | a[i] = a[j]; 28 | a[j] = t; 29 | } 30 | public static void reverse(int a[], int i, int j){ 31 | while(i=0; i--){ 8 | t += s.charAt(i); 9 | } 10 | if(t.equals(s))return 1; 11 | else return 0; 12 | } 13 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Parenthesis_Checker.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/parenthesis-checker2744/1 2 | 3 | class Solution 4 | { 5 | //Function to check if brackets are balanced or not. 6 | static boolean ispar(String s) 7 | { 8 | // add your code here 9 | int n= s.length(); 10 | if(n%2!=0)return false; 11 | 12 | Stackst = new Stack<>(); 13 | boolean isBalanced=true; 14 | for(int i=0; i (n/2)+(n%2)){ 27 | System.out.println("0"); 28 | }else{ 29 | System.out.println("1"); 30 | } 31 | } 32 | } 33 | public static char getcharmax(int freq[]){ 34 | int max = -1; 35 | char maxchar=' '; 36 | for(int i=0; i<26; i++){ 37 | if(max st = new Stack<>(); 6 | int n = s.length(); 7 | String ans=""; 8 | for(int i=0; i=val2){ 11 | ans += val1; 12 | }else{ 13 | ans = ans+ val2-val1; 14 | i++; 15 | } 16 | } 17 | else ans+=val1; 18 | } 19 | return ans; 20 | } 21 | public int value(char c){ 22 | if(c=='I')return 1; 23 | else if(c=='V')return 5; 24 | else if(c=='X')return 10; 25 | else if(c=='L')return 50; 26 | else if(c=='C')return 100; 27 | else if(c=='D')return 500; 28 | else if(c=='M')return 1000; 29 | else return -1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Smallest_dictinct_window.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/smallest-distant-window3132/1 2 | 3 | class Solution { 4 | public String findSubString( String str) { 5 | int n = str.length(); 6 | 7 | // if string is empty or having one char 8 | if (n <= 1) 9 | return str; 10 | 11 | // Count all distinct characters. 12 | int dist_count = 0; 13 | 14 | boolean[] visited = new boolean[256]; 15 | Arrays.fill(visited, false); 16 | for (int i = 0; i < n; i++) { 17 | if (visited[str.charAt(i)] == false) { 18 | visited[str.charAt(i)] = true; 19 | dist_count++; 20 | } 21 | } 22 | 23 | int start = 0, start_index = -1; 24 | int min_len = Integer.MAX_VALUE; 25 | 26 | int count = 0; 27 | int[] curr_count = new int[256]; 28 | for (int j = 0; j < n; j++) { 29 | 30 | curr_count[str.charAt(j)]++; 31 | 32 | 33 | if (curr_count[str.charAt(j)] == 1) 34 | count++; 35 | 36 | // if all the characters are matched 37 | if (count == dist_count) { 38 | 39 | while (curr_count[str.charAt(start)] > 1) { 40 | if (curr_count[str.charAt(start)] > 1) 41 | curr_count[str.charAt(start)]--; 42 | start++; 43 | } 44 | 45 | // Update window size 46 | int len_window = j - start + 1; 47 | if (min_len > len_window) { 48 | min_len = len_window; 49 | start_index = start; 50 | } 51 | } 52 | } 53 | 54 | return str.substring(start_index, 55 | start_index + min_len); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Split_binary_string.java: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/split-the-binary-string-into-substrings-with-equal-number-of-0s-and-1s/ 2 | 3 | static int maxSubStr(String str, int n) 4 | { 5 | 6 | int count0 = 0, count1 = 0; 7 | int cnt = 0; 8 | for (int i = 0; i < n; i++) 9 | { 10 | if (str.charAt(i) == '0') 11 | { 12 | count0++; 13 | } 14 | else 15 | { 16 | count1++; 17 | } 18 | if (count0 == count1) 19 | { 20 | cnt++; 21 | } 22 | } 23 | 24 | if (cnt == 0) 25 | { 26 | return -1; 27 | } 28 | return cnt; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/String_rotation.java: -------------------------------------------------------------------------------- 1 | class StringRotation 2 | { 3 | static boolean areRotations(String str1, String str2) 4 | { 5 | // There lengths must be same and str2 must be 6 | // a substring of str1 concatenated with str1. 7 | return (str1.length() == str2.length()) && 8 | ((str1 + str1).indexOf(str2) != -1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Transform_one_string_to_other.java: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/transform-one-string-to-another-using-minimum-number-of-given-operation/ 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | public class GFG { 7 | 8 | // Function to find minimum number of 9 | // operations required to transform 10 | // A to B. 11 | public static int minOps(String A, String B) 12 | { 13 | 14 | // This parts checks whether conversion is 15 | // possible or not 16 | if(A.length() != B.length()) 17 | return -1; 18 | 19 | int i, j, res = 0; 20 | int count [] = new int [256]; 21 | 22 | // count characters in A 23 | 24 | // subtract count for every character in B 25 | for(i = 0; i < A.length(); i++) 26 | { 27 | count[A.charAt(i)]++; 28 | count[B.charAt(i)]--; 29 | } 30 | 31 | // Check if all counts become 0 32 | for(i = 0; i < 256; i++) 33 | if(count[i] != 0) 34 | return -1; 35 | 36 | i = A.length() - 1; 37 | j = B.length() - 1; 38 | 39 | while(i >= 0) 40 | { 41 | // If there is a mismatch, then 42 | // keep incrementing result 'res' 43 | // until B[j] is not found in A[0..i] 44 | if(A.charAt(i) != B.charAt(j)) 45 | res++; 46 | else 47 | j--; 48 | i--; 49 | } 50 | return res; 51 | } 52 | 53 | // Driver code 54 | public static void main(String[] args) 55 | { 56 | String A = "EACBD"; 57 | String B = "EABCD"; 58 | 59 | System.out.println("Minimum number of " 60 | + "operations required is " 61 | + minOps(A, B)); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /DSA CRACKER 450/String/Wildcard_string_matching.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/wildcard-string-matching1126/1# 2 | 3 | class Solution{ 4 | static boolean match(String wild, String pat) 5 | { 6 | // code here 7 | int n = pat.length(); 8 | int m = wild.length(); 9 | boolean dp[][] = new boolean[n+1][m+1]; 10 | for(int i=0; ij)return 1; 20 | if(s.charAt(i)!=s.charAt(j))return dp[i][j]=0; 21 | if(dp[i][j]!=0)return dp[i][j]; 22 | return dp[i][j] = count(dp, s, i+1, j-1); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Dynamic Programming/CoinChange.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/coin-change2448/1# 2 | // Unbounded Knapsack Variation 3 | 4 | class Solution 5 | { 6 | public long count(int a[], int n, int w) 7 | { 8 | long dp[][] = new long[n+1][w+1]; 9 | for(int i=0; ij){ 22 | dp[i][j] = dp[i-1][j]; 23 | } 24 | } 25 | } 26 | return(dp[n][w]); 27 | } 28 | -------------------------------------------------------------------------------- /Dynamic Programming/knapsack.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1# 2 | 3 | class Solution 4 | { 5 | //Function to return max value that can be put in knapsack of capacity W. 6 | static int knapSack(int W, int wt[], int val[], int n) 7 | { 8 | // Initialize 9 | int dp[][] = new int[n+1][W+1]; 10 | for(int i=0; imp = new HashMap(); 9 | 10 | // Traverse through array elements and 11 | // count frequencies 12 | for (int i = 0 ; i < n; i++) 13 | { 14 | if(mp.containsKey(arr[i])) 15 | { 16 | mp.put(arr[i], mp.get(arr[i]) + 1); 17 | } 18 | else 19 | { 20 | mp.put(arr[i], 1); 21 | } 22 | } 23 | 24 | // Traverse through map and print frequencies 25 | for (Map.Entry entry : mp.entrySet()) 27 | System.out.println(entry.getKey() + " " + 28 | entry.getValue()); 29 | } 30 | 31 | // Driver Code 32 | public static void main(String[] args) 33 | { 34 | int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 }; 35 | int n = arr.length; 36 | countFreq(arr, n); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Hash/SubArray_with_0_sum.java: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1/?category[]=sliding-window&problemStatus=unsolved&page=1&query=category[]sliding-windowproblemStatusunsolvedpage1 2 | 3 | static boolean findsum(int a[],int n) 4 | { 5 | //Your code here 6 | HashSeths = new HashSet(); 7 | int sum=0; 8 | for(int i=0; ihm = new HashMap<>(); 9 | for(int i=0; i hma = new HashMap<>(); 4 | HashMap hmb = new HashMap<>(); 5 | for(int i=0; i> Anagrams(String[] s) { 3 | 4 | Map> map = new HashMap<>(); 5 | String org=""; 6 | for(String e : s){ 7 | org = e; 8 | char c[] = e.toCharArray(); 9 | Arrays.sort(c); 10 | String sorted = Arrays.toString(c); 11 | if(!map.containsKey(sorted)){ 12 | map.put(sorted, new LinkedList()); 13 | } 14 | map.get(sorted).add(org); 15 | } 16 | return new LinkedList<>(map.values()); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Linear Search/MaximunSum.java: -------------------------------------------------------------------------------- 1 | //https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/maximum-sum-4-f8d12458/description/ 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStreamReader; 5 | import java.util.*; 6 | 7 | 8 | class TestClass { 9 | public static void main(String args[] ) throws Exception { 10 | Scanner sc = new Scanner(System.in); 11 | int n = sc.nextInt(); 12 | long a[] = new long[n]; 13 | int c=0; long max = Long.MIN_VALUE; long sum=0l; 14 | for(int i=0; i=0){ 19 | sum+=a[i]; 20 | c++; 21 | } 22 | else{ 23 | max = Math.max(max, a[i]); 24 | } 25 | } 26 | if(c>0)System.out.println(sum+" "+c); 27 | else System.out.println(max+" "+1); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Linear Search/ReastInPeace.java: -------------------------------------------------------------------------------- 1 | //https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/rest-in-peace-21-1/description/ 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStreamReader; 5 | import java.util.*; 6 | 7 | class TestClass { 8 | public static void main(String args[] ) throws Exception { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while(t--!=0){ 12 | int n = sc.nextInt(); 13 | if(n%21==0)System.out.println("The streak is broken!"); 14 | else{ 15 | String s = n+""; 16 | if(s.contains("21"))System.out.println("The streak is broken!"); 17 | else System.out.println("The streak lives still in our heart!"); 18 | 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Linear Search/simpleSearch.java: -------------------------------------------------------------------------------- 1 | //https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/simple-search-4/description/ 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStreamReader; 5 | import java.util.Scanner; 6 | import java.util.*; 7 | 8 | class TestClass { 9 | public static void main(String args[] ) throws Exception { 10 | Scanner s = new Scanner(System.in); 11 | int n = s.nextInt(); 12 | ArrayList a = new ArrayList<>(n); 13 | for(int i=0; inext = NULL, then 25 | the Linked list does not contains 26 | any loop. 27 | Your Task: 28 | The task is to complete the function detectloop() which contains reference to the head as only argument. This function should return 1 if linked list contains loop, else return 0. 29 | 30 | Expected Time Complexity: O(N) 31 | Expected Auxiliary Space: O(1) 32 | 33 | Constraints: 34 | 1 <= N <= 104 35 | 1 <= Data on Node <= 10^3*/ 36 | 37 | 38 | 39 | /* Node is defined as 40 | 41 | class Node 42 | { 43 | int data; 44 | Node next; 45 | Node(int d) {data = d; next = null; } 46 | } 47 | 48 | */ 49 | 50 | 51 | //HARE AND TURTLE METHOD 52 | 53 | class Solution { 54 | public static boolean detectLoop(Node head){ 55 | // Add code here 56 | Node HARE = head.next; 57 | Node TURTLE = head; 58 | while(HARE!=TURTLE){ 59 | if(HARE==null||HARE.next==null) 60 | return false; 61 | HARE = HARE.next.next; 62 | TURTLE = TURTLE.next; 63 | } 64 | return true; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Prefix-Sum Algo/CSUMQ.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | 4 | class csumq 5 | { 6 | public static void main (String[] args) throws java.lang.Exception 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int n=0, q=0; 10 | if(sc.hasNext()) 11 | n = sc.nextInt(); 12 | int a[] = new int[n]; 13 | 14 | for(int i=0; i rev(Queue q){ 33 | Stack st = new Stack<>(); 34 | Queue qrev = new LinkedList<>(); 35 | while(!q.isEmpty()){ 36 | int t = q.peek(); 37 | q.poll(); 38 | st.push(t); 39 | } 40 | while(!st.isEmpty()){ 41 | int t = st.peek(); 42 | st.pop(); 43 | qrev.add(t); 44 | } 45 | return qrev; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Recursion/NumberOfPaths.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | long numberOfPaths(int m, int n) { 4 | if(m==1 || n==1)return 1; 5 | 6 | return numberOfPaths(m-1,n)+numberOfPaths(m,n-1); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Replace_0_with_5.java: -------------------------------------------------------------------------------- 1 | 2 | class GfG { 3 | int convertfive(int num) { 4 | String s = Integer.toString(num); 5 | String s1 = s.replace("0","5"); 6 | return(Integer.parseInt(s1)); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Sliding-Window/Distinct_elem_in_every_window.java: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/count-distinct-elements-in-every-window/1/?category[]=sliding-window&problemStatus=unsolved&page=1&query=category[]sliding-windowproblemStatusunsolvedpage1# 2 | 3 | class Solution 4 | { 5 | ArrayList countDistinct(int a[], int n, int k) 6 | { 7 | ArrayListans = new ArrayList<>(); 8 | HashMaphm = new HashMap<>(); 9 | for(int i=0; iq = new LinkedList<>(); 20 | for(int i=0; i a,int n){ 5 | int i=0,j=0,sum=0, max = Integer.MIN_VALUE; 6 | while(j_push(ArrayList arr,int n) 29 | { 30 | //Your code here 31 | Stackst = new Stack<>(); 32 | for(int i=0; is) 41 | { 42 | //Your code here 43 | while(!s.isEmpty()){ 44 | System.out.print(s.pop()+" "); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Stack/Stack_using_Array.java: -------------------------------------------------------------------------------- 1 | /*Write a program to implement a Stack using Array. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack. 2 | 3 | Example 1: 4 | 5 | Input: 6 | push(2) 7 | push(3) 8 | pop() 9 | push(4) 10 | pop() 11 | Output: 3, 4 12 | Explanation: 13 | push(2) the stack will be {2} 14 | push(3) the stack will be {2 3} 15 | pop() poped element will be 3, 16 | the stack will be {2} 17 | push(4) the stack will be {2 4} 18 | pop() poped element will be 4 19 | Example 2: 20 | 21 | Input: 22 | pop() 23 | push(4) 24 | push(5) 25 | pop() 26 | Output: -1, 5 27 | Your Task: 28 | You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method. 29 | 30 | Expected Time Complexity : O(1) for both push() and pop(). 31 | Expected Auixilliary Space : O(1) for both push() and pop(). 32 | 33 | Constraints: 34 | 1 <= Q <= 100 35 | 1 <= x <= 100*/ 36 | 37 | class MyStack{ 38 | 39 | int top; 40 | int arr[] = new int[1000]; 41 | 42 | MyStack() 43 | { 44 | top = -1; 45 | } 46 | 47 | /* The method push to push element 48 | into the stack */ 49 | void push(int a) 50 | { 51 | arr[++top] = a; 52 | } 53 | 54 | int pop() 55 | { 56 | int t; 57 | // Your code here 58 | if(top == -1){ 59 | return -1; 60 | }else{ 61 | 62 | t = arr[top]; 63 | top--; 64 | } 65 | return t; 66 | 67 | } 68 | } 69 | 70 | 71 | -------------------------------------------------------------------------------- /Stack/String_reversal.java: -------------------------------------------------------------------------------- 1 | /*An string of words is given, the task is to reverse the string using stack. 2 | 3 | Input Format: 4 | The first line of input will contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains a string s of words without spaces. 5 | 6 | Output Format: 7 | For each test case ,print the reverse of the string in new line. 8 | 9 | Your Task: 10 | Since this is a function problem, you don't need to take any input. Just complete the provided function. 11 | 12 | Constraints: 13 | 1 <= T <= 100 14 | 1 <= length of the string <= 100 15 | 16 | Example: 17 | Input: 18 | 2 19 | GeeksQuiz 20 | GeeksforGeeks 21 | Output: 22 | ziuQskeeG 23 | skeeGrofskeeG*/ 24 | 25 | class Solution { 26 | 27 | public void reverse(String str){ 28 | String s = ""; 29 | Stack st = new Stack<>(); 30 | for(int i = 0; i> threeSum(int[] a) { 5 | List> ans = new ArrayList<>(); 6 | 7 | Arrays.sort(a); 8 | int n = a.length; 9 | 10 | for(int i=0; i0 && a[i]!=a[i-1])){ 13 | 14 | int low = i+1, hi = a.length-1, sum = 0-a[i]; 15 | 16 | while(low < hi){ 17 | if(a[low] + a[hi] == sum){ 18 | 19 | ans.add(Arrays.asList(a[i], a[low], a[hi])); 20 | 21 | while(low=0; i--){ 17 | right[i] = Math.max(a[i], right[i+1]); 18 | } 19 | 20 | int trap = 0; 21 | for(int i=0; i=0 && jarr2[j]){ 10 | int temp=arr1[i]; 11 | arr1[i]=arr2[j]; 12 | arr2[j]=temp; 13 | } 14 | i--;j++; 15 | } 16 | Arrays.sort(arr1); 17 | Arrays.sort(arr2); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D1_P4_MaxSubarray(KadaneAlgo).java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/maximum-subarray/submissions/ 2 | 3 | class Solution { 4 | public int maxSubArray(int[] a) { 5 | int max = a[0], sum=0; 6 | for(int i=0; imax)max = sum; 9 | if(sum<0)sum=0; 10 | } 11 | return max; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D1_P5_MergeIntervals.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/merge-intervals/submissions/ 2 | 3 | class Solution { 4 | public int[][] merge(int[][] intervals) { 5 | Listres = new ArrayList<>(); 6 | if(intervals.length==0 || intervals==null){ 7 | return res.toArray(new int[0][]); 8 | } 9 | Arrays.sort(intervals, (a,b)->a[0]-b[0]); 10 | int start = intervals[0][0]; 11 | int end = intervals[0][1]; 12 | for(int []i: intervals){ 13 | if(i[0]<=end){ 14 | end = Math.max(end, i[1]); 15 | }else{ 16 | res.add(new int[]{start,end}); 17 | start = i[0]; 18 | end = i[1]; 19 | } 20 | } 21 | res.add(new int[]{start,end}); 22 | return res.toArray(new int[0][]); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D1_P6_FindDuplicate.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-the-duplicate-number/ 2 | 3 | class Solution { 4 | public int findDuplicate(int[] nums) { 5 | HashMaphm = new HashMap<>(); 6 | int res =0; 7 | for(int i=0; i> generate(int n) { 5 | 6 | List> allRows = new ArrayList>(); 7 | List row = new ArrayList<>(); 8 | 9 | for(int i=0; i(row)); 15 | } 16 | return allRows; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D2_P3_NextPermutation.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/next-permutation/submissions/ 2 | 3 | class Solution { 4 | public void nextPermutation(int[] a) { 5 | if(a.length<=1 || a == null)return; 6 | 7 | int i = a.length - 2; 8 | while(i>=0 && a[i]>=a[i+1])i--; // index which breaks descending order 1342 (index =1) 9 | int j = a.length-1; 10 | if(i>=0){ // if not entire descending 11 | while(a[j]<=a[i])j--; // index of right most first large digit 1342 (index =2) 12 | swap(a, i, j); 13 | } 14 | reverse(a, i+1, a.length-1); //reverse the rest 1432 -> 1423(ans) 15 | } 16 | 17 | public void swap(int []a, int i, int j){ 18 | int t = a[i]; 19 | a[i] = a[j]; 20 | a[j] = t; 21 | } 22 | 23 | public void reverse(int []a, int i, int j){ 24 | while(ilow){ 26 | mid = (low+high)/2; 27 | 28 | count += merge_sort(a,t,low,mid); 29 | count += merge_sort(a,t,mid+1,high); 30 | 31 | count += merge(a,t,low,mid+1,high); 32 | } 33 | return count; 34 | } 35 | 36 | //Function which merges the elem back in sorted form 37 | 38 | public static long merge(long a[], long t[], long low, long mid, long high){ 39 | long count=0l; 40 | int i=(int)low; 41 | int j=(int)mid; 42 | int k=(int)low; 43 | while( (i<=mid-1) && (j<=high) ){ 44 | if(a[i]<=a[j]){ 45 | t[k++] = a[i++]; 46 | }else{ 47 | t[k++] = a[j++]; 48 | count += mid-i; 49 | } 50 | } 51 | while(i<=mid-1){ 52 | t[k++] = a[i++]; 53 | } 54 | while(j<=high){ 55 | t[k++] = a[j++]; 56 | } 57 | for(int x=(int)low; x<=(int)high; x++){ 58 | a[x] = t[x]; 59 | } 60 | return count; 61 | } 62 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D2_P5_BestTimeToBuyStocks.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 2 | 3 | class Solution { 4 | public int maxProfit(int[] prices) { 5 | int min = Integer.MAX_VALUE; 6 | int profit =0; 7 | for(int i=0; i prices[i]){ 9 | min = prices[i]; 10 | }else if(prices[i]-min > profit){ 11 | profit = prices[i]-min; 12 | } 13 | } 14 | return profit; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D2_P6_RotateArray.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/rotate-image/ 2 | 3 | class Solution { 4 | public void rotate(int[][] a) { 5 | 6 | // TRANSPOSE 7 | for(int r=0; r=0){ 8 | if(m[i][j]==target) return true; 9 | else if(m[i][j]>target){ 10 | j--; 11 | }else{ 12 | i++; 13 | } 14 | } 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D3_P2_Power(x,n).java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/powx-n/ 2 | // BINARY EXPONENTIATION 3 | 4 | class Solution { 5 | public double myPow(double x, int n) { 6 | long t = n; 7 | double ans = 1.0; 8 | if(t<0) t = -1*t; 9 | while(t > 0){ 10 | if((t&1)==1){ 11 | ans = ans*x; 12 | t--; 13 | }else{ 14 | x *= x; 15 | t/=2; 16 | } 17 | } 18 | if(n<0) ans = (double)1.0/(double)ans; 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D3_P3_MajorityElements.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/majority-element/ 2 | // MAJORITY ELEMENTS (>N/2) TIMES 3 | 4 | class Solution { 5 | public int majorityElement(int[] nums) { 6 | int count =0; 7 | int elem = 0; 8 | for(int e:nums){ 9 | if(count == 0){ 10 | elem = e; 11 | } 12 | if(elem == e){ 13 | count = count + 1; 14 | }else{ 15 | count--; 16 | } 17 | } 18 | return elem; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D3_P4_MajorityElement2.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/majority-element-ii/ 2 | // BOYER MOORE VOTING ALGORITHM 3 | 4 | class Solution { 5 | public List majorityElement(int[] nums) { 6 | 7 | List res = new ArrayList<>(); 8 | int count1=0, count2=0, num1=-1, num2=-1; 9 | int l = nums.length; 10 | 11 | for(int i=0; il/3)res.add(num1); 33 | if(count2>l/3)res.add(num2); 34 | return res; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /StriverSDE/Arrays/D3_P5_UniquePaths.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/unique-paths/ 2 | // Dynamic programming 3 | 4 | class Solution { 5 | public int uniquePaths(int m, int n) { 6 | int dp[][] = new int[m][n]; 7 | for(int i=0; i=high) return 0; 11 | int mid = (low+high)/2; 12 | int inv = mergeSort(a,low,mid); 13 | inv += mergeSort(a, mid+1, high); 14 | inv += merge(a, low, mid, high); 15 | return inv; 16 | } 17 | public static int merge(int[]a, int low, int mid, int high){ 18 | int cnt=0; 19 | int j = mid+1; 20 | for(int i=low; i<=mid; i++){ 21 | while(j<=high && a[i]> (2*(long)a[j])){ 22 | j++; 23 | } 24 | cnt += (j-(mid+1)); 25 | } 26 | ArrayListal = new ArrayList<>(); 27 | int left = low, right = mid+1; 28 | while(left<=mid && right<=high){ 29 | if(a[left]<=a[right]){ 30 | al.add(a[left++]); 31 | }else{ 32 | al.add(a[right++]); 33 | } 34 | } 35 | while(left<=mid){ 36 | al.add(a[left++]); 37 | } 38 | while(right<=high){ 39 | al.add(a[right++]); 40 | } 41 | for(int i=low; i<=high; i++){ 42 | a[i] = al.get(i-low); 43 | } 44 | return cnt; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /StriverSDE/Greedy/D8_P1_N_meetings_in_1_room.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room-1587115620/1 2 | 3 | class meeting 4 | { 5 | int start; 6 | int end; 7 | int pos; 8 | 9 | meeting(int start, int end, int pos) 10 | { 11 | this.start = start; 12 | this.end = end; 13 | this.pos = pos; 14 | } 15 | } 16 | class mycomparator implements Comparator 17 | { 18 | @Override 19 | public int compare(meeting o1, meeting o2) 20 | { 21 | if (o1.end < o2.end) 22 | { 23 | return -1; 24 | } 25 | else if (o1.end > o2.end) 26 | return 1; 27 | if(o1.pos meet = new ArrayList(); 39 | for(int i=0; ians = new ArrayList<>(); 48 | 49 | ans.add(meet.get(0).pos); 50 | 51 | int limit = meet.get(0).end; 52 | 53 | for(int i=1; ilimit){ 55 | ans.add(meet.get(i).pos); 56 | limit = meet.get(i).end; 57 | } 58 | } 59 | return ans.size(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /StriverSDE/Greedy/D8_P2_Minimun_platforms.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/minimum-platforms-1587115620/1# 2 | 3 | class Solution 4 | { 5 | static int findPlatform(int arr[], int dep[], int n) 6 | { 7 | // Will use 2 pointer approach 8 | Arrays.sort(arr); // sorted arrival time in ascending order 9 | Arrays.sort(dep); // sorted departure time in ascending order 10 | int i = 1; // points the arrival of the train at 1st index 11 | int j = 0; // points the departure time of the 0th index train cause we compare it 12 | 13 | int platform_needed = 1, min_platform = 1; 14 | 15 | while(idep[j]){ 20 | platform_needed--; 21 | j++; 22 | } 23 | if(platform_needed > min_platform){ 24 | min_platform = platform_needed; 25 | } 26 | } 27 | return min_platform; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /StriverSDE/Greedy/D8_P3_Job_sequencing.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/job-sequencing-problem-1587115620/1# 2 | 3 | class Solution 4 | { 5 | //Function to find the maximum profit and the number of jobs done. 6 | int[] JobScheduling(Job arr[], int n) 7 | { 8 | // Your code here 9 | Arrays.sort(arr, (a,b)->(a.profit-b.profit)); 10 | 11 | int max = 0; 12 | for(int i=0; imax) 14 | max = arr[i].deadline; 15 | } 16 | 17 | int res[] = new int[max+1]; 18 | Arrays.fill(res, -1); 19 | 20 | int count=0, profit=0; 21 | for(int i=0; i=0; i--){ 23 | if(res[i]==-1){ 24 | res[j] = i; 25 | count++; 26 | profit += arr[i].profit; 27 | break; 28 | } 29 | } 30 | } 31 | int result[] = new int[2]; 32 | result[0] = count; 33 | result[1] = profit; 34 | return result; 35 | } 36 | -------------------------------------------------------------------------------- /StriverSDE/Greedy/D8_P4_Fractional_knapsack.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/fractional-knapsack-1587115620/1 2 | 3 | /* 4 | class Item { 5 | int value, weight; 6 | Item(int x, int y){ 7 | this.value = x; 8 | this.weight = y; 9 | } 10 | } 11 | */ 12 | class comp implements Comparator{ 13 | public int compare(Item a, Item b){ 14 | double r1 = (double)(a.value)/(double)(a.weight); 15 | double r2 = (double)(b.value)/(double)(b.weight); 16 | if(r1 map = new HashMap<>(); 6 | int l = nums.length; 7 | int a[] = new int[2]; 8 | for(int i=0; i> fourSum(int[] nums, int target) { 5 | 6 | ArrayList> res = new ArrayList>(); 7 | 8 | if(nums==null || nums.length==0){ 9 | return res; 10 | } 11 | Arrays.sort(nums); 12 | 13 | int l = nums.length; 14 | for(int i=0; i targetLeft) right--; 23 | else{ 24 | ArrayList quad = new ArrayList(); 25 | quad.add(nums[i]); 26 | quad.add(nums[j]); 27 | quad.add(nums[left]); 28 | quad.add(nums[right]); 29 | res.add(quad); 30 | 31 | // skipping the duplicates for left pointer 32 | while(left < right && nums[left] == quad.get(2)) ++left; 33 | 34 | // skipping the duplicates for right pointer 35 | while(left < right && nums[right] == quad.get(3)) --right; 36 | } 37 | } 38 | 39 | // skipping the duplicates for j 40 | while(j+1 < l && nums[j+1] == nums[j]) ++j; 41 | } 42 | 43 | // skipping the duplicates for i 44 | while(i+1 < l && nums[i+1] == nums[i]) ++i; 45 | } 46 | return res; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /StriverSDE/Hashing/D4_P3_LongestConsecutiveSeq.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/longest-consecutive-sequence/ 2 | 3 | class Solution { 4 | public int longestConsecutive(int[] a) { 5 | HashSet hs = new HashSet(); 6 | for(int i:a){ 7 | hs.add(i); 8 | } 9 | int longestStreak = 0; 10 | for(int num:a){ 11 | if(!hs.contains(num-1)){ 12 | int currentNum = num; 13 | int currentStreak = 1; 14 | while(hs.contains(currentNum+1)){ 15 | currentNum++; 16 | currentStreak++; 17 | } 18 | longestStreak = Math.max(currentStreak, longestStreak); 19 | } 20 | } 21 | return longestStreak; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /StriverSDE/Hashing/D4_P4_LargestSubarrayWith_0_Sum.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/largest-subarray-with-0-sum/1# 2 | 3 | class GfG 4 | { 5 | int maxLen(int a[], int n) 6 | { 7 | // Your code here 8 | HashMaphm = new HashMap(); 9 | int sum=0, maxLen = 0; 10 | for(int i=0; ihm = new HashMap<>(); 8 | while(right < n){ 9 | if(hm.containsKey(s.charAt(right))){ 10 | left = Math.max(hm.get(s.charAt(right))+1, left); 11 | } 12 | hm.put(s.charAt(right), right); 13 | len = Math.max(len, right-left+1); 14 | right++; 15 | } 16 | return len; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D5_P1_Reverse_linked_list.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reverse-linked-list/ 2 | 3 | class Solution { 4 | public ListNode reverseList(ListNode head) { 5 | ListNode newHead = null; 6 | while(head!=null){ 7 | ListNode next = head.next; 8 | head.next = newHead; 9 | newHead = head; 10 | head = next; 11 | } 12 | return newHead; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D5_P2_Middle_of_the_linked_list.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/middle-of-the-linked-list/ 2 | 3 | class Solution { 4 | public ListNode middleNode(ListNode head) { 5 | ListNode turtle = head, hare = head; 6 | while(hare!=null && hare.next!=null){ 7 | turtle = turtle.next; 8 | hare = hare.next.next; 9 | } 10 | return turtle; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D5_P3_Merge_2_sorted_list.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/merge-two-sorted-lists/ 2 | 3 | class Solution { 4 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 5 | if(l1==null)return l2; 6 | if(l2==null)return l1; 7 | if(l1.val>l2.val){ 8 | ListNode temp = l1; 9 | l1 = l2; 10 | l2 = temp; 11 | } 12 | ListNode res = l1; 13 | while(l1!=null && l2!=null){ 14 | ListNode temp = null; 15 | while(l1!=null && l1.val<=l2.val){ 16 | temp = l1; 17 | l1 = l1.next; 18 | } 19 | temp.next = l2; 20 | 21 | ListNode t = l1; 22 | l1 = l2; 23 | l2 = t; 24 | } 25 | return res; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D5_P4_Remove_nth_node_from_end.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 2 | 3 | class Solution { 4 | public ListNode removeNthFromEnd(ListNode head, int n) { 5 | ListNode start = new ListNode(); 6 | start.next = head; 7 | ListNode fast = start; 8 | ListNode slow = start; 9 | for(int i=1; i<=n; i++){ 10 | fast = fast.next; 11 | } 12 | while(fast.next!=null){ 13 | fast = fast.next; 14 | slow = slow.next; 15 | } 16 | slow.next = slow.next.next; 17 | return start.next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D5_P5_Delete_a_node.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/delete-node-in-a-linked-list/ 2 | 3 | class Solution { 4 | public void deleteNode(ListNode node) { 5 | node.val = node.next.val; 6 | node.next = node.next.next; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D5_P6_Add_two_numbers.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/add-two-numbers/ 2 | 3 | class Solution { 4 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 5 | ListNode dummy = new ListNode(); 6 | ListNode temp = dummy; 7 | int carry=0; 8 | while(l1!=null || l2!=null || carry==1 ){ 9 | int sum=0; 10 | if(l1 != null){ 11 | sum += l1.val; 12 | l1 = l1.next; 13 | } 14 | if(l2 != null){ 15 | sum += l2.val; 16 | l2 = l2.next; 17 | } 18 | sum+=carry; 19 | carry = sum/10; 20 | ListNode node = new ListNode(sum%10); 21 | temp.next = node; 22 | temp = temp.next; 23 | } 24 | return dummy.next; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D6_P1_Intersection_point_of_2_LinkedList.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/intersection-of-two-linked-lists/ 2 | 3 | public class Solution { 4 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 5 | ListNode a = headA; 6 | ListNode b = headB; 7 | if(headA==null || headB==null)return null; 8 | while(a!=b){ 9 | a = a==null ? headB : a.next; 10 | b = b==null ? headA : b.next; 11 | } 12 | return a; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /StriverSDE/Linked List/D6_P4_Check_Palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(ListNode head) { 3 | if(head==null || head.next==null)return true; 4 | 5 | ListNode slow = head; 6 | ListNode fast = head; 7 | 8 | // Find Middle 9 | while(fast.next!=null && fast.next.next!=null){ 10 | slow = slow.next; 11 | fast = fast.next.next; 12 | } 13 | 14 | // reverse the right half 15 | slow.next = reverse(slow.next); 16 | 17 | // move the slow pointer by 1 18 | slow = slow.next; 19 | 20 | //check both left and right half 21 | while(slow!=null){ 22 | if(head.val!=slow.val)return false; 23 | head = head.next; 24 | slow = slow.next; 25 | } 26 | return true; 27 | } 28 | public ListNode reverse(ListNode head){ 29 | ListNode newHead = null; 30 | while(head!=null){ 31 | ListNode next = head.next; 32 | head.next = newHead; 33 | newHead = head; 34 | head = next; 35 | } 36 | return newHead; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /StriverSDE/Recursion/D9_P1_Subset_sums.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/subset-sums2234/1# 2 | 3 | class Solution{ 4 | ArrayList subsetSums(ArrayList arr, int n){ 5 | // code here 6 | ArrayListans = new ArrayList<>(); 7 | subset(0, 0, n, arr, ans); 8 | return ans; 9 | } 10 | public void subset(int l, int sum, int n, ArrayListarr, ArrayListans){ 11 | if(l==n){ 12 | ans.add(sum); 13 | return; 14 | } 15 | // Pick up the element 16 | subset(l+1, arr.get(l)+sum, n, arr, ans); 17 | // Not picking up the element 18 | subset(l+1, sum, n, arr, ans); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /two-pointer algo/Kth_small_element.java: -------------------------------------------------------------------------------- 1 | /*Given an array arr[] and a number K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct. 2 | 3 | Input: 4 | The first line of input contains an integer T, denoting the number of testcases. Then T test cases follow. Each test case consists of three lines. First line of each testcase contains an integer N denoting size of the array. Second line contains N space separated integer denoting elements of the array. Third line of the test case contains an integer K. 5 | 6 | Output: 7 | Corresponding to each test case, print the kth smallest element in a new line. 8 | 9 | Expected Time Complexity: O(N). 10 | Expected Auxiliary Space: O(1). 11 | 12 | Constraints: 13 | 1 <= T <= 100 14 | 1 <= N <= 105 15 | 1 <= arr[i] <= 105 16 | 1 <= K <= N 17 | 18 | Example: 19 | Input: 20 | 2 21 | 6 22 | 7 10 4 3 20 15 23 | 3 24 | 5 25 | 7 10 4 20 15 26 | 4 27 | Output: 28 | 7 29 | 15 30 | 31 | Explanation: 32 | Testcase 1: 3rd smallest element in the given array is 7. 33 | Testcase 2: 4th smallest elemets in the given array is 15.*/ 34 | 35 | 36 | 37 | import java.util.*; 38 | import java.lang.*; 39 | import java.io.*; 40 | 41 | class GFG { 42 | public static void main (String[] args) { 43 | //code 44 | Scanner sc = new Scanner(System.in); 45 | int i,k; 46 | int t = sc.nextInt(); 47 | while(t>0){ 48 | int n = sc.nextInt(); 49 | int a[] = new int[n]; 50 | for(i=0;i0){ 42 | int n=sc.nextInt();; 43 | int a[] = new int[n]; 44 | 45 | for(i=0;ix){ 47 | j--; 48 | }else if(a[i]+a[j]