├── .gitignore ├── .vscode ├── c_cpp_properties.json ├── launch.json ├── settings.json └── tasks.json ├── ARRAY_or_VECTOR.txt ├── Amazon_Wow ├── Amazon_Food_Stall.cpp ├── Amazon_Intern.cpp └── Amazon_Lost_in_the_forest.cpp ├── Array ├── .gitignore ├── 118.pascals-triangle.cpp ├── 1200.minimum-absolute-difference.cpp ├── 128.longest-consecutive-sequence.cpp ├── 1465.maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.cpp ├── 162.find-peak-element.cpp ├── 167.two-sum-ii-input-array-is-sorted.cpp ├── 1752.check-if-array-is-sorted-and-rotated.cpp ├── 1863.sum-of-all-subset-xor-totals.cpp ├── 189.rotate-array.cpp ├── 2012.sum-of-beauty-in-the-array.cpp ├── 238.product-of-array-except-self.cpp ├── 26.remove-duplicates-from-sorted-array.cpp ├── 268.missing-number.cpp ├── 287.find-the-duplicate-number.cpp ├── 31.next-permutation.cpp ├── 34.find-first-and-last-position-of-element-in-sorted-array.cpp ├── 349.intersection-of-two-arrays.cpp ├── 376.wiggle-subsequence.cpp ├── 387.first-unique-character-in-a-string.cpp ├── 442.find-all-duplicates-in-an-array.cpp ├── 485.max-consecutive-ones.cpp ├── 56.merge-intervals.cpp ├── 665.non-decreasing-array.cpp ├── 74.search-a-2-d-matrix.cpp ├── 80.remove-duplicates-from-sorted-array-ii.cpp ├── 88.merge-sorted-array.cpp ├── A_Ezzat_and_Two_Subsequences_CODEFORCE.cpp ├── A_Not_Shading.cpp ├── A_PizzaForces_CODEFORCES.cpp ├── Chef_Restaurant_.cpp ├── Generate_probability_GoldmanSachs.cpp ├── Largest_Sum_Of_Contiguous_Subarray_KADANES_ALGO.cpp ├── Moore_majority_Voting_ALGORITHM_FREQUENCY.cpp ├── Pascal_Triangle.cpp ├── Prefix_Permutation.cpp ├── Remove_Missing_and_find_Repeating_number.cpp ├── The_One_On_The_Last_Night.cpp ├── The_One_with_All_the_Candy.cpp └── tempCodeRunnerFile.cpp ├── AtCoder ├── .cph │ ├── .A_AtCoder_Quiz_2.cpp_d9891d3d879f4f468f0674df2f068408.prob │ ├── .A_Tires.cpp_0757f159a350d7bf06b62f08e2030fb2.prob │ ├── .A_Your_First_Judge.cpp_7a8e767c7419e5e2dd3e579c775d9761.prob │ ├── .B_How_many_.cpp_786c81352a672b86b29445feb6571a50.prob │ ├── .B_Maritozzo.cpp_553cb0e44d711db49b2ec8d7256c4931.prob │ ├── .B_Maritozzo.java_a79a99950b6224447a9790958d7000d1.prob │ ├── .B_String_Shifting.cpp_9197aad554b831920fb23b6eeedfa329.prob │ ├── .B_log2_N_.cpp_31e4479abe22eb50764ffe420765e33b.prob │ ├── .C_Distribution.cpp_d4555b81e64f430e151d2cb329ce874a.prob │ ├── .C_Neo_lexicographic_Ordering.cpp_0fa9f4c5c356fe6c2ed419cc834ce53b.prob │ ├── .C_One_More_aab_aba_baa.cpp_1d9f0f816bd91a34bd12d2634812a7df.prob │ ├── .Chef_And_Salary_Pay.cpp_f37a9eae3c90121a0dad7adc91999784.prob │ ├── .D_Coprime_2.cpp_18eca90b6f1bd7e72d5f1f4748312d0a.prob │ ├── .D_Cutting_Woods.cpp_95201b9b63ba0460ba08a834cabc1d32.prob │ ├── .D_Restricted_Permutation.cpp_15a611efbf9dd117b9d8fda33417fcd7.prob │ ├── .D_Strange_Lunchbox.cpp_c6a06efd4e7ed75efaba9547257a40bc.prob │ ├── .Pass_or_Fail.cpp_93d5cc7166d8434548dac158db569f32.prob │ └── .Professor_And_Directions.cpp_831a33a2bfcf3f37d66868e0d45beed7.prob ├── .vscode │ └── settings.json ├── A_AtCoder_Quiz_2.cpp ├── A_Lexicographic_Order.cpp ├── A_Tires.cpp ├── A_Your_First_Judge.cpp ├── AtCoder_contest1.cpp ├── B_AtCoder_Quiz.cpp ├── B_How_many_.cpp ├── B_Maritozzo.cpp ├── B_String_Shifting.cpp ├── B_log2_N_.cpp ├── C_Distribution.cpp ├── C_Inverse_of_Permutation.cpp ├── C_Neo_lexicographic_Ordering.cpp ├── C_One_More_aab_aba_baa.cpp ├── D_Coprime_2._IMPORTANTcpp ├── D_Cutting_Woods.cpp ├── D_Restricted_Permutation.cpp ├── D_Strange_Lunchbox.cpp └── LegalMove_ATCODER.cpp ├── Binary Search.txt ├── Binary_Search ├── .cph │ ├── .Pick_the_developers.cpp_de1269323ffd4f0684b4ed035d6a1b3a.prob │ ├── .Swap_to_make_Event.cpp_85685b5436e08686be58277b25bb292b.prob │ └── .War.cpp_f94e38154884a2dc1e79f03df8e2baf6.prob ├── .gitignore ├── .vscode │ └── settings.json ├── 74.search-a-2-d-matrix.cpp ├── 875.koko-eating-bananas.java ├── Aggressive_Cows_SPOJ.cpp ├── Allocate_Minimum_Number_Of_Pages_IMPORTANT.cpp ├── Binary_Search.cpp ├── Binary_Search_Recursion.cpp ├── Bitonic_Array_IMPORTANT.cpp ├── Book_Allocation_Problem.cpp ├── Find_X_in_Infinitely_Sorted_Array.cpp ├── Floor_and_Ceil_of_Element.cpp ├── Kth_Strongest_team.cpp ├── Minuimum_Difference_in_Sorted_Array.cpp ├── Occurence_of_the_Element.cpp ├── Peak_Element_IMPORTANT.cpp ├── Rotated_Sorted_Array_IMPORTANT.cpp ├── Searching_In_Nearly_Sorted_Array.cpp └── tempCodeRunnerFile.cpp ├── Bits.cpp ├── Bitwise_Tag_Less ├── 1009.complement-of-base-10-integer.cpp ├── 258.add-digits.cpp ├── 268.missing-number.cpp ├── 476.number-complement.cpp ├── Bits.cpp ├── Reverse_Only_Letters_IMPORTANT.cpp └── Xor_Palindrome.cpp ├── CodeForces ├── .cph │ ├── .A_ABC.cpp_fcc9bb94d612267790af938cca8cdf00.prob │ ├── .A_Dislike_of_Threes.cpp_3a01fcedae9f83dc8b4ed8170b240b93.prob │ ├── .A_PizzaForces_CODEFORCES.cpp_d3706562df61c534ba26fd3a6c1905ea.prob │ ├── .A_The_Miracle_and_the_Sleeper.cpp_eb7c8c326e65bb66ac1419b3979cd734.prob │ ├── .B_Roof_Construction.cpp_e1a43c26b274f68c054f8bce1713ffed.prob │ ├── .B_Scenes_From_a_Memory.cpp_c8b7b073e5aaeb32441f4f76cc33ca22.prob │ ├── .B_Who_s_Opposite_.cpp_a18e85dbeeb64a6a6c29f42edb81d1d1.prob │ ├── .D_Productive_Meeting.cpp_1b75318ae7ef5600954f82b28e87c99d.prob │ ├── .E1_Permutation_Minimization_by_Deque.cpp_4dac8df6b731e6d03e1d1230998bbc99.prob │ └── .E2_Distance_Tree_hard_version_.cpp_28dce4f4e4760821cd3bcf238725b53d.prob ├── .gitignore ├── .vscode │ └── settings.json ├── A_ABC.cpp ├── A_Casimir_s_String_Solitaire.cpp ├── A_Dislike_of_Threes.cpp ├── A_Ezzat_and_Two_Subsequences_CODEFORCE.cpp ├── A_PizzaForces_CODEFORCES.cpp ├── A_The_Miracle_and_the_Sleeper.cpp ├── B_Roof_Construction.cpp ├── B_Scenes_From_a_Memory.cpp ├── B_Who_s_Opposite_.cpp ├── D_Productive_Meeting_IMPORTANT.cpp ├── E1_Permutation_Minimization_by_Deque_IMPORTANT.cpp └── E2_Distance_Tree_hard_version_.cpp ├── CodeVita ├── .cph │ ├── .Coin_collection.cpp_e227727beae4aae775d3b0b2849352d0.prob │ ├── .Max_Funds_MockVita.cpp_a735d02dccf89bc6e9711f130bc282a6.prob │ ├── .Minimum_Gifts.cpp_f07809c9e7920f06c2a4bb4fd7f510e0.prob │ ├── .primeConst.cpp_be6d4dd4ff59a78d1393c791e47c10be.prob │ ├── .primeConst_2.cpp_843aa0c39da7803783fa4f2b34f9804b.prob │ └── .test.cpp_8c829a8dd41ef10a9731576396f9d12a.prob ├── .gitignore ├── Coin_Collection.java ├── Coin_collection.cpp ├── Counting_rock_samples.cpp ├── Distinct_ele_in_window_size_K.cpp ├── Mario.pdf ├── Max_Funds_MockVita.cpp ├── Minimum_Gifts.cpp ├── Railway_Station.cpp ├── belt.cpp ├── coin collection.pdf ├── mario.cpp ├── mario_bihan.cpp ├── prime const.pdf ├── prime villa.pdf ├── primeConst.cpp ├── tempCodeRunnerFile.cpp └── test.cpp ├── DOCS.txt ├── Dynamic_Programming ├── .gitignore ├── .vscode │ └── settings.json ├── 1137.n-th-tribonacci-number.cpp ├── 121.best-time-to-buy-and-sell-stock.cpp ├── 122.best-time-to-buy-and-sell-stock-ii.cpp ├── 139.word-break.cpp ├── 1402.reducing-dishes.cpp ├── 1578.minimum-time-to-make-rope-colorful.cpp ├── 198.house-robber.cpp ├── 213.house-robber-ii.cpp ├── 413.arithmetic-slices.cpp ├── 5.longest-palindromic-substring.cpp ├── 53.maximum-subarray.cpp ├── 62.unique-paths.cpp ├── 62.unique-paths.java ├── 70.climbing-stairs.cpp ├── 740.delete-and-earn.cpp ├── 746.min-cost-climbing-stairs.cpp ├── DP.txt ├── Dp-on-grid │ ├── 62.unique-paths.cpp │ ├── Maze_Obstacles.cpp │ ├── Minimum_Path_Sum.cpp │ └── Triangle.cpp ├── Dp-on-strings_LCS │ ├── 516.longest-palindromic-subsequence.cpp │ ├── 583.delete-operation-for-two-strings.cpp │ ├── Longest_Common_Subsequence.cpp │ ├── Longest_Common_Substring.cpp │ ├── Longest_Increasing_Subsequence.c++ │ ├── Longest_Palindromic_Subsequence.cpp │ └── tempCodeRunnerFile.cpp ├── Fibonacci_Series.cpp ├── Frog_Jump.cpp ├── Frog_Jump_K_Distance.cpp ├── Jump Game │ ├── 1306.jump-game-iii.cpp │ ├── 1696.jump-game-vi.cpp │ ├── 1871.jump-game-vii.cpp │ ├── 45.jump-game-ii.cpp │ └── 55.jump-game.cpp ├── Knapsack-pattern-pick-nonpick-problems │ ├── 2035.partition-array-into-two-arrays-to-minimize-sum-difference.cpp │ ├── 416.partition-equal-subset-sum.cpp │ ├── Check_subset_with_sum_isPresent.cpp │ ├── Count_subset_with_given_difference_IMPORTANT.cpp │ ├── Count_subsets_with_given_sum.cpp │ ├── Equal_Partition_Subset_isPossible.cpp │ ├── Knapsack_01.cpp │ ├── Minimum_Subset_Sum_Diff.cpp │ └── tempCodeRunnerFile.cpp ├── MCM │ ├── MCM.cpp │ ├── Minimum_number_of_palindromic_partition │ └── Minimum_number_of_palindromic_partition.cpp ├── Subsequence_ALL.cpp └── tempCodeRunnerFile.cpp ├── Graphs ├── .gitignore ├── 463.island-perimeter.cpp ├── 733.flood-fill.cpp ├── 797.all-paths-from-source-to-target.cpp ├── General │ ├── Adjacency_list.cpp │ ├── BFS.cpp │ ├── Bipartite.cpp │ ├── Cycle_Detection_Directed_BFS │ ├── Cycle_Detection_Directed_BFS.cpp │ ├── Cycle_Detection_Directed_DFS.cpp │ ├── Cycle_Detection_Undirected.cpp │ ├── DFS.cpp │ ├── Topological_Sort_BFS_Kahns_Algorithm.cpp │ └── Topological_Sort_DFS.cpp ├── MInimum_Cost_Spanning_Tree │ ├── .gitignore │ ├── Disjoint-Set-Union.cpp │ ├── Kruskal_Algorithm.cpp │ ├── Prims_Algorithm │ └── Prims_Algorithm.cpp ├── Shortest_Path_Algorithm │ ├── .gitignore │ ├── Bellman_Ford.cpp │ ├── Dijkstras.cpp │ └── tempCodeRunnerFile.cpp └── tempCodeRunnerFile.cpp ├── Greedy ├── .gitignore ├── .vscode │ └── settings.json ├── 1053.previous-permutation-with-one-swap.cpp ├── 1217.minimum-cost-to-move-chips-to-the-same-position.cpp ├── Check_If_It_Is_Possible_to_Survive_In_Island.cpp ├── Maximise_the_Subsequence_Sum.cpp ├── Maximum_Sum_After_K_Negation.cpp ├── Minimum_num_of_platforms_required.cpp ├── N_meeting_in_one_room.cpp └── tempCodeRunnerFile.cpp ├── HEAP_NOTES.txt ├── Hare_and_Rabbit_method ├── 142.linked-list-cycle-ii.java └── 287.find-the-duplicate-number.java ├── Hash_Map ├── .cph │ └── .Rapid_Re_Orderings.cpp_7af4aea7de001b7431ef50dc1b3d3963.prob ├── .gitignore ├── 1.two-sum.cpp ├── 1094.car-pooling.cpp ├── 128.longest-consecutive-sequence.cpp ├── 1365.how-many-numbers-are-smaller-than-the-current-number.cpp ├── 137.single-number-ii.cpp ├── 1647.minimum-deletions-to-make-character-frequencies-unique.cpp ├── 1710.maximum-units-on-a-truck.cpp ├── 211.design-add-and-search-words-data-structure.cpp ├── 347.top-k-frequent-elements.cpp ├── 389.find-the-difference.cpp ├── 414.third-maximum-number.cpp ├── 438.find-all-anagrams-in-a-string.cpp ├── 448.find-all-numbers-disappeared-in-an-array.cpp ├── 451.sort-characters-by-frequency.cpp ├── 532.k-diff-pairs-in-an-array.cpp ├── 532.k-diff-pairs-in-an-array.java ├── 692.top-k-frequent-words.cpp ├── 825.friends-of-appropriate-ages.cpp ├── 890.find-and-replace-pattern.cpp ├── Anagram.cpp ├── Count_Pairs_With_Absolute_Difference_K.cpp ├── Find_Original_Array_From_Doubled_Array.cpp ├── Hill_Sequence_CC_IMP.cpp ├── List_of_Lists.cpp ├── Ninja_and_String_Operation.cpp ├── Rapid_Re_Orderings.cpp └── tempCodeRunnerFile.cpp ├── Heap ├── .gitignore ├── .vscode │ └── settings.json ├── 1046.last-stone-weight.cpp ├── 1675.minimize-deviation-in-array.cpp ├── 703.kth-largest-element-in-a-stream.cpp ├── Connect_N_Ropes.cpp ├── D_Productive_Meeting_IMPORTANT.cpp ├── FrequencySort_Heap.cpp ├── K_ClosestNumber.cpp ├── K_Max_Sum_combination.cpp ├── K_SortedArray_Sort.cpp ├── Kth_LargestElement.cpp ├── Max_Distinct_Element_After_Kele_removed.cpp ├── Merge_K_Sorted_Arrays.cpp └── tempCodeRunnerFile.cpp ├── Infytq-HackWin-Adv ├── Swap_to_make_Even.cpp └── Xor_finding.cpp ├── Leet_Code_Problems ├── .cph │ └── .E_Stronger_Takahashi.cpp_1b48ef665fc248a02cb74697e00695ea.prob ├── .vscode │ ├── java-formatter.xml │ └── settings.json ├── 1.two-sum.cpp ├── 1.two-sum.java ├── 1008.construct-binary-search-tree-from-preorder-traversal.cpp ├── 1009.complement-of-base-10-integer.cpp ├── 101.symmetric-tree.cpp ├── 1014.best-sightseeing-pair.cpp ├── 102.binary-tree-level-order-traversal.cpp ├── 1022.sum-of-root-to-leaf-binary-numbers.cpp ├── 1026.maximum-difference-between-node-and-ancestor.cpp ├── 104.maximum-depth-of-binary-tree.cpp ├── 1046.last-stone-weight.cpp ├── 105.construct-binary-tree-from-preorder-and-inorder-traversal.cpp ├── 1053.previous-permutation-with-one-swap.cpp ├── 108.convert-sorted-array-to-binary-search-tree.cpp ├── 1094.car-pooling.cpp ├── 1094.car-pooling.java ├── 1095.find-in-mountain-array.cpp ├── 11.container-with-most-water.cpp ├── 110.balanced-binary-tree.cpp ├── 111.minimum-depth-of-binary-tree.cpp ├── 1137.n-th-tribonacci-number.cpp ├── 118.pascals-triangle.cpp ├── 1189.maximum-number-of-balloons.cpp ├── 1200.minimum-absolute-difference.cpp ├── 121.best-time-to-buy-and-sell-stock.cpp ├── 1217.minimum-cost-to-move-chips-to-the-same-position.cpp ├── 122.best-time-to-buy-and-sell-stock-ii.cpp ├── 1260.shift-2-d-grid.cpp ├── 128.longest-consecutive-sequence.cpp ├── 1288.remove-covered-intervals.cpp ├── 1291.sequential-digits.java ├── 1302.deepest-leaves-sum.cpp ├── 1306.jump-game-iii.cpp ├── 134.gas-station.java ├── 136.single-number.cpp ├── 1365.how-many-numbers-are-smaller-than-the-current-number.cpp ├── 137.single-number-ii.cpp ├── 1382.balance-a-binary-search-tree.cpp ├── 139.word-break.cpp ├── 1402.reducing-dishes.cpp ├── 141.linked-list-cycle.cpp ├── 142.linked-list-cycle-ii.java ├── 1423.maximum-points-you-can-obtain-from-cards.cpp ├── 1431.kids-with-the-greatest-number-of-candies.cpp ├── 1446.consecutive-characters.cpp ├── 1465.maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts.cpp ├── 1466.reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp ├── 148.sort-list.cpp ├── 1493.longest-subarray-of-1-s-after-deleting-one-element.cpp ├── 15.3-sum.java ├── 151.reverse-words-in-a-string.cpp ├── 1578.minimum-time-to-make-rope-colorful.cpp ├── 160.intersection-of-two-linked-lists.cpp ├── 162.find-peak-element.cpp ├── 1624.largest-substring-between-two-equal-characters.cpp ├── 1647.minimum-deletions-to-make-character-frequencies-unique.cpp ├── 1658.minimum-operations-to-reduce-x-to-zero.cpp ├── 167.two-sum-ii-input-array-is-sorted.cpp ├── 1672.richest-customer-wealth.java ├── 1675.minimize-deviation-in-array.cpp ├── 1679.max-number-of-k-sum-pairs.cpp ├── 169.majority-element.cpp ├── 1696.jump-game-vi.cpp ├── 171.excel-sheet-column-number.cpp ├── 1710.maximum-units-on-a-truck.cpp ├── 1721.swapping-nodes-in-a-linked-list.cpp ├── 1752.check-if-array-is-sorted-and-rotated.cpp ├── 1863.sum-of-all-subset-xor-totals.cpp ├── 1871.jump-game-vii.cpp ├── 189.rotate-array.cpp ├── 19.remove-nth-node-from-end-of-list.java ├── 198.house-robber.cpp ├── 198.house-robber.java ├── 199.binary-tree-right-side-view.cpp ├── 2.add-two-numbers.cpp ├── 20.valid-parentheses.cpp ├── 2012.sum-of-beauty-in-the-array.cpp ├── 202.happy-number.java ├── 2035.partition-array-into-two-arrays-to-minimize-sum-difference.cpp ├── 206.reverse-linked-list.cpp ├── 206.reverse-linked-list.java ├── 21.merge-two-sorted-lists.cpp ├── 21.merge-two-sorted-lists.java ├── 211.design-add-and-search-words-data-structure.cpp ├── 213.house-robber-ii.cpp ├── 226.invert-binary-tree.cpp ├── 228.summary-ranges.cpp ├── 230.kth-smallest-element-in-a-bst.cpp ├── 231.power-of-two.cpp ├── 234.palindrome-linked-list.java ├── 237.delete-node-in-a-linked-list.cpp ├── 238.product-of-array-except-self.cpp ├── 239.sliding-window-maximum.cpp ├── 24.swap-nodes-in-pairs.java ├── 240.search-a-2-d-matrix-ii.cpp ├── 25.reverse-nodes-in-k-group.cpp ├── 258.add-digits.cpp ├── 26.remove-duplicates-from-sorted-array.cpp ├── 260.single-number-iii.cpp ├── 268.missing-number.cpp ├── 283.move-zeroes.java ├── 287.find-the-duplicate-number.cpp ├── 289.game-of-life.cpp ├── 3.longest-substring-without-repeating-characters.cpp ├── 31.next-permutation.cpp ├── 337.house-robber-iii.cpp ├── 34.find-first-and-last-position-of-element-in-sorted-array.cpp ├── 344.reverse-string.cpp ├── 344.reverse-string.java ├── 347.top-k-frequent-elements.cpp ├── 349.intersection-of-two-arrays.cpp ├── 376.wiggle-subsequence.cpp ├── 387.first-unique-character-in-a-string.cpp ├── 389.find-the-difference.cpp ├── 392.is-subsequence.cpp ├── 402.remove-k-digits.cpp ├── 402.remove-k-digits.java ├── 404.sum-of-left-leaves.cpp ├── 413.arithmetic-slices.cpp ├── 414.third-maximum-number.cpp ├── 416.partition-equal-subset-sum.cpp ├── 42.trapping-rain-water.cpp ├── 421.maximum-xor-of-two-numbers-in-an-array.java ├── 438.find-all-anagrams-in-a-string.cpp ├── 442.find-all-duplicates-in-an-array.cpp ├── 448.find-all-numbers-disappeared-in-an-array.cpp ├── 45.jump-game-ii.cpp ├── 450.delete-node-in-a-bst.cpp ├── 451.sort-characters-by-frequency.cpp ├── 452.minimum-number-of-arrows-to-burst-balloons.cpp ├── 452.minimum-number-of-arrows-to-burst-balloons.java ├── 454.4-sum-ii.cpp ├── 459.repeated-substring-pattern.cpp ├── 461.hamming-distance.cpp ├── 462.minimum-moves-to-equal-array-elements-ii.cpp ├── 463.island-perimeter.cpp ├── 476.number-complement.cpp ├── 48.rotate-image.cpp ├── 485.max-consecutive-ones.cpp ├── 496.next-greater-element-i.cpp ├── 5.longest-palindromic-substring.cpp ├── 50.pow-x-n.cpp ├── 503.next-greater-element-ii.cpp ├── 508.most-frequent-subtree-sum.cpp ├── 516.longest-palindromic-subsequence.cpp ├── 520.detect-capital.java ├── 525.contiguous-array.cpp ├── 53.maximum-subarray.cpp ├── 530.minimum-absolute-difference-in-bst.cpp ├── 532.k-diff-pairs-in-an-array.cpp ├── 532.k-diff-pairs-in-an-array.java ├── 55.jump-game.cpp ├── 56.merge-intervals.cpp ├── 560.subarray-sum-equals-k.cpp ├── 560.subarray-sum-equals-k.java ├── 567.permutation-in-string.cpp ├── 583.delete-operation-for-two-strings.cpp ├── 605.can-place-flowers.java ├── 61.rotate-list.java ├── 617.merge-two-binary-trees.cpp ├── 62.unique-paths.cpp ├── 62.unique-paths.java ├── 630.course-schedule-iii.cpp ├── 646.maximum-length-of-pair-chain.cpp ├── 662.maximum-width-of-binary-tree.cpp ├── 665.non-decreasing-array.cpp ├── 669.trim-a-binary-search-tree.cpp ├── 682.baseball-game.cpp ├── 692.top-k-frequent-words.cpp ├── 695.max-area-of-island.cpp ├── 70.climbing-stairs.cpp ├── 700.search-in-a-binary-search-tree.cpp ├── 701.insert-into-a-binary-search-tree.cpp ├── 703.kth-largest-element-in-a-stream.cpp ├── 706.design-hash-map.cpp ├── 73.set-matrix-zeroes.cpp ├── 733.flood-fill.cpp ├── 74.search-a-2-d-matrix.cpp ├── 740.delete-and-earn.cpp ├── 746.min-cost-climbing-stairs.cpp ├── 75.sort-colors.cpp ├── 763.partition-labels.cpp ├── 78.subsets.cpp ├── 79.word-search.cpp ├── 797.all-paths-from-source-to-target.cpp ├── 80.remove-duplicates-from-sorted-array-ii.cpp ├── 820.short-encoding-of-words.cpp ├── 825.friends-of-appropriate-ages.cpp ├── 83.remove-duplicates-from-sorted-list.java ├── 849.maximize-distance-to-closest-person.java ├── 875.koko-eating-bananas.java ├── 876.middle-of-the-linked-list.java ├── 88.merge-sorted-array.cpp ├── 881.boats-to-save-people.cpp ├── 890.find-and-replace-pattern.cpp ├── 916.word-subsets.cpp ├── 917.reverse-only-letters.java ├── 92.reverse-linked-list-ii.cpp ├── 925.long-pressed-name.cpp ├── 938.range-sum-of-bst.cpp ├── 941.valid-mountain-array.java ├── 993.cousins-in-binary-tree.cpp ├── 997.find-the-town-judge.java ├── Bits.cpp ├── E_Stronger_Takahashi.cpp ├── LinkedList.txt ├── Number_Game.cpp └── trial.cpp ├── LinkedList.txt ├── LinkedList ├── .gitignore ├── 141.linked-list-cycle.cpp ├── 142.linked-list-cycle-ii.java ├── 148.sort-list.cpp ├── 160.intersection-of-two-linked-lists.cpp ├── 1721.swapping-nodes-in-a-linked-list.cpp ├── 19.remove-nth-node-from-end-of-list.java ├── 2.add-two-numbers.cpp ├── 206.reverse-linked-list.cpp ├── 206.reverse-linked-list.java ├── 21.merge-two-sorted-lists.cpp ├── 21.merge-two-sorted-lists.java ├── 234.palindrome-linked-list.java ├── 237.delete-node-in-a-linked-list.cpp ├── 24.swap-nodes-in-pairs.java ├── 25.reverse-nodes-in-k-group.cpp ├── 61.rotate-list.java ├── 706.design-hash-map.cpp ├── 876.middle-of-the-linked-list.java └── 92.reverse-linked-list-ii.cpp ├── Matrix ├── 240.search-a-2-d-matrix-ii.cpp ├── 289.game-of-life.cpp ├── 48.rotate-image.cpp ├── 73.set-matrix-zeroes.cpp └── 74.search-a-2-d-matrix.cpp ├── PAIRS.txt ├── Pairs ├── .cph │ └── .Chef_Restaurant_.cpp_5ec113f1c0beedf38ac7126b1b8b5b6a.prob ├── .vscode │ └── settings.json ├── Chef_Restaurant_.cpp ├── D_Productive_Meeting_IMPORTANT.cpp ├── FrequencySort_Heap.cpp └── K_ClosestNumber.cpp ├── Pointer.c ├── Pointer.cpp ├── Recursion ├── .gitignore ├── 231.power-of-two.cpp ├── 763.partition-labels.cpp ├── 78.subsets.cpp ├── CountNumberWays__Jumps.cpp ├── EuclidianAlgo_GCD.cpp ├── NumberOfJumpsToReachDestination.cpp ├── Number_Game.cpp ├── PERMUTATION.cpp ├── String_IMPORTANT.cpp ├── Subsequence_Count_with_SumK.cpp ├── Subsequence_FindALL.cpp ├── Subsequence_with_SumK.cpp ├── Subset_Powerset.cpp └── tempCodeRunnerFile.cpp ├── SET.txt ├── Set ├── .gitignore ├── .vscode │ └── settings.json ├── 136.single-number.cpp ├── 137.single-number-ii.cpp ├── 2215.Find the Difference of Two Arrays.cpp ├── 260.single-number-iii.cpp ├── Set_basics.cpp ├── tempCodeRunnerFile.cpp └── trial.cpp ├── Sliding_Window + Prefix_Sum ├── .cph │ ├── .Careless_Chef.cpp_7ac3b9dedfb6be5f93a9a35e24815e3f.prob │ ├── .Control_the_Pollution.cpp_597a21e9939ea17d9938ea3386d9f227.prob │ ├── .Count_the_Notebooks.cpp_e16f7066dcf8ffe3a573e0806bd0bda1.prob │ ├── .Crossing_Blocks.cpp_451bdb3b06948cd4c14c1b6b0e798375.prob │ ├── .Hardest_Problem_Bet.cpp_bd9bb6274ddae4ac5e17a961fb619a5f.prob │ ├── .Hostel_Room.cpp_550857c530fc888d6e55a112f9168436.prob │ ├── .K_th_Maximum.cpp_818a7bfe1938ae29660b83737e48af8f.prob │ ├── .Maximise_the_Subsequence_Sum.cpp_e4e6ac0be8b8b460e40631f1769c7bca.prob │ ├── .Maximum_Length_Even_Subarray.cpp_e339d7ab6f2e91e96762a7638879a088.prob │ ├── .Odd_Repeat.cpp_2f555ff0fb61349ba57b802219e2ff1d.prob │ ├── .Programming_Languages.cpp_a1ce9dfada2cd51de83a6892e328a025.prob │ ├── .Programming_Languages.java_c2974a27ad2e2b87da0b32c7eb931c1d.prob │ ├── .Shivigawdz.cpp_8bf50bb04ed1c9c54bca0a5f1b09a270.prob │ ├── .Summary_Power.cpp_2ef5dfb279b9da800b10abb47c7c37be.prob │ └── .Water_Tanks.cpp_19b6b1b9dd4400a2cc07806245d0fd73.prob ├── .gitignore ├── .vscode │ ├── launch.json │ ├── settings.json │ └── tasks.json ├── 1423.maximum-points-you-can-obtain-from-cards.cpp ├── 1493.longest-subarray-of-1-s-after-deleting-one-element.cpp ├── 1658.minimum-operations-to-reduce-x-to-zero.cpp ├── 238.product-of-array-except-self.cpp ├── 3.longest-substring-without-repeating-characters.cpp ├── 42.trapping-rain-water.cpp ├── 438.find-all-anagrams-in-a-string.cpp ├── 567.permutation-in-string.cpp ├── Check_Anagram_or_Not.cpp ├── Count_Subarray_with_Sum_K_IMPORTANT.cpp ├── Count_Subarray_with_given_XOR.cpp ├── First_NegativeNum_of_KsizeWindow.cpp ├── Grumpy_Book_Store.cpp ├── Index_of_Subarray_with_given_Sum.cpp ├── Largest_Sum_Of_Contiguous_Subarray_KADANES_ALGO.cpp ├── Largest_Sum_of_Subarray_length_K.cpp ├── Longest_Subarray_with_0_Sum.cpp ├── Longest_Subarray_with_Equal_0_and_1_IMPORTANT.cpp ├── Longest_Substring_With_K_Unique_Characters_IMPORTANT.cpp ├── Longest_Substring_Without_Repeating_Characters_IMPORTANT.cpp ├── Longest_Turbulent_Subarray.cpp ├── Max_Element_in_Subarray_sizeK_IMPORTANT.cpp ├── Max_Element_in_Subarray_sizeK_UnOptimiseApproach.cpp ├── Sum_both_side_equal_IMPORTANT.cpp ├── Water_Tanks.cpp └── hackwin2.cpp ├── Sortings ├── BubbleSort.cpp ├── InsertionSort.cpp └── SelectionSort.cpp ├── Stack ├── 20.valid-parentheses.cpp ├── 402.remove-k-digits.cpp ├── 402.remove-k-digits.java ├── 496.next-greater-element-i.cpp ├── 503.next-greater-element-ii.cpp ├── 682.baseball-game.cpp ├── Sort_a_Stack.cpp └── Stack.cpp ├── Strings ├── .cph │ ├── .Prime_in_a_binary_string.cpp_4938ec1ffc5e657955c691836dca9ef2.prob │ └── .Retrieve_back_the_Array.cpp_19ccd6f80b3afe1e460478303367f11c.prob ├── .gitignore ├── .vscode │ └── settings.json ├── 1095.find-in-mountain-array.cpp ├── 1431.kids-with-the-greatest-number-of-candies.cpp ├── 1446.consecutive-characters.cpp ├── 151.reverse-words-in-a-string.cpp ├── 1624.largest-substring-between-two-equal-characters.cpp ├── 171.excel-sheet-column-number.cpp ├── 228.summary-ranges.cpp ├── 438.find-all-anagrams-in-a-string.cpp ├── 459.repeated-substring-pattern.cpp ├── 5.longest-palindromic-substring.cpp ├── 820.short-encoding-of-words.cpp ├── 917.reverse-only-letters.java ├── Count_Asterics_LC.cpp ├── Delete_Characters_to_make_fancy_string.cpp ├── Distinct_Binary_Strings.cpp ├── Find_a_string_in_lexicographical_order_between_two_given_strings.cpp ├── Find_all_Anagram_IMPORTANT.cpp ├── IMPORTANT_Palindrome_num.cpp ├── IMPORTANT_String.cpp ├── Kth_Lexicographically_String_given_length.cpp ├── Maxmum_time_to_write_word_using_Tipewritter.cpp ├── Palindromes_Not_Allowed.cpp ├── Prime_in_a_binary_string.cpp ├── Rabin_Karp_Algorithm_StringPatternFind.cpp ├── Remove_duplicates_from_string.cpp ├── Retrieve_back_the_Array.cpp ├── Reverse_Only_Letters_IMPORTANT.cpp ├── Substring_find.cpp ├── Valleys_and_Hills.cpp ├── Ways_to_remove_XOR_becomes_Zero.cpp └── tempCodeRunnerFile.cpp ├── TCS ├── chocolate_factory.cpp ├── decimal_binary_conversion.cpp ├── demo.cpp └── tempCodeRunnerFile.cpp ├── TREE.txt ├── TURBOT ├── Remove.cpp ├── Valid_string.cpp ├── Wave_arr.cpp └── tempCodeRunnerFile.cpp ├── Tree ├── .gitignore ├── .vscode │ └── c_cpp_properties.json ├── 1008.construct-binary-search-tree-from-preorder-traversal.cpp ├── 101.symmetric-tree.cpp ├── 102.binary-tree-level-order-traversal.cpp ├── 1022.sum-of-root-to-leaf-binary-numbers.cpp ├── 1026.maximum-difference-between-node-and-ancestor.cpp ├── 104.maximum-depth-of-binary-tree.cpp ├── 105.construct-binary-tree-from-preorder-and-inorder-traversal.cpp ├── 108.convert-sorted-array-to-binary-search-tree.cpp ├── 110.balanced-binary-tree.cpp ├── 111.minimum-depth-of-binary-tree.cpp ├── 1302.deepest-leaves-sum.cpp ├── 1382.balance-a-binary-search-tree.cpp ├── 199.binary-tree-right-side-view.cpp ├── 226.invert-binary-tree.cpp ├── 230.kth-smallest-element-in-a-bst.cpp ├── 235.lowest-common-ancestor-of-a-binary-search-tree.cpp ├── 404.sum-of-left-leaves.cpp ├── 450.delete-node-in-a-bst.cpp ├── 508.most-frequent-subtree-sum.cpp ├── 530.minimum-absolute-difference-in-bst.cpp ├── 617.merge-two-binary-trees.cpp ├── 669.trim-a-binary-search-tree.cpp ├── 700.search-in-a-binary-search-tree.cpp ├── 701.insert-into-a-binary-search-tree.cpp ├── 79.word-search.cpp ├── 938.range-sum-of-bst.cpp ├── 993.cousins-in-binary-tree.cpp ├── Deepest-Right-Leaf.cpp ├── bottom_view_binary_tree.cpp ├── others.md └── top_view_binary_tree.cpp ├── Two_Pointers ├── .vscode │ └── settings.json ├── 1679.max-number-of-k-sum-pairs.cpp ├── 75.sort-colors.cpp ├── 881.boats-to-save-people.cpp ├── 925.long-pressed-name.cpp ├── Dutch_National_Flag_Problem.cpp ├── Find_Pair_With_Given_Sum.cpp ├── Long_Pressed_Character.cpp ├── Reverse_Only_Letters_IMPORTANT.cpp ├── Squares_of_a_Sorted_Array.cpp └── codeKaze_1.cpp ├── graph_summary.cpp ├── script.sh ├── tempCodeRunnerFile.cpp └── workspace.code-workspace /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Mac", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "macFrameworkPath": [ 10 | "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" 11 | ], 12 | "compilerPath": "/usr/bin/clang", 13 | "cStandard": "c17", 14 | "cppStandard": "c++17", 15 | "intelliSenseMode": "macos-clang-arm64" 16 | } 17 | ], 18 | "version": 4 19 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "(lldb) Launch", 5 | "type": "cppdbg", 6 | "request": "launch", 7 | "program": "enter program name, for example ${workspaceFolder}/a.out", 8 | "args": [], 9 | "stopAtEntry": false, 10 | "cwd": "${fileDirname}", 11 | "environment": [], 12 | "externalConsole": false, 13 | "MIMode": "lldb" 14 | }, 15 | { 16 | "name": "C/C++: cpp build and debug active file", 17 | "type": "cppdbg", 18 | "request": "launch", 19 | "program": "${fileDirname}/${fileBasenameNoExtension}", 20 | "args": [], 21 | "stopAtEntry": false, 22 | "cwd": "${fileDirname}", 23 | "environment": [], 24 | "externalConsole": false, 25 | "MIMode": "lldb", 26 | "preLaunchTask": "C/C++: cpp build active file" 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Array/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Array/118.pascals-triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=118 lang=cpp 3 | * 4 | * [118] Pascal's Triangle 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector> generate(int numRows) 12 | { 13 | vector> pattern(numRows); 14 | 15 | for (int i = 0; i < numRows; i++) 16 | { 17 | pattern[i].resize(i + 1); 18 | pattern[i][0] = pattern[i][i] = 1; // Assign 1st and Last as 1 19 | 20 | for (int j = 1; j < i; j++) 21 | pattern[i][j] = pattern[i - 1][j - 1] + pattern[i - 1][j]; // perform SUM for the middle elements 22 | } 23 | 24 | return pattern; 25 | } 26 | }; 27 | 28 | // @lc code=end 29 | 30 | /* 31 | Pattern Vector will be like this : 32 | 33 | 1 34 | 1 1 35 | 1 2 1 36 | 1 3 3 1 37 | 1 4 6 4 1 38 | 39 | */ -------------------------------------------------------------------------------- /Array/1200.minimum-absolute-difference.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1200 lang=cpp 3 | * 4 | * [1200] Minimum Absolute Difference 5 | * 6 | * https://leetcode.com/problems/minimum-absolute-difference/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | vector> minimumAbsDifference(vector &arr) 15 | { 16 | vector> result; 17 | int minDifference = INT_MAX, i = 0, len = arr.size(); 18 | sort(arr.begin(), arr.end()); 19 | 20 | for (i = 1; i < len; i++) 21 | if (minDifference > (arr[i] - arr[i - 1])) 22 | minDifference = arr[i] - arr[i - 1]; 23 | // minDifference = min(minDifference, (arr[i] - arr[i - 1])); 24 | 25 | for (i = 1; i < len; i++) 26 | if (arr[i] - arr[i - 1] == minDifference) 27 | result.push_back({arr[i - 1], arr[i]}); 28 | 29 | return result; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Array/1752.check-if-array-is-sorted-and-rotated.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1752 lang=cpp 3 | * 4 | * [1752] Check if Array Is Sorted and Rotated 5 | * 6 | * https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | bool check(vector &nums) 15 | { 16 | 17 | int i, count = 0, size = nums.size(); 18 | for (i = 0; i < size; i++) 19 | { 20 | if (nums[i] > nums[(i + 1) % size]) 21 | count++; 22 | } 23 | 24 | return (count <= 1); 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Array/1863.sum-of-all-subset-xor-totals.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1863 lang=cpp 3 | * 4 | * [1863] Sum of All Subset XOR Totals 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int subsetXORSum(vector &nums) 12 | { 13 | // Obsesrve that after performing the sum we are actually performing the addition upto 2^n - 1. 14 | int xor_sum = 0, i, or_total = 0, length = nums.size(); 15 | int limit = pow(2, length - 1); 16 | 17 | for (i = 0; i < length; ++i) 18 | { 19 | or_total = or_total | nums[i]; 20 | } 21 | 22 | xor_sum = limit * or_total; 23 | return xor_sum; 24 | } 25 | }; 26 | // @lc code=end 27 | -------------------------------------------------------------------------------- /Array/268.missing-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=268 lang=cpp 3 | * 4 | * [268] Missing Number 5 | * 6 | * https://leetcode.com/problems/missing-number/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | // Given an array nums containing n distinct numbers in the range [0, n] 15 | 16 | int missingNumber(vector &nums) // [3,0,1] 17 | { 18 | int len = nums.size(), i = 0, result = len; 19 | // Method - I 20 | sort(nums.begin(), nums.end()); 21 | 22 | for (i = 0; i < len; i++) 23 | { 24 | if (i != nums[i]) 25 | return i; 26 | } 27 | return i; 28 | 29 | // Method - II (Using Bits XOR) 30 | for (i = 0; i < len; i++) 31 | { 32 | result = result ^ i; 33 | result = result ^ nums[i]; 34 | } 35 | return result; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Array/376.wiggle-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=376 lang=cpp 3 | * 4 | * [376] Wiggle Subsequence 5 | * 6 | * https://leetcode.com/problems/wiggle-subsequence/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int wiggleMaxLength(vector &arr) 15 | { 16 | int len = arr.size(), count = 0, prev, currDiff; 17 | 18 | if (len == 0 || len == 1) 19 | return len; 20 | 21 | prev = arr[1] - arr[0]; 22 | count = (prev != 0) ? 2 : 1; 23 | 24 | for (int i = 2; i < len; i++) 25 | { 26 | currDiff = arr[i] - arr[i - 1]; 27 | if ((currDiff > 0 && prev <= 0) || (currDiff < 0 && prev >= 0)) 28 | { 29 | count++; 30 | prev = currDiff; 31 | } 32 | } 33 | 34 | return count; 35 | } 36 | }; 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Array/387.first-unique-character-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=387 lang=cpp 3 | * 4 | * [387] First Unique Character in a String 5 | * 6 | * https://leetcode.com/problems/first-unique-character-in-a-string/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int firstUniqChar(string s) 15 | { 16 | int len = s.size(), res = -1; 17 | 18 | unordered_map hashMap; 19 | 20 | for (int i = 0; i < len; i++) 21 | hashMap[s[i]]++; 22 | 23 | for (int i = 0; i < len; i++) 24 | { 25 | if (hashMap.find(s[i]) != hashMap.end() && hashMap.at(s[i]) == 1) 26 | return i; 27 | } 28 | 29 | return res; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Array/485.max-consecutive-ones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=485 lang=cpp 3 | * 4 | * [485] Max Consecutive Ones 5 | * 6 | * https://leetcode.com/problems/max-consecutive-ones/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int findMaxConsecutiveOnes(vector &arr) 15 | { 16 | int len = arr.size(), res = 0, count = 0; 17 | 18 | if (arr[0] == 1 && len == 1) 19 | return 1; 20 | 21 | for (int i = 0; i < len; i++) 22 | { 23 | if (arr[i] == 1) 24 | { 25 | count++; 26 | res = max(res, count); 27 | } 28 | else 29 | count = 0; 30 | } 31 | 32 | return res; 33 | } 34 | }; 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Array/665.non-decreasing-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=665 lang=cpp 3 | * 4 | * [665] Non-decreasing Array 5 | * 6 | * https://leetcode.com/problems/non-decreasing-array/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | // VIDEO : https://www.youtube.com/watch?v=RegQckCegDk 15 | bool checkPossibility(vector &nums) 16 | { 17 | int len = arr.size(); 18 | bool isChange = false; 19 | 20 | for (int i = 0; i < len - 1; i++) 21 | { 22 | if (arr[i] <= arr[i + 1]) 23 | continue; 24 | if (isChange) 25 | return false; 26 | 27 | // Cases : [7 8 1 8] 28 | if (i == 0 || arr[i + 1] >= arr[i - 1]) // if [4 2] for that edge case [i == 0] checking. 29 | arr[i] = arr[i + 1]; 30 | else 31 | arr[i + 1] = arr[i]; 32 | 33 | isChange = true; 34 | } 35 | 36 | return true; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Array/80.remove-duplicates-from-sorted-array-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=80 lang=cpp 3 | * 4 | * https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 5 | * 6 | * [80] Remove Duplicates from Sorted Array II 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | int removeDuplicates(vector &nums) 14 | { 15 | int i, len = nums.size(), result = len; 16 | map hashMap; // Key Count 17 | 18 | for (i = 0; i < len; i++) 19 | hashMap[nums[i]]++; 20 | 21 | nums.clear(); 22 | 23 | for (auto it = hashMap.begin(); it != hashMap.end(); it++) 24 | { 25 | if (it->second > 2) 26 | it->second = 2; 27 | 28 | for (i = 0; i < it->second; i++) 29 | nums.push_back(it->first); 30 | } 31 | 32 | sort(nums.begin(), nums.end()); 33 | return nums.size(); // modified length of the vector 34 | } 35 | }; 36 | // @lc code=end 37 | -------------------------------------------------------------------------------- /Array/88.merge-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=88 lang=cpp 3 | * 4 | * [88] Merge Sorted Array 5 | * 6 | * https://leetcode.com/problems/merge-sorted-array/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | void merge(vector &nums1, int m, vector &nums2, int n) 15 | { 16 | // Time Complexity : O(m * n) 17 | // Space Complexity : O(1) 18 | int i = 0; 19 | for (i = 0; i < n; i++) 20 | { 21 | nums1[m] = nums2[i]; 22 | m++; 23 | } 24 | 25 | sort(nums1.begin(), nums1.end()); 26 | 27 | // We can also use MAX - HEAP 28 | } 29 | }; 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Array/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | arr[line][i] = a -------------------------------------------------------------------------------- /AtCoder/.cph/.A_AtCoder_Quiz_2.cpp_d9891d3d879f4f468f0674df2f068408.prob: -------------------------------------------------------------------------------- 1 | {"name":"A - AtCoder Quiz 2","group":"AtCoder - Sciseed Programming Contest 2021(AtCoder Beginner Contest 219)","url":"https://atcoder.jp/contests/abc219/tasks/abc219_a","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"56\n","output":"14\n","id":1631968035718},{"input":"32\n","output":"8\n","id":1631968035684},{"input":"0\n","output":"40\n","id":1631968035732},{"id":1631968035687,"input":"100","output":"expert"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"AAtCoderQuiz2"}},"batch":{"id":"78d1d1b5-cfbc-498d-816c-ac18594a2774","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\A_AtCoder_Quiz_2.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.A_Tires.cpp_0757f159a350d7bf06b62f08e2030fb2.prob: -------------------------------------------------------------------------------- 1 | {"name":"A - Tires","group":"AtCoder - AtCoder Beginner Contest 224","url":"https://atcoder.jp/contests/abc224/tasks/abc224_a","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"atcoder\n","output":"er\n","id":1634990711943},{"input":"tourist\n","output":"ist\n","id":1634990712006},{"input":"er\n","output":"er\n","id":1634990711979}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ATires"}},"batch":{"id":"5feb44ad-b8c6-45ef-ab5d-5dfab46dd281","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\A_Tires.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.A_Your_First_Judge.cpp_7a8e767c7419e5e2dd3e579c775d9761.prob: -------------------------------------------------------------------------------- 1 | {"name":"A - Your First Judge","group":"AtCoder - AtCoder Beginner Contest 215","url":"https://atcoder.jp/contests/abc215/tasks/abc215_a","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"Hello,World!\n","output":"AC\n","id":1629547942971},{"input":"Hello,world!\n","output":"WA\n","id":1629547942945},{"id":1629547942991,"input":"Hello!World!","output":"WA"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"AYourFirstJudge"}},"batch":{"id":"d105dd04-cc24-4fa3-b760-3f36a34c11c2","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\A_Your_First_Judge.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.B_How_many_.cpp_786c81352a672b86b29445feb6571a50.prob: -------------------------------------------------------------------------------- 1 | {"name":"B - How many?","group":"AtCoder - AtCoder Beginner Contest 214","url":"https://atcoder.jp/contests/abc214/tasks/abc214_b","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"1 0","output":"4","id":1629384161459},{"input":"2 5\n","output":"10\n","id":1629384161533},{"input":"10 10\n","output":"213\n","id":1629384161517},{"id":1629384161529,"input":"30 100","output":"2471"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BHowMany"}},"batch":{"id":"163c5fe0-fd4a-4c58-8af7-369c8d8fc90f","size":1},"srcPath":"c:\\MY_DRIVE_LOCAL\\Programming_Coding\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\B_How_many_.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.B_Maritozzo.cpp_553cb0e44d711db49b2ec8d7256c4931.prob: -------------------------------------------------------------------------------- 1 | {"name":"B - Maritozzo","group":"AtCoder - Sciseed Programming Contest 2021(AtCoder Beginner Contest 219)","url":"https://atcoder.jp/contests/abc219/tasks/abc219_b","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"mari\nto\nzzo\n1321\n","output":"marizzotomari\n","id":1631968543480},{"input":"abra\ncad\nabra\n123\n","output":"abracadabra\n","id":1631968543528},{"id":1631968543449,"input":"a\nb\nc\n1","output":"a"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BMaritozzo"}},"batch":{"id":"6aca2338-c70f-47a6-80b3-d20028a240cf","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\B_Maritozzo.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.B_Maritozzo.java_a79a99950b6224447a9790958d7000d1.prob: -------------------------------------------------------------------------------- 1 | {"name":"B - Maritozzo","group":"AtCoder - Sciseed Programming Contest 2021(AtCoder Beginner Contest 219)","url":"https://atcoder.jp/contests/abc219/tasks/abc219_b","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"mari\nto\nzzo\n1321\n","output":"marizzotomari\n","id":1631968530280},{"input":"abra\ncad\nabra\n123\n","output":"abracadabra\n","id":1631968530340},{"input":"a\nb\nc\n1\n","output":"a\n","id":1631968530323}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BMaritozzo"}},"batch":{"id":"661dfe0b-cf6b-469a-b884-4d98d748ea50","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\B_Maritozzo.java"} -------------------------------------------------------------------------------- /AtCoder/.cph/.B_String_Shifting.cpp_9197aad554b831920fb23b6eeedfa329.prob: -------------------------------------------------------------------------------- 1 | {"name":"B - String Shifting","group":"AtCoder - AtCoder Beginner Contest 223","url":"https://atcoder.jp/contests/abc223/tasks/abc223_b","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"aaba\n","output":"aaab\nbaaa\n","id":1634580167354},{"input":"z\n","output":"z\nz\n","id":1634580167324},{"id":1634580167394,"input":"abracadabra","output":"aabracadabr\nracadabraab"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BStringShifting"}},"batch":{"id":"5bb9f2d5-ebb3-4446-881d-a384e4f070c3","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\B_String_Shifting.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.B_log2_N_.cpp_31e4479abe22eb50764ffe420765e33b.prob: -------------------------------------------------------------------------------- 1 | {"name":"B - log2(N)","group":"AtCoder - AtCoder Beginner Contest 215","url":"https://atcoder.jp/contests/abc215/tasks/abc215_b","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"6\n","output":"2\n","id":1629548595651},{"input":"1\n","output":"0\n","id":1629548595635},{"id":1629548595630,"input":"1000000000000000000","output":"59"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BLog2N"}},"batch":{"id":"92160ee6-46d4-4327-bd1e-f3baf9c802d9","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\B_log2_N_.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.C_Distribution.cpp_d4555b81e64f430e151d2cb329ce874a.prob: -------------------------------------------------------------------------------- 1 | {"name":"C - Distribution","group":"AtCoder - AtCoder Beginner Contest 214","url":"https://atcoder.jp/contests/abc214/tasks/abc214_c","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"3\n4 1 5\n3 10 100","output":"3\n7\n8","id":1629391340386},{"input":"4\n100 100 100 100\n1 1 1 1\n","output":"1\n1\n1\n1\n","id":1629391340390},{"input":"4\n1 2 3 4\n1 2 4 7\n","output":"1\n2\n4\n7\n","id":1629391340456},{"input":"8\n84 87 78 16 94 36 87 93\n50 22 63 28 91 60 64 27\n","output":"50\n22\n63\n28\n44\n60\n64\n27\n","id":1629391340448}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CDistribution"}},"batch":{"id":"117ef77c-0154-43d9-a428-86b55ff6628c","size":1},"srcPath":"c:\\MY_DRIVE_LOCAL\\Programming_Coding\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\C_Distribution.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.C_Neo_lexicographic_Ordering.cpp_0fa9f4c5c356fe6c2ed419cc834ce53b.prob: -------------------------------------------------------------------------------- 1 | {"name":"C - Neo-lexicographic Ordering","group":"AtCoder - Sciseed Programming Contest 2021(AtCoder Beginner Contest 219)","url":"https://atcoder.jp/contests/abc219/tasks/abc219_c","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"bacdefghijklmnopqrstuvwxzy\n4\nabx\nbzz\nbzy\ncaa\n","output":"bzz\nbzy\nabx\ncaa\n","id":1631969535838},{"id":1631969535842,"input":"zyxwvutsrqponmlkjihgfedcba\n5\na\nab\nabc\nac\nb","output":"b\na\nac\nab\nabc"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CNeoLexicographicOrdering"}},"batch":{"id":"1a5a5a26-29c1-4dec-a4b3-00b21f11287d","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\C_Neo_lexicographic_Ordering.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.C_One_More_aab_aba_baa.cpp_1d9f0f816bd91a34bd12d2634812a7df.prob: -------------------------------------------------------------------------------- 1 | {"name":"C - One More aab aba baa","group":"AtCoder - AtCoder Beginner Contest 215","url":"https://atcoder.jp/contests/abc215/tasks/abc215_c","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"aab 2\n","output":"aba\n","id":1629550151371},{"input":"baba 4\n","output":"baab\n","id":1629550151406},{"id":1629550151420,"input":"ydxwacbz 40320","output":"zyxwdcba"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"COneMoreAabAbaBaa"}},"batch":{"id":"9838b869-5a67-43f0-806f-8d223b7ddc0e","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\C_One_More_aab_aba_baa.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.Chef_And_Salary_Pay.cpp_f37a9eae3c90121a0dad7adc91999784.prob: -------------------------------------------------------------------------------- 1 | {"name":"Chef And Salary Pay","group":"CodeChef - CodeChef Starters 16 Division 3 (Rated)","url":"https://www.codechef.com/START16C/problems/HCAGMAM1","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"input":"3\n6 3\n111100110101100000101100011111\n8 2\n111010111101001010100100111101\n5 6\n011101010100101000001101000010\n","output":"117\n152\n78\n","id":1634740677735}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ChefAndSalaryPay"}},"batch":{"id":"235edd4b-811a-4f23-9b09-ae2008c9f250","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\Chef_And_Salary_Pay.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.D_Coprime_2.cpp_18eca90b6f1bd7e72d5f1f4748312d0a.prob: -------------------------------------------------------------------------------- 1 | {"name":"D - Coprime 2","group":"AtCoder - AtCoder Beginner Contest 215","url":"https://atcoder.jp/contests/abc215/tasks/abc215_d","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"id":1629550375918,"input":"3 12\n6 1 5","output":"3\n1\n7\n11"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"DCoprime2"}},"batch":{"id":"1bf39c31-173f-4db6-87e8-f868e02dca69","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\D_Coprime_2.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.D_Cutting_Woods.cpp_95201b9b63ba0460ba08a834cabc1d32.prob: -------------------------------------------------------------------------------- 1 | {"name":"D - Cutting Woods","group":"AtCoder - AtCoder Beginner Contest 217","url":"https://atcoder.jp/contests/abc217/tasks/abc217_d","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"id":1631112860430,"input":"5 3\n2 2\n1 3\n2 2","output":"5\n3"},{"input":"5 3\n1 2\n1 4\n2 3\n","output":"2\n","id":1631112860450},{"id":1631112860458,"input":"100 10\n1 31\n2 41\n1 59\n2 26\n1 53\n2 58\n1 97\n2 93\n1 23\n2 84","output":"69\n31\n6\n38\n38"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"DCuttingWoods"}},"batch":{"id":"2d853aa8-8d82-426c-852a-3dba3bb17457","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\AtCoder\\D_Cutting_Woods.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.D_Restricted_Permutation.cpp_15a611efbf9dd117b9d8fda33417fcd7.prob: -------------------------------------------------------------------------------- 1 | {"name":"D - Restricted Permutation","group":"AtCoder - AtCoder Beginner Contest 223","url":"https://atcoder.jp/contests/abc223/tasks/abc223_d","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"4 3\n2 1\n3 4\n2 4\n","output":"2 1 3 4\n","id":1634721591654},{"id":1634721591585,"input":"2 3\n1 2\n1 2\n2 1","output":"-1"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"DRestrictedPermutation"}},"batch":{"id":"f793466a-5dfd-47a8-be67-af0365029d47","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\D_Restricted_Permutation.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.D_Strange_Lunchbox.cpp_c6a06efd4e7ed75efaba9547257a40bc.prob: -------------------------------------------------------------------------------- 1 | {"name":"D - Strange Lunchbox","group":"AtCoder - Sciseed Programming Contest 2021(AtCoder Beginner Contest 219)","url":"https://atcoder.jp/contests/abc219/tasks/abc219_d","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"3\n5 6\n2 1\n3 4\n2 3\n","output":"2\n","id":1632079767076},{"id":1632079767056,"input":"3\n8 8\n3 4\n2 3\n2 1","output":"-1"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"DStrangeLunchbox"}},"batch":{"id":"9f03553d-5944-4368-a22c-4844972031af","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\D_Strange_Lunchbox.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.Pass_or_Fail.cpp_93d5cc7166d8434548dac158db569f32.prob: -------------------------------------------------------------------------------- 1 | {"name":"Pass or Fail","group":"CodeChef - CodeChef Starters 16 Division 3 (Rated)","url":"https://www.codechef.com/START16C/problems/PASSORFAIL","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"input":"3\n5 2 3\n5 2 4\n4 0 0\n","output":"PASS\nFAIL\nFAIL\n","id":1634740454544}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"PassOrFail"}},"batch":{"id":"4f9e808b-3766-4bf8-8086-e18f92aba75c","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\Pass_or_Fail.cpp"} -------------------------------------------------------------------------------- /AtCoder/.cph/.Professor_And_Directions.cpp_831a33a2bfcf3f37d66868e0d45beed7.prob: -------------------------------------------------------------------------------- 1 | {"name":"Professor And Directions","group":"CodeChef - CodeChef Starters 16 Division 3 (Rated)","url":"https://www.codechef.com/START16C/problems/DIRECTN","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"input":"3\n12\nLRLRRRLRLLLL\n2\nLR\n4\nLRRL\n","output":"YES\nNO\nYES\n","id":1634744980736}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ProfessorAndDirections"}},"batch":{"id":"cb9e66ce-3916-4f35-9cfd-482193a7cebc","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\AtCoder\\Professor_And_Directions.cpp"} -------------------------------------------------------------------------------- /AtCoder/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "random": "cpp", 4 | "deque": "cpp", 5 | "string": "cpp", 6 | "vector": "cpp", 7 | "cmath": "cpp", 8 | "iostream": "cpp", 9 | "ostream": "cpp" 10 | } 11 | } -------------------------------------------------------------------------------- /AtCoder/A_Lexicographic_Order.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ________________________________________ 3 | ---------------------------------------- 4 | Author : Niharika Dutta 5 | Code Link : https://atcoder.jp/contests/abc217/tasks/abc217_a 6 | Time Complexity : 7 | ________________________________________ 8 | ---------------------------------------- 9 | */ 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | #define ll long long 16 | #define vi vector 17 | #define vll vector 18 | #define pb push_back 19 | #define mp make_pair 20 | #define loop1(n) for (ll i = 0; i < (n); i++) 21 | #define loop2(n) for (ll i = 1; i <= (n); i++) 22 | #define test \ 23 | ll t; \ 24 | cin >> t; \ 25 | while (t--) 26 | 27 | int main() 28 | { 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | cout.tie(NULL); 32 | 33 | string s, t; 34 | cin >> s >> t; 35 | 36 | if ((s.compare(t)) < 0) 37 | cout << "Yes\n"; 38 | else 39 | cout << "No\n"; 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /AtCoder/D_Strange_Lunchbox.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ________________________________________ 3 | ---------------------------------------- 4 | Author : Niharika Dutta 5 | Code Link : https://atcoder.jp/contests/abc219/tasks/abc219_d (Uses DP) 6 | Time Complexity : 7 | ________________________________________ 8 | ---------------------------------------- 9 | */ 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | #define ll long long 16 | #define lli long long int 17 | #define vi vector 18 | #define vll vector 19 | #define pb push_back 20 | #define mp make_pair 21 | #define loop1(n) for (ll i = 0; i < (n); i++) 22 | #define loop2(n) for (ll i = 1; i <= (n); i++) 23 | #define test \ 24 | ll t; \ 25 | cin >> t; \ 26 | while (t--) 27 | 28 | int main() 29 | { 30 | ios_base::sync_with_stdio(false); 31 | cin.tie(NULL); 32 | cout.tie(NULL); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Binary Search.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | // BINARY SEARCH // TC : log n 4 | ---------------- 5 | 6 | 7 | 1) ARRAY MUST BE SORTED ALWAYSSSSSSS ........ 8 | 9 | 2) fIND MID = (START + END)/2 10 | 11 | 3) use while loop / recurtion both same 12 | 13 | 4) AT THE LAST ITERATION IN THE WHILE LOOP EVERYTIME THE END < START BY 1 DIFFERENCE , So take that start and end at the end after the 14 | Loop iteration completes (this logic required in Minimum difference in Sorted Array). 15 | 16 | 5) BITONIC ARRAY ---> array where we can apply binary search though it is not sorted 17 | Structure like a hill. 18 | First the values increases then reaches a PEAK and after that it decreases. -------------------------------------------------------------------------------- /Binary_Search/.cph/.Pick_the_developers.cpp_de1269323ffd4f0684b4ed035d6a1b3a.prob: -------------------------------------------------------------------------------- 1 | {"name":"Pick the developers","group":"CodeChef - UCode 4","url":"https://www.codechef.com/UCDE2022/problems/UCODE019","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1646508527744,"input":"2\naA\naAAbbbb\nz\nZZ","output":"3\n0"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"PickTheDevelopers"}},"batch":{"id":"47d0ba33-327c-496b-b05f-1427d05bd312","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Binary_Search\\Pick_the_developers.cpp"} -------------------------------------------------------------------------------- /Binary_Search/.cph/.Swap_to_make_Event.cpp_85685b5436e08686be58277b25bb292b.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: Swap_to_make_Event","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Binary_Search\\Swap_to_make_Event.cpp","tests":[{"id":1646995588782,"input":"1\n431","output":"1"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Binary_Search\\Swap_to_make_Event.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /Binary_Search/.cph/.War.cpp_f94e38154884a2dc1e79f03df8e2baf6.prob: -------------------------------------------------------------------------------- 1 | {"name":"War","group":"CodeChef - UCode 4","url":"https://www.codechef.com/UCDE2022/problems/UCODE018","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1646558062631,"input":"3\n4\n1 1 0 1\n9\n0 1 1 1 0 1 1 0 1\n3\n1 1 1","output":"3\n5\n2"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"War"}},"batch":{"id":"3d23f492-efe7-4cb5-bf1d-19a7a83a0f20","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Binary_Search\\War.cpp"} -------------------------------------------------------------------------------- /Binary_Search/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Binary_Search/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "array": "cpp", 4 | "deque": "cpp", 5 | "forward_list": "cpp", 6 | "list": "cpp", 7 | "string": "cpp", 8 | "unordered_map": "cpp", 9 | "unordered_set": "cpp", 10 | "vector": "cpp", 11 | "string_view": "cpp", 12 | "initializer_list": "cpp", 13 | "ranges": "cpp", 14 | "span": "cpp", 15 | "regex": "cpp", 16 | "valarray": "cpp", 17 | "iostream": "cpp", 18 | "bitset": "cpp", 19 | "utility": "cpp", 20 | "ostream": "cpp" 21 | } 22 | } -------------------------------------------------------------------------------- /Binary_Search/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | solve -------------------------------------------------------------------------------- /Bitwise_Tag_Less/1009.complement-of-base-10-integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1009 lang=cpp 3 | * 4 | * [1009] Complement of Base 10 Integer 5 | */ 6 | // https://leetcode.com/problems/complement-of-base-10-integer/ 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | /* 12 | Approach used here is BitMasking. 13 | Input + Mask = Output 14 | */ 15 | int bitwiseComplement(int n) 16 | { 17 | int ans = 0, mask = 0, num = n; 18 | 19 | // Base Case 20 | if (n == 0) 21 | return 1; 22 | 23 | while (num != 0) 24 | { 25 | num = num >> 1; 26 | mask = (mask << 1) | 1; // Left shift + Bitwise OR 27 | } 28 | 29 | ans = (~n) & mask; 30 | return ans; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /Bitwise_Tag_Less/258.add-digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=258 lang=cpp 3 | * 4 | * [258] Add Digits 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int addDigits(int num) 12 | { 13 | if (num < 10) 14 | return num; 15 | 16 | int k, sum = 0; 17 | 18 | while (num >= 10) 19 | { 20 | sum = 0; 21 | while (num) 22 | { 23 | k = num % 10; 24 | sum = sum + k; 25 | num = num / 10; 26 | } 27 | 28 | num = sum; 29 | } 30 | 31 | return num; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Bitwise_Tag_Less/268.missing-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=268 lang=cpp 3 | * 4 | * [268] Missing Number 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int missingNumber(vector &nums) 12 | { 13 | int len = nums.size(), i = 0, result = 0; 14 | // Method - I 15 | sort(nums.begin(), nums.end()); 16 | 17 | for (i = 0; i < len; i++) 18 | { 19 | if (i != nums[i]) 20 | return i; 21 | } 22 | return i; 23 | 24 | // Method - II (Using Bits XOR) 25 | for (i = 0; i < len; i++) 26 | { 27 | result = result ^ i; 28 | result = result ^ nums[i]; 29 | } 30 | return result; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /CodeForces/.cph/.A_ABC.cpp_fcc9bb94d612267790af938cca8cdf00.prob: -------------------------------------------------------------------------------- 1 | {"name":"A. ABC","group":"Codeforces - Codeforces Round #769 (Div. 2)","url":"https://codeforces.com/contest/1632/problem/A","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1643553511767,"input":"4\n1\n1\n2\n10\n2\n01\n4\n1010","output":"YES\nYES\nYES\nNO"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"AABC"}},"batch":{"id":"3ed378a7-d747-439b-ba2c-c644221cdb86","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\A_ABC.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.A_Dislike_of_Threes.cpp_3a01fcedae9f83dc8b4ed8170b240b93.prob: -------------------------------------------------------------------------------- 1 | {"name":"A. Dislike of Threes","group":"Codeforces - Codeforces Round #739 (Div. 3)","url":"https://codeforces.com/contest/1560/problem/A","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1629297423028,"input":"10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1000","output":"1\n2\n4\n5\n7\n8\n10\n11\n14\n1666"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ADislikeOfThrees"}},"batch":{"id":"fa1a3297-3b96-40ff-88c9-a9f88e9aec5d","size":1},"srcPath":"c:\\MY_DRIVE_LOCAL\\Programming_Coding\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\CodeForces\\A_Dislike_of_Threes.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.A_PizzaForces_CODEFORCES.cpp_d3706562df61c534ba26fd3a6c1905ea.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: A_PizzaForces_CODEFORCES","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\A_PizzaForces_CODEFORCES.cpp","tests":[{"id":1632840809039,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\A_PizzaForces_CODEFORCES.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeForces/.cph/.A_The_Miracle_and_the_Sleeper.cpp_eb7c8c326e65bb66ac1419b3979cd734.prob: -------------------------------------------------------------------------------- 1 | {"name":"A. The Miracle and the Sleeper","group":"Codeforces - Codeforces Round #741 (Div. 2)","url":"https://codeforces.com/contest/1562/problem/A","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1629989872071,"input":"4\n1 1\n999999999 1000000000\n8 26\n1 999999999","output":"0\n1\n12\n499999999"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ATheMiracleAndTheSleeper"}},"batch":{"id":"e86a7d91-e3c8-408e-8cde-89bd5d2467a7","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\CodeForces\\A_The_Miracle_and_the_Sleeper.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.B_Roof_Construction.cpp_e1a43c26b274f68c054f8bce1713ffed.prob: -------------------------------------------------------------------------------- 1 | {"name":"B. Roof Construction","group":"Codeforces - Codeforces Round #769 (Div. 2)","url":"https://codeforces.com/contest/1632/problem/B","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1643558995430,"input":"4\n2\n3\n5\n10","output":"0 1\n2 0 1\n3 2 1 0 4\n4 6 3 2 0 8 9 1 7 5"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BRoofConstruction"}},"batch":{"id":"2ad0a12b-c906-4593-a4fd-3752ebbeaa6f","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\B_Roof_Construction.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.B_Scenes_From_a_Memory.cpp_c8b7b073e5aaeb32441f4f76cc33ca22.prob: -------------------------------------------------------------------------------- 1 | {"name":"B. Scenes From a Memory","group":"Codeforces - Codeforces Round #741 (Div. 2)","url":"https://codeforces.com/contest/1562/problem/B","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1629995313141,"input":"7\n3\n237\n5\n44444\n3\n221\n2\n35\n3\n773\n1\n4\n30\n626221626221626221626221626221","output":"2\n27\n1\n4\n1\n1\n2\n35\n2\n77\n1\n4\n1\n6"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BScenesFromAMemory"}},"batch":{"id":"a1f742d9-09f6-491b-a529-21bf8a751841","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\CodeForces\\B_Scenes_From_a_Memory.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.B_Who_s_Opposite_.cpp_a18e85dbeeb64a6a6c29f42edb81d1d1.prob: -------------------------------------------------------------------------------- 1 | {"name":"B. Who's Opposite?","group":"Codeforces - Codeforces Round #739 (Div. 3)","url":"https://codeforces.com/contest/1560/problem/B","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1629299875814,"input":"7\n6 2 4\n2 3 1\n2 4 10\n5 3 4\n1 3 2\n2 5 4\n4 3 2","output":"8\n-1\n-1\n-1\n4\n1\n-1"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"BWhosOpposite"}},"batch":{"id":"3b8ff153-bd67-4e3d-8b01-61e5007513f2","size":1},"srcPath":"c:\\MY_DRIVE_LOCAL\\Programming_Coding\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\CodeForces\\B_Who_s_Opposite_.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.D_Productive_Meeting.cpp_1b75318ae7ef5600954f82b28e87c99d.prob: -------------------------------------------------------------------------------- 1 | {"name":"D. Productive Meeting","group":"Codeforces - Codeforces Round #744 (Div. 3)","url":"https://codeforces.com/contest/1579/problem/D","interactive":false,"memoryLimit":256,"timeLimit":2000,"tests":[{"id":1634471200077,"input":"8\n2\n2 3\n3\n1 2 3\n4\n1 2 3 4\n3\n0 0 2\n2\n6 2\n3\n0 0 2\n5\n8 2 0 1 1\n5\n0 1 0 0 6","output":"2\n1 2\n1 2\n3\n1 3\n2 3\n2 3\n5\n1 3\n2 4\n2 4\n3 4\n3 4\n0\n2\n1 2\n1 2\n0\n4\n1 2\n1 5\n1 4\n1 2\n1\n5 2"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"DProductiveMeeting"}},"batch":{"id":"d4fdd904-5a10-4837-8e94-531a41201ce3","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\D_Productive_Meeting.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.E1_Permutation_Minimization_by_Deque.cpp_4dac8df6b731e6d03e1d1230998bbc99.prob: -------------------------------------------------------------------------------- 1 | {"name":"E1. Permutation Minimization by Deque","group":"Codeforces - Codeforces Round #744 (Div. 3)","url":"https://codeforces.com/contest/1579/problem/E1","interactive":false,"memoryLimit":256,"timeLimit":2000,"tests":[{"id":1634464261439,"input":"5\n4\n3 1 2 4\n3\n3 2 1\n3\n3 1 2\n2\n1 2\n2\n2 1","output":"1 3 2 4\n1 2 3\n1 3 2\n1 2\n1 2"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"E1PermutationMinimizationByDeque"}},"batch":{"id":"4e819ec1-3645-4bfa-ac07-18b42f65b567","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\E1_Permutation_Minimization_by_Deque.cpp"} -------------------------------------------------------------------------------- /CodeForces/.cph/.E2_Distance_Tree_hard_version_.cpp_28dce4f4e4760821cd3bcf238725b53d.prob: -------------------------------------------------------------------------------- 1 | {"name":"E2. Distance Tree (hard version)","group":"Codeforces - Codeforces Round #769 (Div. 2)","url":"https://codeforces.com/contest/1632/problem/E2","interactive":false,"memoryLimit":512,"timeLimit":2000,"tests":[{"input":"3\n4\n1 2\n2 3\n1 4\n2\n1 2\n7\n1 2\n1 3\n3 4\n3 5\n3 6\n5 7\n","output":"1 2 2 2\n1 1\n2 2 3 3 3 3 3\n","id":1643553487053}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"E2DistanceTreeHardVersion"}},"batch":{"id":"c2d791f3-4e84-4434-80ca-0e5444f42c09","size":6},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeForces\\E2_Distance_Tree_hard_version_.cpp"} -------------------------------------------------------------------------------- /CodeForces/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /CodeForces/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "cmath": "cpp", 4 | "iostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /CodeForces/E2_Distance_Tree_hard_version_.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/CodeForces/E2_Distance_Tree_hard_version_.cpp -------------------------------------------------------------------------------- /CodeVita/.cph/.Coin_collection.cpp_e227727beae4aae775d3b0b2849352d0.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: Coin_collection","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\Coin_collection.cpp","tests":[{"id":1648569067176,"input":"4\nCNNC\nNCCN\nNCCN\nCNNC","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\Coin_collection.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeVita/.cph/.Max_Funds_MockVita.cpp_a735d02dccf89bc6e9711f130bc282a6.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: Max_Funds_MockVita","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\Max_Funds_MockVita.cpp","tests":[{"id":1646408325980,"input":"5\n23 43 123 54 2\n3\n1 3\n2 3\n1 2","output":"189"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\Max_Funds_MockVita.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeVita/.cph/.Minimum_Gifts.cpp_f07809c9e7920f06c2a4bb4fd7f510e0.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: Minimum_Gifts","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\Minimum_Gifts.cpp","tests":[{"id":1645947008135,"input":"1\n5\n1 2 1 5 2","output":"7"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\Minimum_Gifts.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeVita/.cph/.primeConst.cpp_be6d4dd4ff59a78d1393c791e47c10be.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: primeConst","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\primeConst.cpp","tests":[{"id":1648213783109,"input":"2\n3 4 5 1\n3 4 5 2","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\primeConst.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeVita/.cph/.primeConst_2.cpp_843aa0c39da7803783fa4f2b34f9804b.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: primeConst_2","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\primeConst_2.cpp","tests":[{"id":1648214734349,"input":"3 4 5 1","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\primeConst_2.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeVita/.cph/.test.cpp_8c829a8dd41ef10a9731576396f9d12a.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: test","url":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\test.cpp","tests":[{"id":1646415019128,"input":"5\n23 43 123 54 2\n3\n1 3\n2 3\n1 2","output":"189"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\CodeVita\\test.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /CodeVita/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | 6 | -------------------------------------------------------------------------------- /CodeVita/Mario.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/CodeVita/Mario.pdf -------------------------------------------------------------------------------- /CodeVita/coin collection.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/CodeVita/coin collection.pdf -------------------------------------------------------------------------------- /CodeVita/prime const.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/CodeVita/prime const.pdf -------------------------------------------------------------------------------- /CodeVita/prime villa.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/CodeVita/prime villa.pdf -------------------------------------------------------------------------------- /CodeVita/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | int a[n], b[n]; -------------------------------------------------------------------------------- /DOCS.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Get ASCII values 4 | ________________________ 5 | char c = 'A'; 6 | cout << int(c); 7 | 8 | int a = 67; 9 | cout << char(a); 10 | 11 | 12 | // Substring Find 13 | s.substr(start_index , len_of_substring) -------------------------------------------------------------------------------- /Dynamic_Programming/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Dynamic_Programming/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "chrono": "cpp", 4 | "random": "cpp", 5 | "limits": "cpp", 6 | "valarray": "cpp", 7 | "array": "cpp", 8 | "bitset": "cpp", 9 | "string_view": "cpp", 10 | "initializer_list": "cpp", 11 | "regex": "cpp", 12 | "utility": "cpp", 13 | "unordered_set": "cpp", 14 | "iostream": "cpp", 15 | "equal_partition_subset_ispossiblecpp": "cpp" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Dynamic_Programming/1137.n-th-tribonacci-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1137 lang=cpp 3 | * 4 | * [1137] N-th Tribonacci Number 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int tribonacci(int n) 12 | { 13 | // ------------------------- 14 | // RECURSIVE APPROACH METHOD 15 | if (n == 0) 16 | return 0; 17 | else if (n == 1 || n == 2) 18 | return 1; 19 | /* else 20 | return (tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)); */ 21 | 22 | // ----------------------------------- 23 | // DYNAMIC PROGRAMMING APPROACH 24 | int dp[n + 1], i; // array to store memoization 25 | dp[0] = 0; 26 | dp[1] = 1; 27 | dp[2] = 1; 28 | 29 | for (i = 3; i <= n; i++) 30 | dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; 31 | 32 | return dp[n]; 33 | } 34 | }; 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Dynamic_Programming/122.best-time-to-buy-and-sell-stock-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=122 lang=cpp 3 | * 4 | * [122] Best Time to Buy and Sell Stock II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxProfit(vector &prices) 12 | { 13 | int maxprofit = 0, i; 14 | for (i = 1; i < prices.size(); i++) 15 | { 16 | if (prices[i] > prices[i - 1]) 17 | maxprofit += prices[i] - prices[i - 1]; // Sum of all the drop 18 | } 19 | return maxprofit; 20 | } 21 | }; 22 | // @lc code=end 23 | -------------------------------------------------------------------------------- /Dynamic_Programming/413.arithmetic-slices.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=413 lang=cpp 3 | * 4 | * [413] Arithmetic Slices 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int numberOfArithmeticSlices(vector &nums) 12 | { 13 | int i, len = nums.size(), ans = 0; 14 | vector dp(len, 0); // initalises with 0 15 | 16 | if (len < 3) 17 | return 0; 18 | 19 | for (i = 1; i < len - 1; i++) 20 | { 21 | if (nums[i] - nums[i - 1] == nums[i + 1] - nums[i]) 22 | { 23 | // it will consider the whole means in [3 -1 -5 -9] 3 -1 -5 and -1 -5 9 both will be considered when dp[i] = 2; 24 | dp[i] = dp[i - 1] + 1; 25 | ans = ans + dp[i]; 26 | } 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Dynamic_Programming/62.unique-paths.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=62 lang=java 3 | * 4 | * [62] Unique Paths 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | 10 | private int findWays(int row, int col, int[][] Dp) { 11 | if (row == 0 && col == 0) 12 | return 1; 13 | 14 | if (row < 0 || col < 0) // Out Of Bounds 15 | return 0; 16 | 17 | if (Dp[row][col] != -1) 18 | return Dp[row][col]; 19 | 20 | int up = findWays(row - 1, col, Dp); 21 | int left = findWays(row, col - 1, Dp); 22 | 23 | return Dp[row][col] = (up + left); 24 | } 25 | 26 | public int uniquePaths(int m, int n) { 27 | 28 | // Initialize 2D array to -1 29 | int[][] Dp = new int[m][n]; 30 | for (int[] arr1 : Dp) 31 | Arrays.fill(arr1, -1); 32 | 33 | int result = findWays(m - 1, n - 1, Dp); 34 | return result; 35 | } 36 | } 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Dynamic_Programming/70.climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=70 lang=cpp 3 | * 4 | * [70] Climbing Stairs 5 | */ 6 | // https://leetcode.com/problems/climbing-stairs/ 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int helper(int n, vector &dp) 12 | { 13 | if (n <= 2) 14 | return n; 15 | 16 | if (dp[n] != -1) 17 | return dp[n]; 18 | 19 | dp[n] = helper(n - 1, dp) + helper(n - 2, dp); 20 | return dp[n]; 21 | } 22 | 23 | int climbStairs(int n) 24 | { 25 | vector dp(n + 1, -1); 26 | return helper(n, dp); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Dynamic_Programming/740.delete-and-earn.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=740 lang=cpp 3 | * 4 | * [740] Delete and Earn 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public: 10 | int deleteAndEarn(vector& nums) { 11 | 12 | } 13 | }; 14 | // @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /Dynamic_Programming/Dp-on-strings_LCS/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | LCS_Recursive -------------------------------------------------------------------------------- /Dynamic_Programming/Jump Game/1306.jump-game-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1306 lang=cpp 3 | * 4 | * [1306] Jump Game III 5 | * 6 | * https://leetcode.com/problems/jump-game-iii/ 7 | * 8 | */ 9 | 10 | // DYNAMIC PROGRAMMING ----- RECURSIVE METHOD 11 | // @lc code=start 12 | class Solution 13 | { 14 | public: 15 | bool canReach(vector &arr, int start) 16 | { 17 | int result = 0, rightside = 0, leftside = 0, len = arr.size(); 18 | 19 | if (arr[start] == -1) // Boundary Condition + Visited Check 20 | return false; 21 | 22 | if (arr[start] == 0) 23 | return true; 24 | 25 | // Leftside and Rightside Jumps 26 | leftside = start - arr[start]; 27 | rightside = start + arr[start]; 28 | 29 | arr[start] = -1; // mark the start index, if returned to the start again then FALSE not possible. 30 | 31 | result = canReach(arr, leftside) || canReach(arr, rightside); 32 | 33 | return result; 34 | } 35 | }; 36 | // @lc code=end 37 | -------------------------------------------------------------------------------- /Dynamic_Programming/Jump Game/1696.jump-game-vi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1696 lang=cpp 3 | * 4 | * [1696] Jump Game VI 5 | * 6 | * https://leetcode.com/problems/jump-game-vi/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int maxResult(vector &arr, int k) 15 | { 16 | } 17 | }; 18 | // @lc code=end 19 | -------------------------------------------------------------------------------- /Dynamic_Programming/Jump Game/45.jump-game-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=45 lang=cpp 3 | * 4 | * [45] Jump Game II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int jump(vector &nums) 12 | { 13 | int len = nums.size(), i, reachable = 0, jumps = 0, maxReachable = 0; 14 | 15 | if (nums[0] == 0 && len == 1) // [0] 16 | return 0; 17 | 18 | if (nums[0] == 0 || len == 1) // Start 0 19 | return 0; 20 | 21 | // Peak Valley Approach <------ GREEDY APPROACH 22 | for (i = 0; i < len - 1; i++) 23 | { 24 | if (reachable < i) 25 | return 0; 26 | 27 | reachable = max(reachable, i + nums[i]); 28 | if (i == maxReachable) 29 | { 30 | jumps++; 31 | maxReachable = reachable; 32 | } 33 | } 34 | 35 | return jumps; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Dynamic_Programming/Jump Game/55.jump-game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=55 lang=cpp 3 | * 4 | * [55] Jump Game 5 | * 6 | * https://leetcode.com/problems/jump-game/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | bool canJump(vector &nums) 15 | { 16 | int len = nums.size(), i, reachable = 0, count = 0; 17 | 18 | if (arr[0] == 0 && len == 1 || len == 1) // [0] 19 | return true; 20 | 21 | if (arr[0] == 0) 22 | return false; 23 | 24 | // Peak Valley Approach <------ GREEDY APPROACH 25 | for (i = 0; i < len; i++) 26 | { 27 | if (i > reachable) // could not react Index 28 | return false; 29 | 30 | reachable = max(reachable, i + nums[i]); // Index Trace 31 | 32 | if (reachable >= len - 1) 33 | return true; 34 | } 35 | 36 | return false; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Dynamic_Programming/Knapsack-pattern-pick-nonpick-problems/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | weight -------------------------------------------------------------------------------- /Dynamic_Programming/MCM/Minimum_number_of_palindromic_partition: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/Dynamic_Programming/MCM/Minimum_number_of_palindromic_partition -------------------------------------------------------------------------------- /Dynamic_Programming/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | return Dp[len][sum]; 3 | 4 | // When Element is Greater than SUM 5 | if (arr[len - 1] > sum) -------------------------------------------------------------------------------- /Graphs/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Graphs/General/Cycle_Detection_Directed_BFS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/Graphs/General/Cycle_Detection_Directed_BFS -------------------------------------------------------------------------------- /Graphs/MInimum_Cost_Spanning_Tree/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.exe -------------------------------------------------------------------------------- /Graphs/MInimum_Cost_Spanning_Tree/Prims_Algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Compititive_Programming_and_LeetCode-problem-solutions/ba96753d248bf83ee25fc1fb6b113c67db183acb/Graphs/MInimum_Cost_Spanning_Tree/Prims_Algorithm -------------------------------------------------------------------------------- /Graphs/Shortest_Path_Algorithm/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.exe -------------------------------------------------------------------------------- /Graphs/Shortest_Path_Algorithm/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | make_pair -------------------------------------------------------------------------------- /Graphs/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | * -------------------------------------------------------------------------------- /Greedy/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Greedy/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "valarray": "cpp", 4 | "iostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /Greedy/1053.previous-permutation-with-one-swap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1053 lang=cpp 3 | * 4 | * [1053] Previous Permutation With One Swap 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector prevPermOpt1(vector &arr) 12 | { 13 | 14 | int i, count = 0; 15 | vector CopyVec(arr); // coping the vector 16 | sort(arr.begin(), arr.end()); 17 | 18 | for (i = 0; i < CopyVec.size; i++) 19 | { 20 | if (arr[i] != CopyVec[i]) 21 | count++; 22 | } 23 | 24 | if (count == 2) 25 | return CopyVec; 26 | else 27 | return arr; 28 | } 29 | }; 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Greedy/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | if (vec[i] < 0 && k > 0) 3 | { -------------------------------------------------------------------------------- /HEAP_NOTES.txt: -------------------------------------------------------------------------------- 1 | 2 | // _________________ Heap Data Structure _____________________ 3 | 4 | 5 | 6 | * K + Largest Element ---> MIN HEAP USE 7 | * K + Smallest Element ---> MAX HEAP USE 8 | * Default priority_queue in c++ is a Max Heap 9 | 10 | * TIME COMPLEXITY : O(n log k) time 11 | 12 | 13 | ● A min-heap is a binary tree-> the data contained in each node is less than (or equal to) the data in that node's children. 14 | Starting Node(parent) MINIMUM 15 | 16 | 17 | // Creates a max heap (default) 18 | --------------------------------------- 19 | priority_queue pq; 20 | 21 | 22 | 23 | // Creates a min heap 24 | --------------------------------------- 25 | priority_queue , greater > pq; 26 | 27 | 28 | 29 | // Create a min heap of pair 30 | --------------------------------------- 31 | typedef pair P; 32 | priority_queue< P, vector

