├── Arrays ├── Add_one_to_number.cpp ├── Flip.cpp ├── Max_sum_contiguous_subarray.cpp ├── Maximum_absolute_difference.cpp ├── Min_steps_in_infinite_grid.cpp ├── Repeat and Missing Number Array.cpp ├── anti_diagonals.cpp ├── find_permutation.cpp ├── hotel_bookings_possible.cpp ├── kth_row_of_pascal_triangle.cpp ├── largest_number.cpp ├── largest_number_Arrangement.cpp ├── largest_number_Bucketing.cpp ├── max_distance.cpp ├── max_non_negative_subarray.cpp ├── maximum_unsorted_subarray.cpp ├── maxspprod.cpp ├── merge_intervals.cpp ├── merge_overlapping_intervals.cpp ├── next_permutation.cpp ├── noble_integer.cpp ├── pascal_triangle.cpp ├── rotate_matrix.cpp ├── set_matrix_zeros.cpp ├── spiral_order_matrix_ii.cpp ├── wave_array.cpp └── wave_array_.cpp ├── Backtracking ├── combination_sum.cpp ├── combination_sum_ii.cpp ├── combinations.cpp ├── gray_code.cpp ├── letter_phone.cpp ├── modular_expression.cpp ├── nqueens.cpp ├── palindrome_partitioning.cpp ├── permutations.cpp ├── subset.cpp └── subset_ii.cpp ├── Binary Search ├── .Median of Array.cpp.swp ├── Count_element_occurence.cpp ├── MatrixSearch.cpp ├── Matrix_median.cpp ├── Median of Array.cpp ├── Rotated_array.cpp ├── Rotated_array_sorted_search.cpp ├── Search_for_a_range.cpp ├── Sorted_insert_position.cpp ├── Square_root_of_integer.cpp ├── allocate_books.cpp └── implement_power_function.cpp ├── Bit Manipulation ├── different_bits_sum_pairwise.cpp ├── min_xor_value.cpp ├── number_of_1_bits.cpp ├── reverse_bits.cpp ├── single_number.cpp └── single_number_ii.cpp ├── Checkpoint_Level_3 └── Kth_smallest_element_in_the_array.cpp ├── Dynamic Programming ├── Largest area of rectangle with permutations.cpp ├── Max Rectangle in Binary Matrix.cpp ├── Ways to Decode.cpp ├── Word Break.cpp ├── best_time_to_buy_and_sell_stocks_ii.cpp ├── coin_sum_infinite.cpp ├── distinct_subsequences.cpp ├── dungeon_princess.cpp ├── interleaving_strings.cpp ├── jump_game_array.cpp ├── length_of_longest_subsequence.cpp ├── max_product_subarray.cpp ├── min_sum_path_in_triangle.cpp ├── tushar_birthday_party.cpp ├── unique_binary_search_trees_ii.cpp └── unique_paths_in_a_grid.cpp ├── Graph Data Structure & Algorithms ├── Black Shapes.cpp ├── Capture Regions on Board.cpp ├── Clone Graph.cpp ├── Commutable Islands.cpp ├── Knight On Chess Board.cpp ├── Largest Distnace between nodes of a Tree.cpp ├── Smallest sequence with given Primes.cpp ├── Stepping Numbers.cpp ├── Valid path.cpp ├── Word Ladder I.cpp ├── Word Ladder II.cpp ├── Word Search Board.cpp ├── convert_sorted_list_to_binary_search_tree.cpp ├── level_order.cpp ├── possibility_of_finishing_all_courses_given_pre-requisites.cpp └── sum_of_fibonacci_numbers.cpp ├── Greedy Algorithm ├── Assign Mice to Holes.cpp ├── Bulbs.cpp ├── Distribute Candy.cpp ├── Gas Station.cpp ├── Highest Product.cpp └── Majority Element.cpp ├── Hashing ├── 2_sum.cpp ├── 4_sum.cpp ├── Colorful_number.cpp ├── Window String.cpp ├── anagrams.cpp ├── diff_ii.cpp ├── equal.cpp ├── fraction.cpp ├── largest_continuous_sequence_zero_sum.cpp ├── longest_substring_without_repeat.cpp ├── substring_concatenation.cpp └── valid_sudoku.cpp ├── HeapsAndMaps ├── LRUCache.cpp ├── distinct_numbers_in_window.cpp ├── magician_and_chocolates.cpp └── merge_k_sorted_lists.cpp ├── Linked Lists ├── add_two_numbers_as_lists.cpp ├── insertion_sort_list.cpp ├── intersection_of_linked_lists.cpp ├── k_reverse_linked_list.cpp ├── list_cycle.cpp ├── merge_two_sorted_lists.cpp ├── palindrome_list.cpp ├── partition_list.cpp ├── remove_duplicates_from_sorted_list.cpp ├── remove_duplicates_from_sorted_list_II.cpp ├── remove_nth_node_from_list_end.cpp ├── reorder_list.cpp ├── reverse_linked_list.cpp ├── reverse_linked_list_II.cpp ├── rotate_list.cpp ├── sort_list.cpp └── swap_list_nodes_in_pairs.cpp ├── Math ├── Prime_Sum.cpp ├── excel_column_number.cpp ├── excel_column_title.cpp ├── fizzbuzz.cpp ├── greatest_common_divisor.cpp ├── grid_unique_paths.cpp ├── largest_coprime_divisor.cpp ├── palindrome_integer.cpp ├── power_of_two_integers.cpp ├── rearrange_array.cpp ├── reverse_integer.cpp ├── sorted_permutation_rank.cpp └── trailing_zeros_in_factorial.cpp ├── Ninja Test ├── build_identical_trees.cpp ├── get_mode_array_updates.cpp ├── increasing_subarrays.cpp └── maximum_longest_common_subsequence.cpp ├── README.md ├── Stacks And Queues ├── Generate_all_parentheses.cpp ├── Sliding Window Maximum.cpp ├── evaluate_expression.cpp ├── min_stack.cpp ├── nearest_smallest_element.cpp ├── partition_list.cpp ├── rain_water_tapped.cpp ├── redundant_braces.cpp ├── reverse_string.cpp └── simpify_directory_path.cpp ├── Strings ├── Length_of_last_word.cpp ├── Palindrome_string.cpp ├── Reverse_the_string.cpp ├── add_binary_strings.cpp ├── atoi.cpp ├── compare_version_numbers.cpp ├── count_and_say.cpp ├── implement_strstr.cpp ├── integer_to_roman.cpp ├── longest_common_prefix.cpp ├── longest_palindromic_substring.cpp ├── minimum_characters_required_to_make_a_string_palindromic.cpp ├── multiply_strings.cpp ├── power_of_2.cpp ├── pretty_json.cpp ├── roman_to_integer.cpp ├── valid_ip_addresses.cpp ├── valid_number.cpp └── zigzag_string.cpp ├── Trees ├── Inorder Traversal.cpp ├── Least Common Ancestor.cpp ├── Max Depth of Binary Tree.cpp ├── Min Depth of Binary Tree.cpp ├── Postorder Traversal.cpp ├── Preorder Traversal.cpp ├── ZigZag Level Order Traversal BT.cpp ├── binary_tree_from_inorder_and_postorder.cpp ├── construct_binary_tree_from_inorder_and_preorder.cpp ├── flatten_binary_tree_to_linked_list.cpp ├── identical_binary_trees.cpp ├── inorder_traversal_of_cartesian_tree.cpp ├── invert_the_binary_tree.cpp ├── kth_smallest_element_in_tree.cpp ├── path_sum.cpp ├── populate_next_right_pointers_tree.cpp ├── root_to_leaf_paths_with_sum.cpp ├── shortest_unique_prefix.cpp ├── sorted_array_to_balanced_BST.cpp ├── sum_root_to_leaf_numbers.cpp └── symmetric_binary_tree.cpp ├── Two Pointers ├── 3_sum.cpp ├── 3_sum_zero.cpp ├── Counting_triangles.cpp ├── diffk.cpp ├── intersection_of_sorted_arrays.cpp ├── merge_two_sorted_lists_ii.cpp ├── minimize_the_absolute_difference.cpp ├── remoce_duplicates_from_sorted_array_ii.cpp ├── remove_duplicates_from_sorted_array.cpp ├── remove_element_from_array.cpp └── sort_by_color.cpp └── nano.save /Arrays/Add_one_to_number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/add-one-to-number/ 2 | 3 | /** 4 | * @input A : Integer array 5 | * @input n1 : Integer array's ( A ) length 6 | * 7 | * @Output Integer array. You need to malloc memory for result array, and fill result's length in length_of_array 8 | */ 9 | int* plusOne(int* A, int n1, int *len1) { 10 | int i,dig,carry=1,len=0; 11 | int *out = (int *)malloc(sizeof(int)*(n1+1)); 12 | int oind = n1; 13 | for(i=n1-1;i>=0;i--){ 14 | out[oind--] = (A[i]+carry)%10; 15 | carry = (A[i]+carry)/10; 16 | } 17 | out[oind] = carry; 18 | for(i=0;i Solution::flip(string A) { 4 | 5 | int check = 0, another_check = 0; 6 | int left, right, left_final, right_final; 7 | int temp1 = 0, max1 = 0; 8 | char c; 9 | 10 | // cout << A.size(); 11 | for(int i = 0; i < A.size(); i++){ 12 | // char c = A.at(i); 13 | c = A[i]; 14 | check++; 15 | if(check == 1){ 16 | left = i; 17 | } 18 | right = i; 19 | if(c == '1'){ 20 | temp1--; 21 | } 22 | if(c == '0'){ 23 | temp1++; 24 | } 25 | if(temp1 >= 0){ 26 | another_check++; 27 | if(temp1 > max1){ 28 | max1 = temp1; 29 | left_final = left; 30 | right_final = right; 31 | } 32 | else if(temp1 == max1){ 33 | if(left == left_final){ 34 | if(right > right_final){ 35 | // nothing 36 | } 37 | else if(right_final > right){ 38 | right_final = right; 39 | } 40 | } 41 | else if(left > left_final){ 42 | // nothing 43 | } 44 | else{ 45 | // left < left_final 46 | left_final = left; 47 | right_final = right; 48 | } 49 | } 50 | } 51 | else{ 52 | check = 0; 53 | temp1 = 0; 54 | } 55 | } 56 | vector sol; 57 | if(another_check != 0){ 58 | sol.push_back(left_final+1); 59 | sol.push_back(right_final+1); 60 | } 61 | 62 | return sol; 63 | } 64 | -------------------------------------------------------------------------------- /Arrays/Max_sum_contiguous_subarray.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/max-sum-contiguous-subarray/ 2 | 3 | /** 4 | * @input A : Read only ( DON'T MODIFY ) Integer array 5 | * @input n1 : Integer array's ( A ) length 6 | * 7 | * @Output Integer 8 | */ 9 | int max(int x,int y) { 10 | return (x>y)?x:y; 11 | } 12 | int maxSubArray(const int* A, int n1) { 13 | 14 | int curr_max=A[0]; 15 | int max_so_far=A[0]; 16 | int i; 17 | 18 | for(i=1;i &A) { 4 | int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN, max4 = INT_MIN; 5 | assert(!A.empty()); 6 | int ans = INT_MIN; 7 | int size = A.size(); 8 | for (auto i = 0; i Solution::repeatedNumber(const vector &A) { 7 | 8 | ll n = A.size(); 9 | ll sum = 0; 10 | ll sum_sq = 0; 11 | 12 | for(ll i=0;i res; 29 | res.push_back(repeated); 30 | res.push_back(missing); 31 | return res; 32 | } 33 | -------------------------------------------------------------------------------- /Arrays/anti_diagonals.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/anti-diagonals/ 2 | 3 | vector > Solution::diagonal(vector > &A) { 4 | 5 | int sz = 0, row = 0, col = 0; 6 | int n = A.size(); 7 | vector > result; 8 | for(int l=0;l<2*n-1;l++) { 9 | if(l elem; 14 | int k = 0; 15 | int i = row; 16 | int j = col; 17 | 18 | while(k Solution::findPerm(const string A, int B) { 4 | 5 | int l=1, r=B; 6 | 7 | vector result; 8 | 9 | for(int i=0;i &arrive, vector &depart, int K) { 4 | map mp; 5 | 6 | sort(arrive.begin(), arrive.end()); 7 | sort(depart.begin(), depart.end()); 8 | 9 | for(auto a: arrive) 10 | mp[a]++; 11 | 12 | for(auto d: depart) 13 | mp[d]--; 14 | 15 | int ans = 0; 16 | int sum = 0; 17 | for(auto a=mp.begin();a!=mp.end();a++) { 18 | sum += a->second; 19 | ans = max(ans, sum); 20 | } 21 | return (ans>K)?0:1; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Arrays/kth_row_of_pascal_triangle.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/kth-row-of-pascals-triangle/ 2 | 3 | vector Solution::getRow(int A) { 4 | vector > pascal; 5 | 6 | for(int i=0;i<=A;i++) { 7 | vector row(i+1); 8 | for(int j=0;j<=i;j++) 9 | if(j==0 || j==1) 10 | row[j] = 1; 11 | else 12 | row[j] = pascal[i-1][j-1]+pascal[i-1][j]; 13 | pascal.push_back(row); 14 | } 15 | return pascal[A]; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Arrays/largest_number.cpp: -------------------------------------------------------------------------------- 1 | bool comp(string a, string b) { 2 | return a+b > b+a; 3 | } 4 | 5 | string Solution::largestNumber(const vector &A) { 6 | vector B; 7 | int zf = 0; 8 | for(auto a: A) { 9 | B.push_back(to_string(a)); 10 | if(a) 11 | zf++; 12 | } 13 | if(!zf) 14 | return "0"; 15 | 16 | sort(B.begin(), B.end(), comp); 17 | string result = ""; 18 | for(auto b: B) 19 | result += b; 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /Arrays/largest_number_Arrangement.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/largest-number/ 2 | 3 | string sort(vector A); 4 | int comp(string a, string b) 5 | { 6 | return a+b > b+a; 7 | } 8 | 9 | string Solution::largestNumber(const vector &A) 10 | { 11 | int allZero = 1; 12 | vector B; 13 | for(int i=0;i A); 4 | int comp(string a, string b) 5 | { 6 | return a+b > b+a; 7 | } 8 | 9 | string Solution::largestNumber(const vector &A) 10 | { 11 | int allZero = 1; 12 | vector B; 13 | 14 | for(int i=0;i &A) { 2 | int n = A.size(); 3 | vector L(n); 4 | vector R(n); 5 | 6 | int i, j; 7 | L[0] = A[0]; 8 | for(i=1;i=0;i--) 13 | R[i] = max(R[i+1], A[i]); 14 | 15 | int diff; 16 | i = j = diff = 0; 17 | 18 | while(i=0) { 28 | sum = sum + (long long)A[i]; 29 | count++; 30 | if(i==n1-1) { 31 | if(maxmaxCount)) { 32 | max = sum; 33 | maxCount = count; 34 | memcpy(range,A+startIdx,sizeof(int)*maxCount); 35 | } 36 | } 37 | } 38 | 39 | else { 40 | if(maxmaxCount)) { 41 | max = sum; 42 | maxCount = count; 43 | memcpy(range,A+startIdx,sizeof(int)*maxCount); 44 | } 45 | sum = 0; 46 | count = 0; 47 | startIdx = i+1; 48 | //*length_of_array = maxCount; 49 | } 50 | } 51 | *length_of_array = maxCount; 52 | return range; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Arrays/maximum_unsorted_subarray.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/maximum-unsorted-subarray/ 2 | 3 | vector Solution::subUnsort(vector &A) { 4 | int flag=0; 5 | int n = A.size(); 6 | for(int i=1;i result; 12 | if(!flag) { 13 | result.push_back(-1); 14 | return result; 15 | } 16 | 17 | int l = flag-1, r; 18 | for(int j=n-1;j>0;j--) 19 | if(A[j]smallest) { 36 | l = i; 37 | break; 38 | } 39 | for(int j=n-1;j>r;j--) 40 | if(A[j] &A) { 4 | int mod = 1000000007; 5 | int n = A.size(); 6 | vector lsp(n); 7 | vector rsp(n); 8 | stack st; 9 | 10 | for(int i=0;i().swap(st); 24 | 25 | for(int i=n-1;i>=0;i--) { 26 | while(!st.empty()) { 27 | if(A[st.top()] <= A[i]) 28 | st.pop(); 29 | else 30 | break; 31 | } 32 | if(st.empty()) 33 | rsp[i] = 0; 34 | else 35 | rsp[i] = st.top(); 36 | st.push(i); 37 | } 38 | 39 | long long ans = lsp[0]*rsp[0]; 40 | for(int i=1;i Solution::insert(vector &A, Interval B) { 17 | // Do not write main() function. 18 | // Do not read input, instead use the arguments to the function. 19 | // Do not print the output, instead return values as specified 20 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 21 | 22 | A.push_back(B); 23 | sort(A.begin(), A.end(), comp); 24 | 25 | vector result; 26 | Interval temp = A[0]; 27 | int i = 0, n = A.size(); 28 | for(int j=1;j= A[j].start) 30 | temp.end = max(temp.end, A[j].end); 31 | else { 32 | result.push_back(temp); 33 | temp = A[j]; 34 | } 35 | } 36 | result.push_back(temp); 37 | return result; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Arrays/next_permutation.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/next-permutation/# 2 | 3 | void Solution::nextPermutation(vector &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int n = A.size()-1; 10 | int index, i; 11 | 12 | for(i=n;i>0;i--) { 13 | 14 | if(A[i]>A[i-1]) { 15 | index = i-1; 16 | break; 17 | } 18 | 19 | } 20 | if(i==0) { 21 | reverse(A.begin(), A.end()); 22 | return; 23 | } 24 | i = n; 25 | while(A[index]>=A[i]) 26 | i--; 27 | 28 | int temp = A[index]; 29 | A[index] = A[i]; 30 | A[i] = temp; 31 | 32 | sort(A.begin()+index+1, A.end()); 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Arrays/noble_integer.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/noble-integer/ 2 | 3 | int Solution::solve(vector &A) { 4 | int m = A.size(); 5 | sort(A.begin(), A.end()); 6 | for(int i=0;i > Solution::solve(int A) { 4 | 5 | vector > pascal; 6 | 7 | for(int i=0;i row(i+1); 9 | for(int j=0;j<=i;j++) { 10 | if(j==0 || j==i) 11 | row[j] = 1; 12 | else 13 | row[j] = pascal[i-1][j-1]+pascal[i-1][j]; 14 | } 15 | pascal.push_back(row); 16 | } 17 | 18 | return pascal; 19 | } 20 | -------------------------------------------------------------------------------- /Arrays/rotate_matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rotate-matrix/ 2 | 3 | void Solution::rotate(vector > &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | int n = A.size(); 9 | for(int i=0;i > &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int n = A.size(); 10 | int m = A[0].size(); 11 | 12 | int r=1, c=1; 13 | 14 | for(int j=0;j > Solution::generateMatrix(int n) { 4 | vector > data; 5 | 6 | for(int i=0;i row(n, 0); 8 | data.push_back(row); 9 | } 10 | 11 | int dir=0, u=0, r=n-1, l=0, d=n-1; 12 | 13 | int a = 1; 14 | 15 | while(a<=n*n) { 16 | 17 | if(dir==0) { 18 | for(int j=l;j<=r;j++) { 19 | data[u][j] = a++; 20 | } 21 | dir = 1; 22 | u++; 23 | } 24 | 25 | if(dir==1) { 26 | for(int i=u;i<=d;i++) { 27 | data[i][r] = a++; 28 | } 29 | dir = 2; 30 | r--; 31 | } 32 | 33 | if(dir==2) { 34 | for(int j=r;j>=l;j--) { 35 | data[d][j] = a++; 36 | } 37 | dir = 3; 38 | d--; 39 | } 40 | 41 | if(dir==3) { 42 | for(int i=d;i>=u;i--) { 43 | data[i][l] = a++; 44 | } 45 | dir = 0; 46 | l++; 47 | } 48 | } 49 | 50 | return data; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Arrays/wave_array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/wave-array/ 2 | 3 | /** 4 | * @input A : Integer array 5 | * @input n1 : Integer array's ( A ) length 6 | * 7 | * @Output Integer array. You need to malloc memory, and fill the length in len1 8 | */ 9 | 10 | int cmpfunc (const void * a, const void * b) 11 | { 12 | return ( *(int*)a - *(int*)b ); 13 | } 14 | 15 | int* wave(int* A, int n1, int *len1) { 16 | 17 | int i,j; 18 | *len1=n1; 19 | 20 | qsort(A, n1, sizeof(int), cmpfunc); 21 | int temp; 22 | for(i=1;i A, vector ¤t, set > &result) { 4 | if(sum==0) { 5 | result.insert(current); 6 | return; 7 | } 8 | if(index>=A.size() || sum<0) 9 | return; 10 | // include the number 11 | getSum(sum, index+1, A, current, result); 12 | // exclude the number 13 | current.push_back(A[index]); 14 | getSum(sum-A[index], index, A, current, result); 15 | current.pop_back(); 16 | } 17 | vector > Solution::combinationSum(vector &A, int B) { 18 | 19 | vector > result; 20 | set > sets; 21 | vector current; 22 | 23 | sort(A.begin(), A.end()); 24 | getSum(B, 0, A, current, sets); 25 | for(auto st: sets) 26 | result.push_back(st); 27 | sort(result.begin(), result.end()); 28 | return result; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Backtracking/combination_sum_ii.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/combination-sum-ii/ 2 | 3 | vector > Solution::combinationSum(vector &A, int B) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | set > res; 10 | 11 | int n = A.size(); 12 | int lim = pow(2, n); 13 | 14 | for(int i=0;i temp; 16 | int sum = 0; 17 | for(int j=0;j > out; 29 | set > :: iterator it; 30 | for(it=res.begin();it!=res.end();it++) { 31 | out.push_back(*it); 32 | } 33 | return out; 34 | 35 | } 36 | 37 | 38 | ########## ANOTHER SOLUTION ########## 39 | 40 | void getSets(int sum, int index, vector &A, vector ¤t, set > &result) { 41 | if(sum==0) { 42 | result.insert(current); 43 | return; 44 | } 45 | if(index>=A.size() || sum<0) 46 | return; 47 | current.push_back(A[index]); 48 | getSets(sum-A[index], index+1, A, current, result); 49 | current.pop_back(); 50 | getSets(sum, index+1, A, current, result); 51 | } 52 | 53 | vector > Solution::combinationSum(vector &A, int B) { 54 | // Do not write main() function. 55 | // Do not read input, instead use the arguments to the function. 56 | // Do not print the output, instead return values as specified 57 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 58 | 59 | vector a; 60 | sort(A.begin(), A.end()); 61 | 62 | set > result; 63 | vector current; 64 | getSets(B, 0, A, current, result); 65 | 66 | vector > ans; 67 | for(auto it: result) 68 | ans.push_back(it); 69 | 70 | return ans; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Backtracking/combinations.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/combinations/ 2 | 3 | vector > Solution::combine(int n, int B) { 4 | 5 | vector > result; 6 | vector A(n); 7 | for(int i=0;i temp; 12 | for(int i=0;i > Solution::combine(int A, int B) { 31 | vector > result; 32 | 33 | auto lim = pow(2, A); 34 | 35 | for(auto i=0;i temp; 37 | for(auto j=0;j N, int index, vector ¤t, vector > &result, int k) { 54 | if(index>=N.size()) { 55 | if(current.size()==k) 56 | result.push_back(current); 57 | return; 58 | } 59 | generateSet(N, index+1, current, result, k); 60 | current.push_back(N[index]); 61 | generateSet(N, index+1, current, result, k); 62 | current.pop_back(); 63 | } 64 | vector > Solution::combine(int A, int B) { 65 | vector > result; 66 | vector current; 67 | 68 | vector N; 69 | for(auto i=1;i<=A;i++) 70 | N.push_back(i); 71 | 72 | generateSet(N, 0, current, result, B); 73 | sort(result.begin(), result.end()); 74 | return result; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /Backtracking/gray_code.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/gray-code/ 2 | 3 | void generateCode(int A, int index, vector &G) { 4 | if(index>A) 5 | return; 6 | vector R(G); 7 | reverse(R.begin(), R.end()); 8 | for(auto i=0;i Solution::grayCode(int A) { 16 | // Do not write main() function. 17 | // Do not read input, instead use the arguments to the function. 18 | // Do not print the output, instead return values as specified 19 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 20 | vector result; 21 | vector ans; 22 | if(A<2) { 23 | if(A==0) 24 | ans.push_back(0); 25 | else if(A==1) { 26 | ans.push_back(0); 27 | ans.push_back(1); 28 | } 29 | return ans; 30 | } 31 | result.push_back("0"); 32 | result.push_back("1"); 33 | generateCode(A, 2, result); 34 | 35 | for(auto res: result) 36 | ans.push_back(stoi(res, nullptr, 2)); 37 | //cout << stoi(res, nullptr, 2) << endl; 38 | 39 | return ans; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Backtracking/letter_phone.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/letter-phone/ 2 | 3 | void generateComb(string A, int index, string ¤t, set &result, unordered_map mp) { 4 | int m = A.size(); 5 | if(index==m) { 6 | if(current.size()==m) 7 | result.insert(current); 8 | return; 9 | } 10 | if(current.size()==m) { 11 | result.insert(current); 12 | return; 13 | } 14 | else if(current.size()>m) 15 | return; 16 | int key = A[index]-'0'; 17 | 18 | int n = mp[key].size(); 19 | 20 | for(int col=0;col Solution::letterCombinations(string A) { 28 | 29 | string current=""; 30 | set result; 31 | 32 | unordered_map mp; 33 | mp[0] = "0"; 34 | mp[1] = "1"; 35 | mp[2] = "abc"; 36 | mp[3] = "def"; 37 | mp[4] = "ghi"; 38 | mp[5] = "jkl"; 39 | mp[6] = "mno"; 40 | mp[7] = "pqrs"; 41 | mp[8] = "tuv"; 42 | mp[9] = "wxyz"; 43 | 44 | generateComb(A, 0, current, result, mp); 45 | 46 | vector ans; 47 | for(auto s: result) 48 | ans.push_back(s); 49 | sort(ans.begin(), ans.end()); 50 | return ans; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Backtracking/modular_expression.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/modular-expression/ 2 | 3 | long long int calMod(int A, int B, int C) { 4 | if(B==0) 5 | return 1; 6 | else if(B%2==0) { 7 | long long int y = calMod(A, B/2, C); 8 | return (y*y)%C; 9 | } 10 | else { 11 | return (calMod(A, B-1, C)*(A%C))%C; 12 | } 13 | } 14 | int Solution::Mod(int A, int B, int C) { 15 | if(A==0) 16 | return 0; 17 | int res = calMod(A, B, C); 18 | if(res<0) 19 | return C+res; 20 | else 21 | return res; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Backtracking/nqueens.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/nqueens/ 2 | 3 | vector toString(const vector &board) { 4 | int n = board.size(); 5 | vector A(n); 6 | 7 | for(int i=0;i &board) { 14 | for(int queen=0;queen &board, vector > &result) { 20 | int A = board.size(); 21 | if(row==A) { 22 | result.push_back(toString(board)); 23 | return; 24 | } 25 | 26 | for(int col=0;col > Solution::solveNQueens(int A) { 34 | // Do not write main() function. 35 | // Do not read input, instead use the arguments to the function. 36 | // Do not print the output, instead return values as specified 37 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 38 | vector > boards; 39 | vector board(A); 40 | 41 | solveNQueensUtil(0, board, boards); 42 | 43 | return boards; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Backtracking/palindrome_partitioning.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/palindrome-partitioning/ 2 | 3 | bool isPalin(string A, int start, int final) { 4 | int i=start, j=final; 5 | 6 | while(i > &result, vector ¤t) { 15 | int n = A.size(); 16 | if(index>=n) { 17 | result.push_back(current); 18 | return; 19 | } 20 | 21 | for(int i=index;i > Solution::partition(string A) { 30 | // Do not write main() function. 31 | // Do not read input, instead use the arguments to the function. 32 | // Do not print the output, instead return values as specified 33 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 34 | 35 | vector > result; 36 | vector current; 37 | 38 | createPart(A, 0, result, current); 39 | 40 | return result; 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Backtracking/permutations.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/permutations/ 2 | 3 | int nextNum(vector &A) { 4 | int n = A.size(); 5 | int i = n-1; 6 | for(;i>0;i--) { 7 | if(A[i]>A[i-1]) 8 | break; 9 | } 10 | if(i==0) 11 | return 0; 12 | int index = i-1; 13 | 14 | int smallest = i; 15 | for(int j=i+1;j > Solution::permute(vector &A) { 28 | 29 | sort(A.begin(), A.end()); 30 | vector > result; 31 | result.push_back(A); 32 | if(A.size()<2) 33 | return result; 34 | while(1) { 35 | 36 | if(nextNum(A)) { 37 | result.push_back(A); 38 | } 39 | else 40 | break; 41 | } 42 | return result; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Backtracking/subset.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/subset/ 2 | 3 | vector > Solution::subsets(vector &A) { 4 | vector > res; 5 | int n = A.size(); 6 | int LIM = pow(2, n); 7 | 8 | for(int i=0;i temp; 10 | for(int j=0;j &A, int index, vector &set, vector > &sets) { 26 | if(index >= A.size()) { 27 | sets.push_back(set); 28 | return; 29 | } 30 | //ignore the current index 31 | generateSets(A, index+1, set, sets); 32 | //include the current index 33 | set.push_back(A[index]); 34 | generateSets(A, index+1, set, sets); 35 | set.pop_back(); 36 | } 37 | vector > Solution::subsets(vector &A) { 38 | 39 | vector set; 40 | vector > sets; 41 | sort(A.begin(), A.end()); 42 | generateSets(A, 0, set, sets); 43 | sort(sets.begin(), sets.end()); 44 | return sets; 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /Backtracking/subset_ii.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/subsets-ii/ 2 | 3 | vector > Solution::subsetsWithDup(vector &A) { 4 | int n = A.size(); 5 | vector > subs; 6 | int LIM = pow(2, n); 7 | for(int i=0;i temp; 9 | for(int j=0;j > res; 20 | res.push_back(subs[0]); 21 | for(int i=1;i &A, int index, vector ¤t, set > &sets) { 33 | if(index >= A.size()) { 34 | sets.insert(current); 35 | return; 36 | } 37 | generateSet(A, index+1, current, sets); 38 | current.push_back(A[index]); 39 | generateSet(A, index+1, current, sets); 40 | current.pop_back(); 41 | } 42 | vector > Solution::subsetsWithDup(vector &A) { 43 | sort(A.begin(), A.end()); 44 | 45 | vector > result; 46 | vector current; 47 | set > sets; 48 | 49 | generateSet(A, 0, current, sets); 50 | 51 | for(auto set: sets) 52 | result.push_back(set); 53 | return result; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Binary Search/.Median of Array.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mnpal/InterviewBit/055fac9ba797670be368768e49f8ac91aed3d22a/Binary Search/.Median of Array.cpp.swp -------------------------------------------------------------------------------- /Binary Search/Count_element_occurence.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/count-element-occurence/ 2 | 3 | int findIndex(const vector &A, int B) { 4 | int l=0, r = A.size()-1; 5 | int index = -1; 6 | while(l<=r) { 7 | int m = l+(r-l)/2; 8 | if(A[m]==B) { 9 | while(A[m]==B) 10 | m--; 11 | index = m+1; 12 | break; 13 | } 14 | else if(A[m]>B) 15 | r = m-1; 16 | else 17 | l = m+1; 18 | } 19 | return index; 20 | } 21 | int Solution::findCount(const vector &A, int B) { 22 | int index = findIndex(A, B); 23 | if(index == -1) 24 | return 0; 25 | int count = 0; 26 | while(A[index]==B && index A, int B) 4 | { 5 | int start = 0, end = A.size()-1; 6 | while(start <= end) 7 | { 8 | int mid = start + (end-start)/2; 9 | if(A[mid] == B) 10 | return 1; 11 | else if(A[mid] > B) 12 | end = mid-1; 13 | else 14 | start = mid+1; 15 | } 16 | return 0; 17 | } 18 | int Solution::searchMatrix(vector > &A, int B) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | int n = A.size(); 24 | int m = A[0].size(); 25 | int row; 26 | for(int i=0;i= B) 28 | { 29 | row = i; 30 | break; 31 | } 32 | return bsearch(A[row], B); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Binary Search/Matrix_median.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/matrix-median/ 2 | // Though the problem says extra memory not allowed, stil this gets accepted. 3 | 4 | int Solution::findMedian(vector > &A) { 5 | vector elem; 6 | 7 | for(int i=0;i &A, const vector &B, int med, int mid) { 2 | int n1 = upper_bound(A.begin(), A.end(), mid)-A.begin(); 3 | int n2 = upper_bound(B.begin(), B.end(), mid)-B.begin(); 4 | 5 | return (n1+n2>=med); 6 | } 7 | int getM(const vector &A, const vector &B, int med) { 8 | int l = min(A[0], B[0]); 9 | int r = max(A[A.size()-1], B[B.size()-1]); 10 | 11 | while(l>1; 13 | if(isValid(A, B, med, mid)) 14 | r = mid; 15 | else 16 | l = mid+1; 17 | } 18 | if(isValid(A, B, med, l)) 19 | return l; 20 | return r; 21 | } 22 | double Solution::findMedianSortedArrays(const vector &A, const vector &B) { 23 | // Do not write main() function. 24 | // Do not read input, instead use the arguments to the function. 25 | // Do not print the output, instead return values as specified 26 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 27 | 28 | int n1 = A.size(); 29 | int n2 = B.size(); 30 | 31 | if(n1==0 && n2==0) 32 | return 0; 33 | if(n1==0) { 34 | if(n2%2==0) 35 | return ((double)B[n2/2]+(double)B[n2/2-1])/2.00; 36 | else 37 | return B[n2/2]; 38 | } 39 | if(n2==0) { 40 | if(n1%2 ==0) 41 | return ((double)A[n1/2]+(double)A[n1/2-1])/2.00; 42 | else 43 | return A[n1/2]; 44 | } 45 | 46 | int tot = (n1+n2); 47 | 48 | if(tot%2) 49 | return getM(A, B, tot/2+1); 50 | return ((double)getM(A, B, tot/2+1)+(double)getM(A, B, tot/2))/2.00; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Binary Search/Rotated_array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rotated-array/ 2 | 3 | int Solution::findMin(const vector &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | int index = 0; 9 | 10 | int l = 0, r = A.size()-1; 11 | if(A[l]A[r]) 14 | return A[r]; 15 | 16 | while(l<=r) { 17 | int m = l+(r-l)/2; 18 | if(A[m-1]>A[m] && A[m]A[m]) 23 | r = m-1; 24 | else 25 | l = m+1; 26 | } 27 | return A[index]; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Binary Search/Rotated_array_sorted_search.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rotated-sorted-array-search/ 2 | 3 | int pivotFind(const vector &A){ 4 | int l = 0; 5 | int r = A.size()-1; 6 | int n = A.size(); 7 | 8 | while(l<=r) 9 | { 10 | int mid = l+(r-l)/2; 11 | int next = (mid+1)%n; 12 | int prev = (mid-1+n)%n; 13 | if(A[l]<=A[r]) 14 | return l; 15 | else if((A[mid]<=A[prev]) && (A[mid]<=A[next])) 16 | return mid; 17 | else if(A[mid]<=A[r]) 18 | r = mid-1; 19 | else 20 | l = mid+1; 21 | } 22 | return -1; 23 | } 24 | 25 | int bSearch(const vector &A, int B, int l, int r) 26 | { 27 | while(l<=r) 28 | { 29 | int mid = l+(r-l)/2; 30 | if(A[mid]==B) 31 | return mid; 32 | else if(A[mid]>B) 33 | r = mid-1; 34 | else 35 | l = mid+1; 36 | } 37 | return -1; 38 | } 39 | 40 | int Solution::search(const vector &A, int B) { 41 | // Do not write main() function. 42 | // Do not read input, instead use the arguments to the function. 43 | // Do not print the output, instead return values as specified 44 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 45 | 46 | int l = 0, r = A.size()-1; 47 | int pivot = pivotFind(A); 48 | if(A[pivot]==B) 49 | return pivot; 50 | int index = bSearch(A, B, 0, pivot-1); 51 | if(index==-1) 52 | index = bSearch(A, B, pivot, A.size()-1); 53 | return index; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Binary Search/Search_for_a_range.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/search-for-a-range/ 2 | 3 | int bin_search(const vector &A, int B, bool searchFirst) 4 | { 5 | int l = 0, r = A.size()-1, res = -1; 6 | 7 | while(l <= r) 8 | { 9 | int mid = l + (r-l)/2; 10 | 11 | if(A[mid] == B) 12 | { 13 | res = mid; 14 | if(searchFirst) 15 | r = mid-1; 16 | else 17 | l = mid+1; 18 | } 19 | else if(A[mid] > B) 20 | r = mid-1; 21 | else 22 | l = mid+1; 23 | } 24 | 25 | return res; 26 | } 27 | 28 | vector Solution::searchRange(const vector &A, int B) 29 | { 30 | // Do not write main() function. 31 | // Do not read input, instead use the arguments to the function. 32 | // Do not print the output, instead return values as specified 33 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 34 | 35 | vector v; 36 | v.push_back(bin_search(A, B, true)); 37 | v.push_back(bin_search(A, B, false)); 38 | 39 | return v; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Binary Search/Sorted_insert_position.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/sorted-insert-position/ 2 | 3 | int bSearch(vector &A, int B) 4 | { 5 | int l = 0, r = A.size()-1; 6 | while(l<=r) 7 | { 8 | int mid = l+(r-l)/2; 9 | if(A[mid]==B) 10 | return mid; 11 | else if(A[mid]>B) 12 | r = mid-1; 13 | else 14 | l = mid+1; 15 | } 16 | return -1; 17 | } 18 | int Solution::searchInsert(vector &A, int B) { 19 | // Do not write main() function. 20 | // Do not read input, instead use the arguments to the function. 21 | // Do not print the output, instead return values as specified 22 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 23 | int index = bSearch(A, B); 24 | if(index==-1) 25 | { 26 | int l = 0, r = A.size()-1; 27 | if(A[l]>B) 28 | return 0; 29 | else if(A[r]B) 37 | return mid; 38 | else if(A[mid]>B) 39 | r = mid-1; 40 | else 41 | l = mid+1; 42 | } 43 | } 44 | } 45 | return index; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Binary Search/Square_root_of_integer.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/square-root-of-integer/ 2 | 3 | int Solution::sqrt(int A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | double n = A; 9 | double error = 0.00000001; 10 | double s = n; 11 | while((s - n/s) > error) 12 | s = (s + n/s)/2; 13 | return int(s); 14 | } 15 | 16 | 17 | ######### OR ########## 18 | 19 | int Solution::sqrt(int A) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | if(A<2) 25 | return A; 26 | 27 | long long int l=1, r=A/2, n = (long long) A; 28 | 29 | while(l<=r) { 30 | long long int m = l+(r-l)/2; 31 | if(m*m==n) 32 | return m; 33 | else if(m*mn) 34 | return m; 35 | else if(m*m>n) 36 | r = m-1; 37 | else 38 | l = m+1; 39 | } 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Binary Search/allocate_books.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/allocate-books/ 2 | 3 | bool isValidConfig(vector &A, int B, int ans) { 4 | 5 | int students = 1; 6 | int current_pages = 0; 7 | int n = A.size(); 8 | 9 | for(int i=0;ians) { 11 | current_pages = A[i]; 12 | students++; 13 | if(students>B) 14 | return false; 15 | } 16 | else { 17 | current_pages += A[i]; 18 | } 19 | } 20 | return true; 21 | } 22 | 23 | int Solution::books(vector &A, int B) { 24 | 25 | int total_pages = 0; 26 | int s = 0, e = 0; 27 | int n = A.size(); 28 | 29 | if(B>n) 30 | return -1; 31 | 32 | for(int i=0;i &A) { 4 | int n = A.size(); 5 | long long bits[35] = {0}; 6 | int mod = pow(10,9)+7; 7 | for (int i=0;i<35;i++) { 8 | int cnt = 0; 9 | for (int j=0;j &A) { 4 | int n = A.size(); 5 | if(n==0) 6 | return A[0]; 7 | sort(A.begin(), A.end()); 8 | int d = A[0]^A[1]; 9 | 10 | for(int i=1;i &A) { 4 | int x = A[0]; 5 | int n = A.size(); 6 | 7 | for(int i=1;i &A) { 4 | 5 | int n = A.size(); 6 | int res = 0; 7 | 8 | for(int i=0;i<32;i++) { 9 | int bits = 0; 10 | for(int j=0;j &A, int x) { 4 | int count = 0; 5 | for(int i=0;i &A, int B) { 11 | int n = A.size(); 12 | 13 | int start = A[0], end = A[0], mid; 14 | for(auto a: A) { 15 | start = min(a, start); 16 | end = max(a, end); 17 | } 18 | if(n<=B) 19 | return end; 20 | else if(B==1) 21 | return start; 22 | 23 | int reqN; 24 | while(end-start > 1) { 25 | mid = (end+start)>>1; 26 | int count = F(A, mid); 27 | 28 | if(count==B) { 29 | reqN = mid; 30 | break; 31 | } 32 | else if(count > B) { 33 | end = mid; 34 | reqN = mid; 35 | } 36 | else 37 | start = mid; 38 | } 39 | 40 | int ans = INT_MIN; 41 | 42 | for(int i=0;i > &A) { 4 | 5 | int n = A.size(); 6 | int m = A[0].size(); 7 | 8 | int aux[n][m]; 9 | int area, ans = 0; 10 | 11 | for(int j=0;j sa(n+1, 0); 20 | 21 | for(int j=0;j=0) { 27 | while(sa[k]>0) { 28 | aux[i][t] = k; 29 | sa[k]--; 30 | t++; 31 | } 32 | k--; 33 | } 34 | 35 | for(int j=0;j A) { 4 | int n = A.size(); 5 | stack st; 6 | st.push(0); 7 | 8 | int top, area, max_area = 0, i=1; 9 | 10 | while(i A[i]) { 12 | top = st.top(); 13 | st.pop(); 14 | area = (st.empty())?(A[top]*i):(A[top]*(i-st.top()-1)); 15 | max_area = max(area, max_area); 16 | if(st.empty()) 17 | break; 18 | } 19 | st.push(i); 20 | ++i; 21 | } 22 | 23 | while(!st.empty()) { 24 | top = st.top(); 25 | st.pop(); 26 | area = (st.empty())?(A[top]*i):(A[top]*(i-st.top()-1)); 27 | max_area = max(area, max_area); 28 | } 29 | return max_area; 30 | } 31 | int Solution::maximalRectangle(vector > &A) { 32 | 33 | int n = A.size(); 34 | int m = A[0].size(); 35 | int ans = histArea(A[0]); 36 | 37 | for(int i=1;i=A.size()) 5 | return 1; 6 | 7 | if(A[ind]=='0') 8 | return 0; 9 | 10 | int res = decode(A, ind+1); 11 | string st = A.substr(ind, 2); 12 | if(stoi(st)<27 and stoi(st)>9) 13 | res += decode(A, ind+2); 14 | 15 | return res; 16 | } 17 | int Solution::numDecodings(string A) { 18 | return decode(A, 0); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Dynamic Programming/Word Break.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/word-break/ 2 | 3 | bool isAvail(string temp, vector &B) { 4 | for(auto b: B) 5 | if(b==temp) 6 | return true; 7 | return false; 8 | } 9 | int Solution::wordBreak(string A, vector &B) { 10 | int n = A.size(); 11 | vector dp(n, false); 12 | vector junc; 13 | junc.push_back(-1); 14 | 15 | for(int i=0;i=0;--j) { 19 | string temp = A.substr(junc[j]+1, i-junc[j]); 20 | if(isAvail(temp, B)) { 21 | flag = true; 22 | break; 23 | } 24 | } 25 | 26 | if(flag) { 27 | junc.push_back(i); 28 | dp[i] = true; 29 | } 30 | } 31 | return dp[n-1]; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Dynamic Programming/best_time_to_buy_and_sell_stocks_ii.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/best-time-to-buy-and-sell-stocks-ii/ 2 | 3 | int Solution::maxProfit(const vector &A) { 4 | int profit = 0; 5 | int n = A.size(); 6 | for(int i=1;i &A, int B) { 4 | int n = A.size(); 5 | vector ways(B+1, 0); 6 | ways[0] = 1; 7 | 8 | for(int i=0;i= A[i]) 11 | ways[j] = (ways[j]+ways[j-A[i]])%1000007; 12 | 13 | return ways[B]; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Dynamic Programming/distinct_subsequences.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/distinct-subsequences/ 2 | 3 | int Solution::numDistinct(string A, string B) { 4 | int n = A.size(); 5 | int m = B.size(); 6 | 7 | if(m>n) 8 | return 0; 9 | 10 | int dp[n+1][m+1]; 11 | 12 | for(int j=0;j<=m;j++) 13 | dp[0][j] = 0; 14 | 15 | for(int i=0;i<=n;i++) 16 | dp[i][0] = 1; 17 | 18 | for(int i=1;i<=n;i++) { 19 | for(int j=1;j<=m;j++) { 20 | if(A[i-1]!=B[j-1]) 21 | dp[i][j] = dp[i-1][j]; 22 | else 23 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j]; 24 | } 25 | } 26 | /* 27 | for(int i=0;i<=n;i++) { 28 | for(int j=0;j<=m;j++) 29 | cout << dp[i][j] << " "; 30 | cout << endl; 31 | } 32 | */ 33 | return dp[n][m]; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Dynamic Programming/dungeon_princess.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/dungeon-princess/ 2 | 3 | int Solution::calculateMinimumHP(vector > &A) { 4 | 5 | int n = A.size(); 6 | int m = A[0].size(); 7 | 8 | int dp[n][m]; 9 | 10 | dp[n-1][m-1] = (A[n-1][m-1]>=0)?0:abs(A[n-1][m-1]); 11 | 12 | for(int i=n-2;i>=0;i--) { 13 | int ans = dp[i+1][m-1] - A[i][m-1]; 14 | dp[i][m-1] = (ans>0)?ans:0; 15 | } 16 | 17 | for(int j=m-2;j>=0;j--) { 18 | int ans = dp[n-1][j+1] - A[n-1][j]; 19 | dp[n-1][j] = (ans>0)?ans:0; 20 | } 21 | 22 | for(int i=n-2;i>=0;i--) { 23 | for(int j=m-2;j>=0;j--) { 24 | int ans = min(dp[i+1][j], dp[i][j+1]) - A[i][j]; 25 | dp[i][j] = (ans>0)?ans:0; 26 | } 27 | } 28 | /* 29 | for(int i=0;i &A) { 4 | 5 | int n = A.size(); 6 | int mip = n-1; 7 | 8 | for(int i=n-2;i>=0;i--) { 9 | bool ans = false; 10 | 11 | if(i+A[i] >= mip) { 12 | mip = i; 13 | ans = true; 14 | } 15 | 16 | if(i==0) 17 | return ans; 18 | } 19 | return true; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Dynamic Programming/length_of_longest_subsequence.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/length-of-longest-subsequence/ 2 | 3 | int Solution::longestSubsequenceLength(const vector &A) { 4 | 5 | int n = A.size(); 6 | 7 | if(n<2) 8 | return n; 9 | 10 | vector incr(n, 1); 11 | 12 | for(int i=1;iA[j] && incr[i] decr(n, 1); 26 | 27 | for(int i=n-2;i>=0;i--) { 28 | int j = n-1; 29 | while(iA[j] && decr[i] &A) { 4 | int n = A.size(); 5 | 6 | int curr_max_prod = A[0]; 7 | int curr_min_prod = A[0]; 8 | int prev_max_prod = A[0]; 9 | int prev_min_prod = A[0]; 10 | int ans = A[0]; 11 | 12 | for(int i=1;i > &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int N = A.size(); 10 | 11 | if(N==0) 12 | return 0; 13 | 14 | int **dp = new int*[N]; 15 | for(int i=0;i &A, const vector &B, const vector &C) { 5 | int max_cap = 0; 6 | for(auto a: A) 7 | max_cap = max(max_cap, a); 8 | int n = B.size(); 9 | int m = max_cap; 10 | 11 | vector > T(n, vector (m+1)); 12 | 13 | for(int i=0;i > &A) { 4 | 5 | int n = A.size(); 6 | int m = A[0].size(); 7 | 8 | int dp[n][m]; 9 | memset(dp, 0, sizeof(dp)); 10 | 11 | for(int i=0;i &A, vector > &vis) { 7 | vis[r][c] = true; 8 | 9 | for(int i=0;i<4;++i) { 10 | int nr = r+dr[i]; 11 | int nc = c+dc[i]; 12 | if(nr<0 || nc<0 || nr>=n || nc>=m) 13 | continue; 14 | if(vis[nr][nc] || A[nr][nc]=='O') 15 | continue; 16 | dfs(nr, nc, A, vis); 17 | } 18 | } 19 | int Solution::black(vector &A) { 20 | n = A.size(); 21 | m = A[0].size(); 22 | int ans = 0; 23 | vector > vis(n, vector (m, false)); 24 | 25 | for(int i=0;i > &A, int x, int y) { 7 | A[x][y] = 'O'; 8 | 9 | for(int i=0;i<4;i++) { 10 | int nx = x+dx[i]; 11 | int ny = y+dy[i]; 12 | 13 | if(nx<0 || ny<0 || nx>=n || ny>=m) 14 | continue; 15 | 16 | if(A[nx][ny]=='-') 17 | dfs(A, nx, ny); 18 | } 19 | } 20 | void Solution::solve(vector > &A) { 21 | // Do not write main() function. 22 | // Do not read input, instead use the arguments to the function. 23 | // Do not print the output, instead return values as specified 24 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 25 | 26 | n = A.size(); 27 | m = A[0].size(); 28 | 29 | for(int i=0;i neighbors; 6 | * UndirectedGraphNode(int x) : label(x) {}; 7 | * }; 8 | */ 9 | UndirectedGraphNode *Solution::cloneGraph(UndirectedGraphNode *node) { 10 | 11 | map mp; 12 | queue q; 13 | q.push(node); 14 | 15 | mp[node] = new UndirectedGraphNode(node->label); 16 | 17 | while(!q.empty()) { 18 | UndirectedGraphNode* temp = q.front(); 19 | q.pop(); 20 | 21 | vector v(temp->neighbors); 22 | 23 | for(int i=0;ilabel); 26 | mp[v[i]] = copy; 27 | q.push(v[i]); 28 | } 29 | mp[temp]->neighbors.push_back(mp[v[i]]); 30 | } 31 | } 32 | return mp[node]; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Graph Data Structure & Algorithms/Commutable Islands.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/commutable-islands/ 2 | 3 | int find(int parent[], int i) { 4 | if(parent[i]==-1) 5 | return i; 6 | return find(parent, parent[i]); 7 | } 8 | void Union(int parent[], int x, int y) { 9 | int xp = find(parent, x); 10 | int yp = find(parent, y); 11 | parent[xp] = yp; 12 | } 13 | bool comp(vector a, vector b) { 14 | return a[2] < b[2]; 15 | } 16 | int Solution::solve(int A, vector > &B) { 17 | 18 | sort(B.begin(), B.end(), comp); 19 | 20 | int parent[A]; 21 | fill_n(parent, A, -1); 22 | 23 | int ans = 0; 24 | 25 | for(int i=0;i=1 && r<=N) && (c>=1 && c<=M)) 10 | return true; 11 | return false; 12 | } 13 | 14 | int Solution::knight(int A, int B, int r1, int c1, int r2, int c2) { 15 | int dr[] = {-2, -2, -1, 1, 2, 2, 1, -1}; 16 | int dc[] = {1, -1, -2, -2, -1, 1, 2, 2}; 17 | 18 | bool visited[A+1][B+1]; 19 | memset(visited, false, sizeof(visited)); 20 | visited[r1][c1] = true; 21 | queue q; 22 | cell *t = new cell(r1, c1, 0); 23 | q.push(t); 24 | 25 | int next_r, next_c; 26 | 27 | while(!q.empty()) { 28 | t = q.front(); 29 | q.pop(); 30 | 31 | if(t->row==r2 && t->col==c2) 32 | return t->dis; 33 | for(int i=0;i<8;i++) { 34 | next_r = t->row+dr[i]; 35 | next_c = t->col+dc[i]; 36 | 37 | if(isIn(next_r, next_c, A, B) && !visited[next_r][next_c]) { 38 | cell * st = new cell(next_r, next_c, t->dis+1); 39 | visited[next_r][next_c] = true; 40 | q.push(st); 41 | } 42 | } 43 | } 44 | return -1; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Graph Data Structure & Algorithms/Largest Distnace between nodes of a Tree.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/largest-distance-between-nodes-of-a-tree/ 2 | 3 | vector dijkstra(int s, vector adj[], int n) { 4 | 5 | vector dist(n, -1); 6 | dist[s] = 0; 7 | vector visited(n, false); 8 | queue q; 9 | q.push(s); 10 | visited[s] = true; 11 | 12 | while(!q.empty()) { 13 | s = q.front(); 14 | q.pop(); 15 | 16 | for(int i=0;i &A) { 30 | int n = A.size(); 31 | if(n<2) 32 | return 0; 33 | else if(n==2) 34 | return 1; 35 | vector adj[n]; 36 | 37 | for(int i=1;i dist(dijkstra(0, adj, n)); 43 | 44 | int maxD = *max_element(dist.begin(), dist.end()); 45 | int i = 0; 46 | for(;i Solution::solve(int A, int B, int C, int D) { 4 | 5 | vector numbers; 6 | 7 | if(D==0) 8 | return numbers; 9 | 10 | set st; 11 | st.insert(A); 12 | st.insert(B); 13 | st.insert(C); 14 | 15 | while(!st.empty()) { 16 | int curr = *st.begin(); 17 | st.erase(st.begin()); 18 | numbers.push_back(curr); 19 | if(numbers.size()==D) 20 | break; 21 | int p1 = curr*A; 22 | int p2 = curr*B; 23 | int p3 = curr*C; 24 | st.insert(p1); 25 | st.insert(p2); 26 | st.insert(p3); 27 | } 28 | return numbers; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Graph Data Structure & Algorithms/Stepping Numbers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/stepping-numbers/ 2 | 3 | void bfs(int A, int B, int num, vector &result) { 4 | queue q; 5 | q.push(num); 6 | 7 | while(!q.empty()) { 8 | int curr = q.front(); 9 | q.pop(); 10 | 11 | if(curr>=A && curr<=B) 12 | result.push_back(curr); 13 | 14 | if(curr==0 || curr>B) 15 | continue; 16 | 17 | int lastDig = curr%10; 18 | int n1 = curr*10 + lastDig + 1; 19 | int n2 = curr*10 + lastDig - 1; 20 | 21 | if(lastDig==0) 22 | q.push(n1); 23 | else if(lastDig==9) 24 | q.push(n2); 25 | else { 26 | q.push(n1); 27 | q.push(n2); 28 | } 29 | } 30 | } 31 | vector Solution::stepnum(int A, int B) { 32 | vector result; 33 | for(int i=0;i<=9;i++) 34 | bfs(A, B, i, result); 35 | sort(result.begin(), result.end()); 36 | return result; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Graph Data Structure & Algorithms/Valid path.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/valid-path/ 2 | 3 | int mat[101][101]; 4 | bool vis[101][101]; 5 | int n, m; 6 | int dr[] = {-1, -1, 0, 1, 1, 0, -1, 1}; 7 | int dc[] = {0, -1, -1, 0, 1, 1, 1, -1}; 8 | bool Utility(int x, int y, int r, int c) { 9 | if(r==x && c==y) 10 | return true; 11 | 12 | vis[r][c] = true; 13 | 14 | for(int i=0;i<8;i++) { 15 | int nr = r+dr[i]; 16 | int nc = c+dc[i]; 17 | if(nr<0 || nc<0 || nr>x || nc>y) 18 | continue; 19 | if(vis[nr][nc] || mat[nr][nc]==-1) 20 | continue; 21 | if(Utility(x, y, nr, nc)) 22 | return true; 23 | } 24 | return false; 25 | } 26 | string Solution::solve(int x, int y, int N, int R, vector &A, vector &B) { 27 | 28 | n = x+1; 29 | m = y+1; 30 | 31 | for(int i=0;i 1) 13 | return false; 14 | } 15 | if(count==1) 16 | return true; 17 | return false; 18 | } 19 | int Solution::ladderLength(string start, string end, vector &dictV) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | if(start==end) 25 | return 1; 26 | if(isAdjacent(start, end)) 27 | return 2; 28 | dictV.push_back(end); 29 | int n = dictV.size(); 30 | 31 | vector visited(n, false); 32 | queue q; 33 | Qitem item = {start, 1}; 34 | q.push(item); 35 | 36 | while(!q.empty()) { 37 | Qitem curr = q.front(); 38 | q.pop(); 39 | 40 | for(int i=0;i m; 4 | bool ok; 5 | string v; 6 | int n; 7 | int c; 8 | int dx[]={1,-1,0,0}; 9 | int dy[]={0,0,1,-1}; 10 | void solve(int i, int x, int y) { 11 | if(ok) 12 | return; 13 | if(i==v.size()) { 14 | ok = true; 15 | return; 16 | } 17 | 18 | for(int j=0;j<4;j++) { 19 | if(x+dx[j]<0 || x+dx[j]>=n || y+dy[j]<0 || y+dy[j]>=c) 20 | continue; 21 | if(m[x+dx[j]][y+dy[j]]==v[i]) 22 | solve(i+1, x+dx[j], y+dy[j]); 23 | } 24 | } 25 | 26 | int Solution::exist(vector &A, string B) { 27 | ok = false; 28 | n = A.size(); 29 | c = A[0].size(); 30 | m = A; 31 | v = B; 32 | for(int i=0;inext; 26 | while(fast!=tail && fast->next!=tail) { 27 | slow = slow->next; 28 | fast = fast->next->next; 29 | } 30 | 31 | TreeNode* root = new TreeNode(slow->val); 32 | root->left = balancedBST(head, slow); 33 | root->right = balancedBST(slow->next, tail); 34 | 35 | return root; 36 | } 37 | TreeNode* Solution::sortedListToBST(ListNode* A) { 38 | // Do not write main() function. 39 | // Do not read input, instead use the arguments to the function. 40 | // Do not print the output, instead return values as specified 41 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 42 | 43 | ListNode* tail, *temp = A; 44 | while(temp->next) { 45 | temp = temp->next; 46 | } 47 | tail = temp->next; 48 | 49 | return balancedBST(A, tail); 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Graph Data Structure & Algorithms/level_order.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/level-order/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | vector > Solution::levelOrder(TreeNode* A) { 13 | 14 | vector > levels; 15 | if(!A) 16 | return levels; 17 | vector level; 18 | queue q; 19 | q.push(A); 20 | q.push(NULL); 21 | 22 | while(!q.empty()) { 23 | A = q.front(); 24 | q.pop(); 25 | 26 | if(A==NULL) { 27 | if(!q.empty()) 28 | q.push(NULL); 29 | levels.push_back(level); 30 | level.clear(); 31 | } 32 | else { 33 | level.push_back(A->val); 34 | if(A->left) 35 | q.push(A->left); 36 | if(A->right) 37 | q.push(A->right); 38 | } 39 | } 40 | return levels; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Graph Data Structure & Algorithms/possibility_of_finishing_all_courses_given_pre-requisites.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/possibility-of-finishing-all-courses-given-prerequisites/# 2 | 3 | bool isCyclic(int v, vector &visited, vector &recStack, vector adj[]) { 4 | if(visited[v]==false) { 5 | visited[v] = true; 6 | recStack[v] = true; 7 | 8 | for(auto a: adj[v]) { 9 | if(!visited[a] && isCyclic(a, visited, recStack, adj)) 10 | return true; 11 | else if(recStack[a]) 12 | return true; 13 | } 14 | } 15 | recStack[v] = false; 16 | return false; 17 | } 18 | int Solution::solve(int A, vector &B, vector &C) { 19 | int V = A; 20 | int E = B.size(); 21 | 22 | vector adj [V]; 23 | vector visited(A, false); 24 | vector recStack(A, false); 25 | 26 | for(int i=0;i fib; 5 | fib.push_back(1); 6 | fib.push_back(1); 7 | 8 | for(int i=2;;i++) { 9 | int next_fib = fib[i-1] + fib[i-2]; 10 | if(next_fib > A) 11 | break; 12 | fib.push_back(next_fib); 13 | } 14 | 15 | int count = 0; 16 | int i = fib.size()-1; 17 | while(A>0) { 18 | while(A < fib[i]) 19 | i--; 20 | A = A - fib[i]; 21 | count++; 22 | } 23 | return count; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Greedy Algorithm/Assign Mice to Holes.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/assign-mice-to-holes/ 2 | 3 | int Solution::mice(vector &A, vector &B) { 4 | 5 | sort(A.begin(), A.end()); 6 | sort(B.begin(), B.end()); 7 | 8 | int ans = 0; 9 | 10 | for(int i=0;i &A) { 4 | int ans = 0; 5 | int i = 0; 6 | int state = 0; 7 | 8 | for(;i &A) { 4 | 5 | int n = A.size(); 6 | int choc[n]; 7 | fill_n(choc, n, 1); 8 | 9 | for(int i=1;iA[i-1]) 11 | choc[i]= choc[i-1]+1; 12 | } 13 | 14 | for(int i=n-2;i>=0;i--) 15 | if(A[i]>A[i+1] && choc[i]<=choc[i+1]) 16 | choc[i] = 1+choc[i+1]; 17 | 18 | int ans = 0; 19 | for(int i=0;i &A, const vector &B) { 4 | 5 | int gavail = 0; 6 | int i = 0, k; 7 | int n = A.size(); 8 | 9 | while(i=0) { 11 | int start = i; 12 | 13 | gavail = A[i]-B[i]; 14 | int j = (i+1)%n; 15 | k = i+1; 16 | 17 | while(gavail+(A[j]-B[j]) >= 0) { 18 | gavail += (A[j]-B[j]); 19 | //cout << gavail << endl; 20 | if(j==start) 21 | return start; 22 | 23 | j++; 24 | k++; 25 | if(j>=n) 26 | j = j%n; 27 | } 28 | i = j+1; 29 | } else 30 | i++; 31 | if(k>=n) 32 | break; 33 | } 34 | return -1; 35 | } 36 | 37 | 38 | ########## OR ############# 39 | 40 | int Solution::canCompleteCircuit(const vector &gas, const vector &cost) { 41 | 42 | int start = 0; 43 | int n = gas.size(); 44 | 45 | if(n==1) 46 | return 0; 47 | 48 | while(start < n) { 49 | int i = start; 50 | int curr_gas = 0; 51 | while(curr_gas+gas[i] > cost[i]) { 52 | curr_gas += gas[i]-cost[i]; 53 | i++; 54 | i = i%n; 55 | if(i==start) 56 | return start; 57 | } 58 | start++; 59 | } 60 | return -1; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Greedy Algorithm/Highest Product.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/highest-product/ 2 | 3 | int Solution::maxp3(vector &A) { 4 | sort(A.begin(), A.end()); 5 | 6 | int ans = INT_MIN; 7 | int n = A.size(); 8 | if(n<3) 9 | return 0; 10 | return max(A[0]*A[1]*A[n-1], A[n-1]*A[n-2]*A[n-3]); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Greedy Algorithm/Majority Element.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/majority-element/ 2 | 3 | int Solution::majorityElement(const vector &A) { 4 | 5 | // checking for candidate 6 | int count = 0; 7 | int candidate, n = A.size(); 8 | 9 | for(int i=0;in/2)?candidate:-1; 30 | } 31 | 32 | 33 | 34 | ######### OR ############ 35 | 36 | 37 | int Solution::majorityElement(const vector &A) { 38 | 39 | int n = A.size(); 40 | int count = 0, candid; 41 | 42 | for(int i=0;i A, int B) { 4 | for(int i=0;i Solution::twoSum(const vector &A, int B) { 11 | 12 | vector index; 13 | unordered_set hash; 14 | 15 | for(int i=0;i > Solution::fourSum(vector &A, int B) { 4 | sort(A.begin(), A.end()); 5 | vector > res; 6 | auto size = A.size(); 7 | for (auto i = 0; i0 && A[i] == A[i-1]) 10 | continue; 11 | for (auto j = i+1; ji+1 && A[j] == A[j-1]) 14 | continue; 15 | 16 | int ptr1 = j+1, ptr2 = size-1; 17 | while (ptr1 < ptr2) 18 | { 19 | auto sum = A[i] + A[j] + A[ptr1] + A[ptr2]; 20 | if (sum == B) 21 | { 22 | vector temp; 23 | temp.emplace_back(A[i]); 24 | temp.emplace_back(A[j]); 25 | temp.emplace_back(A[ptr1]); 26 | temp.emplace_back(A[ptr2]); 27 | res.emplace_back(temp); 28 | ++ptr1; 29 | while (ptr1 B) 33 | --ptr2; 34 | else if (sum < B) 35 | ++ptr1; 36 | } 37 | } 38 | } 39 | return res; 40 | } 41 | -------------------------------------------------------------------------------- /Hashing/Colorful_number.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/colorful-number/ 2 | 3 | int Solution::colorful(int A) { 4 | unordered_map mp; 5 | string st = to_string(A); 6 | 7 | for(int i=0;i=hasfound[A[end]]) 23 | cnt++; 24 | 25 | if(cnt==m) { 26 | while(shouldfind[A[start]]==0 || hasfound[A[start]]>shouldfind[A[start]]) { 27 | if(hasfound[A[start]]>shouldfind[A[start]]) 28 | hasfound[A[start]]--; 29 | start++; 30 | } 31 | 32 | if(ml > end-start+1) { 33 | ml = end-start+1; 34 | left = start; 35 | right = end; 36 | } 37 | 38 | } 39 | } 40 | return (ml==INT_MAX)?"":A.substr(left, ml); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Hashing/anagrams.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/anagrams/ 2 | 3 | vector > Solution::anagrams(const vector &A) { 4 | 5 | unordered_map > mp; 6 | vector index; 7 | for(int j=0;j > ang_groups; 22 | for(auto a=mp.begin();a!=mp.end();a++) { 23 | ang_groups.push_back(a->second); 24 | } 25 | sort(ang_groups.begin(), ang_groups.end()); 26 | return ang_groups; 27 | } 28 | 29 | 30 | ########## ANOTHER SOLUTION ############ 31 | 32 | vector > Solution::anagrams(const vector &A) { 33 | unordered_map > mp; 34 | for(int i=0;i > (st, {i+1})); 41 | } 42 | 43 | vector > result; 44 | for(auto it=mp.begin();it!=mp.end();it++) 45 | result.push_back(it->second); 46 | sort(result.begin(), result.end()); 47 | return result; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Hashing/diff_ii.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/diffk-ii/ 2 | 3 | int Solution::diffPossible(const vector &A, int B) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | if(A.size()<2) 10 | return 0; 11 | 12 | unordered_map hash; 13 | 14 | for(int i=0;i=0) { 20 | if(B==0) { 21 | if(hash[k]>1) 22 | return 1; 23 | } 24 | else if(hash[k]>0) 25 | return 1; 26 | } 27 | 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Hashing/equal.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/equal/ 2 | 3 | vector Solution::equal(vector &A) { 4 | 5 | vector result; 6 | vector ans; 7 | 8 | int n = A.size(); 9 | 10 | unordered_map > val; 11 | 12 | for(int i=0;i result) { 26 | ans.clear(); 27 | ans = result; 28 | } 29 | } 30 | } 31 | else { 32 | result.push_back(i); 33 | result.push_back(j); 34 | val[sum] = result; 35 | } 36 | result.clear(); 37 | } 38 | } 39 | 40 | return ans; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Hashing/fraction.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/fraction/ 2 | 3 | string Solution::fractionToDecimal(int n1, int n2) { 4 | string result; 5 | int64_t A = n1; 6 | int64_t B = n2; 7 | 8 | int an = 0, bn = 0; 9 | if(A==0) 10 | return "0"; 11 | 12 | if(A<0) { 13 | an = 1; 14 | A *= -1; 15 | } 16 | if(B<0) { 17 | bn = 1; 18 | B *= -1; 19 | } 20 | 21 | int df = 0; 22 | unordered_map mp; 23 | int64_t rem; 24 | while(1) { 25 | if(A==0) 26 | break; 27 | if(A0) { 39 | if(mp.find(A)==mp.end()) { 40 | mp.insert(pair (A, df)); 41 | } 42 | else { 43 | result += ')'; 44 | int i=0; 45 | while(result[i]!='.') { 46 | i++; 47 | if(i==result.size()) 48 | break; 49 | } 50 | if(i Solution::lszero(vector &A) { 4 | 5 | int ans = 0; 6 | int n = A.size(); 7 | unordered_map > mp; 8 | 9 | vector sum; 10 | 11 | for(int i=0;ifirst==0) { 27 | if(it->second.second==-1) 28 | final = it->second.first; 29 | else 30 | final = it->second.second; 31 | start = -1; 32 | } 33 | else if(it->second.second!=-1) { 34 | if((final-start)<(it->second.second-it->second.first)) { 35 | start = it->second.first; 36 | final = it->second.second; 37 | } 38 | if((final-start)==(it->second.second-it->second.first) && (start>it->second.first)) { 39 | start = it->second.first; 40 | final = it->second.second; 41 | } 42 | } 43 | } 44 | 45 | vector result; 46 | 47 | for(int i=start+1;i chars; 10 | int i = 0; 11 | while(i Solution::findSubstring(string A, const vector &B) { 4 | 5 | int x = B[0].size(); 6 | int n = B.size(); 7 | int m = A.size(); 8 | 9 | unordered_map mp; 10 | unordered_map copy; 11 | 12 | for(auto b: B) { 13 | if(mp.find(b)==mp.end()) 14 | mp.insert(pair (b, 1)); 15 | else 16 | mp[b]++; 17 | 18 | if(copy.find(b)==copy.end()) 19 | copy.insert(pair (b, 1)); 20 | else 21 | copy[b]++; 22 | } 23 | 24 | vector result; 25 | 26 | int i = 0; 27 | while(i0) { 36 | mp[s]--; 37 | } 38 | else 39 | break; 40 | } 41 | else 42 | break; 43 | } 44 | 45 | unordered_map :: iterator it; 46 | 47 | int count = 0; 48 | for(it=mp.begin();it!=mp.end();it++) 49 | if(it->second!=0) { 50 | count++; 51 | break; 52 | } 53 | 54 | if(count==0) 55 | result.push_back(i); 56 | 57 | mp.clear(); 58 | mp.insert(copy.begin(), copy.end()); 59 | i++; 60 | } 61 | return result; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /HeapsAndMaps/LRUCache.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/lru-cache/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | list lst; 7 | map mp; 8 | int cap; 9 | 10 | LRUCache::LRUCache(int capacity) { 11 | cap = capacity; 12 | lst.clear(); 13 | mp.clear(); 14 | } 15 | 16 | int LRUCache::get(int key) { 17 | if(mp.find(key)==mp.end()) 18 | return -1; 19 | 20 | lst.remove(key); 21 | lst.push_back(key); 22 | return mp[key]; 23 | } 24 | 25 | void LRUCache::set(int key, int value) { 26 | if(mp.find(key)!=mp.end()) { 27 | lst.remove(key); 28 | lst.push_back(key); 29 | mp[key] = value; 30 | return; 31 | } 32 | 33 | if(mp.size() < cap) { 34 | lst.push_back(key); 35 | mp[key] = value; 36 | return; 37 | } 38 | 39 | auto it = mp.find(lst.front()); 40 | lst.pop_front(); 41 | mp.erase(it); 42 | lst.push_back(key); 43 | mp[key] = value; 44 | } 45 | 46 | 47 | ########### OR ############ 48 | 49 | 50 | #include 51 | using namespace std; 52 | 53 | map mp; 54 | list lst; 55 | int sz; 56 | 57 | LRUCache::LRUCache(int capacity) { 58 | sz = capacity; 59 | mp.clear(); 60 | lst.clear(); 61 | } 62 | 63 | int LRUCache::get(int key) { 64 | if(mp.find(key)==mp.end()) 65 | return -1; 66 | 67 | lst.remove(key); 68 | lst.push_front(key); 69 | return mp[key]; 70 | } 71 | 72 | void LRUCache::set(int key, int value) { 73 | if(mp.find(key)!=mp.end()) { 74 | mp[key] = value; 75 | lst.remove(key); 76 | lst.push_front(key); 77 | return; 78 | } 79 | 80 | if(mp.size() < sz) { 81 | mp[key] = value; 82 | lst.push_front(key); 83 | return; 84 | } 85 | 86 | auto it = mp.find(lst.back()); 87 | mp.erase(it); 88 | lst.pop_back(); 89 | lst.push_front(key); 90 | mp[key] = value; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /HeapsAndMaps/distinct_numbers_in_window.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/distinct-numbers-in-window/ 2 | 3 | vector Solution::dNums(vector &A, int B) { 4 | 5 | unordered_map mp; 6 | vector res; 7 | 8 | int n = A.size(); 9 | 10 | if(n &B) { 4 | long long mod = pow(10, 9)+7; 5 | priority_queue pq; 6 | 7 | for(auto b: B) 8 | pq.push(b); 9 | 10 | long long ans = 0; 11 | 12 | for(int i=0;i &A) { 12 | // Do not write main() function. 13 | // Do not read input, instead use the arguments to the function. 14 | // Do not print the output, instead return values as specified 15 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 16 | ListNode* head = NULL; 17 | priority_queue, greater > pq; 18 | ListNode* temp; 19 | int n = A.size(); 20 | 21 | if(n==0) 22 | return head; 23 | 24 | for(int i=0;ival); 28 | temp = temp->next; 29 | } 30 | } 31 | 32 | temp = NULL; 33 | while(!pq.empty()) { 34 | ListNode* node = new ListNode(pq.top()); 35 | if(temp==NULL) { 36 | head = node; 37 | temp = head; 38 | } 39 | else { 40 | temp->next = node; 41 | temp = node; 42 | } 43 | pq.pop(); 44 | } 45 | 46 | 47 | return head; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Linked Lists/add_two_numbers_as_lists.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/add-two-numbers-as-lists/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | int findLen(ListNode* A) { 12 | int c = 0; 13 | while(A!=NULL) { 14 | c++; 15 | A = A->next; 16 | } 17 | return c; 18 | } 19 | void swap(int& x, int& y) { 20 | x ^= y; 21 | y ^= x; 22 | x ^= y; 23 | } 24 | ListNode* Solution::addTwoNumbers(ListNode* A, ListNode* B) { 25 | 26 | int n = findLen(A); 27 | int m = findLen(B); 28 | 29 | if(n==0) 30 | return B; 31 | if(m==0) 32 | return A; 33 | ListNode* temp; 34 | if(m>n) { 35 | temp = A; 36 | A = B; 37 | B = temp; 38 | swap(n, m); 39 | } 40 | int carry = 0; 41 | int d = A->val+B->val; 42 | ListNode* head = new ListNode(d%10); 43 | carry = d/10; 44 | A = A->next; 45 | B = B->next; 46 | temp = head; 47 | 48 | while(B!=NULL) { 49 | d = A->val+B->val+carry; 50 | carry = d/10; 51 | ListNode* node = new ListNode(d%10); 52 | temp->next = node; 53 | temp = node; 54 | A = A->next; 55 | B = B->next; 56 | } 57 | 58 | while(A!=NULL) { 59 | d = A->val+carry; 60 | carry = d/10; 61 | ListNode* node = new ListNode(d%10); 62 | temp->next = node; 63 | temp = node; 64 | A = A->next; 65 | } 66 | 67 | while(carry>0) { 68 | ListNode* node = new ListNode(carry%10); 69 | temp->next = node; 70 | temp = node; 71 | carry /= 10; 72 | } 73 | 74 | return head; 75 | } 76 | 77 | 78 | -------------------------------------------------------------------------------- /Linked Lists/insertion_sort_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/insertion-sort-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::insertionSortList(ListNode* A) { 12 | vector data; 13 | 14 | ListNode* temp = A; 15 | 16 | while(temp!=NULL) { 17 | data.push_back(temp->val); 18 | temp = temp->next; 19 | } 20 | 21 | int n = data.size(); 22 | for(int j=1;j=0 && data[i]>key) { 26 | data[i+1] = data[i]; 27 | i--; 28 | } 29 | data[i+1] = key; 30 | } 31 | 32 | 33 | 34 | ListNode* head = new ListNode(data[0]); 35 | temp = head; 36 | for(int i=1;inext = node; 39 | temp = node; 40 | } 41 | 42 | return head; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Linked Lists/intersection_of_linked_lists.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/intersection-of-linked-lists/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | int findLength(ListNode* A) { 12 | int c = 0; 13 | while(A!=NULL) { 14 | c++; 15 | A = A->next; 16 | } 17 | return c; 18 | } 19 | ListNode* Solution::getIntersectionNode(ListNode* A, ListNode* B) { 20 | // Do not write main() function. 21 | // Do not read input, instead use the arguments to the function. 22 | // Do not print the output, instead return values as specified 23 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 24 | int m = findLength(A); 25 | int n = findLength(B); 26 | 27 | if(m==0) 28 | return A; 29 | if(n==0) 30 | return B; 31 | 32 | if(n>m) { 33 | ListNode* temp = A; 34 | A = B; 35 | B = temp; 36 | m = findLength(A); 37 | n = findLength(B); 38 | } 39 | 40 | int d = m-n; 41 | for(int i=0;inext; 43 | 44 | while(A!=NULL && B!=NULL) { 45 | if(A == B) 46 | return A; 47 | A = A->next; 48 | B = B->next; 49 | } 50 | 51 | return NULL; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Linked Lists/k_reverse_linked_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/k-reverse-linked-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | int findLength(ListNode* A) { 12 | int c = 0; 13 | while(A) { 14 | c++; 15 | A = A->next; 16 | } 17 | return c; 18 | } 19 | ListNode* Solution::reverseList(ListNode* A, int B) { 20 | if(!A || !A->next || B==1) 21 | return A; 22 | 23 | ListNode* temp, *head, *tA, *ttA; 24 | 25 | int length = findLength(A); 26 | int n = length/B; 27 | 28 | for(auto i=1;i<=n;i++) { 29 | int k = 1; 30 | temp = A; 31 | tA = A; 32 | ListNode *curr = temp, *nxt = temp->next; 33 | while(knext; 36 | } 37 | ListNode* prev = temp->next; 38 | if(temp->next) 39 | A = temp->next; 40 | 41 | ListNode* start = temp; 42 | while(curr!=start) { 43 | curr->next = prev; 44 | prev = curr; 45 | curr = nxt; 46 | nxt = nxt->next; 47 | } 48 | curr->next = prev; 49 | 50 | if(i!=1) 51 | ttA->next = curr; 52 | if(i==1) 53 | head = curr; 54 | ttA = tA; 55 | } 56 | 57 | return head; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Linked Lists/list_cycle.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/list-cycle/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::detectCycle(ListNode* A) { 12 | // Do not write main() function. 13 | // Do not read input, instead use the arguments to the function. 14 | // Do not print the output, instead return values as specified 15 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 16 | if(A==NULL) 17 | return A; 18 | ListNode* slow = A; 19 | ListNode* fast = A; 20 | 21 | while(slow && fast && fast->next) { 22 | //cout << slow->val << " " << fast->val << endl; 23 | slow = slow->next; 24 | fast = fast->next->next; 25 | 26 | if(slow==fast) 27 | break; 28 | } 29 | if(slow==NULL || fast==NULL || fast->next==NULL) 30 | return NULL; 31 | ListNode* temp = A; 32 | while(temp!=slow) { 33 | temp = temp->next; 34 | slow = slow->next; 35 | } 36 | return slow; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Linked Lists/merge_two_sorted_lists.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/merge-two-sorted-lists/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::mergeTwoLists(ListNode* A, ListNode* B) { 12 | 13 | if(!A) 14 | return B; 15 | if(!B) 16 | return A; 17 | 18 | ListNode* head, *temp; 19 | 20 | head = temp = NULL; 21 | 22 | while(A && B) { 23 | if(A->val < B->val) { 24 | if(temp) 25 | temp->next = A; 26 | temp = A; 27 | A = A->next; 28 | if(!head) 29 | head = temp; 30 | } else { 31 | if(temp) 32 | temp->next = B; 33 | temp = B; 34 | B = B->next; 35 | if(!head) 36 | head = temp; 37 | } 38 | } 39 | 40 | if(A) 41 | temp->next = A; 42 | else 43 | temp->next = B; 44 | return head; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Linked Lists/palindrome_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/palindrome-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | int Solution::lPalin(ListNode* A) { 12 | vector data; 13 | 14 | ListNode* temp = A; 15 | 16 | while(temp!=NULL) { 17 | data.push_back(temp->val); 18 | temp = temp->next; 19 | } 20 | int n = data.size(); 21 | 22 | for(int i=0;inext) 14 | return A; 15 | 16 | vector values; 17 | 18 | ListNode* temp = A; 19 | while(temp) { 20 | values.push_back(temp->val); 21 | temp = temp->next; 22 | } 23 | 24 | vector s_values; 25 | 26 | for(int i=0;inext = head; 44 | head = node; 45 | } 46 | 47 | 48 | return head; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Linked Lists/remove_duplicates_from_sorted_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/remove-duplicates-from-sorted-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::deleteDuplicates(ListNode* A) { 12 | // Do not write main() function. 13 | // Do not read input, instead use the arguments to the function. 14 | // Do not print the output, instead return values as specified 15 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 16 | if(A==NULL || A->next==NULL) 17 | return A; 18 | 19 | ListNode* prev = A; 20 | ListNode* curr = A->next; 21 | 22 | while(curr!=NULL) { 23 | while(curr->val==prev->val) { 24 | curr = curr->next; 25 | if(curr==NULL) { 26 | prev->next = curr; 27 | return A; 28 | } 29 | } 30 | prev->next = curr; 31 | prev = curr; 32 | curr = curr->next; 33 | } 34 | return A; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Linked Lists/remove_nth_node_from_list_end.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/remove-nth-node-from-list-end/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | int findLen(ListNode* A) { 12 | int c=0; 13 | while(A!=NULL) { 14 | c++; 15 | A= A->next; 16 | } 17 | return c; 18 | } 19 | ListNode* Solution::removeNthFromEnd(ListNode* A, int B) { 20 | if(B==0) 21 | return A; 22 | int m = findLen(A); 23 | int n = m-B; 24 | 25 | if(B>m || n==0) 26 | return A->next; 27 | 28 | ListNode* temp = A, *prev; 29 | for(int i=0;inext; 32 | } 33 | prev->next = temp->next; 34 | return A; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Linked Lists/reorder_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/reorder-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::reorderList(ListNode* A) { 12 | 13 | ListNode* temp = A; 14 | if(temp==NULL) 15 | return temp; 16 | 17 | vector values; 18 | while(temp!=NULL) { 19 | values.push_back(temp->val); 20 | temp = temp->next; 21 | } 22 | 23 | vector newList; 24 | int n = values.size(); 25 | 26 | for(int i=0;inext = node; 41 | temp = node; 42 | } 43 | 44 | return head; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Linked Lists/reverse_linked_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/reverse-linked-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::reverseList(ListNode* A) { 12 | ListNode* prevNode = NULL; 13 | ListNode* nextNode, *currNode = A; 14 | 15 | if(A==NULL || A->next==NULL) 16 | return A; 17 | 18 | while(currNode!=NULL) { 19 | nextNode = currNode->next; 20 | currNode->next = prevNode; 21 | prevNode = currNode; 22 | currNode = nextNode; 23 | } 24 | A = prevNode; 25 | return A; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Linked Lists/reverse_linked_list_II.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/reverse-link-list-ii/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::reverseBetween(ListNode* A, int B, int C) { 12 | 13 | if(!A || !A->next) 14 | return A; 15 | 16 | ListNode* curr, *prev, *j1, *j2, *temp, *head; 17 | head = A; 18 | int k = 1; 19 | curr = A; 20 | prev = NULL; 21 | 22 | while(knext; 25 | k++; 26 | } 27 | j1 = curr; 28 | 29 | while(knext; 31 | k++; 32 | } 33 | j2 = curr; 34 | 35 | if(prev) 36 | prev->next = j2; 37 | prev = j2->next; 38 | 39 | curr = j1; 40 | temp = j1->next; 41 | 42 | while(prev!=j2) { 43 | curr->next = prev; 44 | prev = curr; 45 | curr = temp; 46 | if(temp!=j2) 47 | temp = temp->next; 48 | } 49 | if(B==1) 50 | head = j2; 51 | return head; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Linked Lists/rotate_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rotate-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | int findLength(ListNode* A) { 12 | int c = 0; 13 | while(A) { 14 | c++; 15 | A = A->next; 16 | } 17 | return c; 18 | } 19 | ListNode* Solution::rotateRight(ListNode* A, int B) { 20 | if(A==NULL || A->next==NULL) 21 | return A; 22 | int n = findLength(A); 23 | B = B%n; 24 | if(B==0) 25 | return A; 26 | int m = n-B; 27 | 28 | ListNode* temp = A; 29 | // Make the list circular 30 | while(temp->next!=NULL) { 31 | temp = temp->next; 32 | } 33 | temp->next = A; 34 | 35 | for(int i=0;inext; 38 | } 39 | temp->next = NULL; 40 | return A; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Linked Lists/sort_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/sort-list/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* mergeLists(ListNode* A, ListNode* B) { 12 | if(!B) 13 | return A; 14 | if(!A) 15 | return B; 16 | 17 | ListNode* head, *temp; 18 | if(A->val < B->val) { 19 | head = A; 20 | A = A->next; 21 | } 22 | else { 23 | head = B; 24 | B = B->next; 25 | } 26 | temp = head; 27 | 28 | while(A && B) { 29 | if(A->val < B->val) { 30 | temp->next = A; 31 | temp = A; 32 | A = A->next; 33 | } 34 | else { 35 | temp->next = B; 36 | temp = B; 37 | B = B->next; 38 | } 39 | } 40 | if(!A) { 41 | temp->next = B; 42 | } 43 | 44 | if(!B) { 45 | temp->next = A; 46 | } 47 | return head; 48 | } 49 | 50 | ListNode* Solution::sortList(ListNode* A) { 51 | 52 | if(!A || !A->next) 53 | return A; 54 | 55 | ListNode* p1 = A; 56 | ListNode* p2 = A->next; 57 | 58 | while(p2 && p2->next) { 59 | p1 = p1->next; 60 | p2 = p2->next->next; 61 | } 62 | 63 | p2 = p1->next; 64 | p1->next = NULL; 65 | 66 | return mergeLists(sortList(A), sortList(p2)); 67 | 68 | } 69 | 70 | -------------------------------------------------------------------------------- /Linked Lists/swap_list_nodes_in_pairs.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/swap-list-nodes-in-pairs/ 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * struct ListNode { 6 | * int val; 7 | * ListNode *next; 8 | * ListNode(int x) : val(x), next(NULL) {} 9 | * }; 10 | */ 11 | ListNode* Solution::swapPairs(ListNode* A) { 12 | 13 | if(!A || !A->next) 14 | return A; 15 | 16 | ListNode *curr, *prev, *pprev, *temp; 17 | pprev = NULL; 18 | prev = A; 19 | curr = A->next; 20 | temp = A->next->next; 21 | A = A->next; 22 | 23 | while(1) { 24 | prev->next = temp; 25 | curr->next = prev; 26 | 27 | if(pprev) 28 | pprev->next = curr; 29 | 30 | if(!temp || !temp->next) 31 | break; 32 | 33 | pprev = prev; 34 | prev = temp; 35 | curr = temp->next; 36 | temp = curr->next; 37 | } 38 | return A; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Math/Prime_Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/prime-sum/ 2 | 3 | int isPrime(int n) 4 | { 5 | for(int i=3;i<=sqrt(n);i++) 6 | if(n%i==0) 7 | return 0; 8 | return 1; 9 | } 10 | vector Solution::primesum(int A) { 11 | vector nums; 12 | if(A==4) 13 | { 14 | nums.push_back(2); 15 | nums.push_back(2); 16 | return nums; 17 | } 18 | for(int i=3;i 0) 6 | { 7 | A--; 8 | s = char(A%26 + 'A') + s; 9 | A /= 26; 10 | } 11 | return s; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Math/fizzbuzz.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/fizzbuzz/ 2 | 3 | vector Solution::fizzBuzz(int A) { 4 | vector data; 5 | 6 | for(int i=1;i<=A;i++) 7 | { 8 | if(i%3==0 && i%5==0) 9 | data.push_back("FizzBuzz"); 10 | else if(i%3==0) 11 | data.push_back("Fizz"); 12 | else if(i%5==0) 13 | data.push_back("Buzz"); 14 | else 15 | { 16 | stringstream ss; 17 | ss << i; 18 | string s = ss.str(); 19 | data.push_back(s); 20 | } 21 | } 22 | return data; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Math/greatest_common_divisor.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/greatest-common-divisor/ 2 | 3 | int Solution::gcd(int n, int m) { 4 | while(m!=0) 5 | { 6 | int r = n%m; 7 | n = m; 8 | m = r; 9 | } 10 | return n; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Math/grid_unique_paths.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/grid-unique-paths/ 2 | 3 | int Solution::uniquePaths(int A, int B) { 4 | long long ans = 1; 5 | for(int i=B;i0) 15 | { 16 | rem = i%10; 17 | num = rem + num*10; 18 | i = i/10; 19 | } 20 | if(num==A) 21 | return 1; 22 | else 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Math/power_of_two_integers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/power-of-two-integers/ 2 | 3 | int Solution::isPower(int A) { 4 | if(A==1) 5 | return 1; 6 | for(int i=2;i<=sqrt(A);i++) 7 | { 8 | float p = i; 9 | while(p <= A) 10 | { 11 | p *= i; 12 | if(p == A) 13 | return 1; 14 | 15 | } 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Math/rearrange_array.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/rearrange-array/ 2 | 3 | void Solution::arrange(vector &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | int n = A.size(); 9 | 10 | for(int i=0;i> num; 22 | return (nflag)?-num:num; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Math/sorted_permutation_rank.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/sorted-permutation-rank/ 2 | 3 | int d=1000003; 4 | int fac[1000000]; 5 | string B; 6 | int frank(char c) 7 | { 8 | int rank=B.find(c); 9 | B.erase(remove(B.begin(), B.end(), c), B.end()); 10 | return rank; 11 | } 12 | int Solution::findRank(string A) { 13 | int s=1; int n = A.size(); 14 | fac[0]=1; 15 | for(int i=1;i=d) 19 | s=s%d; 20 | fac[i]=s; 21 | } 22 | B= A; 23 | sort(B.begin(), B.end()); 24 | if(B==A) 25 | return 1; 26 | int i=0; 27 | long long count=0; int t=0; 28 | 29 | for(int i=0;ileft)+countNode(A->right); 14 | } 15 | int Solution::cntMatrix(TreeNode* A, TreeNode* B) { 16 | int diff = (countNode(B)-countNode(A)); 17 | return (diff<0)?-1:diff; 18 | } 19 | -------------------------------------------------------------------------------- /Ninja Test/get_mode_array_updates.cpp: -------------------------------------------------------------------------------- 1 | typedef pair pii; 2 | 3 | struct compare { 4 | bool operator()(pii a, pii b) { 5 | if(a.second==b.second) 6 | return a.first < b.first; 7 | return a.second > b.second; 8 | } 9 | }; 10 | // bool comp(pair a, pair b) { 11 | // if(a.second==b.second) 12 | // return a.first < b.first; 13 | // return a.second > b.second; 14 | // } 15 | vector Solution::getMode(vector &A, vector > &B) { 16 | set st; 17 | map mp; 18 | for(int i=0;ifirst, it1->second})); 27 | mp[A[i]]++; 28 | st.insert({A[i], mp[A[i]]}); 29 | } 30 | } 31 | 32 | vector res; 33 | 34 | for(int i=0;ifirst, it1->second})); 40 | mp[A[l]]--; 41 | 42 | if(mp[A[l]]==0) { 43 | mp.erase(mp.find(A[l])); 44 | } else { 45 | st.insert({A[l], mp[A[l]]}); 46 | } 47 | 48 | A[l] = r; 49 | 50 | if(mp.find(r)==mp.end()) { 51 | st.insert({r, 1}); 52 | mp[r] = 1; 53 | } 54 | else { 55 | auto it1 = mp.find(r); 56 | st.erase(st.find({it1->first, it1->second})); 57 | mp[r]++; 58 | st.insert({r, mp[r]}); 59 | } 60 | auto it = st.begin(); 61 | res.push_back((*it).first); 62 | } 63 | 64 | return res; 65 | } 66 | -------------------------------------------------------------------------------- /Ninja Test/increasing_subarrays.cpp: -------------------------------------------------------------------------------- 1 | #define MOD 1e9+7 2 | 3 | int Solution::cntInc(vector &A) { 4 | int i=1, n = A.size(); 5 | if(n<1) 6 | return 0; 7 | // if(allSame(A)) 8 | // return n; 9 | vector > ans; 10 | 11 | while(i temp; 13 | if(A[i] > A[i-1]) { 14 | if(A[i-1]) 15 | temp.push_back(i-1); 16 | while(iA[i-1]) { 17 | i++; 18 | temp.push_back(i-1); 19 | } 20 | 21 | } 22 | if(temp.size()) 23 | ans.push_back(temp); 24 | i++; 25 | } 26 | int res = 0; 27 | 28 | for(auto a: ans) { 29 | int temp = a.size()*(a.size()+1)/2-a.size(); 30 | res = fmod((res + fmod(temp, MOD)),MOD); 31 | } 32 | res = fmod(res+n, MOD); 33 | return res; 34 | } 35 | -------------------------------------------------------------------------------- /Ninja Test/maximum_longest_common_subsequence.cpp: -------------------------------------------------------------------------------- 1 | void LCS(string A, string B, vector &ans) { 2 | int n = A.size(); 3 | int m = B.size(); 4 | 5 | int dp[n+1][m+1]; // 2D vector to store LCS value, dp[i][j] stores LCS of first i character of A and first j characters of B 6 | 7 | for(int i=0;i<=n;i++) { 8 | for(int j=0;j<=m;j++) { 9 | if(i==0 || j==0) 10 | dp[i][j] = 0; 11 | else if(A[i-1]==B[j-1]) 12 | dp[i][j] = 1+dp[i-1][j-1]; 13 | else 14 | dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 15 | } 16 | } 17 | 18 | // find out the maximal value and the corresponding index by traversing the 2D vector. 19 | // Traversal is done either from the first charcter of A and last character of B, or from last character of A and first character of B. 20 | for(int i=1,j=n;i<=n;j--,i++) { 21 | if(ans[1] < dp[i][j]) { // When the current maximal value exceeds the stored maximal value, update the stored value. 22 | ans[1] = dp[i][j]; 23 | ans[0] = i; 24 | } 25 | } 26 | 27 | } 28 | vector Solution::maxLCS(string A) { 29 | // to store answer 30 | vector ans(2); 31 | ans[0] = 1; // stores j value for maximal LCS 32 | ans[1] = 0; // stores maximal LCS value 33 | 34 | string B = A; 35 | reverse(B.begin(), B.end()); 36 | B.pop_back(); // trim last character of the reversed string 37 | A.pop_back(); // trim last character of the original string 38 | 39 | /* 40 | 41 | Let's say we have "abbc" as A. 42 | We have to calcute LCS for the following combinations. 43 | A | B 44 | ------ 45 | a bbc 46 | ab bc 47 | abb c 48 | 49 | You can notice that last character in the first column i.e. 'c' and first character in the 2nd column i.e. 'a' are of no use. 50 | That's why, they have been trimmed earlier from A and B respectively. 51 | 52 | */ 53 | 54 | LCS(A, B, ans); // calculate LCS 55 | 56 | 57 | return ans; 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![alt text](https://dajh2p2mfq4ra.cloudfront.net/assets/header_logo_color-8d918751c3952f4b43dba8f398bd5a511072c71e9bd1473d474bdc124287e517.png.gz) 2 | 3 | # [InterviewBit](https://www.interviewbit.com/) Solutions 4 | 5 | ## About 6 | This repository contains solutions for the problems given in Interviewbit Programming section. And, the codes are merely functions you need to write to solve the given problems. So, in order to run these on the c++ compilers, you first need to write down the comlete main function. 7 | 8 | You can have a look at the Programming problems [here](https://www.interviewbit.com/courses/programming/). 9 | 10 | ## Languages 11 | The codes are written in C and C++. 12 | 13 | ## Profile 14 | Here is my [InterviewBit Profile](https://www.interviewbit.com/profile/manishpal). 15 | 16 | ## Contribution 17 | If you wish to contribute, just fork this repository, do the modifications and make a pull request. And, please don't forget to mention about the modifications clearly. 18 | 19 | -------------------------------------------------------------------------------- /Stacks And Queues/Generate_all_parentheses.cpp: -------------------------------------------------------------------------------- 1 | void print(stack st) { 2 | while(!st.empty()) { 3 | cout << st.top() << " "; 4 | st.pop(); 5 | } 6 | cout << endl; 7 | } 8 | int Solution::isValid(string A) { 9 | 10 | stack st; 11 | 12 | auto n = A.size(); 13 | auto i = 0; 14 | if(A[i]==')' || A[i]=='}' || A[i]==']') 15 | return 0; 16 | while(i Solution::slidingMaximum(const vector &A, int B) { 4 | deque dq; 5 | vector ans; 6 | 7 | int i=0; 8 | for(;i=B) 18 | dq.pop_front(); 19 | 20 | while(!dq.empty() && A[dq.back()] st; 4 | priority_queue, greater > pq; 5 | 6 | MinStack::MinStack() { 7 | while(!st.empty()) 8 | st.pop(); 9 | while(!pq.empty()) 10 | pq.pop(); 11 | } 12 | 13 | void MinStack::push(int x) { 14 | st.push(x); 15 | if(pq.empty()) 16 | pq.push(x); 17 | else { 18 | if(x<=pq.top()) 19 | pq.push(x); 20 | else 21 | pq.push(pq.top()); 22 | } 23 | } 24 | 25 | void MinStack::pop() { 26 | if(!st.empty()) { 27 | pq.pop(); 28 | st.pop(); 29 | } 30 | } 31 | 32 | int MinStack::top() { 33 | if(!st.empty()) 34 | return st.top(); 35 | return -1; 36 | } 37 | 38 | int MinStack::getMin() { 39 | if(!pq.empty()) 40 | return pq.top(); 41 | return -1; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /Stacks And Queues/nearest_smallest_element.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/nearest-smaller-element/ 2 | 3 | vector Solution::prevSmaller(vector &A) 4 | { 5 | vector res; 6 | res.push_back(-1); 7 | stack S; 8 | 9 | S.push(A[0]); 10 | 11 | for(int i=1;inext) 14 | return A; 15 | 16 | vector values; 17 | 18 | ListNode* temp = A; 19 | while(temp) { 20 | values.push_back(temp->val); 21 | temp = temp->next; 22 | } 23 | 24 | vector s_values; 25 | 26 | for(int i=0;inext = head; 44 | head = node; 45 | } 46 | 47 | 48 | return head; 49 | } 50 | -------------------------------------------------------------------------------- /Stacks And Queues/redundant_braces.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/redundant-braces/ 2 | 3 | int Solution::braces(string B) { 4 | int n = B.size(); 5 | 6 | string A; 7 | for(int i=0;i st; 15 | 16 | for(int i=0;i0 && i+1 st; 6 | 7 | int l = A.length(); 8 | 9 | for(int i=0;i!=l;i++) 10 | st.push(A[i]); 11 | 12 | A = ""; 13 | while(!st.empty()) { 14 | A += st.top(); 15 | st.pop(); 16 | } 17 | return A; 18 | } 19 | -------------------------------------------------------------------------------- /Stacks And Queues/simpify_directory_path.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/simplify-directory-path/ 2 | 3 | string Solution::simplifyPath(string A) { 4 | stack st; 5 | 6 | int n = A.size(); 7 | for(int i=1;i st; 43 | 44 | auto i=0; 45 | while(i>=1; 8 | } 9 | return bits; 10 | } 11 | 12 | bool multiplication_is_safe(uint32_t a, uint32_t b) { 13 | size_t a_bits=highestOneBitPosition(a), b_bits=highestOneBitPosition(b); 14 | return (a_bits+b_bits<=32); 15 | } 16 | 17 | 18 | 19 | int Solution::atoi(const string A) { 20 | 21 | int n = A.size(); 22 | int num = 0, i=0; 23 | 24 | while(A[i]==' ') 25 | i++; 26 | int d = A[i]-'0'; 27 | //check for garbage characters 28 | if((A[i]!='-' && A[i]!='+') && (d<0 || d>9)) 29 | return 0; 30 | int flag = 0; 31 | if(A[i]=='-') { 32 | flag = 1; 33 | i++; 34 | } 35 | 36 | if(A[i]=='+') 37 | i++; 38 | 39 | for(;i=0 && d<=9) { 43 | 44 | if(multiplication_is_safe(num, 10)) 45 | num = num*10 + d; 46 | else if(num>0 && flag==0) 47 | return INT_MAX; 48 | else 49 | return INT_MIN; 50 | } 51 | 52 | else if(d<0 || d>0) { 53 | if(num!=0 && flag==1) 54 | return -num; 55 | return num; 56 | } 57 | } 58 | if(flag) 59 | return -num; 60 | return num; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Strings/compare_version_numbers.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/compare-version-numbers/ 2 | 3 | #include 4 | int Solution::compareVersion(string A, string B) { 5 | int n, m; 6 | n = A.size(); 7 | m = B.size(); 8 | 9 | int i, j; 10 | i = j = 0; 11 | 12 | while(is1[j]) { 28 | check = flag2 = true; 29 | } else if(s2[j]s2.size()) 40 | return 1; 41 | if(s2.size()>s1.size()) 42 | return -1; 43 | 44 | if(s1==s2) { 45 | flag1 = flag2 = false; 46 | } 47 | 48 | if(flag1) 49 | return 1; 50 | else if(flag2) 51 | return -1; 52 | 53 | i++; 54 | j++; 55 | } 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Strings/count_and_say.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/count-and-say/ 2 | 3 | string Solution::countAndSay(int n) { 4 | string data; 5 | data = "1"; 6 | if(n==1) 7 | return data; 8 | for(int i=1;i=data.size()) 17 | j++; 18 | 19 | else 20 | { 21 | while(data[j]==data[j+1]) 22 | { 23 | freq++; 24 | j++; 25 | if(j+1>=data.size()) 26 | break; 27 | } 28 | j++; 29 | 30 | } 31 | //cout << int(data[j-1])-int('0') << endl; 32 | s = s + to_string(freq) + data[j-1]; 33 | //num = num*100 + freq*10 + int(data[j-1])-int('0'); 34 | } 35 | 36 | data = s; 37 | } 38 | return data; 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Strings/implement_strstr.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/implement-strstr/ 2 | 3 | void preComp(string A, vector &F){ 4 | int m = A.size(); 5 | int j=0,i=1; 6 | F[0] = 0; 7 | while(i pre(pat_len); 43 | 44 | preComp(needle, pre); 45 | 46 | int i=0; // for haystack 47 | int j=0; // for needle 48 | 49 | while(i &A) { 4 | string pref = ""; 5 | int n = A.size(); 6 | 7 | if(n==1) 8 | return A[0]; 9 | 10 | int j = 0, flg = 1; 11 | while(flg) 12 | { 13 | char c = A[0][j]; 14 | for(int i=1;i=0 && high maxLen) { 16 | start = low; 17 | maxLen = high-low+1; 18 | } 19 | --low; 20 | ++high; 21 | } 22 | 23 | low = i-1; 24 | high = i+1; 25 | while(low>=0 && high maxLen) { 27 | start = low; 28 | maxLen = high-low+1; 29 | } 30 | --low; 31 | ++high; 32 | } 33 | } 34 | return A.substr(start, maxLen); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Strings/minimum_characters_required_to_make_a_string_palindromic.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/minimum-characters-required-to-make-a-string-palindromic/ 2 | 3 | vector prefixTable(string A) { 4 | 5 | int n = A.size(); 6 | vector F(n); 7 | F[0] = 0; 8 | int i = 1, j=0; 9 | 10 | while(i=0;i--) 33 | s += A[i]; 34 | 35 | return A + "$" + s; 36 | 37 | } 38 | 39 | int Solution::solve(string A) { 40 | 41 | string st = reverseString(A); 42 | int n = A.size(); 43 | int m = st.size(); 44 | vector F = prefixTable(st); 45 | 46 | return n-F[m-1]; 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Strings/power_of_2.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/power-of-2/ 2 | 3 | string divideBy2(string A) { 4 | 5 | int n = A.size(), i=0, p=0; 6 | string result = ""; 7 | while(i Solution::prettyJSON(string A) { 4 | 5 | vector result; 6 | 7 | int n = A.size(); 8 | int i = 0; 9 | 10 | string t(1, A[i]); 11 | result.push_back(t); 12 | i++; 13 | while(A[i]==' ') 14 | i++; 15 | 16 | string tabs; 17 | 18 | while(i roman_value; 5 | roman_value['I'] = 1; 6 | roman_value['V'] = 5; 7 | roman_value['X'] = 10; 8 | roman_value['L'] = 50; 9 | roman_value['C'] = 100; 10 | roman_value['D'] = 500; 11 | roman_value['M'] = 1000; 12 | 13 | int num = 0, curr = 0, next = 0; 14 | 15 | if(A.size()==0) 16 | return 0; 17 | 18 | if(A.size()==1) 19 | return roman_value[A[0]]; 20 | 21 | 22 | for(int i=0;i curr) 28 | num = num - curr; 29 | else 30 | num = num + curr; 31 | } 32 | num += next; 33 | return num; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Strings/valid_ip_addresses.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/valid-ip-addresses/ 2 | 3 | bool isValid(string A) { 4 | vector ips; 5 | string s; 6 | int i=0; 7 | while(i3 || stoi(ip)<0 || stoi(ip)>255) 19 | return false; 20 | if(ip.size()>1 && (stoi(ip)==0 || ip[0]=='0')) 21 | return false; 22 | } 23 | return true; 24 | } 25 | vector Solution::restoreIpAddresses(string A) { 26 | 27 | vector ans; 28 | if(A.size()>12 || A.size()<1) 29 | return ans; 30 | int n = A.size(); 31 | string ip = A; 32 | for(int i=1;i=0 && A[n]==' ') 17 | --n; 18 | A = A.substr(0, n+1); 19 | n = A.size(); 20 | 21 | int i = 1, flg_e = 0, flg_d = 0; 22 | if(A[0]<'0' || A[0]>'9') 23 | { 24 | if(A[0]!='.' && A[0]!='-') 25 | return 0; 26 | } 27 | if(A[n-1]<'0' || A[n-1]>'9') 28 | return 0; 29 | 30 | while(i'9') && A[i]!='e' && A[i]!='.' && A[i]!='-') 33 | return 0; 34 | else if(A[i]=='e') 35 | { 36 | flg_e++; 37 | if(flg_e>1) 38 | return 0; 39 | } 40 | else if(A[i]=='-') 41 | { 42 | if(A[i-1]!='e') 43 | return 0; 44 | } 45 | else if(A[i]=='.') 46 | { 47 | flg_d++; 48 | if(flg_d>1 || flg_e) 49 | return 0; 50 | if(i+1'9')) 51 | return 0; 52 | } 53 | i++; 54 | } 55 | return 1; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Strings/zigzag_string.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/zigzag-string/ 2 | 3 | string Solution::convert(string A, int B) { 4 | 5 | vector data(B); 6 | if(B==1) 7 | return A; 8 | int a = 0, c, n=A.size(); 9 | 10 | for(int i=0;i Solution::inorderTraversal(TreeNode* A) { 11 | stack st; 12 | vector values; 13 | while(1) { 14 | while(A) { 15 | st.push(A); 16 | A = A->left; 17 | } 18 | if(st.empty()) 19 | break; 20 | A = st.top(); 21 | st.pop(); 22 | values.push_back(A->val); 23 | A = A->right; 24 | } 25 | return values; 26 | } 27 | -------------------------------------------------------------------------------- /Trees/Least Common Ancestor.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/least-common-ancestor/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | /** 13 | * Definition for binary tree 14 | * struct TreeNode { 15 | * int val; 16 | * TreeNode *left; 17 | * TreeNode *right; 18 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 19 | * }; 20 | */ 21 | 22 | 23 | bool find(TreeNode* A, int B) { 24 | if(!A) 25 | return false; 26 | queue q; 27 | q.push(A); 28 | 29 | while(!q.empty()) { 30 | A = q.front(); 31 | q.pop(); 32 | if(A->val == B) 33 | return true; 34 | if(A->left) 35 | q.push(A->left); 36 | if(A->right) 37 | q.push(A->right); 38 | } 39 | return false; 40 | } 41 | TreeNode* LCA(TreeNode* A, int B, int C) { 42 | if(!A || A->val==B || A->val==C) 43 | return A; 44 | TreeNode* left = LCA(A->left, B, C); 45 | TreeNode* right = LCA(A->right, B, C); 46 | 47 | if(left && right) 48 | return A; 49 | return (left?left:right); 50 | } 51 | 52 | int Solution::lca(TreeNode* A, int B, int C) { 53 | 54 | if(!find(A, B) || !find(A, C)) 55 | return -1; 56 | 57 | TreeNode* ancestor = LCA(A, B, C); 58 | if(ancestor) 59 | return ancestor->val; 60 | return -1; 61 | } 62 | -------------------------------------------------------------------------------- /Trees/Max Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int Solution::maxDepth(TreeNode* A) { 11 | if(A==NULL) 12 | return 0; 13 | int l_depth = maxDepth(A->left); 14 | int r_depth = maxDepth(A->right); 15 | if(l_depth > r_depth) 16 | return l_depth+1; 17 | return r_depth+1; 18 | } 19 | -------------------------------------------------------------------------------- /Trees/Min Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int Solution::minDepth(TreeNode* A) { 11 | 12 | if(A==NULL) 13 | return 0; 14 | queue q; 15 | q.push(A); 16 | q.push(NULL); 17 | TreeNode* root = A; 18 | int depth = 0; 19 | 20 | while(!q.empty()) { 21 | A = q.front(); 22 | q.pop(); 23 | 24 | if(A==NULL) { 25 | depth++; 26 | if(!q.empty()) 27 | q.push(NULL); 28 | } 29 | else if(A->left==NULL && A->right==NULL) 30 | return depth+1; 31 | else { 32 | if(A->left) 33 | q.push(A->left); 34 | 35 | if(A->right) 36 | q.push(A->right); 37 | 38 | } 39 | } 40 | return depth; 41 | } 42 | -------------------------------------------------------------------------------- /Trees/Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | vector Solution::postorderTraversal(TreeNode* A) { 11 | stack st; 12 | vector data; 13 | if(!A) 14 | return data; 15 | st.push(A); 16 | 17 | TreeNode* prev = NULL; 18 | while(!st.empty()) { 19 | TreeNode* curr = st.top(); 20 | 21 | if(!prev || prev->left==curr || prev->right==curr) { 22 | if(curr->left) 23 | st.push(curr->left); 24 | else if(curr->right) 25 | st.push(curr->right); 26 | } 27 | else if(curr->left==prev) { 28 | if(curr->right) 29 | st.push(curr->right); 30 | } 31 | else { 32 | data.push_back(curr->val); 33 | st.pop(); 34 | } 35 | prev = curr; 36 | } 37 | return data; 38 | } 39 | ` -------------------------------------------------------------------------------- /Trees/Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | vector Solution::preorderTraversal(TreeNode* A) { 11 | 12 | vector result; 13 | stack st; 14 | 15 | while(1) { 16 | while(A) { 17 | result.push_back(A->val); 18 | st.push(A); 19 | A = A->left; 20 | } 21 | if(st.empty()) 22 | break; 23 | A = st.top(); 24 | st.pop(); 25 | A = A->right; 26 | } 27 | return result; 28 | } 29 | -------------------------------------------------------------------------------- /Trees/ZigZag Level Order Traversal BT.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | vector > Solution::zigzagLevelOrder(TreeNode* A) { 11 | queue q; 12 | q.push(A); 13 | q.push(NULL); 14 | //int level = 1; 15 | vector< vector > nodes; 16 | vector temp; 17 | while(!q.empty()) { 18 | A = q.front(); 19 | q.pop(); 20 | 21 | if(A) 22 | temp.push_back(A->val); 23 | 24 | if(A==NULL) { 25 | //level *= -1; 26 | nodes.push_back(temp); 27 | temp.clear(); 28 | if(!q.empty()) 29 | q.push(NULL); 30 | } 31 | else { 32 | if(A->left) 33 | q.push(A->left); 34 | if(A->right) 35 | q.push(A->right); 36 | } 37 | } 38 | for(int i=1;i &A, int l, int r, int num) { 13 | int i = l; 14 | for(;i &A, vector &B, int l, int r, int *PI) { 21 | if(l>=r) 22 | return NULL; 23 | 24 | TreeNode* root = new TreeNode(B[*PI]); 25 | (*PI)--; 26 | int index = findIndex(A, l, r, root->val); 27 | 28 | root->right = buildBTree(A, B, index+1, r, PI); 29 | root->left = buildBTree(A, B, l, index, PI); 30 | return root; 31 | } 32 | 33 | TreeNode* Solution::buildTree(vector &A, vector &B) { 34 | 35 | int l = 0; 36 | int r = A.size(); 37 | int PI = r-1; 38 | if(r==0) 39 | return NULL; 40 | 41 | TreeNode* root = buildBTree(A, B, l, r, &PI); 42 | 43 | return root; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Trees/construct_binary_tree_from_inorder_and_preorder.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/construct-binary-tree-from-inorder-and-preorder/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | int findIndex(vector &A, int l, int r, int num ) { 13 | int i = 0; 14 | for(i=l;i &A, vector &B, int l, int r, int *PI) { 20 | if(l==r) 21 | return NULL; 22 | 23 | TreeNode* root = new TreeNode(A[*PI]); 24 | (*PI)++; 25 | 26 | int index = findIndex(B, l, r, root->val); 27 | 28 | root->left = buildBTree(A, B, l, index, PI); 29 | root->right = buildBTree(A, B, index+1, r, PI); 30 | 31 | return root; 32 | } 33 | TreeNode* Solution::buildTree(vector &A, vector &B) { 34 | 35 | int l = 0; 36 | int r = A.size(); 37 | int PI = 0; 38 | 39 | if(r==0) 40 | return NULL; 41 | TreeNode* root = buildBTree(A, B, l, r, &PI); 42 | return root; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Trees/flatten_binary_tree_to_linked_list.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/flatten-binary-tree-to-linked-list/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | void flattenTree(TreeNode* A) { 13 | if(!A || (!A->left && !A->right)) 14 | return; 15 | if(A->left) { 16 | flattenTree(A->left); 17 | TreeNode* temp = A->right; 18 | A->right = A->left; 19 | A->left = NULL; 20 | TreeNode* t = A->right; 21 | while(t->right) 22 | t = t->right; 23 | t->right = temp; 24 | } 25 | flattenTree(A->right); 26 | } 27 | TreeNode* Solution::flatten(TreeNode* A) { 28 | // Do not write main() function. 29 | // Do not read input, instead use the arguments to the function. 30 | // Do not print the output, instead return values as specified 31 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 32 | flattenTree(A); 33 | 34 | return A; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Trees/identical_binary_trees.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/identical-binary-trees/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | int Solution::isSameTree(TreeNode* A, TreeNode* B) { 13 | 14 | if(A==NULL && B==NULL) 15 | return 1; 16 | if(A==NULL || B==NULL) 17 | return 0; 18 | return (A->val==B->val && isSameTree(A->left, B->left) && isSameTree(A->right, B->right)); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Trees/inorder_traversal_of_cartesian_tree.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/inorder-traversal-of-cartesian-tree/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | TreeNode* buildBTree(vector &A, int l, int r) { 13 | if(l==r) 14 | return NULL; 15 | 16 | int maxm = *max_element(A.begin()+l, A.begin()+r); 17 | 18 | int dist; 19 | for(int i=l;ileft = buildBTree(A, l, dist); 27 | root->right = buildBTree(A, dist+1, r); 28 | 29 | return root; 30 | } 31 | TreeNode* Solution::buildTree(vector &A) { 32 | 33 | int n = A.size(); 34 | if(n==0) 35 | return NULL; 36 | 37 | TreeNode* root = buildBTree(A, 0, n); 38 | 39 | return root; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Trees/invert_the_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/invert-the-binary-tree/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | TreeNode* Solution::invertTree(TreeNode* A) { 13 | if(A) { 14 | invertTree(A->left); 15 | invertTree(A->right); 16 | TreeNode* temp = A->left; 17 | A->left = A->right; 18 | A->right = temp; 19 | } 20 | return A; 21 | } 22 | -------------------------------------------------------------------------------- /Trees/kth_smallest_element_in_tree.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/kth-smallest-element-in-tree/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | void inorder(TreeNode* A, vector &data) { 13 | if(!A) 14 | return; 15 | inorder(A->left, data); 16 | data.push_back(A->val); 17 | inorder(A->right, data); 18 | } 19 | int Solution::kthsmallest(TreeNode* A, int B) { 20 | vector data; 21 | inorder(A, data); 22 | return data[B-1]; 23 | } 24 | -------------------------------------------------------------------------------- /Trees/path_sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/path-sum/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | int Solution::hasPathSum(TreeNode* A, int B) { 13 | 14 | if(A==NULL) 15 | return (B==0)?1:0; 16 | else { 17 | int sum = B-(A->val); 18 | if((A->left && A->right) || (!A->left && !A->right)) 19 | return (hasPathSum(A->left, sum) || hasPathSum(A->right, sum)); 20 | else if(A->left) 21 | return hasPathSum(A->left, sum); 22 | else 23 | return hasPathSum(A->right, sum); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Trees/populate_next_right_pointers_tree.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/populate-next-right-pointers-tree/ 2 | 3 | /** 4 | * Definition for binary tree with next pointer. 5 | * struct TreeLinkNode { 6 | * int val; 7 | * TreeLinkNode *left, *right, *next; 8 | * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 9 | * }; 10 | */ 11 | void Solution::connect(TreeLinkNode* A) { 12 | queue q; 13 | 14 | if(!A) 15 | return; 16 | 17 | q.push(A); 18 | q.push(NULL); 19 | 20 | TreeLinkNode *prev = NULL; 21 | 22 | while(!q.empty()) { 23 | A = q.front(); 24 | q.pop(); 25 | 26 | if(A==NULL) { 27 | if(!q.empty()) 28 | q.push(NULL); 29 | } 30 | else { 31 | if(A->left) 32 | q.push(A->left); 33 | if(A->right) 34 | q.push(A->right); 35 | } 36 | if(prev) 37 | prev->next = A; 38 | prev = A; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Trees/root_to_leaf_paths_with_sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | void findPaths(TreeNode* A, int B, vector > &paths, vector &path) { 11 | if(!B && !A->left && !A->right) { 12 | paths.emplace_back(path); 13 | } 14 | 15 | if(A->left) { 16 | path.emplace_back(A->left->val); 17 | findPaths(A->left, B-A->left->val, paths, path); 18 | path.pop_back(); 19 | } 20 | 21 | if(A->right) { 22 | path.emplace_back(A->right->val); 23 | findPaths(A->right, B-A->right->val, paths, path); 24 | path.pop_back(); 25 | } 26 | 27 | } 28 | 29 | vector > Solution::pathSum(TreeNode* A, int B) { 30 | vector > paths; 31 | vector path; 32 | 33 | if(!A) 34 | return paths; 35 | path.emplace_back(A->val); 36 | 37 | findPaths(A, B-A->val, paths, path); 38 | 39 | return paths; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Trees/shortest_unique_prefix.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/shortest-unique-prefix/ 2 | 3 | struct TrieNode { 4 | struct TrieNode *children[26]; 5 | bool isEndOfWord; 6 | int freq; 7 | TrieNode(): isEndOfWord(false), freq(0) { 8 | for(int i=0;i<26;i++) 9 | children[i] = NULL; 10 | } 11 | }; 12 | 13 | void insert(TrieNode* root, string key) { 14 | 15 | TrieNode* temp = root; 16 | 17 | for(int i=0;ichildren[index]) { 21 | temp->children[index] = new TrieNode(); 22 | } 23 | temp = temp->children[index]; 24 | temp->freq++; 25 | } 26 | temp->isEndOfWord = true; 27 | } 28 | 29 | void findUniPref(TrieNode* root, vector &prefix, string key) { 30 | 31 | TrieNode* temp = root; 32 | int sz = key.size(); 33 | 34 | string pref; 35 | 36 | for(int i=0;ichildren[index]->freq <= 1) 40 | break; 41 | temp = temp->children[index]; 42 | } 43 | prefix.push_back(pref); 44 | } 45 | 46 | bool search(TrieNode* root, vector &prefix, string key) { 47 | TrieNode* temp = root; 48 | 49 | for(int i=0;ichildren[index]) 52 | return false; 53 | temp = temp->children[index]; 54 | } 55 | return (temp && temp->isEndOfWord); 56 | } 57 | 58 | vector Solution::prefix(vector &A) { 59 | 60 | TrieNode* root = new TrieNode(); 61 | 62 | for(auto a: A) 63 | insert(root, a); 64 | 65 | vector prefix; 66 | 67 | for(auto a: A) 68 | findUniPref(root, prefix, a); 69 | 70 | return prefix; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Trees/sorted_array_to_balanced_BST.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/sorted-array-to-balanced-bst/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | TreeNode* buildBST(int l, int r, const vector &A) { 13 | if(l==r) 14 | return NULL; 15 | int mid = (l+r)>>1; 16 | TreeNode* root = new TreeNode(A[mid]); 17 | root->left = buildBST(l, mid, A); 18 | root->right = buildBST(mid+1, r, A); 19 | 20 | return root; 21 | } 22 | TreeNode* Solution::sortedArrayToBST(const vector &A) { 23 | // Do not write main() function. 24 | // Do not read input, instead use the arguments to the function. 25 | // Do not print the output, instead return values as specified 26 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 27 | 28 | int l = 0; 29 | int r = A.size(); 30 | 31 | TreeNode* root = buildBST(l, r, A); 32 | return root; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Trees/sum_root_to_leaf_numbers.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | void findPaths(TreeNode* A, vector > &paths, vector &path) { 11 | if(!A->left && !A->right) 12 | paths.push_back(path); 13 | 14 | if(A->left) { 15 | path.push_back(A->left->val); 16 | findPaths(A->left, paths, path); 17 | path.pop_back(); 18 | } 19 | 20 | if(A->right) { 21 | path.push_back(A->right->val); 22 | findPaths(A->right, paths, path); 23 | path.pop_back(); 24 | } 25 | } 26 | 27 | int Solution::sumNumbers(TreeNode* A) { 28 | vector > paths; 29 | vector path; 30 | 31 | if(!A) 32 | return 0; 33 | 34 | path.push_back(A->val); 35 | findPaths(A, paths, path); 36 | 37 | int result = 0; 38 | for(auto p:paths) { 39 | int sum = 0; 40 | for(auto i: p) 41 | sum = (sum*10 + i)%1003; 42 | result = (result + sum)%1003; 43 | } 44 | return result; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Trees/symmetric_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/symmetric-binary-tree/ 2 | 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | 13 | vector inOrder(TreeNode* A) { 14 | vector data; 15 | if(!A) 16 | return data; 17 | stack st; 18 | while(1) { 19 | while(A) { 20 | st.push(A); 21 | A = A->left; 22 | } 23 | if(st.empty()) 24 | break; 25 | A = st.top(); 26 | st.pop(); 27 | data.push_back(A->val); 28 | A = A->right; 29 | } 30 | return data; 31 | } 32 | int isPalin(vector data) { 33 | int n = data.size(); 34 | for(int i=0;i data = inOrder(A); 42 | //cout << data.size(); 43 | if(isPalin(data)) 44 | return 1; 45 | return 0; 46 | } 47 | 48 | 49 | 50 | ########## OR ########### 51 | 52 | 53 | /** 54 | * Definition for binary tree 55 | * struct TreeNode { 56 | * int val; 57 | * TreeNode *left; 58 | * TreeNode *right; 59 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 60 | * }; 61 | */ 62 | int util(TreeNode* A, TreeNode* B) { 63 | if(!A && !B) 64 | return 1; 65 | if(!A || !B) 66 | return 0; 67 | if(A->val!=B->val) 68 | return 0; 69 | return util(A->left, B->right) && util(A->right, B->left); 70 | } 71 | int Solution::isSymmetric(TreeNode* A) { 72 | if(!A) 73 | return 1; 74 | if(!A->left && !A->right) 75 | return 1; 76 | return util(A->left, A->right); 77 | } 78 | 79 | -------------------------------------------------------------------------------- /Two Pointers/3_sum.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/3-sum/ 2 | 3 | int Solution::threeSumClosest(vector &A, int B) { 4 | 5 | int n = A.size(); 6 | int minDiff = INT_MAX; 7 | int bestSum = 0; 8 | sort(A.begin(), A.end()); 9 | 10 | for(int i=0;i abs(sum-B)) { 17 | minDiff = abs(sum-B); 18 | bestSum = sum; 19 | } 20 | if(sum==B) 21 | return bestSum; 22 | else if(sum > B) 23 | k--; 24 | else 25 | j++; 26 | } 27 | } 28 | return bestSum; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Two Pointers/3_sum_zero.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/3-sum-zero/ 2 | 3 | vector > Solution::threeSum(vector &A) { 4 | vector > res; 5 | if(A.size()<3) { 6 | return res; 7 | } 8 | set> triplets; 9 | sort(A.begin(), A.end()); 10 | for(int i=0;i elem; 13 | while(j0) 28 | k--; 29 | else 30 | j++; 31 | } 32 | } 33 | 34 | set >::iterator it; 35 | for(it=triplets.begin();it!=triplets.end();it++) 36 | res.push_back(*it); 37 | 38 | return res; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Two Pointers/Counting_triangles.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/counting-triangles/ 2 | 3 | int Solution::nTriang(vector &A) { 4 | sort(A.begin(), A.end()); 5 | long long int d = pow(10, 9); 6 | 7 | int count = 0; 8 | int n = A.size(); 9 | for(int i=0;i &A, int d) { 4 | int l=0, r=A.size()-1; 5 | 6 | int res = -1; 7 | 8 | while(l<=r) { 9 | int m = l+(r-l)/2; 10 | if(A[m]==d) { 11 | res = m; 12 | break; 13 | } 14 | else if(A[m]>d) 15 | r = m-1; 16 | else 17 | l = m+1; 18 | } 19 | return res; 20 | } 21 | int Solution::diffPossible(vector &A, int B) { 22 | 23 | if(A.size()<2) 24 | return 0; 25 | 26 | for(int i=0;i Solution::intersect(const vector &A, const vector &B) { 4 | vector res; 5 | int i=0,j=0; 6 | while(iB[j]) 8 | j++; 9 | else if(A[i] &A, vector &B) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | vector R; 10 | int i=0, j=0; 11 | int n = A.size(), m = B.size(); 12 | 13 | if(n==0) { 14 | A = B; 15 | return; 16 | } 17 | 18 | if(m==0) 19 | return; 20 | 21 | while(i &A, vector &B) { 48 | // Do not write main() function. 49 | // Do not read input, instead use the arguments to the function. 50 | // Do not print the output, instead return values as specified 51 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 52 | 53 | auto n = A.size(); 54 | auto m = B.size(); 55 | 56 | auto i=0, j=0; 57 | while(A[i]>B[j] && j=A.size()) 72 | break; 73 | } 74 | while(i>=n && j &A, vector &B, vector &C) { 4 | auto min_diff = INT_MAX; 5 | 6 | for(int i=0;i &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | for(int i=0;i &A) { 23 | // Do not write main() function. 24 | // Do not read input, instead use the arguments to the function. 25 | // Do not print the output, instead return values as specified 26 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 27 | int i=0, j=2; 28 | if(A.size()<=2) 29 | return A.size(); 30 | while(j &A, int B) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int n = A.size(); 10 | 11 | A.erase(remove(A.begin(), A.end(), B), A.end()); 12 | return A.size(); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Two Pointers/sort_by_color.cpp: -------------------------------------------------------------------------------- 1 | // https://www.interviewbit.com/problems/sort-by-color/ 2 | 3 | void Solution::sortColors(vector &A) { 4 | // Do not write main() function. 5 | // Do not read input, instead use the arguments to the function. 6 | // Do not print the output, instead return values as specified 7 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 8 | 9 | int n = A.size(); 10 | 11 | int hash[3] = {0}; 12 | 13 | for(int i=0;i &A) { 30 | // Do not write main() function. 31 | // Do not read input, instead use the arguments to the function. 32 | // Do not print the output, instead return values as specified 33 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 34 | 35 | sort(A.begin(), A.end()); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /nano.save: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | list lst; 5 | map mp; 6 | int sz; 7 | 8 | LRUCache::LRUCache(int capacity) { 9 | sz = capacity; 10 | mp.clear(); 11 | lst.clear(); 12 | } 13 | 14 | 15 | --------------------------------------------------------------------------------