, greater

> minHeap ; -------------------------------------------------------------------------------- /Hash_Map/.cph/.Rapid_Re_Orderings.cpp_7af4aea7de001b7431ef50dc1b3d3963.prob: -------------------------------------------------------------------------------- 1 | {"name":"Rapid Re-Orderings","group":"CodeChef - November Lunchtime 2021 Division 2","url":"https://www.codechef.com/LTIME102B/problems/RPDRDNG","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1639470911025,"input":"2\n3\n6 5 5 7 6 6\n3\n1 2 1 4 2 4","output":"5 6 7\n-1"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"RapidReOrderings"}},"batch":{"id":"74ead286-eb3e-43be-8c19-aeae31c8bb4b","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Hash_Map\\Rapid_Re_Orderings.cpp"} -------------------------------------------------------------------------------- /Hash_Map/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Hash_Map/1.two-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1 lang=cpp 3 | * 4 | * https://leetcode.com/problems/two-sum/ 5 | * 6 | * [1] Two Sum 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | vector twoSum(vector &nums, int target) 14 | { 15 | 16 | unordered_map hashMap; 17 | int i, len = nums.size(), rem = 0; 18 | 19 | for (i = 0; i < len; i++) 20 | { 21 | rem = target - nums[i]; 22 | if (hashMap.find(rem) != hashMap.end()) // found 23 | return {i, hashMap[rem]}; 24 | 25 | else 26 | hashMap[nums[i]] = i; 27 | } 28 | 29 | return {}; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Hash_Map/1094.car-pooling.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1094 lang=cpp 3 | * 4 | * [1094] Car Pooling 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | /* 12 | 0 -> Passenger 13 | 1 -> Start 14 | 2 -> End 15 | */ 16 | bool carPooling(vector> &trips, int capacity) 17 | { 18 | // Ordered Map as we have to sort 19 | map hashMap; 20 | int currPassenger = 0; 21 | 22 | for (auto arr : trips) 23 | { 24 | hashMap[arr[1]] += arr[0]; // add passsenger start time 25 | hashMap[arr[2]] -= arr[0]; // delete at end / drop when they reach 26 | } 27 | 28 | for (auto it : hashMap) 29 | { 30 | currPassenger = currPassenger + it.second; 31 | if (currPassenger > capacity) 32 | return false; 33 | } 34 | 35 | return true; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Hash_Map/137.single-number-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=137 lang=cpp 3 | * 4 | * [137] Single Number II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int singleNumber(vector &nums) 12 | { 13 | // int i, ans = 0; 14 | // for (i = 0; i < nums.size(); i++) 15 | // ans = ans ^ nums[i]; // Xor of 2 ^ 2 = 0 16 | // return ans; 17 | 18 | unordered_map map; 19 | int i, ans = 0; 20 | 21 | for (i = 0; i < nums.size(); i++) 22 | map[nums[i]]++; 23 | 24 | for (auto j = map.begin(); j != map.end(); j++) 25 | { 26 | if (j->second == 1) 27 | ans = j->first; 28 | } 29 | 30 | return ans; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /Hash_Map/389.find-the-difference.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=389 lang=cpp 3 | * 4 | * https://leetcode.com/problems/find-the-difference/ 5 | * 6 | * [389] Find the Difference 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | char findTheDifference(string s, string t) 14 | { 15 | unordered_map hashMap; // char count 16 | int i, sLen = s.length(), tLen = t.length(); 17 | char ele; 18 | 19 | for (i = 0; i < sLen; i++) 20 | hashMap[s[i]]++; 21 | 22 | for (i = 0; i < tLen; i++) 23 | { 24 | if (hashMap.find(t[i]) != hashMap.end()) // present before end 25 | hashMap[t[i]]--; 26 | else 27 | hashMap[t[i]]++; 28 | } 29 | 30 | for (auto &it : hashMap) 31 | if (it.second) 32 | ele = it.first; 33 | 34 | return ele ; 35 | } 36 | }; 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Hash_Map/448.find-all-numbers-disappeared-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=448 lang=cpp 3 | * 4 | * [448] Find All Numbers Disappeared in an Array 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector findDisappearedNumbers(vector &nums) 12 | { 13 | int i, len = nums.size(); 14 | vector result; 15 | unordered_map hashMap; 16 | 17 | for (i = 0; i < len; i++) 18 | hashMap[nums[i]]++; 19 | 20 | for (i = 1; i <= len; i++) 21 | if (hashMap.find(i) == hashMap.end()) // if NO VALUE corresponding to the index 22 | result.push_back(i); 23 | 24 | return result; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Hash_Map/451.sort-characters-by-frequency.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=451 lang=cpp 3 | * 4 | * [451] Sort Characters By Frequency 5 | * 6 | * https://leetcode.com/problems/sort-characters-by-frequency/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | string frequencySort(string s) 15 | { 16 | int len = s.size(); 17 | unordered_map hashMap; // [char , count] 18 | string res = ""; 19 | 20 | for (int i = 0; i < len; i++) 21 | hashMap[s[i]]++; 22 | 23 | multimap> MM; // [count , char] DESC 24 | 25 | for (auto &it : hashMap) 26 | MM.insert({it.second, it.first}); 27 | // MM.insert({it.second , it.first}); 28 | 29 | for (auto it : MM) 30 | { 31 | int freq = it.first; 32 | while (freq--) 33 | res += it.second; 34 | } 35 | 36 | return res; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Hash_Map/532.k-diff-pairs-in-an-array.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=532 lang=java 3 | * 4 | * [532] K-diff Pairs in an Array 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int findPairs(int[] nums, int k) { 10 | HashMap map = new HashMap<>(); 11 | int i, res = 0, len = nums.length; 12 | 13 | for (i = 0; i < len; i++) 14 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); 15 | 16 | for (int it : map.keySet()) { 17 | if (k > 0 && map.containsKey(it + k) || k == 0 && map.get(it) > 1) 18 | res++; 19 | } 20 | 21 | return res; 22 | } 23 | } 24 | // @lc code=end 25 | -------------------------------------------------------------------------------- /Hash_Map/Anagram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // custom compare function 7 | int com(string s1, string s2) 8 | { 9 | int len1 = s1.length(), len2 = s2.length(); 10 | 11 | for (int i = 0; i < len1; i++) 12 | { 13 | if (s1[i] != s2[i]) 14 | return 0; 15 | } 16 | 17 | return 1; 18 | } 19 | 20 | void check(string s1, string s2, int len1, int len2) 21 | { 22 | if (len1 != len2) 23 | return; 24 | 25 | sort(s1.begin(), s1.end()); 26 | sort(s2.begin(), s2.end()); 27 | 28 | if (com(s1, s2)) 29 | cout << "Both strings are Anagram\n"; 30 | 31 | else 32 | cout << "Strings are NOT Anagram\n"; 33 | } 34 | 35 | int main() 36 | { 37 | string s1 = "test"; 38 | string s2 = "tt"; 39 | 40 | int len1 = s1.length(), len2 = s2.length(); 41 | check(s1, s2, len1, len2); 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Hash_Map/Ninja_and_String_Operation.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | // https://www.codingninjas.com/codestudio/contests/codestudio-weekend-contest-25/problems/19380 4 | 5 | int isPossibleToMakeEqual(int n, vector &s) 6 | { 7 | // Write your code here. 8 | unordered_map map; 9 | int i = 0, j = 0; 10 | 11 | // Store in the HashMap 12 | for (i = 0; i < n; i++) 13 | { 14 | for (j = 0; j < s[i].size(); j++) 15 | map[s[i][j]]++; 16 | } 17 | 18 | for (auto it : map) 19 | { 20 | if (it.second % n != 0) 21 | return 0; 22 | } 23 | 24 | return 1; 25 | } 26 | -------------------------------------------------------------------------------- /Hash_Map/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | /* for (i = 0; i < len; i++) 2 | { 3 | for (j = i + 1; j < len; j++) 4 | if (abs(nums[i] - nums[j]) == k || abs(nums[j] - nums[i]) == k) 5 | count++; 6 | } */ -------------------------------------------------------------------------------- /Heap/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | 6 | -------------------------------------------------------------------------------- /Heap/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } -------------------------------------------------------------------------------- /Heap/1046.last-stone-weight.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1046 lang=cpp 3 | * 4 | * [1046] Last Stone Weight 5 | * 6 | * https://leetcode.com/problems/last-stone-weight/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int lastStoneWeight(vector &stones) 15 | { 16 | int len = stones.size(), i; 17 | priority_queue pq; 18 | 19 | for (i = 0; i < len; i++) 20 | pq.push(stones[i]); 21 | 22 | if (len == 1) 23 | return stones[0]; 24 | 25 | while (!pq.empty() && pq.size() > 1) 26 | { 27 | int y = pq.top(); 28 | pq.pop(); 29 | int x = pq.top(); 30 | pq.pop(); 31 | 32 | if (x == y) 33 | continue; 34 | 35 | pq.push(y - x); 36 | } 37 | 38 | return pq.empty() ? 0 : pq.top(); 39 | } 40 | }; 41 | // @lc code=end 42 | -------------------------------------------------------------------------------- /Heap/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Leet_Code_Problems/.cph/.E_Stronger_Takahashi.cpp_1b48ef665fc248a02cb74697e00695ea.prob: -------------------------------------------------------------------------------- 1 | {"name":"E - Stronger Takahashi","group":"AtCoder - AtCoder Beginner Contest 213","url":"https://atcoder.jp/contests/abc213/tasks/abc213_e","interactive":false,"memoryLimit":1024,"timeLimit":2000,"tests":[{"input":"5 5\n..#..\n#.#.#\n##.##\n#.#.#\n..#..\n","output":"1\n","id":1640530195537},{"id":1640530195543,"input":"8 8\n.#######\n########\n########\n########\n########\n########\n########\n#######.","output":"5"},{"id":1640530195543,"input":"8 8\n.#######\n########\n########\n########\n########\n########\n########\n#######.","output":"5"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"EStrongerTakahashi"}},"batch":{"id":"c9260dcb-3c93-42f1-9e8c-c9fe76a2a7ec","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Leet_Code_Problems\\E_Stronger_Takahashi.cpp"} -------------------------------------------------------------------------------- /Leet_Code_Problems/1.two-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1 lang=cpp 3 | * 4 | * https://leetcode.com/problems/two-sum/ 5 | * 6 | * [1] Two Sum 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | vector twoSum(vector &nums, int target) 14 | { 15 | 16 | unordered_map hashMap; 17 | int i, len = nums.size(), rem = 0; 18 | 19 | for (i = 0; i < len; i++) 20 | { 21 | rem = target - nums[i]; 22 | if (hashMap.find(rem) != hashMap.end()) // found 23 | return {i, hashMap[rem]}; 24 | 25 | else 26 | hashMap[nums[i]] = i; 27 | } 28 | 29 | return {}; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1.two-sum.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | /* 4 | * @lc app=leetcode id=1 lang=java 5 | * 6 | * [1] Two Sum 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public int[] twoSum(int[] nums, int target) { 12 | 13 | int i, len = nums.length, diff = 0; 14 | HashMap hashMap = new HashMap<>(); 15 | int arr[] = new int[2]; 16 | 17 | for (i = 0; i < len; i++) { 18 | diff = target - nums[i]; 19 | if (hashMap.containsKey(diff)) { 20 | arr[1] = i; 21 | arr[0] = hashMap.get(diff); 22 | break; 23 | } 24 | hashMap.put(nums[i], i); 25 | } 26 | 27 | return arr; 28 | } 29 | } 30 | // @lc code=end 31 | 32 | /* 33 | KEY --> Value of Array 34 | VALUE --> Index 35 | */ -------------------------------------------------------------------------------- /Leet_Code_Problems/1009.complement-of-base-10-integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1009 lang=cpp 3 | * 4 | * [1009] Complement of Base 10 Integer 5 | */ 6 | // https://leetcode.com/problems/complement-of-base-10-integer/ 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int bitwiseComplement(int n) 12 | { 13 | int ans = 0, mask = 0, num = n; 14 | 15 | // Base Case 16 | if (n == 0) 17 | return 1; 18 | 19 | while (num != 0) 20 | { 21 | num = num >> 1; 22 | mask = (mask << 1) | 1; // Left shift + Bitwise OR 23 | } 24 | 25 | ans = (~n) & mask; 26 | return ans; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1014.best-sightseeing-pair.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1014 lang=cpp 3 | * 4 | * [1014] Best Sightseeing Pair 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxScoreSightseeingPair(vector &a) 12 | { 13 | int j, answer = INT_MIN, len = a.size(), aiplusi = a[0] + 0; 14 | // i < j 15 | // a[i] + a[j] + i -j; 16 | // (a[i] + i) + (a[j] - j) <---- we have to maximise both the part 17 | 18 | for (j = 1; j < len; j++) 19 | { 20 | answer = max(answer, aiplusi + (a[j] - j)); 21 | aiplusi = max(aiplusi, a[j] + j); // modified a[i] + i 22 | } 23 | 24 | return answer; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1046.last-stone-weight.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1046 lang=cpp 3 | * 4 | * [1046] Last Stone Weight 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int lastStoneWeight(vector &stones) 12 | { 13 | int len = stones.size(), i; 14 | priority_queue pq; 15 | 16 | for (i = 0; i < len; i++) 17 | pq.push(stones[i]); 18 | 19 | if (len == 1) 20 | return stones[0]; 21 | 22 | while (!pq.empty() && pq.size() > 1) 23 | { 24 | int y = pq.top(); 25 | pq.pop(); 26 | int x = pq.top(); 27 | pq.pop(); 28 | 29 | if (x == y) 30 | continue; 31 | 32 | pq.push(y - x); 33 | } 34 | 35 | return pq.empty() ? 0 : pq.top(); 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1053.previous-permutation-with-one-swap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1053 lang=cpp 3 | * 4 | * [1053] Previous Permutation With One Swap 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector prevPermOpt1(vector &arr) 12 | { 13 | 14 | int i, count = 0; 15 | vector CopyVec(arr); // coping the vector 16 | sort(arr.begin(), arr.end()); 17 | // CopyVec = arr; 18 | 19 | for (i = 0; i < CopyVec.size; i++) 20 | { 21 | if (arr[i] != CopyVec[i]) 22 | count++; 23 | } 24 | 25 | if (count == 2) 26 | return CopyVec; 27 | else 28 | return arr; 29 | } 30 | }; 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1094.car-pooling.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1094 lang=cpp 3 | * 4 | * [1094] Car Pooling 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | /* 12 | 0 -> Passenger 13 | 1 -> Start 14 | 2 -> End 15 | */ 16 | bool carPooling(vector> &trips, int capacity) 17 | { 18 | // Ordered Map as we have to sort 19 | map hashMap; 20 | int currPassenger = 0; 21 | 22 | for (auto arr : trips) 23 | { 24 | hashMap[arr[1]] += arr[0]; // add passsenger start time 25 | hashMap[arr[2]] -= arr[0]; // delete at end / drop when they reach 26 | } 27 | 28 | for (auto it : hashMap) 29 | { 30 | currPassenger = currPassenger + it.second; 31 | if (currPassenger > capacity) 32 | return false; 33 | } 34 | 35 | return true; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1094.car-pooling.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1094 lang=java 3 | * 4 | * [1094] Car Pooling 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public boolean carPooling(int[][] trips, int capacity) { 10 | 11 | // HashMap hashMap = new HashMap<>(); 12 | int lengthOfTrip[] = new int[1001]; 13 | int currPassenger = 0, i; 14 | 15 | // 2D - Array 16 | for (int arr[] : trips) { 17 | lengthOfTrip[arr[1]] += arr[0]; // add passsenger start time 18 | lengthOfTrip[arr[2]] -= arr[0]; // delete at end / drop when they reach 19 | } 20 | 21 | for (i = 0; i < lengthOfTrip.length; i++) { 22 | currPassenger = currPassenger + lengthOfTrip[i]; 23 | if (currPassenger > capacity) 24 | return false; 25 | } 26 | 27 | return true; 28 | } 29 | } 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1095.find-in-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1095 lang=cpp 3 | * 4 | * [1095] Find in Mountain Array 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * // This is the MountainArray's API interface. 10 | * // You should not implement it, or speculate about its implementation 11 | * class MountainArray { 12 | * public: 13 | * int get(int index); 14 | * int length(); 15 | * }; 16 | */ 17 | 18 | class Solution { 19 | public: 20 | int findInMountainArray(int target, MountainArray &mountainArr) { 21 | 22 | } 23 | }; 24 | // @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /Leet_Code_Problems/11.container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=11 lang=cpp 3 | * 4 | * [11] Container With Most Water 5 | * 6 | * https://leetcode.com/problems/container-with-most-water/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int maxArea(vector &height) 15 | { 16 | int len = height.size(), result = 0; 17 | 18 | return result; 19 | } 20 | }; 21 | // @lc code=end 22 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1137.n-th-tribonacci-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1137 lang=cpp 3 | * 4 | * [1137] N-th Tribonacci Number 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int tribonacci(int n) 12 | { 13 | // ------------------------- 14 | // RECURSIVE APPROACH METHOD 15 | if (n == 0) 16 | return 0; 17 | else if (n == 1 || n == 2) 18 | return 1; 19 | /* else 20 | return (tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)); */ 21 | 22 | // ----------------------------------- 23 | // DYNAMIC PROGRAMMING APPROACH 24 | int dp[n + 1], i; // array to store memoization 25 | dp[0] = 0; 26 | dp[1] = 1; 27 | dp[2] = 1; 28 | 29 | for (i = 3; i <= n; i++) 30 | dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; 31 | 32 | return dp[n]; 33 | } 34 | }; 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Leet_Code_Problems/118.pascals-triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=118 lang=cpp 3 | * 4 | * [118] Pascal's Triangle 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector> generate(int numRows) 12 | { 13 | vector> pattern(numRows); 14 | 15 | for (int i = 0; i < numRows; i++) 16 | { 17 | pattern[i].resize(i + 1); 18 | pattern[i][0] = pattern[i][i] = 1; // Assign 1st and Last as 1 19 | 20 | for (int j = 1; j < i; j++) 21 | pattern[i][j] = pattern[i - 1][j - 1] + pattern[i - 1][j]; // perform SUM for the middle elements 22 | } 23 | 24 | return pattern; 25 | } 26 | }; 27 | 28 | // @lc code=end 29 | 30 | /* 31 | Pattern Vector will be like this : 32 | 33 | 1 34 | 1 1 35 | 1 2 1 36 | 1 3 3 1 37 | 1 4 6 4 1 38 | 39 | */ -------------------------------------------------------------------------------- /Leet_Code_Problems/1189.maximum-number-of-balloons.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1189 lang=cpp 3 | * 4 | * [1189] Maximum Number of Balloons 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxNumberOfBalloons(string text) 12 | { 13 | int i, ans = 0; 14 | unordered_map hashMap; 15 | 16 | for (i = 0; i < text.length(); i++) 17 | hashMap[text[i]]++; 18 | 19 | // If all the letters of BALLON present then only enters in the loop as otherwise it is not Possible. 20 | while (hashMap['b'] > 0 && hashMap['a'] > 0 && hashMap['l'] > 1 && hashMap['o'] > 1 && hashMap['n'] > 0) 21 | { 22 | hashMap['b'] -= 1; 23 | hashMap['a'] -= 1; 24 | hashMap['l'] -= 2; 25 | hashMap['o'] -= 2; 26 | hashMap['n'] -= 1; 27 | 28 | ans++; 29 | } 30 | 31 | return ans; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1200.minimum-absolute-difference.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1200 lang=cpp 3 | * 4 | * [1200] Minimum Absolute Difference 5 | * 6 | * https://leetcode.com/problems/minimum-absolute-difference/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | vector> minimumAbsDifference(vector &arr) 15 | { 16 | vector> result; 17 | int minDifference = INT_MAX, i = 0, len = arr.size(); 18 | sort(arr.begin(), arr.end()); 19 | 20 | for (i = 1; i < len; i++) 21 | if (minDifference > (arr[i] - arr[i - 1])) 22 | minDifference = arr[i] - arr[i - 1]; 23 | // minDifference = min(minDifference, (arr[i] - arr[i - 1])); 24 | 25 | for (i = 1; i < len; i++) 26 | if (arr[i] - arr[i - 1] == minDifference) 27 | result.push_back({arr[i - 1], arr[i]}); 28 | 29 | return result; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1217.minimum-cost-to-move-chips-to-the-same-position.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1217 lang=cpp 3 | * 4 | * [1217] Minimum Cost to Move Chips to The Same Position 5 | */ 6 | 7 | // @lc code=start 8 | // GREEDY ALGORITHM 9 | 10 | /* 11 | We can move all chips at EVEN positions to position 0, and chips at the ODD positions to position 1. 12 | 13 | */ 14 | class Solution 15 | { 16 | public: 17 | int minCostToMoveChips(vector &position) 18 | { 19 | int i, len = position.size(), minPosition = 0, even = 0, odd = 0; 20 | 21 | // Counting ODD and EVEN positions 22 | for (i = 0; i < len; i++) 23 | { 24 | if (position[i] % 2 == 0) // EVEN position 25 | even++; 26 | else 27 | odd++; 28 | } 29 | 30 | minPosition = min(even, odd); 31 | return minPosition; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/122.best-time-to-buy-and-sell-stock-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=122 lang=cpp 3 | * 4 | * [122] Best Time to Buy and Sell Stock II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxProfit(vector &prices) 12 | { 13 | int maxprofit = 0, i; 14 | for (i = 1; i < prices.size(); i++) 15 | { 16 | if (prices[i] > prices[i - 1]) 17 | maxprofit += prices[i] - prices[i - 1]; // Sum of all the drop 18 | } 19 | return maxprofit; 20 | } 21 | }; 22 | // @lc code=end 23 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1260.shift-2-d-grid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1260 lang=cpp 3 | * 4 | * [1260] Shift 2D Grid 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector> shiftGrid(vector> &grid, int k) 12 | { 13 | int n = grid.size(); // row 14 | int m = grid[0].size(); // column 15 | vector> ans(n, vector(m)); 16 | 17 | for (int i = 0; i < n; i++) 18 | { 19 | for (int j = 0; j < m; j++) 20 | { 21 | int newCol = (j + k) % m; // (j + numbers of columns added)%m 22 | int newRow = (i + (j + k) / m) % n; // (i + numbers of rows added)%n 23 | 24 | ans[newRow][newCol] = grid[i][j]; 25 | } 26 | } 27 | return ans; 28 | } 29 | }; 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1288.remove-covered-intervals.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1288 lang=cpp 3 | * 4 | * [1288] Remove Covered Intervals 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public: 10 | int removeCoveredIntervals(vector>& intervals) { 11 | 12 | } 13 | }; 14 | // @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1291.sequential-digits.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1291 lang=java 3 | * 4 | * [1291] Sequential Digits 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | 10 | List vec = new ArrayList(); 11 | 12 | public List sequentialDigits(int low, int high) { 13 | int i; 14 | // Range 1 to 9 15 | for (i = 1; i < 10; i++) { 16 | dfs(low, high, i, 0); 17 | } 18 | 19 | Collections.sort(vec); 20 | return vec; 21 | } 22 | 23 | void dfs(int low, int high, int i, int num) { 24 | 25 | if (low <= num && num <= high) 26 | vec.add(num); 27 | 28 | if (num > high || i > 9) // Out of Bounds 29 | return; 30 | 31 | dfs(low, high, i + 1, num * 10 + i); 32 | } 33 | } 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1306.jump-game-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1306 lang=cpp 3 | * 4 | * [1306] Jump Game III 5 | */ 6 | 7 | // DYNAMIC PROGRAMMING ----- RECURSIVE METHOD 8 | // @lc code=start 9 | class Solution 10 | { 11 | public: 12 | bool canReach(vector &arr, int start) 13 | { 14 | int result = 0, rightside = 0, leftside = 0, len = arr.size(); 15 | 16 | if (start < 0 || start >= len || arr[start] == -1) // Boundary Condition + Visited Check 17 | return false; 18 | 19 | if (arr[start] == 0) 20 | return true; 21 | 22 | // Leftside and Rightside Jumps 23 | leftside = start - arr[start]; 24 | rightside = start + arr[start]; 25 | 26 | arr[start] = -1; 27 | 28 | result = canReach(arr, leftside) || canReach(arr, rightside); 29 | 30 | return result; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /Leet_Code_Problems/137.single-number-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=137 lang=cpp 3 | * 4 | * [137] Single Number II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int singleNumber(vector &nums) 12 | { 13 | // int i, ans = 0; 14 | // for (i = 0; i < nums.size(); i++) 15 | // ans = ans ^ nums[i]; // Xor of 2 ^ 2 = 0 16 | // return ans; 17 | 18 | unordered_map map; 19 | int i, ans = 0; 20 | 21 | for (i = 0; i < nums.size(); i++) 22 | map[nums[i]]++; 23 | 24 | for (auto j = map.begin(); j != map.end(); j++) 25 | { 26 | if (j->second == 1) 27 | ans = j->first; 28 | } 29 | 30 | return ans; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /Leet_Code_Problems/141.linked-list-cycle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=141 lang=cpp 3 | * 4 | * [141] Linked List Cycle 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * struct ListNode { 11 | * int val; 12 | * ListNode *next; 13 | * ListNode(int x) : val(x), next(NULL) {} 14 | * }; 15 | */ 16 | class Solution 17 | { 18 | public: 19 | bool hasCycle(ListNode *head) 20 | { 21 | ListNode *slow = head, *fast = head; 22 | 23 | while (fast != NULL && fast->next != NULL) 24 | { 25 | slow = slow->next; 26 | fast = fast->next->next; 27 | 28 | if (slow == fast) 29 | return true; 30 | } 31 | 32 | return false; 33 | } 34 | }; 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1431.kids-with-the-greatest-number-of-candies.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1431 lang=cpp 3 | * 4 | * [1431] Kids With the Greatest Number of Candies 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector kidsWithCandies(vector &candies, int extraCandies) 12 | { 13 | vector out; 14 | int max = *max_element(candies.begin(), candies.end()); 15 | 16 | for (int i = 0; i < candies.size(); i++) 17 | { 18 | if (candies[i] + extraCandies >= max) 19 | out.push_back(true); 20 | else 21 | out.push_back(false); 22 | } 23 | 24 | return out; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1446.consecutive-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1446 lang=cpp 3 | * 4 | * [1446] Consecutive Characters 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxPower(string s) 12 | { 13 | int count = 1, maxCount = 0, i = 0; 14 | 15 | for (int i = 0; i < s.length() - 1; i++) 16 | { 17 | if (s[i] == s[i + 1]) 18 | count++; 19 | 20 | else 21 | { 22 | if (maxCount < count) 23 | maxCount = count; 24 | count = 1; 25 | } 26 | } 27 | 28 | if (count > maxCount) // EDGE CASE ..... 29 | maxCount = count; 30 | 31 | return maxCount; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1466.reorder-routes-to-make-all-paths-lead-to-the-city-zero.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1466 lang=cpp 3 | * 4 | * [1466] Reorder Routes to Make All Paths Lead to the City Zero 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public: 10 | int minReorder(int n, vector>& connections) { 11 | 12 | } 13 | }; 14 | // @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1493.longest-subarray-of-1-s-after-deleting-one-element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1493 lang=cpp 3 | * 4 | * [1493] Longest Subarray of 1's After Deleting One Element 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int longestSubarray(vector &nums) 12 | { 13 | int n = nums.size(), i, res = 0, one = 0, right = 0, start = 0, prevZeroIndex = -1; 14 | 15 | if (one == n) 16 | return n - 1; 17 | 18 | while (right < n) 19 | { 20 | if (nums[right] == 0) 21 | { 22 | if (prevZeroIndex != -1) // zero encounter NOT 1st time(Window Shrink) 23 | start = prevZeroIndex + 1; 24 | 25 | prevZeroIndex = right; 26 | } 27 | 28 | res = max(res, right - start + 1); 29 | right++; 30 | } 31 | 32 | return res - 1; 33 | } 34 | }; 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Leet_Code_Problems/162.find-peak-element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=162 lang=cpp 3 | * 4 | * [162] Find Peak Element 5 | * 6 | * https://leetcode.com/problems/find-peak-element/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int findPeakElement(vector &nums) 15 | { 16 | int peakElement = nums[0], i, len = nums.size(); 17 | 18 | if (len == 0 || len == 1) 19 | return 0; 20 | 21 | if (len == 2) 22 | return (nums[0] > nums[1] ? 0 : 1); 23 | 24 | for (i = 1; i < len - 1; i++) 25 | { 26 | if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) 27 | peakElement = max(peakElement, nums[i]); 28 | } 29 | 30 | auto it = find(nums.begin(), nums.end(), peakElement); 31 | return it - nums.begin(); 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1624.largest-substring-between-two-equal-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1624 lang=cpp 3 | * 4 | * [1624] Largest Substring Between Two Equal Characters 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxLengthBetweenEqualCharacters(string s) 12 | { 13 | 14 | unordered_map map; // key: character, value: least index of character in s 15 | int len = -1, compare; 16 | for (int i = 0; i < s.length(); i++) 17 | { 18 | if (map.count(s[i])) // whether this ele is present or not 19 | { 20 | compare = i - map[s[i]] - 1; 21 | len = max(len, compare); 22 | } 23 | else 24 | map[s[i]] = i; 25 | } 26 | return len; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1672.richest-customer-wealth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1672 lang=java 3 | * 4 | * [1672] Richest Customer Wealth 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int maximumWealth(int[][] accounts) { 10 | 11 | int len = accounts.length, maxWealth = 0, currWealth = 0; 12 | System.out.println(len); 13 | 14 | for (int[] cust : accounts) { 15 | currWealth = 0; // initialises0 16 | 17 | for (int money : cust) { 18 | currWealth = currWealth + money; // each row 's SUM ' 19 | } 20 | 21 | maxWealth = Math.max(currWealth, maxWealth); 22 | } 23 | 24 | return maxWealth; 25 | } 26 | } 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1679.max-number-of-k-sum-pairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1679 lang=cpp 3 | * 4 | * [1679] Max Number of K-Sum Pairs 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxOperations(vector &nums, int k) 12 | { 13 | // DOUBLE POINTER // 14 | 15 | sort(nums.begin(), nums.end()); 16 | int i = 0, j = nums.size() - 1, count = 0; 17 | while (i < j) 18 | { 19 | if (nums[i] + nums[j] == k) 20 | { 21 | count++; 22 | i++; 23 | j--; 24 | } 25 | else if (nums[i] + nums[j] < k) 26 | i++; 27 | else 28 | j--; 29 | } 30 | 31 | return count; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/169.majority-element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=169 lang=cpp 3 | * 4 | * https://leetcode.com/problems/majority-element/ 5 | * 6 | * [169] Majority Element 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | // Time Complexity : O(n) ------ Using HashMap 14 | // Space Complexity : O(n) 15 | int majorityElement(vector &nums) 16 | { 17 | unordered_map hashMap; 18 | int len = nums.size(), i, maxEle = len / 2, ans = 0; 19 | 20 | for (i = 0; i < len; i++) 21 | hashMap[nums[i]]++; 22 | 23 | for (auto it : hashMap) 24 | { 25 | if (it.second > maxEle) 26 | ans = it.first; 27 | } 28 | 29 | return ans; 30 | } 31 | 32 | // Time Complexity : O(n) ------ Using Moore's Majority Algorithm 33 | // Space Complexity : O(1) 34 | int majorityElement(vector &nums) 35 | { 36 | } 37 | }; 38 | 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1696.jump-game-vi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1696 lang=cpp 3 | * 4 | * [1696] Jump Game VI 5 | * 6 | * https://leetcode.com/problems/jump-game-vi/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int maxResult(vector &arr, int k) 15 | { 16 | } 17 | }; 18 | // @lc code=end 19 | -------------------------------------------------------------------------------- /Leet_Code_Problems/171.excel-sheet-column-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=171 lang=cpp 3 | * 4 | * https://leetcode.com/problems/excel-sheet-column-number/ 5 | * 6 | * [171] Excel Sheet Column Number 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | int titleToNumber(string columnTitle) 14 | { 15 | int i = 0, len = columnTitle.size(), k = 0; 16 | 17 | for (i = 0; i < len; i++) 18 | k = (k * 26) + (columnTitle[i] - 'A' + 1); 19 | 20 | return k; 21 | } 22 | }; 23 | // @lc code=end 24 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1752.check-if-array-is-sorted-and-rotated.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1752 lang=cpp 3 | * 4 | * [1752] Check if Array Is Sorted and Rotated 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool check(vector &nums) 12 | { 13 | 14 | int i, count = 0, size = nums.size(); 15 | for (i = 0; i < size; i++) 16 | { 17 | if (nums[i] > nums[(i + 1) % size]) 18 | count++; 19 | } 20 | 21 | return (count <= 1); 22 | } 23 | }; 24 | // @lc code=end 25 | -------------------------------------------------------------------------------- /Leet_Code_Problems/1863.sum-of-all-subset-xor-totals.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1863 lang=cpp 3 | * 4 | * [1863] Sum of All Subset XOR Totals 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int subsetXORSum(vector &nums) 12 | { 13 | // Obsesrve that after performing the sum we are actually performing the addition upto 2^n - 1. 14 | int xor_sum = 0, i, or_total = 0, length = nums.size(); 15 | int limit = pow(2, length - 1); 16 | 17 | for (i = 0; i < length; ++i) 18 | { 19 | or_total = or_total | nums[i]; 20 | } 21 | 22 | xor_sum = limit * or_total; 23 | return xor_sum; 24 | } 25 | }; 26 | // @lc code=end 27 | -------------------------------------------------------------------------------- /Leet_Code_Problems/189.rotate-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=189 lang=cpp 3 | * 4 | * [189] Rotate Array 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | void rotate(vector &nums, int k) 12 | { 13 | int len = nums.size(); 14 | k = k % len; 15 | 16 | reverse(nums.begin(), nums.end()); 17 | reverse(nums.begin(), nums.begin() + k); 18 | reverse(nums.begin() + k, nums.end()); 19 | } 20 | }; 21 | // @lc code=end 22 | 23 | /* 24 | Time Complexity : O(n) 25 | Space Complexity : O(1) 26 | 27 | 1) Reverse the whole vector 28 | 2) Reverse First K elements 29 | 3) Reverse the rest elements. 30 | 31 | 32 | // Manual Reverse Function \\ 33 | void reverse_range(int[] nums, int i, int j) { 34 | while (i < j) { 35 | int temp = nums[i]; 36 | nums[i] = nums[j]; 37 | nums[j] = temp; 38 | i++; 39 | j--; 40 | } 41 | } 42 | */ -------------------------------------------------------------------------------- /Leet_Code_Problems/198.house-robber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=198 lang=java 3 | * 4 | * [198] House Robber 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int rob(int[] nums) { 10 | 11 | } 12 | } 13 | // @lc code=end 14 | 15 | -------------------------------------------------------------------------------- /Leet_Code_Problems/2.add-two-numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=2 lang=cpp 3 | * 4 | * [2] Add Two Numbers 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * struct ListNode { 11 | * int val; 12 | * ListNode *next; 13 | * ListNode() : val(0), next(nullptr) {} 14 | * ListNode(int x) : val(x), next(nullptr) {} 15 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 16 | * }; 17 | */ 18 | class Solution { 19 | public: 20 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 21 | 22 | } 23 | }; 24 | // @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /Leet_Code_Problems/206.reverse-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=206 lang=cpp 3 | * 4 | * * https://leetcode.com/problems/reverse-linked-list/ 5 | * 6 | * [206] Reverse Linked List 7 | */ 8 | 9 | // @lc code=start 10 | /** 11 | * Definition for singly-linked list. 12 | * struct ListNode { 13 | * int val; 14 | * ListNode *next; 15 | * ListNode() : val(0), next(nullptr) {} 16 | * ListNode(int x) : val(x), next(nullptr) {} 17 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 18 | * }; 19 | */ 20 | class Solution 21 | { 22 | public: 23 | ListNode *reverseList(ListNode *head) 24 | { 25 | ListNode *curr = head, *temp, *newHead = NULL; 26 | 27 | while (curr != NULL) 28 | { 29 | temp = curr->next; // address 30 | curr->next = newHead; 31 | newHead = curr; 32 | curr = temp; 33 | } 34 | 35 | return newHead; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/206.reverse-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=206 lang=java 3 | * 4 | * https://leetcode.com/problems/reverse-linked-list/ 5 | * 6 | * [206] Reverse Linked List 7 | */ 8 | 9 | // @lc code=start 10 | /** 11 | * Definition for singly-linked list. 12 | * public class ListNode { 13 | * int val; 14 | * ListNode next; 15 | * ListNode() {} 16 | * ListNode(int val) { this.val = val; } 17 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 18 | * } 19 | */ 20 | class Solution { 21 | public ListNode reverseList(ListNode head) { 22 | ListNode curr = head, temp, newHead = null; 23 | 24 | while (curr != null) { 25 | temp = curr.next; // address 26 | curr.next = newHead; 27 | newHead = curr; 28 | curr = temp; 29 | } 30 | 31 | return newHead; 32 | } 33 | } 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/228.summary-ranges.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=228 lang=cpp 3 | * 4 | * https://leetcode.com/problems/summary-ranges/ 5 | * 6 | * [228] Summary Ranges 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | vector summaryRanges(vector &nums) 14 | { 15 | int len = nums.size(), start = 0, i = 0; 16 | vector ranges; 17 | 18 | if (len == 0) 19 | return ranges; 20 | 21 | while (i < len) 22 | { 23 | if (i == len - 1 || nums[i] + 1 != nums[i + 1]) // when NEXT ele === prev + 1 (Skip) 24 | { 25 | if (start == i) 26 | ranges.push_back(to_string(nums[start])); 27 | else 28 | ranges.push_back(to_string(nums[start]) + "->" + to_string(nums[i])); 29 | 30 | start = i + 1; 31 | } 32 | 33 | i++; 34 | } 35 | 36 | return ranges; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Leet_Code_Problems/231.power-of-two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=231 lang=cpp 3 | * 4 | * [231] Power of Two 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool isPowerOfTwo(int n) 12 | { 13 | bool ans = false; 14 | if (n == 1) 15 | return true; 16 | 17 | if (n && n % 2 == 0) // if N Exsist + Even. 18 | ans = isPowerOfTwo(n / 2); 19 | 20 | return ans; 21 | } 22 | }; 23 | // @lc code=end 24 | -------------------------------------------------------------------------------- /Leet_Code_Problems/237.delete-node-in-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=237 lang=cpp 3 | * 4 | * [237] Delete Node in a Linked List 5 | * 6 | * https://leetcode.com/problems/delete-node-in-a-linked-list/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | /** 12 | * Definition for singly-linked list. 13 | * struct ListNode { 14 | * int val; 15 | * ListNode *next; 16 | * ListNode(int x) : val(x), next(NULL) {} 17 | * }; 18 | */ 19 | class Solution 20 | { 21 | public: 22 | void deleteNode(ListNode *node) 23 | { 24 | // Just COPY the value of the adjacent node. 25 | node->val = node->next->val; 26 | node->next = node->next->next; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/239.sliding-window-maximum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=239 lang=cpp 3 | * 4 | * https://leetcode.com/problems/sliding-window-maximum/ 5 | * 6 | * [239] Sliding Window Maximum 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | vector maxSlidingWindow(vector &nums, int k) 14 | { 15 | deque dq; 16 | vector ans; 17 | 18 | // Storing in Decreasing Order. 19 | for (int i = 0; i < nums.size(); i++) 20 | { 21 | if (!dq.empty() && dq.front() == i - k) 22 | dq.pop_front(); 23 | 24 | while (!dq.empty() && nums[dq.back()] < nums[i]) 25 | dq.pop_back(); 26 | 27 | dq.push_back(i); 28 | 29 | // when there is more than K ele in the DeQue pushBack in the Resultant Vector . 30 | if (i >= k - 1) 31 | ans.push_back(nums[dq.front()]); 32 | } 33 | 34 | return ans; 35 | } 36 | }; 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/240.search-a-2-d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=240 lang=cpp 3 | * 4 | * [240] Search a 2D Matrix II 5 | * 6 | * https://leetcode.com/problems/search-a-2d-matrix-ii/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | bool searchMatrix(vector> &matrix, int target) 15 | { 16 | int row = matrix.size(), col = matrix[0].size(); 17 | int r = 0, c = col - 1; // initially we are starting from Row 1 , Col last 18 | 19 | while (r >= 0 && r < row && c >= 0 && c < col) 20 | { 21 | if (target == matrix[r][c]) 22 | return true; 23 | 24 | else if (target > matrix[r][c]) 25 | r++; // means prev row strike off 26 | 27 | else 28 | c--; 29 | } 30 | 31 | return false; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/25.reverse-nodes-in-k-group.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=25 lang=cpp 3 | * 4 | * [25] Reverse Nodes in k-Group 5 | * 6 | * https://leetcode.com/problems/reverse-nodes-in-k-group/ 7 | * 8 | * https://takeuforward.org/data-structure/reverse-linked-list-in-groups-of-size-k/ 9 | */ 10 | 11 | // @lc code=start 12 | /** 13 | * Definition for singly-linked list. 14 | * struct ListNode { 15 | * int val; 16 | * ListNode *next; 17 | * ListNode() : val(0), next(nullptr) {} 18 | * ListNode(int x) : val(x), next(nullptr) {} 19 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 20 | * }; 21 | */ 22 | class Solution 23 | { 24 | public: 25 | ListNode *reverseKGroup(ListNode *head, int k) 26 | { 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/258.add-digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=258 lang=cpp 3 | * 4 | * [258] Add Digits 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int addDigits(int num) 12 | { 13 | if (num < 10) 14 | return num; 15 | 16 | int k, sum = 0; 17 | 18 | while (num >= 10) 19 | { 20 | sum = 0; 21 | while (num) 22 | { 23 | k = num % 10; 24 | sum = sum + k; 25 | num = num / 10; 26 | } 27 | 28 | num = sum; 29 | } 30 | 31 | return num; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/26.remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=26 lang=cpp 3 | * 4 | * https://leetcode.com/problems/find-the-duplicate-number/ 5 | * 6 | * [26] Remove Duplicates from Sorted Array 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | int removeDuplicates(vector &nums) 14 | { 15 | int i, len = nums.size(), index = 1; // 1 as one Unique Element , INDEX is the position for the NEXT Unique Element 16 | 17 | if (len == 0) 18 | return 0; 19 | 20 | for (i = 0; i < len - 1; i++) 21 | { 22 | if (nums[i] != nums[i + 1]) 23 | { 24 | nums[index] = nums[i + 1]; 25 | index++; 26 | } 27 | } 28 | 29 | return index; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Leet_Code_Problems/260.single-number-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=260 lang=cpp 3 | * 4 | * [260] Single Number III 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector singleNumber(vector &nums) 12 | { 13 | int i, num = 0; 14 | vector ans; 15 | set Set; 16 | 17 | for (i = 0; i < nums.size(); i++) 18 | { 19 | if (Set.find(nums[i]) == Set.end()) // can't find and iterator reaches the end 20 | Set.insert(nums[i]); 21 | else 22 | Set.erase(nums[i]); 23 | } 24 | 25 | for (auto itr = Set.begin(); itr != Set.end(); itr++) 26 | ans.push_back(*itr); 27 | 28 | return ans; 29 | } 30 | }; 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Leet_Code_Problems/268.missing-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=268 lang=cpp 3 | * 4 | * [268] Missing Number 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int missingNumber(vector &nums) 12 | { 13 | int len = nums.size(), i = 0, result = 0; 14 | // Method - I 15 | sort(nums.begin(), nums.end()); 16 | 17 | for (i = 0; i < len; i++) 18 | { 19 | if (i != nums[i]) 20 | return i; 21 | } 22 | return i; 23 | 24 | // Method - II (Using Bits XOR) 25 | for (i = 0; i < len; i++) 26 | { 27 | result = result ^ i; 28 | result = result ^ nums[i]; 29 | } 30 | return result; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /Leet_Code_Problems/283.move-zeroes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=283 lang=java 3 | * 4 | * [283] Move Zeroes 5 | * 6 | */ 7 | 8 | // @lc code=start 9 | class Solution { 10 | public void moveZeroes(int[] nums) { 11 | 12 | int len = nums.length, i, j, store = 0; 13 | 14 | if (nums[0] == 1 && len == 1) 15 | return; 16 | 17 | for (j = 0; j < len; j++) { 18 | if (nums[j] != 0) { 19 | nums[store] = nums[j]; 20 | store++; 21 | } 22 | } 23 | 24 | for (i = store; i < len; i++) 25 | nums[i] = 0; 26 | } 27 | } 28 | // @lc code=end 29 | -------------------------------------------------------------------------------- /Leet_Code_Problems/344.reverse-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=344 lang=cpp 3 | * 4 | * [344] Reverse String 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | void getReverse(vector &s, int left, int right) 12 | { 13 | if (left >= right) 14 | return; 15 | 16 | swap(s[left], s[right]); 17 | getReverse(s, left + 1, right - 1); 18 | } 19 | 20 | void reverseString(vector &s) 21 | { 22 | int left = 0, right = s.size() - 1; 23 | if (left >= right) 24 | return; 25 | 26 | /* // Method 2 27 | while (left < right) 28 | { 29 | swap(s[left], s[right]); 30 | left++; 31 | right--; 32 | } */ 33 | 34 | getReverse(s, left, right); 35 | } 36 | }; 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/344.reverse-string.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=344 lang=java 3 | * 4 | * [344] Reverse String 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public void reverseString(char[] s) { 10 | int right = s.length - 1, left = 0; 11 | if (left >= right) 12 | return; 13 | 14 | while (left < right) { 15 | char temp = s[left]; 16 | s[left] = s[right]; 17 | s[right] = temp; 18 | 19 | left++; 20 | right--; 21 | } 22 | } 23 | } 24 | // @lc code=end 25 | -------------------------------------------------------------------------------- /Leet_Code_Problems/349.intersection-of-two-arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=349 lang=cpp 3 | * 4 | * [349] Intersection of Two Arrays 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector intersection(vector &nums1, vector &nums2) 12 | { 13 | 14 | vector ans; 15 | unordered_map map; 16 | int i; 17 | 18 | for (i = 0; i < nums1.size(); i++) 19 | map.insert({nums1[i], i}); // (key , value ) --> wse are taking here kwy = VALUE bcz its unique 20 | 21 | for (i = 0; i < nums2.size(); i++) 22 | { 23 | if (map.find(nums2[i]) != map.end()) // find 24 | { 25 | auto it = find(ans.begin(), ans.end(), nums2[i]); // check already exsist in the ANSWER vector 26 | if (it == ans.end()) // not found 27 | ans.push_back(nums2[i]); 28 | } 29 | } 30 | 31 | return ans; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Leet_Code_Problems/376.wiggle-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=376 lang=cpp 3 | * 4 | * [376] Wiggle Subsequence 5 | * 6 | * https://leetcode.com/problems/wiggle-subsequence/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int wiggleMaxLength(vector &arr) 15 | { 16 | int len = arr.size(), count = 0, prev, currDiff; 17 | 18 | if (len == 0 || len == 1) 19 | return len; 20 | 21 | prev = arr[1] - arr[0]; 22 | count = (prev != 0) ? 2 : 1; 23 | 24 | for (int i = 2; i < len; i++) 25 | { 26 | currDiff = arr[i] - arr[i - 1]; 27 | if ((currDiff > 0 && prev <= 0) || (currDiff < 0 && prev >= 0)) 28 | { 29 | count++; 30 | prev = currDiff; 31 | } 32 | } 33 | 34 | return count; 35 | } 36 | }; 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/387.first-unique-character-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=387 lang=cpp 3 | * 4 | * [387] First Unique Character in a String 5 | * 6 | * https://leetcode.com/problems/first-unique-character-in-a-string/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int firstUniqChar(string s) 15 | { 16 | int len = s.size(), res = -1; 17 | 18 | unordered_map hashMap; 19 | 20 | for (int i = 0; i < len; i++) 21 | hashMap[s[i]]++; 22 | 23 | for (int i = 0; i < len; i++) 24 | { 25 | if (hashMap.find(s[i]) != hashMap.end() && hashMap.at(s[i]) == 1) 26 | return i; 27 | } 28 | 29 | return res; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Leet_Code_Problems/389.find-the-difference.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=389 lang=cpp 3 | * 4 | * https://leetcode.com/problems/find-the-difference/ 5 | * 6 | * [389] Find the Difference 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | char findTheDifference(string s, string t) 14 | { 15 | unordered_map hashMap; // char count 16 | int i, sLen = s.length(), tLen = t.length(); 17 | char ele; 18 | 19 | for (i = 0; i < sLen; i++) 20 | hashMap[s[i]]++; 21 | 22 | for (i = 0; i < tLen; i++) 23 | { 24 | if (hashMap.find(t[i]) != hashMap.end()) // present before end 25 | hashMap[t[i]]--; 26 | else 27 | hashMap[t[i]]++; 28 | } 29 | 30 | for (auto &it : hashMap) 31 | if (it.second) 32 | ele = it.first; 33 | 34 | return ele ; 35 | } 36 | }; 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/392.is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=392 lang=cpp 3 | * 4 | * [392] Is Subsequence 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool isSubsequence(string s, string t) 12 | { 13 | int lenSubString = s.length(), lenMainString = t.length(), i = 0, j = lenSubString - 1; 14 | 15 | if (s == "" || t == s) 16 | return true; 17 | 18 | for (i = lenMainString; i >= 0; i--) 19 | { 20 | if (t[i] == s[j]) 21 | j--; 22 | if (j < 0) 23 | break; 24 | } 25 | 26 | if (j < 0) 27 | return true; 28 | return false; 29 | } 30 | }; 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Leet_Code_Problems/413.arithmetic-slices.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=413 lang=cpp 3 | * 4 | * [413] Arithmetic Slices 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int numberOfArithmeticSlices(vector &nums) 12 | { 13 | int i, len = nums.size(), ans = 0; 14 | vector dp(len, 0); // initalises with 0 15 | 16 | if (len < 3) 17 | return 0; 18 | 19 | for (i = 1; i < len - 1; i++) 20 | { 21 | if (nums[i] - nums[i - 1] == nums[i + 1] - nums[i]) 22 | { 23 | // it will consider the whole means in [3 -1 -5 -9] 3 -1 -5 and -1 -5 9 both will be considered when dp[i] = 2; 24 | dp[i] = dp[i - 1] + 1; 25 | ans = ans + dp[i]; 26 | } 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Leet_Code_Problems/421.maximum-xor-of-two-numbers-in-an-array.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=421 lang=java 3 | * 4 | * https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/ 5 | * 6 | * [421] Maximum XOR of Two Numbers in an Array 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public int findMaximumXOR(int[] nums) { 12 | 13 | } 14 | } 15 | // @lc code=end 16 | -------------------------------------------------------------------------------- /Leet_Code_Problems/448.find-all-numbers-disappeared-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=448 lang=cpp 3 | * 4 | * [448] Find All Numbers Disappeared in an Array 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector findDisappearedNumbers(vector &nums) 12 | { 13 | int i, len = nums.size(); 14 | vector result; 15 | unordered_map hashMap; 16 | 17 | for (i = 0; i < len; i++) 18 | hashMap[nums[i]]++; 19 | 20 | for (i = 1; i <= len; i++) 21 | if (hashMap.find(i) == hashMap.end()) // if NO VALUE corresponding to the index 22 | result.push_back(i); 23 | 24 | return result; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Leet_Code_Problems/45.jump-game-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=45 lang=cpp 3 | * 4 | * [45] Jump Game II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int jump(vector &nums) 12 | { 13 | int len = nums.size(), i, reachable = 0, jumps = 0, maxReachable = 0; 14 | 15 | if (nums[0] == 0 && len == 1) // [0] 16 | return 0; 17 | 18 | if (nums[0] == 0 || len == 1) // Start 0 19 | return 0; 20 | 21 | // Peak Valley Approach <------ GREEDY APPROACH 22 | for (i = 0; i < len - 1; i++) 23 | { 24 | if (reachable < i) 25 | return 0; 26 | 27 | reachable = max(reachable, i + nums[i]); 28 | if (maxReachable == i) 29 | { 30 | jumps++; 31 | maxReachable = reachable; 32 | } 33 | } 34 | 35 | return jumps; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/451.sort-characters-by-frequency.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=451 lang=cpp 3 | * 4 | * [451] Sort Characters By Frequency 5 | * 6 | * https://leetcode.com/problems/sort-characters-by-frequency/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | string frequencySort(string s) 15 | { 16 | int len = s.size(); 17 | unordered_map hashMap; // [char , count] 18 | string res = ""; 19 | 20 | for (int i = 0; i < len; i++) 21 | hashMap[s[i]]++; 22 | 23 | multimap> MM; // [count , char] DESC 24 | 25 | for (auto &it : hashMap) 26 | MM.insert({it.second, it.first}); 27 | // MM.insert({it.second , it.first}); 28 | 29 | for (auto it : MM) 30 | { 31 | int freq = it.first; 32 | while (freq--) 33 | res += it.second; 34 | } 35 | 36 | return res; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Leet_Code_Problems/452.minimum-number-of-arrows-to-burst-balloons.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=452 lang=java 3 | * 4 | * [452] Minimum Number of Arrows to Burst Balloons 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int findMinArrowShots(int[][] points) { 10 | 11 | int len = points.length, i, arrows = 1, prevEnd; 12 | if (len == 0) 13 | return 0; 14 | if (len == 1) 15 | return 1; 16 | 17 | Arrays.sort(points, (a, b) -> a[1] - b[1]); // Sort with the Ending Diameter 18 | 19 | prevEnd = points[0][1]; 20 | 21 | for (i = 0; i < len; i++) { 22 | if (points[i][0] <= prevEnd) 23 | continue; 24 | prevEnd = points[i][1]; 25 | arrows++; 26 | } 27 | 28 | return arrows; 29 | } 30 | } 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Leet_Code_Problems/459.repeated-substring-pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=459 lang=cpp 3 | * 4 | * [459] Repeated Substring Pattern 5 | * 6 | * https://leetcode.com/problems/repeated-substring-pattern/description/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | // Remove first char to avoid to match with input string which is from 0 to s.length() 15 | // Remove last char - appended after s.length() 16 | // Search for the input string is repeated 17 | 18 | bool repeatedSubstringPattern(string s) 19 | { 20 | string tmp = s + s; 21 | 22 | tmp = tmp.substr(1); 23 | tmp = tmp.substr(0, tmp.length() - 1); 24 | return tmp.find(s) != string::npos ? true : false; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Leet_Code_Problems/461.hamming-distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=461 lang=cpp 3 | * 4 | * [461] Hamming Distance 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int hammingDistance(int x, int y) 12 | { 13 | int Xor = x ^ y, count = 0; 14 | 15 | // METHOD -1 16 | // bitset<8>(Xor).to_string(); // binary string 000010 17 | return bitset<32>(x ^ y).count(); // number of 1's bcz it wil 18 | 19 | // METHOD 2 20 | while (Xor > 0) 21 | { 22 | if (Xor % 2 == 1) 23 | count++; 24 | Xor = Xor / 2; 25 | } 26 | return count; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/462.minimum-moves-to-equal-array-elements-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=462 lang=cpp 3 | * 4 | * [462] Minimum Moves to Equal Array Elements II 5 | * 6 | * https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int minMoves2(vector &nums) 15 | { 16 | int len = arr.size(), res = 0; 17 | sort(arr.begin(), arr.end()); 18 | 19 | int midIndex = len / 2; 20 | 21 | int midEle = arr[midIndex]; 22 | 23 | for (int i = 0; i < len; i++) 24 | res += abs(nums[i] - midEle); // if abs not taken Runtime err was coming abs(midEle - nums[i]); && abs(nums[i] - midEle) 25 | 26 | return res; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/48.rotate-image.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=48 lang=cpp 3 | * 4 | * [48] Rotate Image 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | // TC : O(N^2) SC : O(1) 12 | void rotate(vector> &matrix) 13 | { 14 | int len = matrix.size(), i, j; 15 | 16 | // Transpose of the Matrix arr[i][j] convert to arr[j][i] 17 | for (i = 0; i < len; i++) 18 | for (j = 0; j < i; j++) 19 | swap(matrix[i][j], matrix[j][i]); 20 | 21 | // Reverse the ROWS 22 | for (i = 0; i < len; i++) 23 | reverse(matrix[i].begin(), matrix[i].end()); 24 | } 25 | }; 26 | 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Leet_Code_Problems/485.max-consecutive-ones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=485 lang=cpp 3 | * 4 | * [485] Max Consecutive Ones 5 | * 6 | * https://leetcode.com/problems/max-consecutive-ones/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int findMaxConsecutiveOnes(vector &arr) 15 | { 16 | int len = arr.size(), res = 0, count = 0; 17 | 18 | if (arr[0] == 1 && len == 1) 19 | return 1; 20 | 21 | for (int i = 0; i < len; i++) 22 | { 23 | if (arr[i] == 1) 24 | { 25 | count++; 26 | res = max(res, count); 27 | } 28 | else 29 | count = 0; 30 | } 31 | 32 | return res; 33 | } 34 | }; 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Leet_Code_Problems/503.next-greater-element-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=503 lang=cpp 3 | * 4 | * [503] Next Greater Element II 5 | * 6 | * https://leetcode.com/problems/next-greater-element-ii/ 7 | * 8 | * CIRCULARLY FINDING NEXT GREATER ELEMENT 9 | * 10 | */ 11 | 12 | // @lc code=start 13 | class Solution 14 | { 15 | public: 16 | vector nextGreaterElements(vector &nums) 17 | { 18 | int len = arr.size(); 19 | 20 | stack s; 21 | vector result(len, -1); 22 | 23 | for (int i = 2 * len - 1; i >= 0; i--) 24 | { 25 | while (!s.empty() && s.top() <= arr[i % len]) 26 | s.pop(); 27 | 28 | if (i < len && !s.empty()) 29 | { 30 | int nge = s.top(); 31 | // result.push_back(nge ? nge : -1); 32 | result[i] = nge; 33 | } 34 | 35 | s.push(arr[i % len]); 36 | } 37 | 38 | return result; 39 | } 40 | }; 41 | // @lc code=end 42 | -------------------------------------------------------------------------------- /Leet_Code_Problems/520.detect-capital.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=520 lang=java 3 | * 4 | * https://leetcode.com/problems/detect-capital/ 5 | * 6 | * [520] Detect Capital 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public boolean detectCapitalUse(String word) { 12 | 13 | int len = word.length(), i, count = 0; 14 | int firstletter = (int) word.charAt(0); 15 | 16 | if (len == 1) 17 | return true; 18 | 19 | for (i = 0; i < len; i++) { 20 | if ((int) word.charAt(i) >= 97 && (int) word.charAt(i) <= 122) 21 | count++; 22 | } 23 | 24 | // All capital OR all small 25 | if (count == 0 || count == len) 26 | return true; 27 | 28 | // means first letter in Capital 29 | if ((firstletter >= 65 && firstletter <= 90) && count == len - 1) 30 | return true; 31 | 32 | return false; 33 | } 34 | } 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /Leet_Code_Problems/532.k-diff-pairs-in-an-array.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=532 lang=java 3 | * 4 | * [532] K-diff Pairs in an Array 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int findPairs(int[] nums, int k) { 10 | HashMap map = new HashMap<>(); 11 | int i, res = 0, len = nums.length; 12 | 13 | for (i = 0; i < len; i++) 14 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); 15 | 16 | for (int it : map.keySet()) { 17 | if (k > 0 && map.containsKey(it + k) || k == 0 && map.get(it) > 1) 18 | res++; 19 | } 20 | 21 | return res; 22 | } 23 | } 24 | // @lc code=end 25 | -------------------------------------------------------------------------------- /Leet_Code_Problems/55.jump-game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=55 lang=cpp 3 | * 4 | * [55] Jump Game 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool canJump(vector &nums) 12 | { 13 | int len = nums.size(), i, reachable = 0, count = 0; 14 | 15 | if (nums[0] == 0 && len == 1) // [0] 16 | return true; 17 | 18 | if (nums[0] == 0) 19 | return false; 20 | 21 | // Peak Valley Approach <------ GREEDY APPROACH 22 | for (i = 0; i < len; i++) 23 | { 24 | if (i > reachable) // could not react Index 25 | return false; 26 | 27 | reachable = max(reachable, i + nums[i]); // Index Trace 28 | 29 | if (reachable >= len) 30 | return true; 31 | } 32 | 33 | return true; 34 | } 35 | }; 36 | // @lc code=end 37 | -------------------------------------------------------------------------------- /Leet_Code_Problems/560.subarray-sum-equals-k.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=560 lang=cpp 3 | * 4 | * https://leetcode.com/problems/subarray-sum-equals-k/ 5 | * 6 | * [560] Subarray Sum Equals K 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | int subarraySum(vector &nums, int k) 14 | { 15 | int len = nums.size(), i, sum = 0, count = 0; 16 | unordered_map hashMap; 17 | 18 | for (i = 0; i < len; i++) 19 | { 20 | sum = sum + nums[i]; 21 | if (hashMap.find(sum - k) != hashMap.end()) 22 | count += hashMap[sum - k]; 23 | if (sum == k) 24 | count++; 25 | hashMap[sum]++; 26 | } 27 | 28 | return count; 29 | } 30 | }; 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Leet_Code_Problems/605.can-place-flowers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=605 lang=java 3 | * 4 | * https://leetcode.com/problems/can-place-flowers/ 5 | * 6 | * [605] Can Place Flowers 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public boolean canPlaceFlowers(int[] flowerbed, int n) { 12 | 13 | int len = flowerbed.length, i, count = 0; 14 | 15 | for (i = 0; i < len; i++) { 16 | 17 | // Boundary Case + Condition 18 | if ((i == 0 || flowerbed[i - 1] == 0) && (i == len - 1 || flowerbed[i + 1] == 0) && flowerbed[i] == 0) { 19 | count++; 20 | flowerbed[i] = 1; 21 | i++; // Shifts by 2 22 | } 23 | } 24 | 25 | if (n <= count) 26 | return true; 27 | 28 | return false; 29 | } 30 | } 31 | // @lc code=end 32 | 33 | /* 34 | Test Cases Sample : 35 | 36 | 1) [0,0,1,0,1] , n = 1 true 37 | 2) [1,0,0,0,0,1] , n = 2 false 38 | */ -------------------------------------------------------------------------------- /Leet_Code_Problems/62.unique-paths.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=62 lang=java 3 | * 4 | * [62] Unique Paths 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | 10 | private int findWays(int row, int col, int[][] Dp) { 11 | if (row == 0 && col == 0) 12 | return 1; 13 | 14 | if (row < 0 || col < 0) // Out Of Bounds 15 | return 0; 16 | 17 | if (Dp[row][col] != -1) 18 | return Dp[row][col]; 19 | 20 | int up = findWays(row - 1, col, Dp); 21 | int left = findWays(row, col - 1, Dp); 22 | 23 | return Dp[row][col] = (up + left); 24 | } 25 | 26 | public int uniquePaths(int m, int n) { 27 | 28 | // Initialize 2D array to -1 29 | int[][] Dp = new int[m][n]; 30 | for (int[] arr1 : Dp) 31 | Arrays.fill(arr1, -1); 32 | 33 | int result = findWays(m - 1, n - 1, Dp); 34 | return result; 35 | } 36 | } 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/646.maximum-length-of-pair-chain.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=646 lang=cpp 3 | * 4 | * [646] Maximum Length of Pair Chain 5 | * 6 | * https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | int findLongestChain(vector> &pairs) 15 | { 16 | } 17 | }; 18 | // @lc code=end 19 | -------------------------------------------------------------------------------- /Leet_Code_Problems/662.maximum-width-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=662 lang=cpp 3 | * 4 | * [662] Maximum Width of Binary Tree 5 | * 6 | * https://leetcode.com/problems/maximum-width-of-binary-tree/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | /** 12 | * Definition for a binary tree node. 13 | * struct TreeNode { 14 | * int val; 15 | * TreeNode *left; 16 | * TreeNode *right; 17 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 18 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 19 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 20 | * }; 21 | */ 22 | class Solution 23 | { 24 | public: 25 | int widthOfBinaryTree(TreeNode *root) 26 | { 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Leet_Code_Problems/665.non-decreasing-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=665 lang=cpp 3 | * 4 | * [665] Non-decreasing Array 5 | * 6 | * https://leetcode.com/problems/non-decreasing-array/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | // VIDEO : https://www.youtube.com/watch?v=RegQckCegDk 15 | bool checkPossibility(vector &nums) 16 | { 17 | int len = arr.size(); 18 | bool isChange = false; 19 | 20 | for (int i = 0; i < len - 1; i++) 21 | { 22 | if (arr[i] <= arr[i + 1]) 23 | continue; 24 | if (isChange) 25 | return false; 26 | 27 | // Cases : [7 8 1 8] 28 | if (i == 0 || arr[i + 1] >= arr[i - 1]) // if [4 2] for that edge case [i == 0] checking. 29 | arr[i] = arr[i + 1]; 30 | else 31 | arr[i + 1] = arr[i]; 32 | 33 | isChange = true; 34 | } 35 | 36 | return true; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Leet_Code_Problems/695.max-area-of-island.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=695 lang=cpp 3 | * 4 | * [695] Max Area of Island 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public: 10 | int maxAreaOfIsland(vector>& grid) { 11 | 12 | } 13 | }; 14 | // @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /Leet_Code_Problems/70.climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=70 lang=cpp 3 | * 4 | * [70] Climbing Stairs 5 | */ 6 | // https://leetcode.com/problems/climbing-stairs/ 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int climbStairs(int n) 12 | { 13 | // int Z; 14 | // if (n == 0 || n < 1) 15 | // return 1; 16 | // if (n < 0) 17 | // return 0; 18 | 19 | // Z = climbStairs(n - 1) + climbStairs(n - 2); 20 | // return Z; 21 | 22 | int step[45], i; 23 | step[0] = 1; 24 | step[1] = 2; 25 | for (i = 2; i < n; i++) 26 | step[i] = step[i - 1] + step[i - 2]; 27 | 28 | return step[n - 1]; 29 | } 30 | }; 31 | // @lc code=end 32 | 33 | // WRONG -----> DO USING DP 34 | -------------------------------------------------------------------------------- /Leet_Code_Problems/700.search-in-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=700 lang=cpp 3 | * 4 | * [700] Search in a Binary Search Tree 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for a binary tree node. 10 | * struct TreeNode { 11 | * int val; 12 | * TreeNode *left; 13 | * TreeNode *right; 14 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 15 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 16 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 17 | * }; 18 | */ 19 | class Solution 20 | { 21 | public: 22 | TreeNode *searchBST(TreeNode *root, int val) 23 | { 24 | if (!root) 25 | return NULL; 26 | 27 | if (root->val == val) 28 | return root; 29 | 30 | if (root && root->val > val) // val is LESS than root 31 | return searchBST(root->left, val); 32 | 33 | return searchBST(root->right, val); 34 | } 35 | }; 36 | 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/706.design-hash-map.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=706 lang=cpp 3 | * 4 | * [706] Design HashMap 5 | */ 6 | 7 | // @lc code=start 8 | class MyHashMap 9 | { 10 | public: 11 | vector vec; 12 | MyHashMap() 13 | { 14 | vec.resize(1e6 + 1, -1); 15 | } 16 | 17 | void put(int key, int value) 18 | { 19 | vec[key] = value; 20 | } 21 | 22 | int get(int key) 23 | { 24 | // if (vec[key]) 25 | return vec[key]; 26 | } 27 | 28 | void remove(int key) 29 | { 30 | vec[key] = -1; 31 | } 32 | }; 33 | 34 | /** 35 | * Your MyHashMap object will be instantiated and called as such: 36 | * MyHashMap* obj = new MyHashMap(); 37 | * obj->put(key,value); 38 | * int param_2 = obj->get(key); 39 | * obj->remove(key); 40 | */ 41 | // @lc code=end 42 | -------------------------------------------------------------------------------- /Leet_Code_Problems/740.delete-and-earn.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=740 lang=cpp 3 | * 4 | * [740] Delete and Earn 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public: 10 | int deleteAndEarn(vector& nums) { 11 | 12 | } 13 | }; 14 | // @lc code=end 15 | 16 | -------------------------------------------------------------------------------- /Leet_Code_Problems/763.partition-labels.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=763 lang=cpp 3 | * 4 | * [763] Partition Labels 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector partitionLabels(string s) 12 | { 13 | 14 | vector A(26, 0); 15 | vector result; 16 | 17 | int i, start = 0, end = 0, size = s.length(); 18 | 19 | // a = 97 20 | for (i = 0; i < size; i++) 21 | A[s[i] - 'a'] = i; 22 | 23 | for (i = 0; i < size; i++) 24 | { 25 | end = max(end, A[s[i] - 'a']); 26 | 27 | // end is not set till the iterator reaches the END 28 | if (end == i) 29 | { 30 | result.push_back(end - start + 1); 31 | start = i + 1; // for the new partition 32 | } 33 | } 34 | 35 | return result; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/78.subsets.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=78 lang=cpp 3 | * 4 | * https://leetcode.com/problems/subsets/ 5 | * 6 | * [78] Subsets 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | // Backtracking Problem , draw the RECURSION TREE 14 | vector> result; 15 | 16 | void helper(int i, vector &nums, vector temp) 17 | { 18 | if (i == nums.size()) 19 | { 20 | result.push_back(temp); 21 | return; 22 | } 23 | 24 | helper(i + 1, nums, temp); // exclude the current element 25 | temp.push_back(nums[i]); 26 | helper(i + 1, nums, temp); // include element part 27 | } 28 | 29 | vector> subsets(vector &nums) 30 | { 31 | vector temp; 32 | int i, len = nums.size(); 33 | 34 | helper(0, nums, temp); 35 | return result; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/80.remove-duplicates-from-sorted-array-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=80 lang=cpp 3 | * 4 | * https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 5 | * 6 | * [80] Remove Duplicates from Sorted Array II 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | int removeDuplicates(vector &nums) 14 | { 15 | int i, len = nums.size(), result = len; 16 | map hashMap; // Key Count 17 | 18 | for (i = 0; i < len; i++) 19 | hashMap[nums[i]]++; 20 | 21 | nums.clear(); 22 | 23 | for (auto it = hashMap.begin(); it != hashMap.end(); it++) 24 | { 25 | if (it->second > 2) 26 | it->second = 2; 27 | 28 | for (i = 0; i < it->second; i++) 29 | nums.push_back(it->first); 30 | } 31 | 32 | sort(nums.begin(), nums.end()); 33 | return nums.size(); // modified length of the vector 34 | } 35 | }; 36 | // @lc code=end 37 | -------------------------------------------------------------------------------- /Leet_Code_Problems/83.remove-duplicates-from-sorted-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=83 lang=java 3 | * 4 | * https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 5 | * 6 | * [83] Remove Duplicates from Sorted List 7 | */ 8 | 9 | // @lc code=start 10 | /** 11 | * Definition for singly-linked list. 12 | * public class ListNode { 13 | * int val; 14 | * ListNode next; 15 | * ListNode() {} 16 | * ListNode(int val) { this.val = val; } 17 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 18 | * } 19 | */ 20 | class Solution { 21 | public ListNode deleteDuplicates(ListNode head) { 22 | 23 | ListNode temp = head; 24 | 25 | if (head == null) 26 | return head; 27 | 28 | while (temp != null) { 29 | if ((temp.next != null) && (temp.val == temp.next.val)) { 30 | temp.next = temp.next.next; 31 | } else 32 | temp = temp.next; 33 | } 34 | 35 | return head; 36 | } 37 | } 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Leet_Code_Problems/849.maximize-distance-to-closest-person.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=849 lang=java 3 | * 4 | * [849] Maximize Distance to Closest Person 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int maxDistToClosest(int[] seats) { 10 | 11 | int i, len = seats.length, prevFull = -1, futureFull = 0, result = 0; 12 | 13 | for (i = 0; i < len; i++) { 14 | if (seats[i] == 1) 15 | prevFull = i; 16 | 17 | else { 18 | // Get the NEXT Full seat 19 | while (futureFull < len && seats[futureFull] == 0 || futureFull < i) 20 | futureFull++; 21 | 22 | int left = prevFull == -1 ? len : i - prevFull; // 23 | int right = futureFull == len ? len : futureFull - i; // Right Gap/Distance to reach the NEXT filled chai 24 | result = Math.max(result, Math.min(left, right)); 25 | } 26 | } 27 | 28 | return result; 29 | } 30 | } 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Leet_Code_Problems/88.merge-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=88 lang=cpp 3 | * 4 | * [88] Merge Sorted Array 5 | * 6 | * https://leetcode.com/problems/merge-sorted-array/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | void merge(vector &nums1, int m, vector &nums2, int n) 15 | { 16 | // Time Complexity : O(m * n) 17 | // Space Complexity : O(1) 18 | int i = 0; 19 | for (i = 0; i < n; i++) 20 | { 21 | nums1[m] = nums2[i]; 22 | m++; 23 | } 24 | 25 | sort(nums1.begin(), nums1.end()); 26 | 27 | // We can also use MAX - HEAP 28 | } 29 | }; 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Leet_Code_Problems/881.boats-to-save-people.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=881 lang=cpp 3 | * 4 | * [881] Boats to Save People 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int numRescueBoats(vector &people, int limit) 12 | { 13 | 14 | // DOUBLE POINTERS // 15 | int sum = 0, r = people.size() - 1, l = 0; // indexes 16 | sort(people.begin(), people.end()); 17 | 18 | // for (i=0 ; l <= r ; i++) 19 | while (l <= r) 20 | { 21 | if (people[l] + people[r] <= limit) 22 | l++; 23 | r--; 24 | sum++; 25 | } 26 | 27 | return sum; 28 | } 29 | }; 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Leet_Code_Problems/916.word-subsets.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=916 lang=cpp 3 | * 4 | * [916] Word Subsets 5 | * 6 | * https://leetcode.com/problems/word-subsets/discuss/2353150/Frequency-Vector-or-C-%2B-%2B 7 | * 8 | * https://leetcode.com/problems/word-subsets/ 9 | * 10 | */ 11 | 12 | // @lc code=start 13 | class Solution 14 | { 15 | public: 16 | vector wordSubsets(vector &words1, vector &words2) 17 | { 18 | } 19 | }; 20 | // @lc code=end 21 | -------------------------------------------------------------------------------- /Leet_Code_Problems/925.long-pressed-name.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=925 lang=cpp 3 | * 4 | * [925] Long Pressed Name 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool isLongPressedName(string name, string typed) 12 | { 13 | int n = name.size(), m = typed.size(), j = 0, i = 0; 14 | 15 | while (j < m) 16 | { 17 | if (name[i] == typed[j]) 18 | { 19 | i++; 20 | j++; 21 | } 22 | else if (j != 0 && typed[j] == typed[j - 1]) 23 | j++; 24 | 25 | else 26 | return false; 27 | } 28 | 29 | return i == n; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Leet_Code_Problems/941.valid-mountain-array.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=941 lang=java 3 | * 4 | * https://leetcode.com/problems/valid-mountain-array/ 5 | * 6 | * [941] Valid Mountain Array 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public boolean validMountainArray(int[] arr) { 12 | 13 | int len = arr.length, i = 0; 14 | int MaxElement = Arrays.stream(arr).max().getAsInt(); 15 | 16 | if (len < 3) 17 | return false; 18 | 19 | // Start can't be more 20 | if (arr[0] > arr[1]) 21 | return false; 22 | 23 | // Upward the slope 24 | while (i + 1 < len && arr[i] < arr[i + 1]) 25 | i++; 26 | 27 | if (i == 0 || i == len - 1) 28 | return false; 29 | 30 | // Down the slope 31 | while (i + 1 < len && arr[i] > arr[i + 1]) 32 | i++; 33 | 34 | return (i == len - 1) ? true : false; 35 | } 36 | } 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /Leet_Code_Problems/LinkedList.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Length of the Linked List 4 | --------------------------------- 5 | int len = 0 ; 6 | Listnode *curr = head; 7 | 8 | while( curr != head ){ // Iterative Method 9 | len ++; 10 | curr = curr->next; 11 | } 12 | 13 | 14 | int getCount(Node* head) // Using Recursion 15 | { 16 | if (head == NULL) 17 | return 0; 18 | 19 | else 20 | return 1 + getCount(head->next); 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /LinkedList.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Length of the Linked List 4 | --------------------------------- 5 | int len = 0 ; 6 | Listnode *curr = head; 7 | 8 | while( curr != head ){ // Iterative Method 9 | len ++; 10 | curr = curr->next; 11 | } 12 | 13 | 14 | int getCount(Node* head) // Using Recursion 15 | { 16 | if (head == NULL) 17 | return 0; 18 | 19 | else 20 | return 1 + getCount(head->next); 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /LinkedList/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | 6 | -------------------------------------------------------------------------------- /LinkedList/206.reverse-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=206 lang=cpp 3 | * 4 | * * https://leetcode.com/problems/reverse-linked-list/ 5 | * 6 | * [206] Reverse Linked List 7 | */ 8 | 9 | // @lc code=start 10 | /** 11 | * Definition for singly-linked list. 12 | * struct ListNode { 13 | * int val; 14 | * ListNode *next; 15 | * ListNode() : val(0), next(nullptr) {} 16 | * ListNode(int x) : val(x), next(nullptr) {} 17 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 18 | * }; 19 | */ 20 | class Solution 21 | { 22 | public: 23 | ListNode *reverseList(ListNode *head) 24 | { 25 | ListNode *curr = head, *temp, *prevNode = NULL; // prevNode var stores the previous address. 26 | 27 | while (curr != NULL) 28 | { 29 | temp = curr->next; // address 30 | curr->next = prevNode; 31 | prevNode = curr; 32 | curr = temp; 33 | } 34 | 35 | return prevNode; // curr / last Node 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /LinkedList/206.reverse-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=206 lang=java 3 | * 4 | * https://leetcode.com/problems/reverse-linked-list/ 5 | * 6 | * [206] Reverse Linked List 7 | */ 8 | 9 | // @lc code=start 10 | /** 11 | * Definition for singly-linked list. 12 | * public class ListNode { 13 | * int val; 14 | * ListNode next; 15 | * ListNode() {} 16 | * ListNode(int val) { this.val = val; } 17 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 18 | * } 19 | */ 20 | class Solution { 21 | public ListNode reverseList(ListNode head) { 22 | ListNode curr = head, temp, newHead = null; 23 | 24 | while (curr != null) { 25 | temp = curr.next; // address 26 | curr.next = newHead; 27 | newHead = curr; 28 | curr = temp; 29 | } 30 | 31 | return newHead; 32 | } 33 | } 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /LinkedList/237.delete-node-in-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=237 lang=cpp 3 | * 4 | * [237] Delete Node in a Linked List 5 | * 6 | * https://leetcode.com/problems/delete-node-in-a-linked-list/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | /** 12 | * Definition for singly-linked list. 13 | * struct ListNode { 14 | * int val; 15 | * ListNode *next; 16 | * ListNode(int x) : val(x), next(NULL) {} 17 | * }; 18 | */ 19 | class Solution 20 | { 21 | public: 22 | void deleteNode(ListNode *node) 23 | { 24 | // Just COPY the value of the adjacent node. 25 | node->val = node->next->val; 26 | node->next = node->next->next; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /LinkedList/25.reverse-nodes-in-k-group.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=25 lang=cpp 3 | * 4 | * [25] Reverse Nodes in k-Group 5 | * 6 | * https://leetcode.com/problems/reverse-nodes-in-k-group/ 7 | * 8 | * https://takeuforward.org/data-structure/reverse-linked-list-in-groups-of-size-k/ 9 | */ 10 | 11 | // @lc code=start 12 | /** 13 | * Definition for singly-linked list. 14 | * struct ListNode { 15 | * int val; 16 | * ListNode *next; 17 | * ListNode() : val(0), next(nullptr) {} 18 | * ListNode(int x) : val(x), next(nullptr) {} 19 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 20 | * }; 21 | */ 22 | class Solution 23 | { 24 | public: 25 | ListNode *reverseKGroup(ListNode *head, int k) 26 | { 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /LinkedList/706.design-hash-map.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=706 lang=cpp 3 | * 4 | * [706] Design HashMap 5 | */ 6 | 7 | // @lc code=start 8 | class MyHashMap 9 | { 10 | public: 11 | vector vec; 12 | MyHashMap() 13 | { 14 | vec.resize(1e6 + 1, -1); 15 | } 16 | 17 | void put(int key, int value) 18 | { 19 | vec[key] = value; 20 | } 21 | 22 | int get(int key) 23 | { 24 | // if (vec[key]) 25 | return vec[key]; 26 | } 27 | 28 | void remove(int key) 29 | { 30 | vec[key] = -1; 31 | } 32 | }; 33 | 34 | /** 35 | * Your MyHashMap object will be instantiated and called as such: 36 | * MyHashMap* obj = new MyHashMap(); 37 | * obj->put(key,value); 38 | * int param_2 = obj->get(key); 39 | * obj->remove(key); 40 | */ 41 | // @lc code=end 42 | -------------------------------------------------------------------------------- /Matrix/240.search-a-2-d-matrix-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=240 lang=cpp 3 | * 4 | * [240] Search a 2D Matrix II 5 | * 6 | * https://leetcode.com/problems/search-a-2d-matrix-ii/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | bool searchMatrix(vector> &matrix, int target) 15 | { 16 | int row = matrix.size(), col = matrix[0].size(); 17 | int r = 0, c = col - 1; // initially we are starting from Row 1 , Col last 18 | 19 | while (r >= 0 && r < row && c >= 0 && c < col) 20 | { 21 | if (target == matrix[r][c]) 22 | return true; 23 | 24 | else if (target > matrix[r][c]) 25 | r++; // means prev row strike off 26 | 27 | else 28 | c--; 29 | } 30 | 31 | return false; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Matrix/48.rotate-image.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=48 lang=cpp 3 | * 4 | * [48] Rotate Image 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | // TC : O(N^2) SC : O(1) 12 | void rotate(vector> &matrix) 13 | { 14 | int len = matrix.size(), i, j; 15 | 16 | // Transpose of the Matrix arr[i][j] convert to arr[j][i] 17 | for (i = 0; i < len; i++) 18 | for (j = 0; j <= i; j++) 19 | swap(matrix[i][j], matrix[j][i]); 20 | 21 | // Reverse the ROWS 22 | for (i = 0; i < len; i++) 23 | reverse(matrix[i].begin(), matrix[i].end()); 24 | } 25 | }; 26 | 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Pairs/.cph/.Chef_Restaurant_.cpp_5ec113f1c0beedf38ac7126b1b8b5b6a.prob: -------------------------------------------------------------------------------- 1 | {"name":"Chef Restaurant ","group":"CodeChef - Practice(Easy)","url":"https://www.codechef.com/problems/CHEFRES","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1639412258855,"input":"1\n4 5\n5 7\n9 10\n2 3\n20 30\n5\n6\n7\n35\n1","output":"0\n0\n2\n-1\n1"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ChefRestaurant"}},"batch":{"id":"9fcff2a1-223b-4da1-bb01-07aea0aedf22","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Pairs_C++\\Chef_Restaurant_.cpp"} -------------------------------------------------------------------------------- /Pairs/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "array": "cpp", 5 | "deque": "cpp", 6 | "forward_list": "cpp", 7 | "list": "cpp", 8 | "vector": "cpp", 9 | "string_view": "cpp", 10 | "ostream": "cpp" 11 | } 12 | } -------------------------------------------------------------------------------- /Recursion/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Recursion/231.power-of-two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=231 lang=cpp 3 | * 4 | * [231] Power of Two 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool isPowerOfTwo(int n) 12 | { 13 | bool ans = false; 14 | if (n == 1) 15 | return true; 16 | 17 | if (n && n % 2 == 0) // if N Exsist + Even. 18 | ans = isPowerOfTwo(n / 2); 19 | 20 | return ans; 21 | } 22 | }; 23 | // @lc code=end 24 | -------------------------------------------------------------------------------- /Recursion/763.partition-labels.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=763 lang=cpp 3 | * 4 | * [763] Partition Labels 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector partitionLabels(string s) 12 | { 13 | 14 | vector A(26, 0); 15 | vector result; 16 | 17 | int i, start = 0, end = 0, size = s.length(); 18 | 19 | // a = 97 20 | for (i = 0; i < size; i++) 21 | A[s[i] - 'a'] = i; 22 | 23 | for (i = 0; i < size; i++) 24 | { 25 | end = max(end, A[s[i] - 'a']); 26 | 27 | // end is not set till the iterator reaches the END 28 | if (end == i) 29 | { 30 | result.push_back(end - start + 1); 31 | start = i + 1; // for the new partition 32 | } 33 | } 34 | 35 | return result; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Recursion/78.subsets.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=78 lang=cpp 3 | * 4 | * https://leetcode.com/problems/subsets/ 5 | * 6 | * [78] Subsets 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | // Backtracking Problem , draw the RECURSION TREE 14 | vector> result; 15 | 16 | void helper(int i, vector &nums, vector temp) 17 | { 18 | if (i == nums.size()) 19 | { 20 | result.push_back(temp); 21 | return; 22 | } 23 | 24 | helper(i + 1, nums, temp); // exclude the current element 25 | temp.push_back(nums[i]); 26 | helper(i + 1, nums, temp); // include element part 27 | } 28 | 29 | vector> subsets(vector &nums) 30 | { 31 | vector temp; 32 | int i, len = nums.size(); 33 | 34 | helper(0, nums, temp); 35 | return result; 36 | } 37 | }; 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /Recursion/CountNumberWays__Jumps.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | // https://www.geeksforgeeks.org/count-number-of-ways-to-cover-a-distance/ 4 | #include 5 | using namespace std; 6 | 7 | int countWays(int dist) 8 | { 9 | int Z; 10 | if (dist == 0) 11 | return 1; 12 | if (dist == 1) 13 | return 1; 14 | if (dist < 0) 15 | return 0; 16 | 17 | Z = countWays(dist - 1) + countWays(dist - 2) + countWays(dist - 3); 18 | return Z; 19 | } 20 | 21 | int main() 22 | { 23 | int distance = 4; 24 | cout << "Number of Ways he can jump to react his destination : " << countWays(distance); 25 | } -------------------------------------------------------------------------------- /Recursion/EuclidianAlgo_GCD.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // formal parameter 8 | int GCD(int a, int b) 9 | { 10 | if (b == 0) 11 | return a; 12 | return GCD(b, a % b); 13 | } 14 | 15 | int main() 16 | { 17 | int a, b; 18 | cout << "?? TYPE 2 NUMBER WHOSE GCD YOU WANT TO INVESTIGATE ?? "; 19 | cin >> a >> b; 20 | int ans = GCD(a, b); // actual parameter 21 | cout << "GCD = " << ans; 22 | } -------------------------------------------------------------------------------- /Recursion/NumberOfJumpsToReachDestination.cpp: -------------------------------------------------------------------------------- 1 | 2 | // https://www.geeksforgeeks.org/count-number-of-ways-to-cover-a-distance/ 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | } -------------------------------------------------------------------------------- /Recursion/PERMUTATION.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | void getpermutation(string s, int index) 6 | { 7 | if (index >= s.length()) 8 | { 9 | cout << s << endl; 10 | return; 11 | } 12 | 13 | for (int i = index; i < s.length(); i++) 14 | { 15 | swap(s[index], s[i]); 16 | getpermutation(s, index + 1); 17 | swap(s[index], s[i]); 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | string s = "abc"; 24 | cout << "All permutation of the string = " << endl; 25 | getpermutation(s, 0); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Recursion/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | str -------------------------------------------------------------------------------- /SET.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | // ---------------------------------------------------------------------------- 4 | // SET 5 | // ------------------------------------------------------------------------------ 6 | 7 | 8 | Set is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal). 9 | The time complexity of set operations is O(log n) 10 | Unordered_set, it is O(1). 11 | 12 | 13 | set > s1 - O(log n) 14 | unordered_set stringSet ; - O(1) -------------------------------------------------------------------------------- /Set/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Set/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "ostream": "cpp", 4 | "deque": "cpp", 5 | "string": "cpp", 6 | "vector": "cpp" 7 | } 8 | } -------------------------------------------------------------------------------- /Set/137.single-number-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=137 lang=cpp 3 | * 4 | * [137] Single Number II 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int singleNumber(vector &nums) 12 | { 13 | // int i, ans = 0; 14 | // for (i = 0; i < nums.size(); i++) 15 | // ans = ans ^ nums[i]; // Xor of 2 ^ 2 = 0 16 | // return ans; 17 | 18 | unordered_map map; 19 | int i, ans = 0; 20 | 21 | for (i = 0; i < nums.size(); i++) 22 | map[nums[i]]++; 23 | 24 | for (auto j = map.begin(); j != map.end(); j++) 25 | { 26 | if (j->second == 1) 27 | ans = j->first; 28 | } 29 | 30 | return ans; 31 | } 32 | }; 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /Set/2215.Find the Difference of Two Arrays.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | // #include 4 | 5 | // https://leetcode.com/problems/find-the-difference-of-two-arrays/ 6 | 7 | class Solution 8 | { 9 | public: 10 | vector> findDifference(vector &nums1, vector &nums2) 11 | { 12 | // distinct/unique element 13 | unordered_set set1(nums1.begin(), nums1.end()); 14 | unordered_set set2(nums2.begin(), nums2.end()); 15 | 16 | vector> result(2); 17 | 18 | for (auto &it : set1) 19 | { 20 | if (set2.find(it) == set2.end()) // not found 21 | result[0].push_back(it); 22 | } 23 | 24 | for (auto &it : set2) 25 | { 26 | if (set1.find(it) == set1.end()) // not found 27 | result[1].push_back(it); 28 | } 29 | 30 | return result; 31 | } 32 | }; -------------------------------------------------------------------------------- /Set/260.single-number-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=260 lang=cpp 3 | * 4 | * [260] Single Number III 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector singleNumber(vector &nums) 12 | { 13 | int i, num = 0; 14 | vector ans; 15 | set Set; 16 | 17 | for (i = 0; i < nums.size(); i++) 18 | { 19 | if (Set.find(nums[i]) == Set.end()) // can't find and iterator reaches the end 20 | Set.insert(nums[i]); 21 | else 22 | Set.erase(nums[i]); 23 | } 24 | 25 | for (auto itr = Set.begin(); itr != Set.end(); itr++) 26 | ans.push_back(*itr); 27 | 28 | return ans; 29 | } 30 | }; 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /Set/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | .begin(); i != s.end(); i++) 2 | cout << *i << "\t"; -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Careless_Chef.cpp_7ac3b9dedfb6be5f93a9a35e24815e3f.prob: -------------------------------------------------------------------------------- 1 | {"name":"Careless Chef","group":"CodeChef - CodeChef Starters 26 Division 3 (Rated)","url":"https://www.codechef.com/START26C/problems/LOSTSEQ","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1645023954125,"input":"3\n1\n1 1\n2\n16 2 2022 2001\n2\n2 4 -2 4","output":"YES\nNO\nYES"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CarelessChef"}},"batch":{"id":"0cb4f15b-18e9-407f-b1e1-62ce2d8dfe3e","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Sliding_Window + Prefix_Sum\\Careless_Chef.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Control_the_Pollution.cpp_597a21e9939ea17d9938ea3386d9f227.prob: -------------------------------------------------------------------------------- 1 | {"name":"Control the Pollution","group":"CodeChef - CodeChef Starters 26 Division 3 (Rated)","url":"https://www.codechef.com/START26C/problems/SMOKE","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"id":1645046734524,"input":"3\n9 40 8\n15 20 20\n105 80 10","output":"24\n20\n100"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ControlThePollution"}},"batch":{"id":"694b6784-33a3-47cd-8d91-c437a485091c","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Sliding_Window + Prefix_Sum\\Control_the_Pollution.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Count_the_Notebooks.cpp_e16f7066dcf8ffe3a573e0806bd0bda1.prob: -------------------------------------------------------------------------------- 1 | {"name":"Count the Notebooks","group":"CodeChef - CodeChef Starters 26 Division 3 (Rated)","url":"https://www.codechef.com/START26C/problems/NOTEBOOK","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1645022084536,"input":"3\n1\n100\n50","output":"10\n1000\n500"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CountTheNotebooks"}},"batch":{"id":"e00a65ec-5822-4e56-8415-a5ca8bedac4f","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Sliding_Window + Prefix_Sum\\Count_the_Notebooks.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Crossing_Blocks.cpp_451bdb3b06948cd4c14c1b6b0e798375.prob: -------------------------------------------------------------------------------- 1 | {"name":"Crossing Blocks","group":"CodeChef - August Lunchtime 2021 Division 3","url":"https://www.codechef.com/LTIME99C/problems/CROSBLK","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"input":"2\n5\n9 15 8 13 8\n9\n20 16 13 9 17 11 15 8 7\n","output":"-1\n4\n","id":1630169837077}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"CrossingBlocks"}},"batch":{"id":"52546d0c-cd09-45cb-9ea9-a2baae2d6757","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Crossing_Blocks.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Hardest_Problem_Bet.cpp_bd9bb6274ddae4ac5e17a961fb619a5f.prob: -------------------------------------------------------------------------------- 1 | {"name":"Hardest Problem Bet","group":"CodeChef - CodeChef Starters 10 Division 3 (Rated)","url":"https://www.codechef.com/START10C/problems/HARDBET","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"id":1630233086740,"input":"3\n1 4 2\n16 8 10\n14 15 9","output":"Draw\nBob\nAlice"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"HardestProblemBet"}},"batch":{"id":"28510bac-3d8f-4dec-ab40-bd660a9eef9a","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Hardest_Problem_Bet.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Hostel_Room.cpp_550857c530fc888d6e55a112f9168436.prob: -------------------------------------------------------------------------------- 1 | {"name":"Hostel Room","group":"CodeChef - CodeChef Starters 26 Division 3 (Rated)","url":"https://www.codechef.com/START26C/problems/HOSTELROOM","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1645022357980,"input":"4\n5 8\n0 3 3 -13 5\n4 5\n0 -2 2 3\n3 5\n-2 5 -2\n3 2\n4 -7 1","output":"14\n8\n8"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"HostelRoom"}},"batch":{"id":"3eeccea7-6673-4ee1-8a54-6c2aac93b94a","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Sliding_Window + Prefix_Sum\\Hostel_Room.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.K_th_Maximum.cpp_818a7bfe1938ae29660b83737e48af8f.prob: -------------------------------------------------------------------------------- 1 | {"name":"K-th Maximum","group":"CodeChef - August Lunchtime 2021 Division 3","url":"https://www.codechef.com/LTIME99C/problems/KMAX2","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"input":"1\n5 3\n1 2 3 4 5\n","output":"1\n","id":1630160932168},{"id":1630166440777,"input":"1\n4 2\n2 5 6 1","output":"2"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"KThMaximum"}},"batch":{"id":"0b7c1665-91e7-45c7-b900-05ef68ca7ee2","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\K_th_Maximum.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Maximise_the_Subsequence_Sum.cpp_e4e6ac0be8b8b460e40631f1769c7bca.prob: -------------------------------------------------------------------------------- 1 | {"name":"Maximise the Subsequence Sum","group":"CodeChef - CodeChef Starters 10 Division 3 (Rated)","url":"https://www.codechef.com/START10C/problems/SIGNFLIP","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1630236158667,"input":"3\n6 2\n6 -10 -1 0 -4 2\n5 0\n-1 -1 -1 -1 -1\n3 3\n1 2 3","output":"22\n0\n6"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"MaximiseTheSubsequenceSum"}},"batch":{"id":"8be2afad-346d-4a7f-9221-6b8fc1068c18","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Maximise_the_Subsequence_Sum.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Maximum_Length_Even_Subarray.cpp_e339d7ab6f2e91e96762a7638879a088.prob: -------------------------------------------------------------------------------- 1 | {"name":"Maximum Length Even Subarray","group":"CodeChef - August Lunchtime 2021 Division 3","url":"https://www.codechef.com/LTIME99C/problems/MXEVNSUB","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"id":1630160304726,"input":"3\n3\n4\n5","output":"3\n4\n4"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"MaximumLengthEvenSubarray"}},"batch":{"id":"003dc00f-7c07-4c63-9ea1-fdb0ba5b5faf","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Maximum_Length_Even_Subarray.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Odd_Repeat.cpp_2f555ff0fb61349ba57b802219e2ff1d.prob: -------------------------------------------------------------------------------- 1 | {"name":"Odd Repeat","group":"CodeChef - CodeChef Starters 10 Division 3 (Rated)","url":"https://www.codechef.com/START10C/problems/REPEAT","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"id":1630233797795,"input":"3\n3 2 14\n5 4 28\n2 3 10","output":"5\n1\n3"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"OddRepeat"}},"batch":{"id":"31bb2163-ff5b-4252-b646-681c298bfaee","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Odd_Repeat.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Programming_Languages.cpp_a1ce9dfada2cd51de83a6892e328a025.prob: -------------------------------------------------------------------------------- 1 | {"name":"Programming Languages","group":"CodeChef - August Lunchtime 2021 Division 3","url":"https://www.codechef.com/LTIME99C/problems/PROGLANG","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"id":1630159289000,"input":"3\n1 2 2 1 3 4\n3 4 2 1 4 3\n1 2 1 3 2 4","output":"1\n2\n0"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ProgrammingLanguages"}},"batch":{"id":"d5dae896-993e-4d72-9392-5974ef5b19b7","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Programming_Languages.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Programming_Languages.java_c2974a27ad2e2b87da0b32c7eb931c1d.prob: -------------------------------------------------------------------------------- 1 | {"name":"Programming Languages","group":"CodeChef - August Lunchtime 2021 Division 3","url":"https://www.codechef.com/LTIME99C/problems/PROGLANG","interactive":false,"memoryLimit":256,"timeLimit":500,"tests":[{"input":"3\n1 2 2 1 3 4\n3 4 2 1 4 3\n1 2 1 3 2 4\n","output":"1\n2\n0\n","id":1630159274385}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"ProgrammingLanguages"}},"batch":{"id":"ce013414-b186-4289-9956-18fc3b70ea39","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Programming_Languages.java"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Shivigawdz.cpp_8bf50bb04ed1c9c54bca0a5f1b09a270.prob: -------------------------------------------------------------------------------- 1 | {"name":"Shivigawdz","group":"CodeChef - Practice(Extcontest)","url":"https://www.codechef.com/problems/SHIVIGOD","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1630096901499,"input":"1\n5 3 2\n1 2 3 4 5","output":"4.5000000"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"Shivigawdz"}},"batch":{"id":"59a79d12-c3dc-4776-ac25-38337925e703","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Shivigawdz.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Summary_Power.cpp_2ef5dfb279b9da800b10abb47c7c37be.prob: -------------------------------------------------------------------------------- 1 | {"name":"Summary Power","group":"CodeChef - Practice(Easy)","url":"https://www.codechef.com/problems/SUMPOWER","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1630830947134,"input":"3\n6 3\naabbcc\n5 2\nabccc\n4 3\naabb","output":"4\n3\n1"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"SummaryPower"}},"batch":{"id":"40b545e7-133e-4c90-8610-0610f5634c0c","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Summary_Power.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.cph/.Water_Tanks.cpp_19b6b1b9dd4400a2cc07806245d0fd73.prob: -------------------------------------------------------------------------------- 1 | {"name":"Water Tanks","group":"CodeChef - UCode 3","url":"https://www.codechef.com/UCO32021/problems/UCODE015","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1630314165671,"input":"2\n2\n5 6\n2 3\n3\n2 3 2\n1 3 1","output":"5\n2"},{"id":1630314890141,"input":"1\n3\n4 3 5\n3 2 2","output":"5"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"WaterTanks"}},"batch":{"id":"90f32af3-5691-449f-873e-8ea149f76fee","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_and_LeetCode-problem-solutions-main-\\Sliding_Window + Prefix_Sum\\Water_Tanks.cpp"} -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "array": "cpp", 4 | "any": "cpp", 5 | "string_view": "cpp", 6 | "ranges": "cpp", 7 | "span": "cpp", 8 | "regex": "cpp", 9 | "iostream": "cpp", 10 | "random": "cpp", 11 | "ostream": "cpp", 12 | "map": "cpp" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++.exe build active file", 6 | "command": "C:\\TDM-GCC-64\\bin\\g++.exe", 7 | "args": [ 8 | "-g", 9 | "${file}", 10 | "-o", 11 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 12 | ], 13 | "options": { 14 | "cwd": "${fileDirname}" 15 | }, 16 | "problemMatcher": [ 17 | "$gcc" 18 | ], 19 | "group": { 20 | "kind": "build", 21 | "isDefault": true 22 | }, 23 | "detail": "Task generated by Debugger." 24 | } 25 | ], 26 | "version": "2.0.0" 27 | } -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/1493.longest-subarray-of-1-s-after-deleting-one-element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1493 lang=cpp 3 | * 4 | * [1493] Longest Subarray of 1's After Deleting One Element 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int longestSubarray(vector &nums) 12 | { 13 | int n = nums.size(), i, res = 0, right = 0, start = 0, prevZeroIndex = -1; 14 | 15 | while (right < n) 16 | { 17 | if (nums[right] == 0) 18 | { 19 | if (prevZeroIndex != -1) // zero encounter NOT 1st time(Window Shrink) 20 | start = prevZeroIndex + 1; 21 | 22 | prevZeroIndex = right; 23 | } 24 | 25 | res = max(res, right - start + 1); 26 | right++; 27 | } 28 | 29 | return res - 1; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /Sliding_Window + Prefix_Sum/Count_Subarray_with_given_XOR.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | // https://takeuforward.org/data-structure/count-the-number-of-subarrays-with-given-xor-k/ 4 | 5 | // https://www.codingninjas.com/codestudio/problems/1115652?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=1 6 | 7 | int subarraysXor(vector &arr, int x) 8 | { 9 | int len = arr.size(), prefixXor = 0, count = 0; 10 | unordered_map map; 11 | 12 | for (int i = 0; i < len; i++) 13 | { 14 | prefixXor = prefixXor ^ arr[i]; 15 | 16 | if (prefixXor == x) 17 | count++; 18 | 19 | if (map.find(prefixXor ^ x) != map.end()) 20 | count = count + map[prefixXor ^ x]; 21 | 22 | map[prefixXor]++; 23 | } 24 | 25 | return count; 26 | } -------------------------------------------------------------------------------- /Stack/503.next-greater-element-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=503 lang=cpp 3 | * 4 | * [503] Next Greater Element II 5 | * 6 | * https://leetcode.com/problems/next-greater-element-ii/ 7 | * 8 | * CIRCULARLY FINDING NEXT GREATER ELEMENT 9 | * 10 | */ 11 | 12 | // @lc code=start 13 | class Solution 14 | { 15 | public: 16 | vector nextGreaterElements(vector &nums) 17 | { 18 | int len = arr.size(); 19 | 20 | stack s; 21 | vector result(len, -1); 22 | 23 | for (int i = 2 * len - 1; i >= 0; i--) 24 | { 25 | while (!s.empty() && s.top() <= arr[i % len]) 26 | s.pop(); 27 | 28 | if (i < len && !s.empty()) 29 | { 30 | int nge = s.top(); 31 | // result.push_back(nge ? nge : -1); 32 | result[i] = nge; 33 | } 34 | 35 | s.push(arr[i % len]); 36 | } 37 | 38 | return result; 39 | } 40 | }; 41 | // @lc code=end 42 | -------------------------------------------------------------------------------- /Strings/.cph/.Prime_in_a_binary_string.cpp_4938ec1ffc5e657955c691836dca9ef2.prob: -------------------------------------------------------------------------------- 1 | {"name":"Prime in a binary string","group":"CodeChef - January Long 2022 - I, Division 3 (Rated)","url":"https://www.codechef.com/JAN221C/problems/PINBS","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1641762995396,"input":"3\n1\n111\n101101","output":"No\nYes\nYes"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"PrimeInABinaryString"}},"batch":{"id":"d44d1ec0-a22d-44b6-9574-67c70b476994","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Strings\\Prime_in_a_binary_string.cpp"} -------------------------------------------------------------------------------- /Strings/.cph/.Retrieve_back_the_Array.cpp_19ccd6f80b3afe1e460478303367f11c.prob: -------------------------------------------------------------------------------- 1 | {"name":"Retrieve back the Array","group":"CodeChef - January Long 2022 - I, Division 3 (Rated)","url":"https://www.codechef.com/JAN221C/problems/XORED","interactive":false,"memoryLimit":256,"timeLimit":1000,"tests":[{"id":1641800728710,"input":"3\n1 10\n2 4\n3 1","output":"10\n7 3\n5 6 2"}],"testType":"single","input":{"type":"stdin"},"output":{"type":"stdout"},"languages":{"java":{"mainClass":"Main","taskClass":"RetrieveBackTheArray"}},"batch":{"id":"046a3195-c5a3-41d5-8c5c-1fb5811cd391","size":1},"srcPath":"c:\\LOCAL_DRIVE_Niharika\\Programming_Code\\Compititive_Programming_Algorithm\\Strings\\Retrieve_back_the_Array.cpp"} -------------------------------------------------------------------------------- /Strings/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | 6 | -------------------------------------------------------------------------------- /Strings/1095.find-in-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1095 lang=cpp 3 | * 4 | * [1095] Find in Mountain Array 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * // This is the MountainArray's API interface. 10 | * // You should not implement it, or speculate about its implementation 11 | * class MountainArray { 12 | * public: 13 | * int get(int index); 14 | * int length(); 15 | * }; 16 | */ 17 | 18 | class Solution { 19 | public: 20 | int findInMountainArray(int target, MountainArray &mountainArr) { 21 | 22 | } 23 | }; 24 | // @lc code=end 25 | 26 | -------------------------------------------------------------------------------- /Strings/1431.kids-with-the-greatest-number-of-candies.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1431 lang=cpp 3 | * 4 | * [1431] Kids With the Greatest Number of Candies 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | vector kidsWithCandies(vector &candies, int extraCandies) 12 | { 13 | vector out; 14 | int max = *max_element(candies.begin(), candies.end()); 15 | 16 | for (int i = 0; i < candies.size(); i++) 17 | { 18 | if (candies[i] + extraCandies >= max) 19 | out.push_back(true); 20 | else 21 | out.push_back(false); 22 | } 23 | 24 | return out; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Strings/1446.consecutive-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1446 lang=cpp 3 | * 4 | * [1446] Consecutive Characters 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxPower(string s) 12 | { 13 | int count = 1, maxCount = 0, i = 0; 14 | 15 | for (int i = 0; i < s.length() - 1; i++) 16 | { 17 | if (s[i] == s[i + 1]) 18 | count++; 19 | 20 | else 21 | { 22 | if (maxCount < count) 23 | maxCount = count; 24 | count = 1; 25 | } 26 | } 27 | 28 | if (count > maxCount) // EDGE CASE ..... 29 | maxCount = count; 30 | 31 | return maxCount; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Strings/1624.largest-substring-between-two-equal-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1624 lang=cpp 3 | * 4 | * [1624] Largest Substring Between Two Equal Characters 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxLengthBetweenEqualCharacters(string s) 12 | { 13 | 14 | unordered_map map; // key: character, value: least index of character in s 15 | int len = -1, compare; 16 | for (int i = 0; i < s.length(); i++) 17 | { 18 | if (map.count(s[i])) // whether this ele is present or not 19 | { 20 | compare = i - map[s[i]] - 1; 21 | len = max(len, compare); 22 | } 23 | else 24 | map[s[i]] = i; 25 | } 26 | return len; 27 | } 28 | }; 29 | // @lc code=end 30 | -------------------------------------------------------------------------------- /Strings/171.excel-sheet-column-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=171 lang=cpp 3 | * 4 | * https://leetcode.com/problems/excel-sheet-column-number/ 5 | * 6 | * [171] Excel Sheet Column Number 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | int titleToNumber(string columnTitle) 14 | { 15 | int i = 0, len = columnTitle.size(), k = 0; 16 | 17 | for (i = 0; i < len; i++) 18 | k = (k * 26) + (columnTitle[i] - 'A' + 1); 19 | 20 | return k; 21 | } 22 | }; 23 | // @lc code=end 24 | -------------------------------------------------------------------------------- /Strings/228.summary-ranges.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=228 lang=cpp 3 | * 4 | * https://leetcode.com/problems/summary-ranges/ 5 | * 6 | * [228] Summary Ranges 7 | */ 8 | 9 | // @lc code=start 10 | class Solution 11 | { 12 | public: 13 | vector summaryRanges(vector &nums) 14 | { 15 | int len = nums.size(), start = 0, i = 0; 16 | vector ranges; 17 | 18 | if (len == 0) 19 | return ranges; 20 | 21 | while (i < len) 22 | { 23 | if (i == len - 1 || nums[i] + 1 != nums[i + 1]) // when NEXT ele === prev + 1 (Skip) 24 | { 25 | if (start == i) 26 | ranges.push_back(to_string(nums[start])); 27 | else 28 | ranges.push_back(to_string(nums[start]) + "->" + to_string(nums[i])); 29 | 30 | start = i + 1; 31 | } 32 | 33 | i++; 34 | } 35 | 36 | return ranges; 37 | } 38 | }; 39 | // @lc code=end 40 | -------------------------------------------------------------------------------- /Strings/459.repeated-substring-pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=459 lang=cpp 3 | * 4 | * [459] Repeated Substring Pattern 5 | * 6 | * https://leetcode.com/problems/repeated-substring-pattern/description/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution 12 | { 13 | public: 14 | // Remove first char to avoid to match with input string which is from 0 to s.length() 15 | // Remove last char - appended after s.length() 16 | // Search for the input string is repeated 17 | 18 | bool repeatedSubstringPattern(string s) 19 | { 20 | string tmp = s + s; 21 | 22 | tmp = tmp.substr(1); 23 | tmp = tmp.substr(0, tmp.length() - 1); 24 | return tmp.find(s) != string::npos ? true : false; 25 | } 26 | }; 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /Strings/Delete_Characters_to_make_fancy_string.cpp: -------------------------------------------------------------------------------- 1 | 2 | // https://leetcode.com/contest/biweekly-contest-58/problems/delete-characters-to-make-fancy-string/ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int i = 0, del, count = 1; 12 | string s = "aaabaaaa"; 13 | string ans = ""; // empty string 14 | 15 | ans = ans + s[0]; 16 | 17 | for (i = 1; i < s.length(); i++) 18 | { 19 | if (s[i] == s[i - 1]) 20 | count++; 21 | else 22 | count = 1; 23 | 24 | if (count < 3) 25 | ans = ans + s[i]; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Strings/Substring_find.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | // Function to print all sub strings 6 | // s.substr(start_index , len_of_substring) 7 | void subString(string s, int n) 8 | { 9 | for (int i = 0; i < n; i++) // start index 10 | for (int j = 1; j <= n - i; j++) // Length of sub string 11 | cout << s.substr(i, j) << endl; 12 | } 13 | 14 | // Driver program to test above function 15 | int main() 16 | { 17 | string s = "abcd"; 18 | subString(s, s.length()); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Strings/Ways_to_remove_XOR_becomes_Zero.cpp: -------------------------------------------------------------------------------- 1 | 2 | // https://www.geeksforgeeks.org/ways-remove-one-element-binary-string-xor-becomes-zero/ 3 | #include 4 | using namespace std; 5 | 6 | int xorZero(string str) 7 | { 8 | int one_count = 0, zero_count = 0, i, len = str.length(); 9 | 10 | for (i = 0; i < len; i++) 11 | if (str[i] == '1') 12 | one_count++; 13 | else 14 | zero_count++; 15 | 16 | // If count of ones is EVEN then return count of zero 17 | // else count of one 18 | if (one_count % 2 == 0) 19 | return zero_count; 20 | return one_count; 21 | } 22 | 23 | int main() 24 | { 25 | string str = "11111"; 26 | cout << xorZero(str) << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Strings/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | if (map[str[i]] == -------------------------------------------------------------------------------- /TCS/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | // while (count--) 2 | // vec.push_back(0); 3 | -------------------------------------------------------------------------------- /TURBOT/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | aabbcffr -------------------------------------------------------------------------------- /Tree/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type 3 | *.exe 4 | 5 | -------------------------------------------------------------------------------- /Tree/.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "compilerPath": "C:\\usr\\bin\\gcc", 14 | "cStandard": "c17", 15 | "cppStandard": "c++17", 16 | "intelliSenseMode": "windows-gcc-x64" 17 | } 18 | ], 19 | "version": 4 20 | } -------------------------------------------------------------------------------- /Tree/Deepest-Right-Leaf.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Link : 3 | // https: // www.codingninjas.com/codestudio/problems/deepest-right-leaf_764143?leftPanelTab=1 4 | 5 | int currLevel = 0; 6 | BinaryTreeNode *res = NULL; 7 | 8 | void Helper(BinaryTreeNode *root, int level, bool isRight) 9 | { 10 | if (!root) 11 | return; 12 | 13 | // check is leaf node & right 14 | if (isRight && !root->left && !root->right && level >= currLevel) 15 | { 16 | currLevel = level; 17 | res = root; 18 | } 19 | 20 | Helper(root->left, level + 1, false); 21 | Helper(root->right, level + 1, true); 22 | } 23 | 24 | BinaryTreeNode *deepestRightLeaf(BinaryTreeNode *root) 25 | { 26 | // Write your code here. 27 | if (!root) 28 | return NULL; 29 | 30 | Helper(root, 0, false); 31 | return res; 32 | } 33 | -------------------------------------------------------------------------------- /Tree/others.md: -------------------------------------------------------------------------------- 1 | ## Other Problems on Binary Tree 2 | 3 | --- 4 | 5 | - **Top view of a Binary Tree** (https://practice.geeksforgeeks.org/problems/top-view-of-binary-tree/1) 6 | 7 | - **Bottom view of a Binary Tree** (https://practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article) 8 | -------------------------------------------------------------------------------- /Two_Pointers/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "string_view": "cpp", 4 | "regex": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /Two_Pointers/1679.max-number-of-k-sum-pairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1679 lang=cpp 3 | * 4 | * [1679] Max Number of K-Sum Pairs 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int maxOperations(vector &nums, int k) 12 | { 13 | // DOUBLE POINTER // 14 | 15 | sort(nums.begin(), nums.end()); 16 | int i = 0, j = nums.size() - 1, count = 0; 17 | while (i < j) 18 | { 19 | if (nums[i] + nums[j] == k) 20 | { 21 | count++; 22 | i++; 23 | j--; 24 | } 25 | else if (nums[i] + nums[j] < k) 26 | i++; 27 | else 28 | j--; 29 | } 30 | 31 | return count; 32 | } 33 | }; 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /Two_Pointers/881.boats-to-save-people.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=881 lang=cpp 3 | * 4 | * [881] Boats to Save People 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | int numRescueBoats(vector &people, int limit) 12 | { 13 | 14 | // DOUBLE POINTERS // 15 | int sum = 0, r = people.size() - 1, l = 0; // indexes 16 | sort(people.begin(), people.end()); 17 | 18 | // for (i=0 ; l <= r ; i++) 19 | while (l <= r) 20 | { 21 | if (people[l] + people[r] <= limit) 22 | l++; 23 | r--; 24 | sum++; 25 | } 26 | 27 | return sum; 28 | } 29 | }; 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /Two_Pointers/925.long-pressed-name.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=925 lang=cpp 3 | * 4 | * [925] Long Pressed Name 5 | */ 6 | 7 | // @lc code=start 8 | class Solution 9 | { 10 | public: 11 | bool isLongPressedName(string name, string typed) 12 | { 13 | int n = name.size(), m = typed.size(), j = 0, i = 0; 14 | 15 | while (j < m) 16 | { 17 | if (name[i] == typed[j]) 18 | { 19 | i++; 20 | j++; 21 | } 22 | else if (j != 0 && typed[j] == typed[j - 1]) 23 | j++; 24 | 25 | else 26 | return false; 27 | } 28 | 29 | return i == n; 30 | } 31 | }; 32 | // @lc code=end 33 | -------------------------------------------------------------------------------- /script.sh: -------------------------------------------------------------------------------- 1 | 2 | # ---------------------------------------- 3 | # SCRIPT FOR AUTO PUSH CODE IN GITHUB 4 | # ---------------------------------------- 5 | 6 | git add . 7 | 8 | echo -n 'Enter the commit message --> ' 9 | read commitMessage 10 | 11 | git commit -m "$commitMessage" 12 | 13 | # echo 'Enter Branch Name --> ' 14 | # read branch 15 | git push origin main 16 | 17 | 18 | # -n remove new line 19 | 20 | 21 | -------------------------------------------------------------------------------- /tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | // int temp; 2 | // int arr1[10] = {1, 2, 3, 4, 5, 6, 9, 8}; 3 | // temp = (arr1 + 1)[3]; // address of a[1] = 1004[3] = means index val of from address 1004 4 | // cout << temp << endl; // 5 -------------------------------------------------------------------------------- /workspace.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "path": "." 5 | }, 6 | { 7 | "name": "LeetCode_Files", 8 | "path": "..\\C++_codes\\LeetCode_Files" 9 | } 10 | ], 11 | "settings": {} 12 | } --------------------------------------------------------------------------------