├── Dynamic Programming ├── 10. Regular Expression Matching.cpp ├── 44. Wildcard Matching.cpp ├── B. Array Walk.cpp ├── Boboniu and Bit Operations_recursive.cpp ├── Ceaser_legions_memoized.cpp ├── Ceaser_legions_recursive.cpp ├── E. Sleeping Schedule_recursive.cpp ├── Modified_mcm.cpp ├── amex.cpp ├── catalan.cpp ├── fibonacci.cpp ├── flowers_memoization.cpp ├── hard_prob_memoization.cpp ├── hard_prob_recursive.cpp ├── k_tree_memization.cpp ├── k_trees_recursive_forces.cpp ├── lps_KMP_algo.cpp ├── min_coin_change_memoization.cpp ├── min_coin_change_recursive.cpp ├── stair_climbing_memoization.cpp ├── stair_climbing_recursive.cpp ├── sum_recursive.cpp ├── tiles_recursive.cpp ├── ways_coin_change_memoization.cpp └── ways_coin_change_recursive.cpp ├── Leetcode ├── Array │ ├── 1004. Max Consecutive Ones III.cpp │ ├── 1011. Capacity To Ship Packages Within D Days.cpp │ ├── 1053. Previous Permutation With One Swap.cpp │ ├── 11. Container With Most Water.cpp │ ├── 1277. Count Square Submatrices with All Ones.cpp │ ├── 1329. Sort the Matrix Diagonally.cpp │ ├── 141. Linked List Cycle.cpp │ ├── 1442. Count Triplets That Can Form Two Arrays of Equal XOR.cpp │ ├── 1456. Maximum Number of Vowels in a Substring of Given Length.cpp │ ├── 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts.cpp │ ├── 1471. The k Strongest Values in an Array.cpp │ ├── 1497. Check If Array Pairs Are Divisible by k.cpp │ ├── 1503. Last Moment Before All Ants Fall Out of a Plank.cpp │ ├── 1509. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp │ ├── 152. Maximum Product Subarray.cpp │ ├── 162. Find Peak Element.cpp │ ├── 169. Majority Element.cpp │ ├── 1725. Number Of Rectangles That Can Form The Largest Square.cpp │ ├── 1736. Latest Time by Replacing Hidden Digits.cpp │ ├── 179. Largest Number.cpp │ ├── 18. 4Sum.cpp │ ├── 1848. Minimum Distance to the Target Element.cpp │ ├── 209. Minimum Size Subarray Sum.cpp │ ├── 216. Combination Sum III.cpp │ ├── 229. Majority Element II.cpp │ ├── 238. Product of Array Except Self.cpp │ ├── 240. Search a 2D Matrix II.cpp │ ├── 287. Find the Duplicate Number.cpp │ ├── 324. Wiggle Sort II.cpp │ ├── 33. Search in Rotated Sorted Array.cpp │ ├── 334. Increasing Triplet Subsequence.cpp │ ├── 4. Median of Two Sorted Arrays.cpp │ ├── 40. Combination Sum II.cpp │ ├── 406. Queue Reconstruction by Height.cpp │ ├── 41. First Missing Positive.cpp │ ├── 42. Trapping Rain Water.cpp │ ├── 442. Find All Duplicates in an Array.cpp │ ├── 448. Find All Numbers Disappeared in an Array.cpp │ ├── 45. Jump Game II.cpp │ ├── 452. Minimum Number of Arrows to Burst Balloons.cpp │ ├── 48. Rotate Image.cpp │ ├── 55. Jump Game.cpp │ ├── 56. Merge Intervals.cpp │ ├── 560. Subarray Sum Equals K.cpp │ ├── 5649. Decode XORed Array.cpp │ ├── 665. Non-decreasing Array.cpp │ ├── 667. Beautiful Arrangement II.cpp │ ├── 769. Max Chunks To Make Sorted.cpp │ ├── 792. Number of Matching Subsequences.cpp │ ├── 80. Remove Duplicates from Sorted Array II.cpp │ ├── 873. Length of Longest Fibonacci Subsequence.cpp │ ├── 915. Partition Array into Disjoint Intervals.cpp │ ├── 918. Maximum Sum Circular Subarray.cpp │ ├── 926. Flip String to Monotone Increasing.cpp │ ├── 962. Maximum Width Ramp.cpp │ ├── 992. Subarrays with K Different Integers.cpp │ ├── Kadane_max_sum.cpp │ ├── Maximum Eraser value.cpp │ ├── Maximum Length of Repeated Subarray.cpp │ ├── Minimum Moves to Equal Array Elements II.cpp │ ├── My Calendar I.cpp │ ├── Range Sum Query 2D - Immutable.cpp │ └── Sort Colors.cpp ├── Backtracking │ ├── 131. Palindrome Partitioning.cpp │ ├── 17. Letter Combinations of a Phone Number.cpp │ ├── 1849. Splitting a String Into Descending Consecutive Values.cpp │ ├── 22. Generate Parentheses.cpp │ ├── 50. Pow(x, n).cpp │ └── 79. Word Search.cpp ├── DSU │ ├── 128. Longest Consecutive Sequence.cpp │ ├── 684. Redundant Connection.cpp │ ├── 959. Regions Cut By Slashes.cpp │ └── 990. Satisfiability of Equality Equations.cpp ├── Deque │ └── 239. Sliding Window Maximum.cpp ├── Dynamic Programming │ ├── 1035. Uncrossed Lines.cpp │ ├── 1043. Partition Array for Maximum Sum.cpp │ ├── 1049. Last Stone Weight II.cpp │ ├── 1074. Number of Submatrices That Sum to Target.cpp │ ├── 1140. Stone Game II.cpp │ ├── 1140. StoneGameII.cpp │ ├── 1187. Make Array Strictly Increasing.cpp │ ├── 123. Best Time to Buy and Sell Stock III.cpp │ ├── 1235. Maximum Profit in Job Scheduling.cpp │ ├── 1262. Greatest Sum Divisible by Three.cpp │ ├── 1312. Minimum Insertion Steps to Make a String Palindrome.cpp │ ├── 1349. Maximum Students Taking Exam.cpp │ ├── 1372. Longest ZigZag Path in a Binary Tree.cpp │ ├── 139. Word Break.cpp │ ├── 1406. Stone Game III.cpp │ ├── 1434. Number of Ways to Wear Different Hats to Each Other.cpp │ ├── 1449. Form Largest Integer With Digits That Add up to Target.cpp │ ├── 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum.cpp │ ├── 1510. Stone Game IV.cpp │ ├── 1553. Minimum Number of Days to Eat N Oranges.cpp │ ├── 1563. Stone Game V.cpp │ ├── 1643. Kth Smallest Instructions.cpp │ ├── 1690. Stone Game VII.cpp │ ├── 1696. Jump Game VI.cpp │ ├── 1727. Largest Submatrix With Rearrangements.cpp │ ├── 1738. Find Kth Largest XOR Coordinate Value.cpp │ ├── 174. Dungeon Game.cpp │ ├── 1755. Closest Subsequence Sum.cpp │ ├── 1770. Maximum Score from Performing Multiplication Operations.cpp │ ├── 1871. Jump Game VII.cpp │ ├── 1911. Maximum Alternating Subsequence Sum.cpp │ ├── 198. House Robber.cpp │ ├── 213. House Robber II.cpp │ ├── 279. Perfect Squares.cpp │ ├── 300. Longest Increasing Subsequence.cpp │ ├── 309. Best Time to Buy and Sell Stock with Cooldown.cpp │ ├── 329. Longest Increasing Path in a Matrix.cpp │ ├── 338. Counting Bits │ ├── 377. Combination Sum IV.cpp │ ├── 403. Frog Jump.cpp │ ├── 46. Permutations.cpp │ ├── 494. Target Sum.cpp │ ├── 5. Longest Palindromic Substring.cpp │ ├── 53. Maximum Subarray.cpp │ ├── 552. Student Attendance Record II.cpp │ ├── 600. Non-negative Integers without Consecutive Ones.cpp │ ├── 62. Unique Paths.cpp │ ├── 64. Minimum Path Sum.cpp │ ├── 647. Palindromic Substrings.cpp │ ├── 714. Best Time to Buy and Sell Stock with Transaction Fee.cpp │ ├── 72. Edit Distance.cpp │ ├── 871. Minimum Number of Refueling Stops_recursive.cpp │ ├── 931. Minimum Falling Path Sum.cpp │ ├── 940. Distinct Subsequences II.cpp │ ├── 96. Unique Binary Search Trees.cpp │ ├── 97. Interleaving String.cpp │ ├── 983. Minimum Cost For Tickets.cpp │ ├── BItamskin_dp.cpp │ ├── Burst Ballons.cpp │ ├── Combination Sum.cpp │ ├── Delete Operation for Two Strings Solution.cpp │ ├── Matchsticks to Square.cpp │ ├── Maximum Points You Can Obtain from Cards.cpp │ ├── Submatices with 1.cpp │ ├── Wrod_break_KMP.cpp │ ├── best-time-to-buy-and-sell-stock-iii.cpp │ ├── longest_palondromic_sub_tabulation.cpp │ └── minimum-xor-sum-of-two-arrays.cpp ├── Gaph │ ├── 1192. Critical Connections in a Netwok.cpp │ ├── 1267. Count Servers that Communicate.cpp │ ├── 127. Word Ladder.cpp │ ├── 1293. Shortest Path in a Grid with Obstacles Elimination.cpp │ ├── 1319. Number of Operations to Make Network Connected.cpp │ ├── 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance.cpp │ ├── 1361. Validate Binary Tree Nodes.cpp │ ├── 1387. Sort Integers by The Power Value.cpp │ ├── 1483. Kth Ancestor of a Tree Node.cpp │ ├── 1557. Minimum Number of Vertices to Reach All Nodes.cpp │ ├── 1559. Detect Cycles in 2D Grid.cpp │ ├── 1722. Minimize Hamming Distance After Swap Operations.cpp │ ├── 200. Number of Islands.cpp │ ├── 207. Course Schedule.cpp │ ├── 210. Course Schedule II.cpp │ ├── 301. Remove Invalid Parentheses.cpp │ ├── 399. Evaluate Division.cpp │ ├── 695. Max Area of Island.cpp │ ├── 752. Open the Lock.cpp │ ├── 778. Swim in Rising Water.cpp │ ├── 797. All Paths From Source to Target.cpp │ ├── 847. Shortest Path Visiting All Nodes.cpp │ ├── 864. Shortest Path to Get All Keys.cpp │ ├── 886. Possible Bipartition.cpp │ ├── 994. Rotting Oranges.cpp │ ├── Djkstra.cpp │ ├── Jump Game 3.cpp │ ├── Keys and rooms.cpp │ └── Longest String Chain .cpp ├── Greedy │ ├── 1007. Minimum Domino Rotations For Equal Row.cpp │ ├── 1338. Reduce Array Size to The Half.cpp │ ├── 134. Gas Station.cpp │ ├── 135. Candy.cpp │ ├── 1383. Maximum Performance of a Team.cpp │ ├── 435. Non-overlapping Intervals.cpp │ ├── 85. Maximal Rectangle.cpp │ ├── 910. Smallest Range II.cpp │ └── 984. String Without AAA or BBB.cpp ├── Heap │ ├── 1354. Construct Target Array With Multiple Sums.cpp │ ├── 1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows.cpp │ ├── 1882. Process Tasks Using Servers.cpp │ ├── 215. Kth Largest Element in an Array.cpp │ ├── 218. The Skyline Problem.cpp │ ├── 295. Find Median from Data Stream.cpp │ ├── 378. Kth Smallest Element in a Sorted Matrix.cpp │ ├── 480. Sliding Window Median.cpp │ └── 632. Smallest Range Covering Elements from K Lists.cpp ├── Linked List │ ├── 1290. Convert Binary Number in a Linked List to Integer.cpp │ ├── 138. Copy List with Random Pointer.cpp │ ├── 142. Linked List Cycle II.cpp │ ├── 146. LRU Cache.cpp │ ├── 160. Intersection of Two Linked Lists.cpp │ ├── 1721. Swapping Nodes in a Linked List.cpp │ ├── 19. Remove Nth Node From End of List.cpp │ ├── 2. Add Two Numbers.cpp │ ├── 206. Reverse Linked List_recusrsion.cpp │ ├── 21. Merge Two Sorted Lists.cpp │ ├── 23. Merge k Sorted Lists.cpp │ ├── 234. Palindrome Linked List │ ├── 237. Delete Node in a Linked List.cpp │ ├── 25. Reverse Nodes in k-Group.cpp │ ├── 341. Flatten Nested List Iterator │ ├── 82. Remove Duplicates from Sorted List II.cpp │ ├── 83. Remove Duplicates from Sorted List.cpp │ └── Palindrome Linked List Solution.cpp ├── Maths │ ├── 1104. Path In Zigzag Labelled Binary Tree.cpp │ ├── 1726. Tuple with Same Product.cpp │ ├── 1806. Minimum Number of Operations to Reinitialize a Permutation.cpp │ ├── 201. Bitwise AND of Numbers Range.cpp │ ├── 477. Total Hamming Distance.cpp │ └── seive.cpp ├── Merge Sort │ ├── 307. Range Sum Query - Mutable.cpp │ ├── 493. Reverse Pairs.cpp │ └── Count of range sum.cpp ├── Stack │ ├── 1209. Remove All Adjacent Duplicates in String II.cpp │ ├── 132 Pattern.cpp │ ├── 739. Daily Temperatures.cpp │ ├── 84. Largest Rectangle in Histogram.cpp │ ├── 856. Score of Parentheses.cpp │ ├── 895. Maximum Frequency Stack.cpp │ ├── Evaluate Reverse Polish Notation .cpp │ └── Remove All Adjacent Duplicates In String .cpp ├── Strings │ ├── 1805. Number of Different Integers in a String.cpp │ ├── 1807. Evaluate the Bracket Pairs of a String.cpp │ ├── 3. Longest Substring Without Repeating Characters.cpp │ ├── 395. Longest Substring with At Least K Repeating Characters.cpp │ ├── 438. Find All Anagrams in a String.cpp │ ├── 525. Contiguous Array.cpp │ ├── 696. Count Binary Substrings.cpp │ ├── 76. Minimum Window Substring.cpp │ ├── 763. Partition Labels.cpp │ ├── Permutation.cpp │ └── Reformat Phone no.cpp ├── Tree │ ├── 1008. Construct Binary Search Tree from Preorder Traversal │ ├── 1008. Construct Binary Search Tree from Preorder Traversal.cpp │ ├── 1028. Recover a Tree From Preorder Traversal.cpp │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal.cpp │ ├── 106. Construct Binary Tree from Inorder and Postorder Traversal.cpp │ ├── 108. Convert Sorted Array to Binary Search Tree.cpp │ ├── 113. Path Sum II.cpp │ ├── 116. Populating Next Right Pointers in Each Node.cpp │ ├── 117. Populating Next Right Pointers in Each Node II.cpp │ ├── 1305. All Elements in Two Binary Search Trees.cpp │ ├── 199. Binary Tree Right Side View.cpp │ ├── 230. Kth Smallest Element in a BST.cpp │ ├── 297. Serialize and Deserialize Binary Tree.cpp │ ├── 337. House Robber III.cpp │ ├── 34. Find First and Last Position of Element in Sorted Array.cpp │ ├── 623. Add One Row to Tree.cpp │ ├── 687. Longest Univalue Path.cpp │ ├── 701. Insert into a Binary Search Tree.cpp │ ├── 814. Binary Tree Pruning.cpp │ ├── 834. Sum of Distances in Tree.cpp │ ├── 863. All Nodes Distance K in Binary Tree.cpp │ ├── 94. Binary Tree Inorder Traversal.cpp │ ├── 95. Unique Binary Search Trees II.cpp │ ├── 951. Flip Equivalent Binary Trees.cpp │ ├── 968. Binary Tree Cameras.cpp │ ├── 979. Distribute Coins in Binary Tree.cpp │ ├── 98. Validate Binary Search Tree.cpp │ ├── Binary Tree Cameras.cpp │ ├── Binary Tree Pruning.cpp │ ├── Convert Sorted List to Binary Search Tree Solution.cpp │ ├── Path SumIII.cpp │ └── binary-tree-maximum-path-sum.cpp ├── Trie │ ├── 208. Implement Trie (Prefix Tree).cpp │ └── 745. Prefix and Suffix Search.cpp └── binary search │ ├── 316. Remove Duplicate Letters.cpp │ ├── 33. Search in Rotated Sorted Array.cpp │ ├── 668. Kth Smallest Number in Multiplication Table.cpp │ └── 792. Number of Matching Subsequences.cpp ├── README.md ├── SDE Sheet └── array │ ├── Kth smallest element.cpp │ ├── Minimize the Heights II .cpp │ └── Minimum number of jumps.cpp ├── Segment_tree.cpp └── https: ├── atcoder.jp └── contests │ └── zone2021 │ └── tasks │ ├── zone2021_a.cpp │ ├── zone2021_b.cpp │ └── zone2021_d.cpp ├── codeforces.com ├── contest │ ├── 131 │ │ └── problem │ │ │ └── C.cpp │ ├── 165 │ │ └── problem │ │ │ └── B.cpp │ ├── 166 │ │ └── problem │ │ │ └── E.cpp │ ├── 192 │ │ └── problem │ │ │ └── A.cpp │ ├── 274 │ │ └── problem │ │ │ └── A.cpp │ ├── 276 │ │ └── problem │ │ │ └── C.cpp │ ├── 296 │ │ └── problem │ │ │ └── C.cpp │ ├── 388 │ │ └── problem │ │ │ └── A.cpp │ ├── 441 │ │ └── problem │ │ │ └── C.cpp │ ├── 466 │ │ └── problem │ │ │ └── C.pp │ ├── 500 │ │ └── problem │ │ │ └── B.cpp │ ├── 510 │ │ └── problem │ │ │ └── B.cpp │ ├── 548 │ │ └── B.cpp │ ├── 574 │ │ └── problem │ │ │ └── B.cpp │ ├── 576 │ │ └── problem │ │ │ └── A.cpp │ ├── 580 │ │ └── problem │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 670 │ │ └── problem │ │ │ ├── D1.cpp │ │ │ └── D2.cpp │ ├── 675 │ │ └── problem │ │ │ └── B │ ├── 676 │ │ └── problem │ │ │ └── C.cpp │ ├── 677 │ │ └── problem │ │ │ └── B.cpp │ ├── 701 │ │ └── problem │ │ │ └── C.cpp │ ├── 744 │ │ └── problem │ │ │ └── A.cpp │ ├── 762 │ │ └── problem │ │ │ └── A │ ├── 804 │ │ └── problem │ │ │ └── B.cpp │ ├── 812 │ │ └── problem │ │ │ └── C.cpp │ ├── 839 │ │ └── problem │ │ │ └── C.cpp │ ├── 940 │ │ └── problem │ │ │ └── B.cpp │ ├── 950 │ │ └── problem │ │ │ └── C.cpp │ ├── 977 │ │ └── problem │ │ │ └── E.cpp │ ├── 982 │ │ └── problem │ │ │ └── C.cpp │ ├── 988 │ │ └── problem │ │ │ └── C.cpp │ ├── 991 │ │ └── problem │ │ │ └── C.cpp │ ├── 994 │ │ └── problem │ │ │ └── B.cpp │ ├── 1003 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1005 │ │ └── problem │ │ │ └── C.cpp │ ├── 1010 │ │ └── problem │ │ │ └── A.cpp │ ├── 1077 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1088 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1102 │ │ └── problem │ │ │ └── B │ ├── 1105 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1131 │ │ └── problem │ │ │ └── B.cpp │ ├── 1139 │ │ └── problem │ │ │ └── C.cpp │ ├── 1144 │ │ └── problem │ │ │ └── D.cpp │ ├── 1185 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C1.cpp │ │ │ └── C2.cpp │ ├── 1191 │ │ └── problem │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1201 │ │ └── problem │ │ │ └── C │ ├── 1203 │ │ └── problem │ │ │ └── E.cpp │ ├── 1207 │ │ └── problem │ │ │ └── B.cpp │ ├── 1208 │ │ └── problem │ │ │ └── B.cpp │ ├── 1217 │ │ └── problem │ │ │ └── B.cpp │ ├── 1256 │ │ └── problem │ │ │ └── D.cpp │ ├── 1260 │ │ └── problem │ │ │ └── B │ ├── 1263 │ │ └── problem │ │ │ └── D.cpp │ ├── 1264 │ │ └── problem │ │ │ └── A.cpp │ ├── 1282 │ │ └── problem │ │ │ ├── B1.cpp │ │ │ └── B2.cpp │ ├── 1294 │ │ └── problem │ │ │ └── D.cpp │ ├── 1295 │ │ └── problem │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1296 │ │ └── problem │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1304 │ │ └── problem │ │ │ └── C.cpp │ ├── 1325 │ │ └── problem │ │ │ └── C.cpp │ ├── 1333 │ │ └── problem │ │ │ └── C.cpp │ ├── 1336 │ │ └── problem │ │ │ └── A.cpp │ ├── 1350 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1352 │ │ ├── E │ │ ├── F │ │ └── problem │ │ │ ├── 271 │ │ │ └── B │ │ │ ├── A │ │ │ ├── D │ │ │ └── G │ ├── 1358 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1360 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ ├── D.cpp │ │ │ ├── E.cpp │ │ │ └── F.cpp │ ├── 1362 │ │ └── problem │ │ │ └── D.cpp │ ├── 1364 │ │ └── problem │ │ │ └── C.cpp │ ├── 1366 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1367 │ │ └── problem │ │ │ └── D.cpp │ ├── 1368 │ │ └── problem │ │ │ └── B.cpp │ ├── 1370 │ │ └── problem │ │ │ └── D.cpp │ ├── 1373 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1379 │ │ └── problem │ │ │ ├── A.cpp │ │ │ └── B.cpp │ ├── 1382 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C1.cpp │ ├── 1385 │ │ ├── B.cpp │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1388 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1389 │ │ └── problem │ │ │ ├── A.cpp │ │ │ └── C.cpp │ ├── 1391 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1395 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1397 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1399 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1401 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1407 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1409 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1419 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ ├── D1.cpp │ │ │ └── D2.cpp │ ├── 1421 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1422 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ ├── 1430 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1433 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ ├── D.cpp │ │ │ └── E.cpp │ ├── 1435 │ │ └── problem │ │ │ ├── A.cpp │ │ │ └── B.cpp │ ├── 1436 │ │ └── problem │ │ │ └── C.cpp │ ├── 1437 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ ├── 1445 │ │ └── problem │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C │ └── 1465 │ │ └── problem │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp └── problemset │ └── problem │ ├── 550 │ └── C.cpp │ ├── 839 │ └── C.cpp │ ├── 977 │ └── F.cpp │ ├── 1166 │ └── C.cpp │ ├── 1272 │ └── D.cpp │ ├── 1305 │ └── C.cpp │ ├── 1324 │ └── E.cpp │ ├── 1325 │ └── C.cpp │ ├── 1336 │ └── B.cpp │ ├── 1368 │ └── D.cpp │ ├── 1433 │ └── F.cpp │ └── 1462 │ └── D.cpp └── www.codechef.com └── COOK119B └── problems ├── CACHEHIT.cpp ├── MAXMEX.cpp └── PERCAPTA.cpp /Dynamic Programming/10. Regular Expression Matching.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int i,j,k,n,m; 5 | n=s.size();m=p.size(); 6 | int dp[m+1][n+1]; 7 | for(i=0;i<=m;i++){ 8 | for(j=0;j<=n;j++) dp[i][j]=0; 9 | } 10 | dp[0][0]=1; 11 | for(i=1;i<=n;i++) dp[0][i]=0; 12 | for(i=1;i<=m;i++){ 13 | if(p[i-1]=='*') dp[i][0]=dp[i-2][0]; 14 | else dp[i][0]=0; 15 | } 16 | for(i=1;i<=m;i++){ 17 | for(j=1;j<=n;j++){ 18 | if(p[i-1]==s[j-1]) dp[i][j]=dp[i-1][j-1]; 19 | else if(p[i-1]=='*'){ 20 | if(p[i-2]==s[j-1]|| p[i-2]=='.'){ 21 | dp[i][j]=dp[i-1][j] || dp[i][j-1]; 22 | } 23 | dp[i][j]=dp[i][j] || dp[i-2][j]; 24 | } 25 | else if(p[i-1]=='.') dp[i][j]=dp[i-1][j-1]; 26 | else dp[i][j]=0; 27 | } 28 | } 29 | 30 | if(dp[m][n]==0) return false; 31 | else return true; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Dynamic Programming/catalan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | ll a[1000]; 19 | ll cat(ll n) { 20 | if (n == 0) return 1; 21 | if (a[n] != -1) return a[n]; 22 | else { 23 | a[n] = 0; 24 | for (ll i = 0; i <= n - 1; i++) { 25 | a[n] += cat(i) * cat(n - 1 - i); 26 | } 27 | return a[n]; 28 | } 29 | } 30 | int main() { 31 | #ifndef ONLINE_JUDGE 32 | freopen("input.txt", "r", stdin); 33 | freopen("output.txt", "w", stdout); 34 | freopen("err.txt", "w", stderr); 35 | #endif 36 | ll i, j, k, n, m, ct = 0, t, ans = 0; 37 | cin >> t; 38 | while (t--) { 39 | cin >> n; 40 | memset(a, -1, sizeof(a)); 41 | a[0] = 1; 42 | cout << cat(n) << endl; 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Dynamic Programming/fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll a[10000]; 18 | ll max1 = (ll)1e9; 19 | ll fib(ll n) { 20 | if (n == 0 || n == 1) return 1; 21 | if (a[n] == -1) return fib(n - 1) + fib(n - 2); 22 | else { 23 | return a[n]; 24 | } 25 | } 26 | int main() { 27 | #ifndef ONLINE_JUDGE 28 | freopen("input.txt", "r", stdin); 29 | freopen("output.txt", "w", stdout); 30 | freopen("err.txt", "w", stderr); 31 | #endif 32 | ll i, j, k, n, m, ct = 0, t, ans = 0; 33 | cin >> t; 34 | while (t--) { 35 | cin >> n; 36 | memset(a, -1, sizeof(a)); 37 | a[0] = 1; a[1] = 1; 38 | cout << fib(n - 1) << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Dynamic Programming/stair_climbing_memoization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll a[100005] = {0}; 18 | ll ways(ll n) { 19 | if (n <= 1) return n; 20 | else if (a[n] == 0) a[n] = ways(n - 1) + ways(n - 2); 21 | return a[n]; 22 | } 23 | int main() { 24 | #ifndef ONLINE_JUDGE 25 | freopen("input.txt", "r", stdin); 26 | freopen("output.txt", "w", stdout); 27 | freopen("err.txt", "w", stderr); 28 | #endif 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | ll i, j, k, n, m, ct = 0, t, ans = 0; 32 | cin >> t; 33 | while (t--) { 34 | cin >> n; 35 | cout << ways(n) << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Dynamic Programming/stair_climbing_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll ways(ll n) { 18 | if (n <= 1) return n; 19 | //at any stair he will reach by stairs just below it so ways is sum of ways to climb stairs below nth stair 20 | return ways(n - 1) + ways(n - 2); 21 | } 22 | int main() { 23 | #ifndef ONLINE_JUDGE 24 | freopen("input.txt", "r", stdin); 25 | freopen("output.txt", "w", stdout); 26 | freopen("err.txt", "w", stderr); 27 | #endif 28 | ios_base::sync_with_stdio(false); 29 | cin.tie(NULL); 30 | ll i, j, k, n, m, ct = 0, t, ans = 0; 31 | cin >> t; 32 | while (t--) { 33 | cin >> n; 34 | cout << ways(n) << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Dynamic Programming/sum_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll ways(ll a[], ll j, ll n) { 18 | if (j == 0) return 0; 19 | else if (n == 0) return 1; 20 | if (a[j] <= n) return ways(a, j, n - a[j]) + ways(a, j - 1, n); 21 | else return ways(a, j - 1, n); 22 | } 23 | int main() { 24 | #ifndef ONLINE_JUDGE 25 | freopen("input.txt", "r", stdin); 26 | freopen("output.txt", "w", stdout); 27 | freopen("err.txt", "w", stderr); 28 | #endif 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | ll i, j, k, n, m, ct = 0, t, ans = 0; 32 | cin >> t; 33 | while (t--) { 34 | cin >> n; 35 | ll a[n]; 36 | for (i = 1; i <= n - 1; i++) a[i] = i; 37 | cout << ways(a, n - 1, n) << endl; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Dynamic Programming/tiles_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll ways(ll n) { 18 | if (n <= 2) return n; 19 | ll ans = 0; 20 | if (n >= 2) ans += ways(n - 2); 21 | ans += ways(n - 1); 22 | return ans; 23 | } 24 | int main() { 25 | #ifndef ONLINE_JUDGE 26 | freopen("input.txt", "r", stdin); 27 | freopen("output.txt", "w", stdout); 28 | freopen("err.txt", "w", stderr); 29 | #endif 30 | ios_base::sync_with_stdio(false); 31 | cin.tie(NULL); 32 | ll i, j, k, n, m, ct = 0, t, ans = 0; 33 | cin >> t; 34 | while (t--) { 35 | cin >> n; 36 | cout << ways(n) << endl; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Dynamic Programming/ways_coin_change_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll ways(ll n, ll a[], ll k) { 18 | if (n == 0) return 1; 19 | else if (k == 0) return 0; 20 | if (n >= a[k]) return ways(n - a[k], a, k) + ways(n, a, k - 1); 21 | else return ways(n, a, k - 1); 22 | } 23 | int main() { 24 | #ifndef ONLINE_JUDGE 25 | freopen("input.txt", "r", stdin); 26 | freopen("output.txt", "w", stdout); 27 | freopen("err.txt", "w", stderr); 28 | #endif 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | ll i, j, k, n, m, ct = 0, t, ans = 0; 32 | cin >> t; 33 | while (t--) { 34 | cin >> n >> k; 35 | ll a[k + 1]; 36 | for (i = 1; i <= k; i++) cin >> a[i]; 37 | cout << ways(n, a, k) << endl; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Leetcode/Array/1004. Max Consecutive Ones III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestOnes(vector& nums, int k) { 4 | //Using two pointers approach,st->start pointer,en->end pointer 5 | int st=0,en=0,n = nums.size(), ans = 0; 6 | //outer loop for starting pointer 7 | while(st=0 && en& w,int D){ 2 | int i,ct=0,sum=0; 3 | for(i=0;imid){ 9 | sum=w[i]; 10 | ct++; 11 | if(i==w.size()-1) ct++; 12 | } 13 | else if(w[i]>mid) return 0; 14 | } 15 | cout<& w, int D) { 22 | int sum=0; 23 | for(int i=0;i<(int)w.size();i++) sum+=w[i]; 24 | int l=1,r=sum,ans=0; 25 | while(l<=r){ 26 | int mid=(l+r)/2; 27 | if(check(mid,w,D)){ 28 | ans=mid; 29 | r=mid-1; 30 | } 31 | else l=mid+1; 32 | } 33 | return ans; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Leetcode/Array/1053. Previous Permutation With One Swap.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector prevPermOpt1(vector& a) { 4 | for(int n=a.size()-1;n>=1;n--){ 5 | if(a[n]& vect) { 4 | int i,j,k,n=(int)vect.size(); 5 | int l=0,r=n-1; 6 | int area=0; 7 | while(l> diagonalSort(vector>& mat) { 4 | int m = mat.size(); 5 | int n = mat[0].size(); 6 | for(int i=m-2; i>=0; i--){ 7 | diagsort(mat, i, 0, m, n); 8 | } 9 | for(int j=1; j>& mat, int p, int q, int m, int n) { 17 | vector tosort; 18 | int i = p; 19 | int j = q; 20 | for(i,j;inext; 18 | if(slow==NULL) return false; 19 | else if(fast==NULL || fast->next==NULL) return false; 20 | fast=fast->next->next; 21 | ct++; 22 | } 23 | } 24 | return true; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/Array/1442. Count Triplets That Can Form Two Arrays of Equal XOR.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countTriplets(vector& a) { 4 | int i,j,k,n,m,ct=0; 5 | n=a.size(); 6 | int sum[n+1];k=0;sum[0]=0; 7 | for(i=1;i<=n;i++){ 8 | sum[i]=k xor a[i-1]; 9 | // cout<& hor, vector& ver) { 4 | int i,k,j,n,m,ct=0,ans=0,mod = 1e9+7; 5 | hor.push_back(0); 6 | ver.push_back(0); 7 | sort(hor.begin(),hor.end()); 8 | sort(ver.begin(),ver.end()); 9 | n=hor.size()-1;m=ver.size()-1; 10 | if(hor[n]!=h) hor.push_back(h); 11 | if(ver[m]!=w) ver.push_back(w); 12 | int max_diff=0,max_ans=0; 13 | for(i=1;i getStrongest(vector& arr, int k) { 4 | sort(arr.begin(),arr.end()); 5 | vector vect; 6 | int med=arr[((int)arr.size()-1)/2]; 7 | int start=0,end=(int)arr.size()-1; 8 | while(vect.size()!=arr.size()){ 9 | if(abs(med-arr[start])>abs(med-arr[end])){ 10 | vect.push_back(arr[start]); 11 | start++; 12 | } 13 | else{ 14 | vect.push_back(arr[end]); 15 | end--; 16 | } 17 | } 18 | vector vect2; 19 | for(int i=0;i& arr, int k) { 4 | int i,j,n,ct=0; 5 | n=(int)arr.size(); 6 | int freq[100005]={0}; 7 | for(i=0;i& left, vector& right) { 4 | int n1=(int)left.size(); 5 | int n2=(int)right.size(); 6 | sort(left.begin(),left.end()); 7 | sort(right.begin(),right.end()); 8 | reverse(left.begin(),left.end()); 9 | int c1=0,c2=n,i,j,k; 10 | if(n1>=1) c1=left[0]; 11 | if(n2>=1) c2=right[0]; 12 | return max(abs(n-c2),abs(0-c1)); 13 | 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/Array/1509. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDifference(vector& a) { 4 | sort(a.begin(),a.end()); 5 | int n=a.size(); 6 | int min1=a[0],max1=a[n-1]; 7 | //chnaging left 3 8 | int diff=2e9+7; 9 | if(a.size()<=3) return 0; 10 | diff=min(max1-a[3],diff); 11 | diff=min(a[n-4]-min1,diff); 12 | diff = min(a[n - 2] - a[2], diff); 13 | diff = min(a[n - 3] - a[1], diff); 14 | return diff; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Array/152. Maximum Product Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& a) { 4 | int prev_max=a[0]; 5 | int prev_min=a[0]; 6 | int curr_max=a[0];int ans=a[0]; 7 | for(int i=1;i& nums) { 4 | int low=0,high=nums.size()-1,i,j,k;int ans=0,n=nums.size(); 5 | while(low<=high){ 6 | int mid=(low+high)/2; 7 | if(mid!=0 && nums[mid]& a) { 4 | int ct1=0; 5 | int c=a[0]; 6 | for(int i=0;i>& rect) { 4 | int i,j,k,n,m,ct=0; 5 | n=rect.size(); 6 | map dict; 7 | for(i=0;isecond;; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Array/1736. Latest Time by Replacing Hidden Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string maximumTime(string s) { 4 | for(int i=0;i> fourSum(vector& a, int target) { 4 | int i,j,k,n=a.size()-1,m,ct=0; 5 | sort(a.begin(),a.end()); 6 | set> vect; 7 | vector> vect2; 8 | for(i=0;i<=n-3;i++){ 9 | for(j=i+1;j<=n-2;j++){ 10 | int start=j+1,end=n; 11 | while(starttarget-a[i]-a[j]) end--; 14 | else if(sum temp; 17 | temp.push_back(a[i]);temp.push_back(a[j]); 18 | temp.push_back(a[start]);temp.push_back(a[end]); 19 | vect.insert(temp); 20 | start++;end--; 21 | } 22 | } 23 | } 24 | } 25 | for(auto it=vect.begin();it!=vect.end();it++){ 26 | vect2.push_back(*it); 27 | } 28 | return vect2; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/Array/1848. Minimum Distance to the Target Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getMinDistance(vector& nums, int target, int start) { 4 | int i,j,k,n,m; 5 | n=nums.size(); 6 | int ans=1e9; 7 | for(i=0;i& a) { 4 | int i,j,k,n=a.size(),m,ct=0,sum=0,pref[500007]={0}; 5 | pref[0]=0;cout<=x) {ans2=mid;high=mid-1;} 19 | else low=mid+1; 20 | } 21 | if(ans2==-1) ans2=1e9; 22 | ans=min(ans2-i,ans); 23 | } 24 | if(pref[n-1]>=s) ans=min(n,ans); 25 | if(ans>1e5) return 0; 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Array/216. Combination Sum III.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int a[]={1,2,3,4,5,6,7,8,9}; 3 | vector> vect;int kt=0; 4 | void ways(int n,vector str){ 5 | if(n<0){ 6 | vector temp; 7 | for(int i=0;i> combinationSum3(int k, int n) { 19 | vector str; 20 | ways(8,str);int ct=0; 21 | vector> ans; 22 | for(int i=0;i<512;i++){ 23 | if(vect[i].size()==k){ 24 | int sum=0; 25 | for(auto x:vect[i]) sum+=x; 26 | if(sum==n){ 27 | ans.push_back(vect[i]); 28 | ct++; 29 | } 30 | } 31 | } 32 | return ans; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode/Array/229. Majority Element II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector majorityElement(vector& a) { 4 | int cand1=-1e9,cand2=-1e9,ct1=0,ct2=0,n=a.size(); 5 | for(int i=0;i vect; 27 | if(ct1>n/3) vect.push_back(cand1); 28 | if(ct2>n/3 && cand1!=cand2) vect.push_back(cand2); 29 | return vect; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Leetcode/Array/238. Product of Array Except Self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | int val=1,i,j,k,n,m,ct=0; 5 | n=nums.size()-1; 6 | vector vect; 7 | vect.push_back(val); 8 | for(i=1;i<=n;i++){ 9 | val=val*nums[i-1]; 10 | vect.push_back(val); 11 | } 12 | int val2=1; 13 | for(j=n;j>=0;j--){ 14 | if(j-1>=0){ 15 | vect[j]=vect[j]*val2; 16 | val2=val2*nums[j]; 17 | } 18 | else{ 19 | vect[j]=val2; 20 | } 21 | } 22 | return vect; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Array/240. Search a 2D Matrix II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& mat, int t) { 4 | int ROW=mat.size(); 5 | int COL=ROW>0 ? mat[0].size() : 0; 6 | int i=0, j=COL-1; 7 | while(i=0) { 8 | if(mat[i][j] == t) 9 | return true; 10 | if(mat[i][j] < t) 11 | i++; 12 | else 13 | j--; 14 | } 15 | return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Array/287. Find the Duplicate Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int slow=nums[0]; 5 | int fast=nums[0];int ct=0; 6 | do{ 7 | slow=nums[slow]; 8 | fast=nums[nums[fast]]; 9 | }while(slow!=fast); 10 | slow=nums[0]; 11 | while(slow!=fast){ 12 | slow=nums[slow]; 13 | fast=nums[fast]; 14 | } 15 | return fast; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Array/324. Wiggle Sort II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void wiggleSort(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | 6 | vector left; 7 | vector right; 8 | 9 | int l=0; 10 | int r = nums.size()-1; 11 | 12 | int mid = l+(r-l)/2; 13 | 14 | for(int i = 0; i<=mid; i++){ 15 | left.push_back(nums[i]); 16 | } 17 | for(int k = mid+1; k& a, int target) { 4 | int n=a.size()-1; 5 | int left=0,right=n; 6 | while(lefta[right]){ 9 | left=mid+1; 10 | } 11 | else right=mid; 12 | } 13 | cout<target) j=mid-1; 18 | else if(a[mid]=0){ 23 | while(i<=j){ 24 | int mid=(i+j)/2; 25 | if(a[mid]>target) j=mid-1; 26 | else if(a[mid]& nums) { 4 | int k,n,m,ct=0; 5 | int low=INT_MAX,mid=INT_MAX; 6 | if(nums.size()<=2) return false; 7 | for(int i=0;imid) return true; 9 | else{ 10 | if(nums[i]low && mid>nums[i]) mid=nums[i]; 12 | } 13 | } 14 | return false; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Array/40. Combination Sum II.cpp: -------------------------------------------------------------------------------- 1 | set> vect3; 2 | void ways(vector a,vector emp,int n,int sum){ 3 | if(sum==0) {sort(emp.begin(),emp.end()); 4 | // for(int i=0;i=a[n]) ways(a,emp,n-1,sum-a[n]); 11 | } 12 | class Solution { 13 | public: 14 | vector> combinationSum2(vector& a, int sum) { 15 | int i,j,k,n=a.size(),m; 16 | vector emp; 17 | vect3.clear(); 18 | ways(a,emp,n-1,sum); 19 | vector> vect; 20 | for(auto it=vect3.begin();it!=vect3.end();it++){ 21 | vect.push_back(*it); 22 | } 23 | return vect; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Leetcode/Array/41. First Missing Positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int i,j,k,n,m; 5 | n=nums.size(); 6 | for(i=0;i0 && nums[i]!=i+1){ 8 | k = nums[nums[i]-1]; 9 | if(k==nums[i]) break; 10 | nums[nums[i]-1]=nums[i]; 11 | nums[i]=k; 12 | } 13 | } 14 | for(i=0;i& height) { 4 | int i,j,k,n,ct=0; 5 | int lmax=-1,rmax=-1; 6 | n=height.size(); 7 | int l=0,r=n-1; 8 | int ans=0; 9 | while(l<=r){ 10 | if(height[l]<=height[r]){ 11 | if(lmax>=height[l]){ 12 | ans+=min(lmax,height[r])-height[l]; 13 | } 14 | else lmax=height[l]; 15 | l++; 16 | } 17 | else{ 18 | if(rmax>=height[r]){ 19 | ans+=min(rmax,height[l])-height[r]; 20 | } 21 | else rmax=height[r]; 22 | r--; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Array/442. Find All Duplicates in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& a) { 4 | int i,j,k,n,m,ct=0; 5 | vector ans; 6 | set dict; 7 | for(i=0;i findDisappearedNumbers(vector& a) { 4 | int i = 0,n;n=a.size(); 5 | while (i < n) { 6 | if (a[i] == i + 1) i++; 7 | else { 8 | int k = a[i]; 9 | int j = a[k - 1]; 10 | if (j != k) { 11 | a[i] = j; 12 | a[k - 1] = k; 13 | } 14 | else if (j == k) { 15 | i++; 16 | } 17 | } 18 | } 19 | vector vect; 20 | //for (i = 0; i < n; i++) cout << a[i] << " "; 21 | //cout << endl; 22 | for (i = 0; i < n; i++) { 23 | if (a[i] != i + 1) vect.push_back(i+1); 24 | } 25 | return vect; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Array/45. Jump Game II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | int i,j,k,n,m,ct=0; 5 | int curr_reach=0,curr_max=0,jump=0; 6 | for(i=0;icurr_max){ 8 | curr_max=i+nums[i]; 9 | } 10 | if(i==curr_reach){ 11 | curr_reach=curr_max; 12 | jump++; 13 | } 14 | } 15 | return jump; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Array/452. Minimum Number of Arrows to Burst Balloons.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | static bool comp(const vector& vec1, const vector& vec2) 5 | { 6 | return vec1[1] < vec2[1]; 7 | } 8 | 9 | int findMinArrowShots(vector>& points) 10 | { 11 | if (points.size() <= 1) 12 | return points.size(); 13 | 14 | sort(points.begin(), points.end(), comp); 15 | 16 | int minNumArrow = 0; 17 | int tmp = points[0][1]; 18 | 19 | for (int i = 1; i < points.size(); i++) 20 | { 21 | minNumArrow += points[i][0] > tmp; 22 | tmp = points[i][0] > tmp ? points[i][1] : tmp; 23 | } 24 | 25 | return minNumArrow + 1; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Array/48. Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& a) { 4 | int i,j,k,n,m; 5 | int col=a[0].size(),row=col;int ct=0,kt=0;; 6 | for(i=0;i& a) { 4 | int i,j,k,n=a.size(),m,ct=0; 5 | int sub[30005]={0}; 6 | sub[n-1]=1;int last=n-1; 7 | for(j=n-2;j>=0;j--){ 8 | int max_jump=a[j]; 9 | if(j+a[j]>=last){ 10 | cout<& nums, int k) { 4 | int sum = 0 ; 5 | map m; 6 | m[0] = 1; 7 | int ans = 0; 8 | for( int i = 0 ; i < nums.size();i++) 9 | { 10 | sum+=nums[i]; 11 | if(m.find(sum-k) != m.end()) 12 | ans+=m[sum-k]; 13 | m[sum]++; 14 | } 15 | return ans; 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Array/5649. Decode XORed Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector decode(vector& b, int first) { 4 | int i,j,k,n; 5 | int m=b.size(); 6 | vector ans; 7 | ans.push_back(first); 8 | for(i=0;i& nums) { 4 | int count = 1; 5 | int min_value = INT_MIN; 6 | for(int i = 0; i < nums.size()-1; i++){ 7 | if(nums[i] <= nums[i + 1]){ 8 | min_value = nums[i]; 9 | }else{ 10 | if(nums[i + 1] < min_value){ 11 | nums[i+1] = nums[i]; 12 | } 13 | count -= 1; 14 | } 15 | } 16 | if(count < 0){ 17 | return false; 18 | }else{ 19 | return true; 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Array/667. Beautiful Arrangement II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructArray(int n, int k) { 4 | int diff,i,j,ct=0; 5 | vector vect; 6 | int start=1,end=1+k; 7 | do{ if(end==start) {vect.push_back(start);break;} 8 | if(ct%2==0) {vect.push_back(start);start++;ct++;} 9 | else {vect.push_back(end);end--;ct++;} 10 | 11 | }while(abs(end-start)>=0); 12 | while(vect.size()!=n){ 13 | vect.push_back(k+2); 14 | k++; 15 | } 16 | return vect; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Array/769. Max Chunks To Make Sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxChunksToSorted(vector& arr) { 4 | int max2=-1,ct=0; 5 | for(int i=0;i& nums) { 4 | int i,j,k,n=(int)nums.size(),ct=0,sum; 5 | int l=2,r; 6 | if(n==0) return 0; 7 | else if(n==1) return 1; 8 | for(r=2;r& A) { 4 | int ans=1; 5 | unordered_set dict2; 6 | for(int i=0;i& a) { 4 | int i,j,k,n=a.size(),m,ct=0; 5 | int b[n],c[n]; 6 | int a1=-1e9,a2=1e9; 7 | for(i=0;i=0;i--){ 12 | a2=min(a2,a[i]); 13 | c[i]=a2; 14 | } 15 | for(i=1;i& a) { 4 | int i,j,k,n=a.size(),m,t=0; 5 | int sum=0,ans=1e9; 6 | for(i=0;i=0;i--){ 15 | int zeros_left=a[i]; 16 | int ones_left=i+1-a[i]; 17 | int zeros_right=a[S.size()-1]-a[i]; 18 | ans=min(ans,ones_left+zeros_right); 19 | } 20 | int a1=a[S.size()-1],a2=S.size()-a[S.size()-1]; 21 | int ans2= min(a1,a2); 22 | ans = min( ans,ans2); 23 | return ans; 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/Array/962. Maximum Width Ramp.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxWidthRamp(vector& a) { 4 | int i,j,k,n=a.size(),m=0,ct=0; 5 | vector> vect; 6 | for(i=0;i 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> n; ll sum = 0, curr_sum = 0, prev_sum = 0, min_sum = sum; 27 | ll a[n]; 28 | for (i = 0; i < n; i++) cin >> a[i]; 29 | sum = a[0]; ans = 0; 30 | for (i = 1; i < n; i++) { 31 | ans = max(ans, max(sum + a[i], a[i])); 32 | sum = max(sum + a[i], a[i]); 33 | } 34 | cout << ans << endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Leetcode/Array/Maximum Eraser value.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumUniqueSubarray(vector& a) { 4 | set dict; 5 | int n=a.size(); 6 | int pref[10005]={0}; 7 | int ans=0; 8 | pref[a[0]]=a[0]; 9 | if(n==1) return a[0]; 10 | int i=0,j=1;int sum=a[0]; 11 | dict.insert(a[0]); 12 | while(i& A, vector& B) { 6 | n1=(int)A.size(),n2=(int)B.size();int ans=0; 7 | for(int i=0;i<=n1;i++){ 8 | for(int j=0;j<=n2;j++) dp[i][j]=0; 9 | } 10 | for(int i=n1-1;i>=0;i--){ 11 | for(int j=n2-1;j>=0;j--){ 12 | if(A[i]==B[j]){ 13 | dp[i][j]=dp[i+1][j+1]+1; 14 | if(ans& nums) { 4 | long long int i,j,k,n=nums.size(),m;long long int ct=1e18; 5 | long long int ans = 1e18; 6 | sort(nums.begin(),nums.end()); 7 | long long int left=0,right=0; 8 | for(i=0;i> vect; 4 | MyCalendar() { 5 | this->vect.clear(); 6 | } 7 | 8 | bool book(int start, int end) { 9 | int flag=0; 10 | for(int i=0;i=high) continue; 13 | else flag=1; 14 | } 15 | if(flag==1) return false; 16 | else this->vect.push_back({start,end}); 17 | return true; 18 | } 19 | }; 20 | 21 | /** 22 | * Your MyCalendar object will be instantiated and called as such: 23 | * MyCalendar* obj = new MyCalendar(); 24 | * bool param_1 = obj->book(start,end); 25 | */ 26 | -------------------------------------------------------------------------------- /Leetcode/Array/Range Sum Query 2D - Immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumMatrix { 2 | public: 3 | int sum[205][205]; 4 | int r1=0,c2=0; 5 | NumMatrix(vector>& matrix) { 6 | int i,j,k,n,m; 7 | n = matrix.size(); 8 | m = matrix[0].size(); 9 | this->sum[0][0] = matrix[0][0]; 10 | for(i=0;isum[i][j+1]=(matrix[i][j]+this->sum[i][j]); 14 | } 15 | } 16 | 17 | int sumRegion(int row1, int col1, int row2, int col2) { 18 | int ans=0,i,j,k,n,m; 19 | for(j=row1;j<=row2;j++){ 20 | ans += sum[j][col2+1]-sum[j][col1]; 21 | } 22 | return ans; 23 | } 24 | }; 25 | 26 | /** 27 | * Your NumMatrix object will be instantiated and called as such: 28 | * NumMatrix* obj = new NumMatrix(matrix); 29 | * int param_1 = obj->sumRegion(row1,col1,row2,col2); 30 | */ 31 | -------------------------------------------------------------------------------- /Leetcode/Array/Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& a) { 4 | int i=0,ct2=a.size()-1,ct0=0,j; 5 | while (i <= ct2 && ct0 < ct2) { 6 | if (a[i] == 0) { 7 | a[i] = a[ct0]; 8 | a[ct0] = 0; 9 | ct0++; 10 | } 11 | if (a[i] == 2) { 12 | a[i] = a[ct2]; 13 | a[ct2] = 2; 14 | ct2--; 15 | if (a[i] == 0) { 16 | a[i] = a[ct0]; 17 | a[ct0] = 0; 18 | ct0++; 19 | } 20 | else i--; 21 | //for (j = 0; j < n; j++) cout << a[j] << " "; 22 | //cout << endl; 23 | 24 | } i++; 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/22. Generate Parentheses.cpp: -------------------------------------------------------------------------------- 1 | vector vect; 2 | int m; 3 | void ways(int ct,int n,string str){ 4 | if(ct==2*n){ 5 | stack st;int flag=0; 6 | for(int i=0;i generateParenthesis(int n) { 25 | int i,j,k,ct=0;m=n; 26 | vect.clear();string str=""; 27 | ways(0,n,str); 28 | return vect; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/50. Pow(x, n).cpp: -------------------------------------------------------------------------------- 1 | double ways(double x,int n){ 2 | if(n==1) return x; 3 | else if(n==-1) return 1/x; 4 | else if(n%2==0) return ways(x,n/2)*ways(x,n/2); 5 | else if(n>=0) return ways(x,n/2)*x*ways(x,n/2); 6 | else return ways(x,n/2)*ways(x,n/2)*1/x; 7 | } 8 | class Solution { 9 | public: 10 | double myPow(double x, int n) { 11 | if(n==0) return 1; 12 | return ways(x,n); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/79. Word Search.cpp: -------------------------------------------------------------------------------- 1 | int n,m,l; 2 | bool ways(vector> b,string w,set> dict,int i,int j,int ind){ 3 | if(ind==w.size()) return true; 4 | if(i<0 || j<0 || i>=n || j>=m) return false; 5 | 6 | else if(dict.find(make_pair(i,j))==dict.end() && w[ind]==b[i][j]){ 7 | dict.insert(make_pair(i,j)); 8 | return (ways(b,w,dict,i+1,j,ind+1)||ways(b,w,dict,i-1,j,ind+1)||ways(b,w,dict,i,j+1,ind+1)||ways(b,w,dict,i,j-1,ind+1)); 9 | } 10 | else return false; 11 | } 12 | class Solution { 13 | public: 14 | bool exist(vector>& b, string w) { 15 | int i,j,k,ct=0; 16 | n=b.size(),m=b[0].size(); 17 | l=w.size(); 18 | for(i=0;i> dict; 21 | if(ways(b,w,dict,i,j,0)==true) return true; 22 | } 23 | } 24 | return false; 25 | 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/DSU/128. Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfSeq(unordered_set& s, int key) 4 | { 5 | int count=1; 6 | if(s.find(key+1)!=s.end()) 7 | { 8 | s.erase(key); 9 | count+=lengthOfSeq(s,key+1); 10 | } 11 | return count; 12 | } 13 | int longestConsecutive(vector& nums) 14 | { 15 | if(nums.size()<=1) 16 | return nums.size(); 17 | unordered_set s; 18 | int seqLen=1; 19 | for(int i=0;i maxSlidingWindow(vector& nums, int k) { 4 | int i,j,n,m,ct=0,t;vector vect; 5 | n=nums.size(); 6 | deque d; 7 | for(i=0;i A,vector B,int st1,int st2){ 5 | if(st1>=A.size()) return 0; 6 | else if(st2>=B.size()) return 0; 7 | else if(dp[st1][st2]==-1){ 8 | if(A[st1]==B[st2]) return dp[st1][st2]=1+ways(A,B,st1+1,st2+1); 9 | else return dp[st1][st2]=max(ways(A,B,st1+1,st2),ways(A,B,st1,st2+1)); 10 | } 11 | else return dp[st1][st2]; 12 | } 13 | int maxUncrossedLines(vector& A, vector& B) { 14 | int i,j,k,n1=A.size(),n2=B.size(),ans=0; 15 | memset(dp,-1,sizeof(dp)); 16 | return ways(A,B,0,0); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1049. Last Stone Weight II.cpp: -------------------------------------------------------------------------------- 1 | int dp[3005][3005]; 2 | class Solution { 3 | public: 4 | int ways(int i,vector stones,int sum1,int sum2){ 5 | if(i==stones.size()) return abs(sum1-sum2); 6 | else if(dp[sum1][sum2]==-1){ 7 | return dp[sum1][sum2]=min(ways(i+1,stones,sum1+stones[i],sum2),ways(i+1,stones,sum1,sum2+stones[i])); 8 | } 9 | return dp[sum1][sum2]; 10 | } 11 | int lastStoneWeightII(vector& stones) { 12 | int i,j,k,n,m,ct=0; 13 | n=stones.size(); 14 | memset(dp,-1,sizeof(dp)); 15 | return ways(0,stones,0,0); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1140. Stone Game II.cpp: -------------------------------------------------------------------------------- 1 | int ways(int a[], int i, int n, int m) { 2 | if (i >= n) return 0; 3 | else { 4 | int st = i, en = min(n - 1, i + 2 * m - 1); 5 | int sum = 0, max3 = 0;; 6 | for (int j = st; j <= en; j++) { 7 | int max2 = 0; 8 | sum += a[j]; int ans = sum; 9 | int taken = j - st + 1; 10 | int skip = max(m, taken); 11 | int end = min(n, j + 2 * skip + 1); 12 | //cout << st << " " << k << " " << skip << endl; 13 | max2 = max(max2, ways(a, end, n,2 * skip)); 14 | //cout << ways(a, i + k + 1, n, skip) << " " << i + k + 1 << " " << endl; 15 | ans += max2; 16 | max3 = max(ans, max3); 17 | } 18 | return max3; 19 | } 20 | } 21 | class Solution { 22 | public: 23 | int stoneGameII(vector& piles) { 24 | int a[piles.size()]; 25 | for(int i=0;i piles,int i,int flag,int m){ 5 | if(i==piles.size()) return dp[i][m][flag]=0; 6 | else if(dp[i][m][flag]==-1){ 7 | int sum=0;int ans=0; 8 | if(flag==0) ans=0; 9 | else ans = 1e9; 10 | int k=piles.size(); 11 | for(int j=i;j& piles) { 21 | int i,j,k,n,m,ct=0; 22 | memset(dp,-1,sizeof(dp)); 23 | return ways(piles,0,0,1); 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1262. Greatest Sum Divisible by Three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSumDivThree(vector& nums) { 4 | int i,j,k,n,m; 5 | n = nums.size(); 6 | int dp[n+1][3]; 7 | memset(dp,0,sizeof(dp)); 8 | dp[n-1][nums[n-1]%3] = nums[n-1]; 9 | //cout<=0;i--){ 11 | dp[i][0] = dp[i+1][0]; 12 | dp[i][1] = dp[i+1][1]; 13 | dp[i][2] = dp[i+1][2]; 14 | for(j=0;j<3;j++){ 15 | int res = (dp[i+1][j]+nums[i])%3; 16 | dp[i][res] = max(dp[i+1][j]+nums[i],dp[i][res]); 17 | } 18 | } 19 | return dp[0][0]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1312. Minimum Insertion Steps to Make a String Palindrome.cpp: -------------------------------------------------------------------------------- 1 | int dp[505][505]; 2 | class Solution { 3 | public: 4 | int lcs(string s,string r,int i1,int i2){ 5 | for(int i1=s.size()-1;i1>=0;i1--){ 6 | for(int i2=r.size()-1;i2>=0;i2--){ 7 | if(s[i1]==r[i2]) dp[i1][i2]=dp[i1+1][i2+1]+1; 8 | else dp[i1][i2] = max(dp[i1+1][i2],dp[i1][i2+1]); 9 | } 10 | } 11 | return dp[0][0]; 12 | } 13 | int minInsertions(string s) { 14 | int i,j,k,n,m,ct=0,ans; 15 | n=s.size();ans=n; 16 | string r = s; 17 | memset(dp,0,sizeof(dp)); 18 | reverse(r.begin(),r.end()); 19 | m = lcs(s,r,0,0); 20 | return n-m; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/139. Word Break.cpp: -------------------------------------------------------------------------------- 1 | long long int max1=1e9+7; 2 | class Solution { 3 | public: 4 | bool wordBreak(string s, vector& wordDict) { 5 | long long int i,j,k,n,m,ct=0;set dict; 6 | for(i=0;i0) dp[i]+=dp[j-1]%max1; 18 | else dp[i]+=1; 19 | } 20 | } 21 | } 22 | cout<0) return true; 24 | else return false; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1406. Stone Game III.cpp: -------------------------------------------------------------------------------- 1 | int max1=20000000; 2 | int dp[50005]; 3 | int ways(vector a,int i){ 4 | if(i>=a.size()) return 0; 5 | else if(dp[i]==-1e9){ 6 | dp[i]=a[i]-ways(a,i+1); 7 | if(i+1<=a.size()-1) dp[i]=max(dp[i],a[i]+a[i+1]-ways(a,i+2)); 8 | if(i+2<=a.size()-1) dp[i]=max(dp[i],a[i]+a[i+1]+a[i+2]-ways(a,i+3)); 9 | return dp[i]; 10 | } 11 | else return dp[i]; 12 | } 13 | class Solution { 14 | public: 15 | string stoneGameIII(vector& a) { 16 | int i,j,k,n=a.size(); 17 | for(i=0;i<=a.size()+4;i++) dp[i]=0; 18 | i=n-1; 19 | while(i>=0){ 20 | if(i>=a.size()) dp[i]=0; 21 | else{ 22 | dp[i]=a[i]-dp[i+1]; 23 | if(i+1<=a.size()-1) dp[i]=max(dp[i],a[i]+a[i+1]-dp[i+2]); 24 | if(i+2<=a.size()-1) dp[i]=max(dp[i],a[i]+a[i+1]+a[i+2]-dp[i+3]); 25 | } 26 | i--; 27 | } 28 | int val=dp[0]; 29 | if(val==0) return "Tie"; 30 | else if(val>0) return "Alice"; 31 | else return "Bob"; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1510. Stone Game IV.cpp: -------------------------------------------------------------------------------- 1 | int dp[100005]; 2 | bool player1(int n){ 3 | if(n<=0) return false; 4 | else if(dp[n]==-1){ 5 | for(int j=1;j*j<=n;j++){ 6 | if(player1(n-j*j)==false) {dp[n]=1;return true;} 7 | } 8 | dp[n]=0; 9 | return false; 10 | } 11 | else if(dp[n]==0) return false; 12 | else return true; 13 | } 14 | class Solution { 15 | public: 16 | bool winnerSquareGame(int n) { 17 | int i,j,k,m,ct=0; 18 | for(i=0;i<=n+2;i++) dp[i]=-1; 19 | if(n==1) return true; 20 | return player1(n); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1553. Minimum Number of Days to Eat N Oranges.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int ways(int n){ 4 | if(n==0) return 0; 5 | else if(n==1) return 1; 6 | else if(n==2 || n==3) return 2; 7 | else return 1+min(n%2+ways(n/2),n%3+ways(n/3)); 8 | } 9 | int minDays(int n) { 10 | int ans=ways(n); 11 | return ans; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1563. Stone Game V.cpp: -------------------------------------------------------------------------------- 1 | int dp[505][505],sum[505][505]; 2 | int ways(int st,int en,vector a){ 3 | if(st>en) return 0; 4 | else if(dp[st][en]==-1){ 5 | int ans=0; 6 | for(int j=st;j& a) { 21 | int i,j,k,n=a.size(),m,ct=0; 22 | for(i=0;i<=n+2;i++){ 23 | for(j=0;j<=n+2;j++) {dp[i][j]=-1;sum[i][j]=0;} 24 | } 25 | for(i=0;i& a, int k) { 4 | int i,j,n=a.size(),m,ct=0; 5 | // int ans=-1e9; 6 | multiset dict; 7 | dict.insert(a[n-1]); 8 | int dp[100005]={0}; 9 | dp[n-1]=a[n-1]; 10 | for(i=n-2;i>=0;i--){ 11 | if(dict.size()<=k){ 12 | auto it=dict.end(); 13 | it--; 14 | int sum=*it; 15 | dp[i]=a[i]+sum; 16 | dict.insert(dp[i]); 17 | } 18 | else{ 19 | dict.erase(dp[i+k+1]); 20 | auto it=dict.end(); 21 | it--; 22 | int sum=*it; 23 | dp[i]=a[i]+sum; 24 | dict.insert(dp[i]); 25 | } 26 | } 27 | return dp[0]; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1770. Maximum Score from Performing Multiplication Operations.cpp: -------------------------------------------------------------------------------- 1 | int dp[1005][1005]; 2 | class Solution { 3 | public: 4 | int ways(vector nums,vector m,int st,int en){ 5 | if(st+en>=m.size()) return 0; 6 | else if(dp[st][en]==-1){ 7 | int i1 = st+en; 8 | int t = nums.size()-1; 9 | return dp[st][en]= max(m[i1]*nums[st] + ways(nums,m,st+1,en),m[i1]*nums[t-en]+ways(nums,m,st,en+1)); 10 | } 11 | else return dp[st][en]; 12 | } 13 | int maximumScore(vector& nums, vector& multipliers) { 14 | int i,j,k,n=nums.size(),m=multipliers.size(),ct=0; 15 | for(i=0;i<=m+3;i++){ 16 | for(j=0;j<=m+3;j++) dp[i][j]=-1; 17 | } 18 | return ways(nums,multipliers,0,0); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/1871. Jump Game VII.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canReach(string s, int minJ, int maxJ) { 4 | int n = s.size(), pre = 0; 5 | vector dp(n, false); 6 | dp[0] = true; 7 | for (int i = 1; i < n; ++i) { 8 | if (i >= minJ) 9 | pre += dp[i - minJ]; 10 | if (i > maxJ) 11 | pre -= dp[i - maxJ - 1]; 12 | dp[i] = pre > 0 && s[i] == '0'; 13 | } 14 | return dp[n - 1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/198. House Robber.cpp: -------------------------------------------------------------------------------- 1 | int dp[105][2]; 2 | int ways(vector nums,int i,int flag){ 3 | if(i==nums.size()) return 0; 4 | else if(dp[i][flag]==-1){ 5 | if(flag==1) return dp[i][flag]=ways(nums,i+1,0); 6 | else return dp[i][flag]=max(nums[i]+ways(nums,i+1,1),ways(nums,i+1,0)); 7 | } 8 | else return dp[i][flag]; 9 | } 10 | class Solution { 11 | public: 12 | int rob(vector& nums) { 13 | int i,j,k,n,m,ct=0; 14 | memset(dp,-1,sizeof(dp)); 15 | return ways(nums,0,0); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/213. House Robber II.cpp: -------------------------------------------------------------------------------- 1 | int dp[105][105][2]; 2 | int ways(vector a,int i,int last,int n,int flag){ 3 | if(i>=n) return 0; 4 | else if(dp[i][last][flag]==-1){ 5 | int k=last; 6 | if(last==104){ 7 | int j;j=flag; 8 | if(i==0) j=0; 9 | return dp[i][last][flag]=max(ways(a,i+1,last,n,flag),ways(a,i+2,i,n,j)+a[i]);} 10 | else{ 11 | if(i-1==last || (flag==0 && i==n-1)) return dp[i][last][flag]=ways(a,i+1,last,n,flag); 12 | else return dp[i][last][flag]=max(ways(a,i+1,last,n,flag),ways(a,i+1,i,n,flag)+a[i]); 13 | } 14 | } 15 | else return dp[i][last][flag]; 16 | } 17 | class Solution { 18 | public: 19 | int rob(vector& nums) { 20 | int n=nums.size(); 21 | 22 | memset(dp,-1,sizeof(dp)); 23 | return ways(nums,0,104,n,1); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/279. Perfect Squares.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSquares(int n) { 4 | vector dp(n+1,n); 5 | 6 | dp[0]=0; 7 | dp[1]=1; 8 | 9 | for(int i=1;i<=n;i++) 10 | { 11 | for(int j=1;j*j<=i;j++) 12 | { 13 | dp[i] =min(dp[i],dp[i-(j*j)]+1); 14 | } 15 | } 16 | 17 | return dp[n]; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/300. Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | int dp[4002][4002]; 2 | vector a; 3 | int ways(int i,int last){ 4 | if(i==a.size()) return 0; 5 | else if(dp[i][last]==-1){ 6 | if(last==4000) return dp[i][last]=max(ways(i+1,last),ways(i+1,i)+1); 7 | else{ 8 | int val1=0,val2; 9 | if(a[last]& nums) { 19 | ios_base::sync_with_stdio(false); 20 | cin.tie(NULL);a.clear(); 21 | for(int i=0;i a,int i,int flag){ 3 | if(i>=a.size()) return 0; 4 | else if(dp[i][flag]==-1){ 5 | if(flag==0){ 6 | return dp[i][flag]=max(ways(a,i+1,1)-a[i],ways(a,i+1,0)); 7 | } 8 | else if(flag==1){ 9 | return dp[i][flag]=max(ways(a,i+2,0)+a[i],ways(a,i+1,1)); 10 | } 11 | } 12 | else return dp[i][flag]; 13 | return 0; 14 | } 15 | class Solution { 16 | public: 17 | int maxProfit(vector& prices) { 18 | memset(dp,-1,sizeof(dp)); 19 | return ways(prices,0,0); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/338. Counting Bits: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | int i,j,k; 5 | vector vect; 6 | vect.push_back(0); 7 | if(n==0) return vect; 8 | vect.push_back(1); 9 | if(n==1) return vect; 10 | vect.push_back(1); 11 | if(n==2) return vect; 12 | for(i=3;i<=n;i++){ 13 | j=(int)(log2(i)); 14 | j=pow(2,j); 15 | cout<=vect.size()) vect.push_back(1); 17 | else{ 18 | vect.push_back(vect[j]+vect[i-j]); 19 | } 20 | } 21 | return vect; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/377. Combination Sum IV.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int combinationSum4(vector& nums, int target) { 4 | vector dp(target + 1, 0); 5 | // zero sum is always possible 6 | dp[0] = 1; 7 | 8 | // finds all permutations 9 | for(int i = 1; i <= target; i++) 10 | for(const int& num : nums) 11 | dp[i] += (i >= num) ? dp[i - num] : 0; 12 | 13 | return dp[target]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/46. Permutations.cpp: -------------------------------------------------------------------------------- 1 | int inf=1e9,n; 2 | vector> vect; 3 | void ways(set ans,vector vect1){ 4 | //if(ans.size()==0) return; 5 | if(vect1.size()==n) { 6 | //for(int i=0;i> permute(vector& nums) { 26 | vect.clear(); 27 | set ans;vector vect1;n=nums.size(); 28 | for(int i=0;i,int> dict; 4 | int ways(int i,int ans){ 5 | if(i==n){ 6 | if(ans==t) return dict[{i,ans}]=1; 7 | else return dict[{i,ans}]=0; 8 | } 9 | else if(dict[{i,ans}]==-1){ 10 | return dict[{i,ans}]=ways(i+1,ans+a[i])+ways(i+1,ans-a[i]); 11 | } 12 | else return dict[{i,ans}]; 13 | } 14 | class Solution { 15 | public: 16 | int findTargetSumWays(vector& nums, int S) { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | int i,j,m; 20 | t=S; 21 | int sum=0; 22 | for(i=0;i arr, int l, int m, int h) 2 | { // Include elements on left of mid. 3 | int sum = 0; 4 | int left_sum = INT_MIN; 5 | for (int i = m; i >= l; i--) 6 | { 7 | sum = sum + arr[i]; 8 | if (sum > left_sum) 9 | left_sum = sum; 10 | } 11 | // Include elements on right of mid 12 | sum = 0; 13 | int right_sum = INT_MIN; 14 | for (int i = m+1; i <= h; i++) 15 | { 16 | sum = sum + arr[i]; 17 | if (sum > right_sum) 18 | right_sum = sum; 19 | } 20 | return max(left_sum + right_sum, max(left_sum, right_sum)); 21 | } 22 | int fun(vector nums,int st,int en){ 23 | if(st==en) return nums[st]; 24 | else{ 25 | int mid=(st+en)/2; 26 | int j= max(fun(nums,st,mid),fun(nums,mid+1,en)); 27 | int k=oth(nums,st,mid,en); 28 | return max(j,k); 29 | } 30 | } 31 | class Solution { 32 | public: 33 | int maxSubArray(vector& nums) { 34 | int n=nums.size()-1; 35 | return fun(nums,0,n); 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/552. Student Attendance Record II.cpp: -------------------------------------------------------------------------------- 1 | long long mod=1e9+7; 2 | long long dp[100005][2][3]; 3 | long long ways(int i,int a,int l,int n){ 4 | if(l==3) return 0; 5 | else if(a==2) return 0; 6 | else if(i==n) return 1; 7 | else if(dp[i][a][l]==-1){ 8 | if(a==1 && l==2) return dp[i][a][l]=ways(i+1,a,0,n)%mod; 9 | else if(a==1 && l<2) return dp[i][a][l]=(ways(i+1,a,0,n)%mod+ways(i+1,a,l+1,n)%mod)%mod; 10 | else if(a==0 && l==2) return dp[i][a][l]=(ways(i+1,a,0,n)%mod+ways(i+1,a+1,0,n)%mod)%mod; 11 | else if(a==0 && l<2) return dp[i][a][l]=(ways(i+1,a,l+1,n)%mod+ways(i+1,a,0,n)%mod+ways(i+1,1,0,n)%mod); 12 | return 0; 13 | } 14 | else return dp[i][a][l]%mod; 15 | } 16 | class Solution { 17 | public: 18 | long long checkRecord(int n) { 19 | int i,j,k,m,ct=0; 20 | for(i=0;i<=n+2;i++){ 21 | for(j=0;j<=1;j++){ 22 | for(k=0;k<=2;k++) dp[i][j][k]=-1; 23 | } 24 | } 25 | return ways(0,0,0,n)%mod; 26 | //return k; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/600. Non-negative Integers without Consecutive Ones.cpp: -------------------------------------------------------------------------------- 1 | typedef int ll; 2 | class Solution { 3 | public: 4 | int findIntegers(int num) { 5 | int a[32]={0}; 6 | a[0]=1;a[1]=2; 7 | for(int i=2;i<=30;i++) a[i]=a[i-1]+a[i-2]; 8 | int i,prev_bit; 9 | i=30;prev_bit=0;int ans=0; 10 | while(i>=0){ 11 | if((num & 1<> a){ 4 | if(i==n-1 && j==m-1) return dp[i][j]=a[i][j]; 5 | else if(dp[i][j]==-1){ 6 | int ans=1e9; 7 | if(i!=n-1) ans=min(ans,a[i][j]+dfs(i+1,j,a)); 8 | if(j!=m-1) ans=min(ans,a[i][j]+dfs(i,j+1,a)); 9 | return dp[i][j]=ans; 10 | } 11 | else return dp[i][j]; 12 | } 13 | class Solution { 14 | public: 15 | int minPathSum(vector>& a) { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int i,j,k,ct=0; 19 | ans=1e9; 20 | m=a[0].size(); 21 | n=a.size(); 22 | for(i=0;i& prices, int fee) { 6 | int i,j,k,m,ct=0; 7 | n=prices.size(); 8 | dp[n][0]=0; 9 | dp[n][1]=-1e9; 10 | for(i=n-1;i>=0;i--){ 11 | for(j=0;j<=1;j++){ 12 | if(j==0){ 13 | dp[i][0]=max(dp[i+1][0],dp[i+1][1]-prices[i]); 14 | } 15 | else{ 16 | dp[i][1]=max(dp[i+1][1],dp[i+1][0]+prices[i]-fee); 17 | } 18 | } 19 | } 20 | return dp[0][0]; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/72. Edit Distance.cpp: -------------------------------------------------------------------------------- 1 | int n1,n2; 2 | int dp[505][505]; 3 | int ways(int st1,int st2,string w1,string w2){ 4 | if(st2==n2) return n1-st1; 5 | else if(st1==n1){ 6 | return n2-st2; 7 | } 8 | else if(dp[st1][st2]==-1){ 9 | if(w1[st1]==w2[st2]) return dp[st1][st2]=ways(st1+1,st2+1,w1,w2); 10 | else return dp[st1][st2]=min(1+ways(st1,st2+1,w1,w2),min(1+ways(st1+1,st2,w1,w2),1+ways(st1+1,st2+1,w1,w2))); 11 | } 12 | else return dp[st1][st2]; 13 | } 14 | class Solution { 15 | public: 16 | int minDistance(string word1, string word2) { 17 | n1=word1.size(); 18 | n2=word2.size(); 19 | for(int i=0;i<=n1;i++){ 20 | for(int j=0;j<=n2;j++) dp[i][j]=-1; 21 | } 22 | if(n1==0) return n2; 23 | return ways(0,0,word1,word2); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/871. Minimum Number of Refueling Stops_recursive.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | int ways(long long int target,long long int curr,vector> vect,int ind){ 5 | if(ind>=vect.size()){ 6 | if(curr-target>=0) return 0; 7 | else return 1000; 8 | } 9 | else{ 10 | long long int new_target = target-vect[ind][0]; 11 | long long int new_curr = curr-vect[ind][0]; 12 | if(new_target<=0) return 1; 13 | if(new_curr<0) return 1000; 14 | int m = ways(new_target,new_curr,vect,ind+1); 15 | int n = ways(new_target,new_curr+vect[ind][1],vect,ind+1)+1; 16 | return min(m,n); 17 | } 18 | } 19 | int minRefuelStops(int target, int startFuel, vector>& stations) { 20 | int i,j,k,n,m,ct=0; 21 | n=stations.size(); 22 | for(i=n-1;i>=1;i--) stations[i][0]=stations[i][0]-stations[i-1][0]; 23 | int ans=ways(target,startFuel,stations,0); 24 | if(ans>=1e3) return -1; 25 | else return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/940. Distinct Subsequences II.cpp: -------------------------------------------------------------------------------- 1 | long long int mod = 1e9+7; 2 | class Solution { 3 | public: 4 | int distinctSubseqII(string s) { 5 | map dict; 6 | long long int dp[s.size()+1]; 7 | memset(dp,0,sizeof(dp)); 8 | dp[0]=1; 9 | for(long long int i=1;i<=s.size();i++){ 10 | if(dict.find(s[i-1])==dict.end()){ 11 | dict.insert({s[i-1],i-1}); 12 | dp[i] = (2*dp[i-1])%mod; 13 | } 14 | else{ 15 | auto k = dp[dict[s[i-1]]]; 16 | dp[i] = ((2*dp[i-1])%mod+mod-k)%mod; 17 | dict[s[i-1]]=i-1; 18 | } 19 | // cout< a) { 3 | if (i >= n || i==-1) return 0; 4 | else { 5 | //for 1 day pass 6 | if (dp[i] == -1) { 7 | int d1 = i + 1, d2 = -1, d3 = -1; 8 | int fin1 = a[i] + 6, fin2 = a[i] + 29, fin3 = a[i] + 1; 9 | for (int k = i + 1; k < n; k++) { 10 | if (a[k] > fin1) { 11 | d2 = k; 12 | break; 13 | } 14 | } 15 | for (int k = i + 1; k < n; k++) { 16 | if (a[k] > fin2) { 17 | d3 = k; 18 | break; 19 | } 20 | } 21 | return dp[i] = min(ways(d2,a) + b[1], min(ways(d3,a) + b[2], ways(d1,a) + b[0])); 22 | } 23 | else return dp[i]; 24 | } 25 | } 26 | class Solution { 27 | public: 28 | int mincostTickets(vector& days, vector& costs) { 29 | for(int i=0;i<3;i++) b[i]=costs[i]; 30 | int i; 31 | n=days.size(); 32 | memset(dp,-1,sizeof(dp)); 33 | return ways(0,days); 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/Burst Ballons.cpp: -------------------------------------------------------------------------------- 1 | typedef int ll; 2 | ll max1 = (ll)1e9, n, dp[505][505], a[505]; 3 | ll ways(ll st, ll en) { 4 | if (st > en) return 0; 5 | else if (dp[st][en] == -1) { 6 | ll max2 = 0; 7 | //cout << max2 << endl; 8 | for (ll j = st; j <= en; j++) { 9 | ll ans; 10 | int k=a[j]*a[st-1]; 11 | ans = ways(st, j - 1) + ways(j + 1, en) + k*a[en + 1]; 12 | max2 = max(max2, ans); 13 | } 14 | return dp[st][en] = max2; 15 | } 16 | else return dp[st][en]; 17 | } 18 | class Solution { 19 | public: 20 | int maxCoins(vector& nums) { 21 | memset(dp, -1, sizeof(dp));a[0]=1; 22 | for(int i=0;i> dict; 2 | void ways(int i,vector a,int sum,vector vect){ 3 | if(i>=a.size()) return; 4 | else{ 5 | if(sum-a[i]>=0){ 6 | ways(i+1,a,sum,vect); 7 | vect.push_back(a[i]); 8 | if(sum-a[i]==0){ 9 | sort(vect.begin(),vect.end()); 10 | dict.insert(vect); 11 | } 12 | else {ways(i,a,sum-a[i],vect);} 13 | } 14 | else ways(i+1,a,sum,vect); 15 | } 16 | } 17 | class Solution { 18 | public: 19 | vector> combinationSum(vector& candidates, int target) { 20 | dict.clear(); 21 | int i,j,k,n,m,ct=0; 22 | set dict2; 23 | vector vect; 24 | ways(0,candidates,target,vect); 25 | vector> ans; 26 | for(auto it=dict.begin();it!=dict.end();it++){ 27 | // ans.push_back(*it); 28 | ans.push_back(*it); 29 | // cout<=s1.size() || j>=s2.size()) return 0; 6 | else if(dp[i][j]==-1){ 7 | if(s1[i]==s2[j]) return dp[i][j]=ways(i+1,j+1,s1,s2)+1; 8 | return dp[i][j]=max(ways(i+1,j,s1,s2),ways(i,j+1,s1,s2)); 9 | } 10 | else return dp[i][j]; 11 | } 12 | int minDistance(string word1, string word2) { 13 | int i,j,k,n,m,ct=0; 14 | int n1,n2; 15 | n1 = word1.size(); 16 | n2 = word2.size(); 17 | for(i=0;i<=n1;i++){ 18 | for(j=0;j<=n2;j++) dp[i][j]=-1; 19 | } 20 | k = ways(0,0,word1,word2); 21 | return (n1+n2-2*k); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/Matchsticks to Square.cpp: -------------------------------------------------------------------------------- 1 | typedef long long ll; 2 | class Solution { 3 | public: 4 | bool ways(vector match,ll i,ll s1,ll s2,ll s3,ll s4){ 5 | if(s1==0 && s2==0 && s3==0 && s4==0 && i==match.size()) return true; 6 | else if(i>=match.size()) return false; 7 | else if(s1<0 || s2<0 || s3<0 || s4<0) return false; 8 | else{ 9 | return (ways(match,i+1,s1-match[i],s2,s3,s4) or ways(match,i+1,s1,s2-match[i],s3,s4) or ways(match,i+1,s1,s2,s3-match[i],s4) or ways(match,i+1,s1,s2,s3,s4-match[i])); 10 | } 11 | } 12 | bool makesquare(vector& match) { 13 | ll i,j,k,n,m,ct=0,sum=0; 14 | n= match.size(); 15 | for(i=0;i& cardPoints, int k) { 4 | int i,j,n=cardPoints.size(),m,ct=0; 5 | int st_sum[n+1],en_sum[n+1]; 6 | memset(st_sum,0,sizeof(st_sum)); 7 | memset(en_sum,0,sizeof(en_sum)); 8 | for(i=0;i=0;i--) en_sum[n-i]+=en_sum[n-i-1]+cardPoints[i]; 10 | int ans=0; 11 | for(i=0;i<=k;i++){ 12 | ans = max(ans,st_sum[i]+en_sum[k-i]); 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/Submatices with 1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubmat(vector>& mat) { 4 | int res = 0; 5 | int m = mat.size(); 6 | int n = mat[0].size(); 7 | vector> dp(m, vector(n ,0)); 8 | 9 | for(int i=0;i=0)?dp[i][j-1]+1:1; 15 | int min_len = dp[i][j]; 16 | res += min_len; 17 | for(int k=i-1;k>=0;k--){ 18 | min_len = min(min_len, dp[k][j]); 19 | res += min_len; 20 | } 21 | } 22 | } 23 | 24 | 25 | 26 | return res; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/best-time-to-buy-and-sell-stock-iii.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/submissions/ 2 | int dp[100005][3][3],n; 3 | vector a; 4 | int ways(int i,int brought,int ct){ 5 | if(ct>=2 && brought==0) return 0; 6 | else if(ct>=2 && brought==1) return -2e9; 7 | else if(i==n && brought==0) return 0; 8 | else if(i==n && brought==1) return -2e9; 9 | else if(dp[i][brought][ct]==-1){ 10 | if(brought==1){ 11 | return dp[i][brought][ct]=max(ways(i+1,0,ct+1)+a[i],ways(i+1,1,ct)); 12 | } 13 | else{ 14 | return dp[i][brought][ct]=max(ways(i+1,1,ct)-a[i],ways(i+1,0,ct)); 15 | } 16 | } 17 | else return dp[i][brought][ct]; 18 | } 19 | class Solution { 20 | public: 21 | int maxProfit(vector& prices) { 22 | n=prices.size(); 23 | a.clear(); 24 | memset(dp,-1,sizeof(dp)); 25 | for(int i=0;i nums1,vector nums2,int m,int i){ 5 | if(i==nums1.size() && m==0) return 0; 6 | else if(i==nums1.size() && m!=0) return 1e9; 7 | if(m==0) return 0; 8 | else if(dp[i][m]==-1){ 9 | int ans = 1e9; 10 | for(int j=0;j& nums1, vector& nums2) { 22 | int i,j,k,n,m,ct=0; 23 | n = nums1.size(); 24 | m = pow(2,n)-1; 25 | for(i=0;i<=n;i++){ 26 | for(j=0;j<=m+2;j++) dp[i][j]=-1; 27 | } 28 | return ways(nums1,nums2,m,0); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/Gaph/1319. Number of Operations to Make Network Connected.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int find_parent(int arr[],int i){ 4 | while(arr[i]!=i) i=arr[i]; 5 | return i; 6 | } 7 | void join(int arr[],int x,int y){ 8 | int m1 = find_parent(arr,x); 9 | int m2 = find_parent(arr,y); 10 | arr[m1] = m2; 11 | } 12 | int makeConnected(int n, vector>& c) { 13 | int i,j,k,n1,m; 14 | n1=c.size(); 15 | int ct=0; 16 | int arr[n]; 17 | for(i=0;ict+1) return -1; 32 | else return kt-1; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode/Gaph/1557. Minimum Number of Vertices to Reach All Nodes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findSmallestSetOfVertices(int n, vector>& edges) { 4 | int freq_in[500005]={0};int max1=0; 5 | for(int i=0;i vect; 10 | for(int i=0;i<=max1;i++) if(freq_in[i]==0) vect.push_back(i); 11 | return vect; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Leetcode/Gaph/200. Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | int n,m; 2 | void dfs(vector>& grid,int i,int j){ 3 | grid[i][j]='0'; 4 | if(i+1!=n && grid[i+1][j]=='1') dfs(grid,i+1,j); 5 | if(j+1!=m && grid[i][j+1]=='1') dfs(grid,i,j+1); 6 | if(i!=0 && grid[i-1][j]=='1' ) dfs(grid,i-1,j); 7 | if(j!=0 && grid[i][j-1]=='1') dfs(grid,i,j-1); 8 | } 9 | class Solution { 10 | public: 11 | int numIslands(vector>& grid) { 12 | int i,j,k,ct=0; 13 | n=grid.size();m=grid[0].size(); 14 | 15 | for(i=0;i>& prerequisites) { 4 | int i,j,k,n,m,ct=0; 5 | n=num;m=prerequisites.size(); 6 | int indeg[n+1];vector adj[n+1]; 7 | for(i=0;i<=n;i++) indeg[i]=0; 8 | for(i=0;i q; 13 | for(i=0;i<=n-1;i++){ 14 | if(indeg[i]==0) q.push(i); 15 | } 16 | if(q.empty()==true) return false; 17 | while(q.empty()==false){ 18 | for(auto x:adj[q.front()]) { 19 | indeg[x]--; 20 | if(indeg[x]==0) q.push(x); 21 | } 22 | {q.pop();ct++;} 23 | } 24 | if(ct==n) return true; 25 | else return false; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Gaph/210. Course Schedule II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findOrder(int num, vector>& prerequisites) { 4 | int i,j,k,n,m,ct=0; 5 | vector vect; 6 | n=num;m=prerequisites.size(); 7 | int indeg[n+1];vector adj[n+1]; 8 | for(i=0;i<=n;i++) indeg[i]=0; 9 | for(i=0;i q; 14 | for(i=0;i<=n-1;i++){ 15 | if(indeg[i]==0) q.push(i); 16 | } 17 | while(q.empty()==false){ 18 | for(auto x:adj[q.front()]) { 19 | indeg[x]--; 20 | if(indeg[x]==0) q.push(x); 21 | } 22 | vect.push_back(q.front()); 23 | q.pop(); 24 | ct++; 25 | } 26 | if(vect.size()==n) return vect; 27 | else { 28 | vect.clear(); 29 | return vect; 30 | } 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Leetcode/Greedy/1007. Minimum Domino Rotations For Equal Row.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDominoRotations(vector& a, vector& b) { 4 | int i,j,k,n,m;n= a.size(); 5 | int freq[7]={0}; 6 | for(i=0;i dict; 14 | for(i=1;i<=6;i++){ 15 | if(freq[i]==n) dict.insert(i); 16 | } 17 | int ans=1e9; 18 | if(dict.size()==0) return -1; 19 | for(auto it=dict.begin();it!=dict.end();it++){ 20 | int val=*it; 21 | int ct=0,kt=0; 22 | for(i=0;i=kt){ 28 | ans=min(ans,kt); 29 | } 30 | else ans=min(ans,ct); 31 | } 32 | return ans; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode/Greedy/1338. Reduce Array Size to The Half.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSetSize(vector& arr) { 4 | int i,j,k,n,m,ct=0; 5 | int freq[100005]={0}; 6 | n=arr.size(); 7 | for(i=0;i=0;i--){ 11 | sum+=freq[i]; 12 | if(sum>=n/2){ 13 | k=i; 14 | break; 15 | } 16 | } 17 | // cout<<100004-k+1<& ratings) { 4 | int i,j,k,n,m,ct=0; 5 | n=ratings.size(); 6 | int left[n],right[n]; 7 | left[0]=1; 8 | right[n-1]=1; 9 | for(i=1;i=0;i--){ 14 | if(ratings[i+1] A,pair B){ 2 | if(A.first!=B.first) return A.first>& intervals) { 9 | int i,j,k,n,m,ct=0; 10 | n=intervals.size(); 11 | vector> vect; 12 | for(i=0;i& a, int K) { 4 | int i,j,k,n,m,ct=0; 5 | n= a.size(); 6 | sort(a.begin(),a.end()); 7 | int min1=1e9; 8 | if(n==1) return 0; 9 | min1=min(a[n-1]-a[0],min1); 10 | for(i=0;i& target) { 4 | long long int i,j,k,n,m,sum=0; 5 | n = target.size(); 6 | priority_queue,vector>> pq; 7 | for(i=0;i=m && sum!=1) return false; 18 | int val = m%sum; 19 | if(val==0 && sum==1) return true; 20 | if(val<=0 || sum<=0) return false; 21 | sum+=val; 22 | pq.push(make_pair(val,i)); 23 | } 24 | return true; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/Heap/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/ 2 | class Solution { 3 | public: 4 | int kthSmallest(vector>& mat, int k) { 5 | priority_queue,greater> pq; 6 | int n = mat.size(),m=mat[0].size(),i,j; 7 | for(i=0;i vect; 10 | while(pq.empty()==false){ 11 | int y = pq.top(); 12 | pq.pop(); 13 | for(j=0;j& nums, int k) { 4 | int i,j,n,m,ct=0; 5 | priority_queue> pq(nums.begin(),nums.end()); 6 | while(ct!=k){ 7 | int val=pq.top(); 8 | pq.pop(); 9 | ct++; 10 | if(ct==k) return val; 11 | } 12 | return 0; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Heap/378. Kth Smallest Element in a Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | typedef pair> pi; 2 | class Solution { 3 | public: 4 | int kthSmallest(vector>& matrix, int k) { 5 | int i,j,n,m,ct=0,ans; 6 | priority_queue, greater > pq; 7 | for(i=0;i medianSlidingWindow(vector& nums, int k) { 4 | multiset window(nums.begin(), nums.begin() + k); 5 | auto mid = next(window.begin(), k / 2); 6 | vector medians; 7 | for (int i=k; ; i++) { 8 | 9 | // Push the current median. 10 | medians.push_back((double(*mid) + *prev(mid, 1 - k%2)) / 2); 11 | 12 | // If all done, return. 13 | if (i == nums.size()) 14 | return medians; 15 | 16 | // Insert nums[i]. 17 | window.insert(nums[i]); 18 | if (nums[i] < *mid) 19 | mid--; 20 | 21 | // Erase nums[i-k]. 22 | if (nums[i-k] <= *mid) 23 | mid++; 24 | window.erase(window.lower_bound(nums[i-k])); 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Heap/632. Smallest Range Covering Elements from K Lists.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector smallestRange(vector>& nums) { 4 | int i,j,k,n=nums.size(),m,ans=INT_MAX; 5 | multimap> dict; 6 | for(i=0;isecond; 11 | int st = it->first; 12 | auto mt=dict.end();mt--; 13 | int en = mt->first; 14 | if(ans>en-st+1){ 15 | ans=en-st+1; 16 | m=it->first; 17 | } 18 | dict.erase(it); 19 | if(nums[kt.first].size()==kt.second+1) {flag=1;break;} 20 | dict.insert({nums[kt.first][kt.second+1],make_pair(kt.first,kt.second+1)}); 21 | } 22 | vector temp; 23 | temp.push_back(m); 24 | temp.push_back(m+ans-1); 25 | return temp; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Linked List/1290. Convert Binary Number in a Linked List to Integer.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | int getDecimalValue(ListNode* head) { 14 | int ct=0; 15 | ListNode *temp =head; 16 | while(temp!=NULL){ 17 | ct++; 18 | temp=temp->next; 19 | } 20 | int ans=0; 21 | temp=head;int kt=0; 22 | while(temp!=NULL){ 23 | if(temp->val!=0){ 24 | ans+=pow(2,ct-1); 25 | } 26 | temp=temp->next; 27 | ct--; 28 | } 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Leetcode/Linked List/142. Linked List Cycle II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *detectCycle(ListNode *head) { 12 | if(head==NULL || head->next==NULL || head->next->next==NULL) return NULL; 13 | ListNode *slow=head,*fast=head; 14 | int flag=0; 15 | while(flag==0){ 16 | if(fast->next==NULL || fast->next->next==NULL) break; 17 | else{ 18 | fast = fast->next->next; 19 | slow = slow->next; 20 | if(fast==slow) {flag=1;break;} 21 | } 22 | } 23 | // cout<val<<" "<val<val<next; 30 | slow = slow->next; 31 | } 32 | return slow; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode/Linked List/19. Remove Nth Node From End of List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* removeNthFromEnd(ListNode* head, int n) { 14 | ListNode *first=head,*nthnode=head,*prev=head; 15 | int ct=1; 16 | while(ct!=n){ 17 | nthnode=nthnode->next; 18 | ct++; 19 | } 20 | while(nthnode->next!=NULL){ 21 | nthnode=nthnode->next; 22 | prev=first; 23 | first=first->next; 24 | } 25 | if(first==head) return head->next; 26 | prev->next = first->next; 27 | return head; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Linked List/206. Reverse Linked List_recusrsion.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | ListNode* rev(ListNode* prev,ListNode* curr){ 12 | if(curr==NULL) return prev; 13 | else{ 14 | ListNode *temp2=curr->next; 15 | curr->next=prev; 16 | ListNode *temp=rev(curr,temp2); 17 | return temp; 18 | } 19 | } 20 | class Solution { 21 | public: 22 | ListNode* reverseList(ListNode* head) { 23 | return rev(NULL,head); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Leetcode/Linked List/237. Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | ListNode *temp=node; 13 | // ListNode *temp2=temp->next; 14 | temp->val=temp->next->val; 15 | temp->next=temp->next->next; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Linked List/25. Reverse Nodes in k-Group.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | ListNode* reverseKGroup(ListNode* head, int k) { 5 | bool end = false; 6 | ListNode* ktail = nullptr; 7 | ListNode* ans = nullptr; 8 | while (end != true) { 9 | ListNode* old_head = head; 10 | int m = k; 11 | while (m != 0 && old_head) { 12 | if (m == 1) { 13 | ans = !ans ?old_head:ans; 14 | } 15 | old_head = old_head->next; 16 | m--; 17 | } 18 | if (m != 0) { 19 | ktail->next = head; 20 | end = true; 21 | break; 22 | } 23 | old_head = head; 24 | ListNode* prev = nullptr; 25 | m = k; 26 | while (old_head && m) { 27 | ListNode* nxt = old_head->next; 28 | old_head->next = prev; 29 | prev = old_head; 30 | old_head = nxt; 31 | m--; 32 | } 33 | ktail?ktail->next = prev:nullptr; 34 | ktail = head; 35 | head = old_head; 36 | if (!head) 37 | end = true; 38 | } 39 | return ans ? ans : head; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Leetcode/Linked List/83. Remove Duplicates from Sorted List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | ListNode *temp1=head,*temp2=head,*temp3=head;int flag=0; 15 | while(temp1!=NULL){ 16 | if(temp2->val!=temp1->val){ 17 | cout<val<<" "<val<next=temp1; 19 | temp2=temp1; 20 | //temp1=temp1->next; 21 | } 22 | else { 23 | temp1=temp1->next; 24 | temp2->next=NULL; 25 | } 26 | } 27 | return head; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Maths/1104. Path In Zigzag Labelled Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | vector ways(int n){ 2 | int i, j, k, m, ct = 0, t; 3 | j = n; 4 | int level = 0; 5 | vector terms, vect; int ans = 1; int val = 0; 6 | while (n > 0) { 7 | n -= ans; 8 | val += ans; 9 | terms.push_back(val); 10 | cout << terms[level] << " "; 11 | level++; 12 | ans = ans * 2; 13 | } 14 | cout << endl; 15 | m = terms.size() - 1; k = j; vect.push_back(k); m--; 16 | while (m >= 0) { 17 | int val = terms[m]; 18 | int parent = k / 2; 19 | parent = terms[m] - (parent - pow(2, m)); 20 | k = parent; 21 | vect.push_back(parent); 22 | m--; 23 | } 24 | for (i = 0; i < vect.size(); i++) cout << vect[i] << " "; 25 | reverse(vect.begin(),vect.end()); 26 | return vect; 27 | } 28 | class Solution { 29 | public: 30 | vector pathInZigZagTree(int label) { 31 | return ways(label); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Leetcode/Maths/1726. Tuple with Same Product.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tupleSameProduct(vector& nums) { 4 | int i,j,k,n,m,ct=0,ans=0; 5 | n=nums.size(); 6 | map dict; 7 | for(i=0;i vect){ 2 | int i,n=vect.size(); 3 | for(i=0;i vect1,vect2; 14 | for(i=0;i& nums) { 4 | vector> vect; 5 | int i,j,k,n,m,ct=0,ans=0; 6 | n = nums.size()-1; 7 | for(i=0;i<=n;i++){ 8 | vector temp(33,0); 9 | k = nums[i]; 10 | j=32; 11 | while(k!=0){ 12 | int m1 = k%2; 13 | temp[j]=m1; 14 | k=k/2; 15 | j--; 16 | } 17 | vect.push_back(temp); 18 | } 19 | for(i=0;i<=32;i++){ 20 | int ct0=0,ct1=0; 21 | for(j=0;j<=n;j++){ 22 | if(vect[j][i]==0) ct0++; 23 | else ct1++; 24 | } 25 | ans += ct0*ct1; 26 | } 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Maths/seive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | //using seive of erasthonenses 4 | int countPrimes(int n) { 5 | int isprime[n+5],ct=0; 6 | long long int i,j,k,m; 7 | memset(isprime,0,sizeof(isprime)); 8 | for(i=2;i& sum, int lower, int upper, int low, int high) 4 | { 5 | if(high-low <= 1) return 0; 6 | int mid = (low+high)/2, m = mid, n = mid, count =0; 7 | count =mergeSort(sum,lower,upper,low,mid) +mergeSort(sum,lower,upper,mid,high); 8 | for(int i =low; i< mid; i++) 9 | { 10 | while(m < high && sum[m] - sum[i] < lower) m++; 11 | while(n < high && sum[n] - sum[i] <= upper) n++; 12 | count += n - m; 13 | } 14 | inplace_merge(sum.begin()+low, sum.begin()+mid, sum.begin()+high); 15 | return count; 16 | } 17 | 18 | int countRangeSum(vector& nums, int lower, int upper) { 19 | int len = nums.size(); 20 | vector sum(len + 1, 0); 21 | for(int i =0; i< len; i++) sum[i+1] = sum[i]+nums[i]; 22 | return mergeSort(sum, lower, upper, 0, len+1); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Stack/1209. Remove All Adjacent Duplicates in String II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s, int k) { 4 | int i,j,n,m; 5 | n=s.size(); 6 | stack> st; 7 | for(i=0;i& nums) { 4 | 5 | stack stack; 6 | int second = -2147483648; 7 | for (int i = nums.size() - 1; i >= 0; i--) { 8 | if (nums [i] < second) 9 | return true; 10 | while (stack.size() >0 && nums [i] > stack.top()){ 11 | second = stack.top (); 12 | stack.pop(); 13 | } 14 | 15 | stack.push (nums [i]); 16 | } 17 | return false; 18 | 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Leetcode/Stack/739. Daily Temperatures.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dailyTemperatures(vector& T) { 4 | stack st; 5 | int n=T.size(); 6 | vector vect(n,0); 7 | vect[n-1]=0; 8 | st.push(n-1); 9 | int j=n-2; 10 | for(j=n-2;j>=0;j--){ 11 | while(st.empty()==false && T[st.top()]<=T[j]) st.pop(); 12 | if(st.empty()==false) vect[j]=st.top()-j; 13 | st.push(j); 14 | } 15 | return vect; 16 | 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Stack/856. Score of Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string s) { 4 | int i,j,k,n,m,ct=0,ans=0; 5 | n=s.size(); 6 | stack st; 7 | i=0; 8 | while(i& tokens) { 4 | int i,j,k,n,m,ct=0; 5 | n=tokens.size(); 6 | stack st; 7 | st.push(stoi(tokens[0])); 8 | // cout<=0 && tokens[i][0]-'0'<=9){ 13 | st.push(stoi(tokens[i])); 14 | } 15 | else{ 16 | int m1 = st.top();st.pop(); 17 | int m2 = st.top();st.pop(); 18 | if(tokens[i][0]=='/') st.push(m2/m1); 19 | else if(tokens[i][0]=='*') st.push(m2*m1); 20 | else if(tokens[i][0]=='-') st.push(m2-m1); 21 | else st.push(m2+m1); 22 | } 23 | } 24 | else st.push(stoi(tokens[i])); 25 | i++; 26 | 27 | } 28 | return st.top(); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/Stack/Remove All Adjacent Duplicates In String .cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeDuplicates(string s) { 4 | int i,j,k,n = s.size(),m,ct=0; 5 | stack st; 6 | for(i=0;i>& s2) { 4 | int i,j,k,n,m,ct=0; 5 | string temp; 6 | n=str.size(); 7 | m=s2.size(); 8 | map dict; 9 | for(i=0;isecond.size();j++) temp+=it->second[j];} 23 | else temp+='?'; 24 | } 25 | else temp+=str[i]; 26 | } 27 | return temp; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Strings/3. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | set dict; 5 | int i,j,k,n,m,ct=0,ans=0; 6 | n=s.size()-1; 7 | i=0;j=0; 8 | while(i<=n){ 9 | if(dict.find(s[i])==dict.end()){ 10 | dict.insert(s[i]); 11 | k=dict.size(); 12 | ans=max(ans,k); 13 | } 14 | else{ 15 | while(dict.find(s[i])!=dict.end()) { 16 | dict.erase(s[j]); 17 | j++; 18 | } 19 | dict.insert(s[i]); 20 | k=dict.size(); 21 | ans=max(ans,k); 22 | } 23 | i++; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Strings/438. Find All Anagrams in a String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | #define pb push_back 4 | vector findAnagrams(string str, string t) { 5 | int s = 0; 6 | int e = 0; 7 | unordered_mapm; 8 | for (char ch : t) { 9 | m[ch]++; 10 | } 11 | int counter = m.size(); 12 | vectorans; 13 | while (e < str.size()) { 14 | char ch = str[e]; 15 | if (m.find(ch) != m.end()) { 16 | m[ch]--; 17 | if (m[ch] == 0) { 18 | counter--; 19 | } 20 | } 21 | e++; 22 | while (counter == 0) { 23 | if ((e - s) == t.size()) { 24 | ans.pb(s); 25 | } 26 | char temp = str[s]; 27 | if (m.count(temp) == 1) { 28 | m[temp]++; 29 | if (m[temp] > 0) { 30 | counter++; 31 | } 32 | } 33 | s++; 34 | } 35 | } 36 | return ans; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Leetcode/Strings/525. Contiguous Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxLength(vector& nums) { 4 | int i,j,k,n,m,ct=0; 5 | n=nums.size(); 6 | int val=0;int pref[n+2]; 7 | if(n==1) return 0; 8 | pref[0]=0; 9 | for(i=1;i<=n;i++){ 10 | if(nums[i-1]==0) val--; 11 | else val++; 12 | pref[i]=val; 13 | } 14 | map dict; 15 | dict.insert({0,0}); 16 | for(i=1;i<=n;i++) dict[pref[i]]=i; 17 | //for(auto it=dict.begin();it!=dict.end();it++) cout<first<<" "<second<second-i; 23 | len=max(len,val); 24 | if(it->second==i) dict.erase(it); 25 | } 26 | } 27 | return len; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Strings/696. Count Binary Substrings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBinarySubstrings(string str) { 4 | int ct=0,i,j,k,n=str.size(); 5 | vector vect; 6 | char last=str[i]; 7 | for(i=0;i partitionLabels(string str) { 5 | ll i, j, k, n, m, ct = 0, t, ans = 0; 6 | n = str.size(); 7 | ll map[26] = {0}; 8 | memset(map, -1, sizeof(map)); 9 | for (i = n - 1; i >= 0; i--) { 10 | //cout << str[i] - 'a' << endl; 11 | map[str[i] - 'a'] = max(map[str[i] - 'a'], i); 12 | } 13 | vector vect; 14 | int max1 = -1e9; 15 | for (i = 0; i <= n - 1; i++) { 16 | ct = 1; max1 = -1e9; 17 | max1 = max(max1, map[str[i] - 'a']); 18 | while (i < n && max1 != i) { 19 | i++; ct++; 20 | max1 = max(max1, map[str[i] - 'a']); 21 | } 22 | vect.push_back(ct); 23 | } 24 | //for (i = 0; i < vect.size(); i++) cout << vect[i] << " "; 25 | return vect; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Strings/Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(int freq[],int freq2[]){ 4 | for(int i=0;i<26;i++){ 5 | if(freq[i]!=freq2[i]) return false; 6 | } 7 | return true; 8 | } 9 | bool checkInclusion(string s1, string s2) { 10 | int freq[26]={0}; 11 | for(int i=0;im) return false; 15 | ct=0;int freq2[26]={0}; 16 | for(i=0;i& preorder) { 4 | return helper(preorder, 0, preorder.size()-1); 5 | } 6 | TreeNode* helper(vector& preorder, int left, int right){ 7 | if(left <= right){ 8 | TreeNode* root = new TreeNode(preorder[left]); 9 | int rightStart = partition(preorder, left, right); 10 | root->left = helper(preorder, left+1, rightStart-1); 11 | root->right = helper(preorder, rightStart, right); 12 | return root; 13 | } 14 | return NULL; 15 | } 16 | int partition(vector& preorder, int left, int right){ 17 | for(int i = left+1; i <= right; i++){ 18 | if(preorder[left] < preorder[i]){ 19 | return i; 20 | } 21 | } 22 | return right+1; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Tree/108. Convert Sorted Array to Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* ways(int st,int en,vector vect){ 15 | if(st>en) return NULL; 16 | if(st==en){ 17 | TreeNode *node = new TreeNode(vect[st]); 18 | return node; 19 | } 20 | else{ 21 | int mid = (st+en)/2; 22 | TreeNode *node = new TreeNode(vect[mid]); 23 | node->left = ways(st,mid-1,vect); 24 | node->right = ways(mid+1,en,vect); 25 | return node; 26 | } 27 | } 28 | TreeNode* sortedArrayToBST(vector& nums) { 29 | return ways(0,nums.size()-1,nums); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Leetcode/Tree/113. Path Sum II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | vector> ans; 13 | void ways(TreeNode* node,int sum,vector temp,int curr_sum){ 14 | if(node==NULL) return; 15 | else{ 16 | temp.push_back(node->val); 17 | if(sum==curr_sum+node->val && node->left==NULL && node->right==NULL) ans.push_back(temp); 18 | ways(node->left,sum,temp,curr_sum+node->val); 19 | ways(node->right,sum,temp,curr_sum+node->val); 20 | temp.pop_back(); 21 | } 22 | } 23 | class Solution { 24 | public: 25 | vector> pathSum(TreeNode* root, int targetSum) { 26 | vector temp; 27 | ans.clear(); 28 | ways(root,targetSum,temp,0); 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Leetcode/Tree/116. Populating Next Right Pointers in Each Node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node* next; 9 | 10 | Node() : val(0), left(NULL), right(NULL), next(NULL) {} 11 | 12 | Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} 13 | 14 | Node(int _val, Node* _left, Node* _right, Node* _next) 15 | : val(_val), left(_left), right(_right), next(_next) {} 16 | }; 17 | */ 18 | Node *global=NULL; 19 | void dfs(Node *root,int i,int j){ 20 | if(root==NULL) return; 21 | j++; 22 | if(j==i){ 23 | root->next=global; 24 | global=root; 25 | return; 26 | } 27 | else{ 28 | dfs(root->right,i,j); 29 | dfs(root->left,i,j); 30 | } 31 | } 32 | class Solution { 33 | public: 34 | Node* connect(Node* root) { 35 | for(int i=0;i<=12;i++){ 36 | dfs(root,i,-1); 37 | global=NULL; 38 | } 39 | return root; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Leetcode/Tree/117. Populating Next Right Pointers in Each Node II.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node* next; 9 | 10 | Node() : val(0), left(NULL), right(NULL), next(NULL) {} 11 | 12 | Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} 13 | 14 | Node(int _val, Node* _left, Node* _right, Node* _next) 15 | : val(_val), left(_left), right(_right), next(_next) {} 16 | }; 17 | */ 18 | Node *global=NULL; 19 | void dfs(Node *root,int i,int j){ 20 | if(root==NULL) return; 21 | j++; 22 | if(j==i){ 23 | root->next=global; 24 | global=root; 25 | return; 26 | } 27 | else{ 28 | dfs(root->right,i,j); 29 | dfs(root->left,i,j); 30 | } 31 | } 32 | class Solution { 33 | public: 34 | Node* connect(Node* root) { 35 | for(int i=0;i<=64;i++){ 36 | dfs(root,i,-1); 37 | global=NULL; 38 | } 39 | return root; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Leetcode/Tree/230. Kth Smallest Element in a BST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | int ct=0,val; 13 | void fun(TreeNode* root,int k){ 14 | if(root==NULL) return; 15 | else{ 16 | fun(root->left,k); 17 | ct++; 18 | if(k==ct) val=root->val; 19 | fun(root->right,k); 20 | } 21 | } 22 | class Solution { 23 | public: 24 | int kthSmallest(TreeNode* root, int k) { 25 | ct=0; 26 | fun(root,k); 27 | return val; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Tree/337. House Robber III.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | int ways(TreeNode* root,int flag){ 13 | if(root==NULL) return 0; 14 | // else if(root->left==NULL && root->right==NULL && flag==1) return 0; 15 | else{ 16 | if(flag==1) return ways(root->left,0)+ways(root->right,0); 17 | else{ 18 | return max(root->val+ways(root->left,1)+ways(root->right,1),ways(root->left,0)+ways(root->right,0)); 19 | } 20 | } 21 | } 22 | class Solution { 23 | public: 24 | int rob(TreeNode* root) { 25 | return ways(root,0); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Tree/34. Find First and Last Position of Element in Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* flat(TreeNode* &root, TreeNode* next){ 4 | if(!root) return NULL; 5 | 6 | if(!root->left && !root->right){ 7 | root->right = next; 8 | return root; 9 | } 10 | 11 | TreeNode *cur = root, *left = root->left, *right = root->right; 12 | 13 | cur->left = NULL; 14 | if(left){ 15 | cur->right = flat(left,right); 16 | } 17 | flat(cur->right,next); 18 | 19 | return root; 20 | } 21 | 22 | void flatten(TreeNode* &root) { 23 | if(!root) return; 24 | 25 | root = flat(root,NULL); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Tree/687. Longest Univalue Path.cpp: -------------------------------------------------------------------------------- 1 | int max1=-5000000; 2 | /** 3 | * Definition for a binary tree node. 4 | * struct TreeNode { 5 | * int val; 6 | * TreeNode *left; 7 | * TreeNode *right; 8 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 10 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 11 | * }; 12 | */ 13 | int preorder(TreeNode* root){ 14 | if(root==NULL) return 0; 15 | else{ 16 | int l=0,r=0,lans=0,rans=0; 17 | l=preorder(root->left); 18 | r=preorder(root->right); 19 | if(root->left!=NULL && root->val==root->left->val) lans=l+1; 20 | if(root->right!=NULL && root->val==root->right->val) rans=r+1; 21 | max1=max(lans+rans,max1); 22 | return max(lans,rans); 23 | } 24 | } 25 | class Solution { 26 | public: 27 | int longestUnivaluePath(TreeNode* root) { 28 | int i,j,k,n,m,ct=0; 29 | max1=-5000000; 30 | k=preorder(root); 31 | if(max1==-5000000) return 0; 32 | else return max1; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode/Tree/701. Insert into a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode *ways(TreeNode *node,int min_val,int max_val,int val){ 15 | if(node==NULL){ 16 | TreeNode *new_node = new TreeNode(val); 17 | return new_node; 18 | } 19 | if(val>=min_val && val<=node->val) node->left = ways(node->left,min_val,node->val,val); 20 | else if(val>=node->val && val<=max_val) node->right = ways(node->right,node->val,max_val,val); 21 | return node; 22 | } 23 | TreeNode* insertIntoBST(TreeNode* root, int val) { 24 | int i,j,k,n,m,ct=0; 25 | TreeNode* temporay = ways(root,-1e9,1e9,val); 26 | return temporay; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Tree/814. Binary Tree Pruning.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | bool order(TreeNode* root){ 13 | if(root==NULL) return false; 14 | //if(root->val==1) return true; 15 | else{ 16 | bool l=order(root->left); 17 | bool r=order(root->right); 18 | if(l==false) root->left=NULL; 19 | if(r==false) root->right=NULL; 20 | if(root->val==1) return true; 21 | else if(l==false && r==false) {root=NULL;return false;} 22 | else{ 23 | return true; 24 | } 25 | } 26 | } 27 | class Solution { 28 | public: 29 | TreeNode* pruneTree(TreeNode* root) { 30 | int i,j,k,n,m,ct=0; 31 | bool flag=order(root); 32 | if(flag==false) return NULL; 33 | return root; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Leetcode/Tree/94. Binary Tree Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | vector vect; 13 | void ways(TreeNode* root){ 14 | if(root==NULL) return; 15 | else{ 16 | ways(root->left); 17 | vect.push_back(root->val); 18 | ways(root->right); 19 | } 20 | } 21 | class Solution { 22 | public: 23 | vector inorderTraversal(TreeNode* root) { 24 | int i,j,k,n,m,ct=0; 25 | vect.clear(); 26 | ways(root); 27 | return vect; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Tree/951. Flip Equivalent Binary Trees.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool ways(TreeNode* node1,TreeNode* node2){ 15 | if(node1==NULL && node2==NULL) return true; 16 | else if(node1==NULL || node2==NULL) return false; 17 | else if(node1->val!=node2->val) return false; 18 | else{ 19 | bool k = ((ways(node1->left,node2->left) && ways(node1->right,node2->right))); 20 | bool j =((ways(node1->left,node2->right) && ways(node1->right,node2->left))); 21 | return (k or j); 22 | } 23 | } 24 | bool flipEquiv(TreeNode* root1, TreeNode* root2) { 25 | int i,j,k,n,m,ct=0; 26 | return ways(root1,root2); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Tree/979. Distribute Coins in Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int ans; 4 | int distributeCoins(TreeNode* root) 5 | { 6 | ans = 0; 7 | solve(root); 8 | return ans; 9 | } 10 | int solve(TreeNode* root) 11 | { 12 | if(!root) return 0; 13 | int left = solve(root->left); 14 | int right = solve(root->right); 15 | ans += abs(left) + abs(right); 16 | return root->val + left + right - 1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Tree/Path SumIII.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int currsum=0,count=0; 4 | unordered_mapprefix; 5 | 6 | void helper(TreeNode* root, int sum) 7 | { 8 | if(root==NULL) 9 | return; 10 | 11 | currsum+=root->val; 12 | if(currsum==sum) 13 | count++; 14 | if(prefix.find(currsum-sum)!=prefix.end()) 15 | count+=prefix[currsum-sum]; 16 | 17 | prefix[currsum]++; 18 | 19 | helper(root->left,sum); 20 | helper(root->right,sum); 21 | 22 | prefix[currsum]--; 23 | currsum-=root->val; 24 | 25 | } 26 | 27 | int pathSum(TreeNode* root, int sum) { 28 | helper(root,sum); 29 | return count; 30 | } 31 | 32 | }; 33 | -------------------------------------------------------------------------------- /Leetcode/Trie/745. Prefix and Suffix Search.cpp: -------------------------------------------------------------------------------- 1 | class WordFilter { 2 | private: 3 | unordered_map hashMap; 4 | 5 | public: 6 | WordFilter(vector& words) { 7 | int n = words.size(); 8 | for (int i = 0; i < n; i++) { 9 | string word = words[i]; 10 | int wordSize = word.size(); 11 | for (int j = 1; j <= wordSize; j++) { 12 | string p = word.substr(0, j); 13 | for (int k = 0; k < wordSize; k++) { 14 | string s = word.substr(k, wordSize); 15 | hashMap[p + "|" + s] = i + 1; 16 | } 17 | } 18 | } 19 | } 20 | 21 | int f(string prefix, string suffix) { 22 | string s = prefix + "|" + suffix; 23 | return hashMap[s] - 1; 24 | } 25 | }; 26 | 27 | /** 28 | * Your WordFilter object will be instantiated and called as such: 29 | * WordFilter* obj = new WordFilter(words); 30 | * int param_1 = obj->f(prefix,suffix); 31 | */ 32 | -------------------------------------------------------------------------------- /Leetcode/binary search/33. Search in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int low=0,high=nums.size()-1; 5 | int i,j,k,n,m,ct=0; 6 | while(low<=high){ 7 | int mid = (low+high)/2; 8 | if(nums[mid]==target) return mid; 9 | //Target is between low and mid 10 | else if(nums[mid]>=nums[low] && nums[mid]>target && nums[low]<=target) high=mid-1; 11 | else if(nums[mid]target)) high=mid-1; 12 | //Target is between mid and high 13 | else if(nums[high]>=nums[mid] && nums[high]>=target && nums[mid] pq(arr,arr+n); 6 | 7 | for(i=1;i<=n-k;i++) pq.pop(); 8 | return pq.top(); 9 | } 10 | -------------------------------------------------------------------------------- /SDE Sheet/array/Minimize the Heights II .cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int getMinDiff(int arr[], int n, int k) { 4 | sort(arr,arr+n); 5 | int i,j,m=0;int ans=1e9; 6 | for(i=0;i=0 && i>=1){ 8 | int min1=min(arr[0]+k,min(arr[i]-k,arr[n-1]-k)); 9 | int max1=max(arr[i-1]+k,arr[n-1]-k); 10 | ans=min(ans,max1-min1); 11 | } 12 | else ans=min(ans,arr[n-1]-arr[0]); 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /SDE Sheet/array/Minimum number of jumps.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int minJumps(int a[], int n){ 4 | int i=0,j=0,k,m,ct=0,flag=0; 5 | int max1=0,max2=0; 6 | j=min(a[0],n-1);max1=min(a[0],n-1);i=0; 7 | while(i!=n){ 8 | if(j>i){ 9 | max1=max(max1,min(n-1,i+a[i])); 10 | i++; 11 | } 12 | else if(j==i){ 13 | ct++; 14 | max1=max(max1,min(n-1,i+a[i])); 15 | j=max1; 16 | if(i==j && i!=n-1) return -1; 17 | i++; 18 | } 19 | } 20 | return ct; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /https:/atcoder.jp/contests/zone2021/tasks/zone2021_a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | string str; 27 | cin >> str; 28 | for (i = 0; i < str.size() - 3; i++) { 29 | if (str[i] == 'Z' && str[i + 1] == 'O' && str[i + 2] == 'N' && str[i + 3] == 'e') ct++; 30 | } 31 | cout << ct << endl; 32 | 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /https:/atcoder.jp/contests/zone2021/tasks/zone2021_b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, n, d, h, ct = 0, t; 26 | std::cout << setprecision(18); 27 | double ans = 0; 28 | cin >> n >> d >> h; 29 | for (int t = 0; t < n; t++) { 30 | cin >> i >> j; 31 | double m = (double)(d - i) / (h - j); 32 | double k = (double)i / m; 33 | //cout << j - k << endl; 34 | ans = max(ans, j - k); 35 | } 36 | cout << ans << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1005/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll max1 =(ll)1e9; 5 | int main(){ 6 | ll i,j,k,n,m,ct=0,t,ans=0,flag=0; 7 | cin>>n; 8 | ll a[31]={0}; 9 | for(i=0;i<31;i++) a[i]=pow(2,i); 10 | map dict; 11 | for(i=0;i>k; 13 | if(dict.find(k)!=dict.end()) dict[k]++; 14 | else dict.insert({k,1}); 15 | } 16 | for(auto it=dict.begin();it!=dict.end();it++){ 17 | ll val=it->first;flag=0; 18 | for(i=1;i<31;i++){ 19 | if(a[i]>val){ 20 | ll x=a[i]-val; 21 | if(dict.find(x)!=dict.end()){ 22 | if(x==val && dict[x]>1){ 23 | flag=1;break;} 24 | else if(x!=val){ 25 | flag=1;break;} 26 | else flag=0; 27 | } 28 | else flag=0; 29 | } 30 | } 31 | if(flag==0) ct+=dict[val]; 32 | else flag=0; 33 | } 34 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | ll a, b; 28 | cin >> a >> b >> k; 29 | ll c1 = (k + 2 - 1) / 2; 30 | ll c2 = k / 2; 31 | cout << c1*a - c2*b << endl; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1077/problem/B.cpp: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/1077/problem/B 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define in insert 11 | #define mp make_pair 12 | #define F first 13 | #define S second 14 | #define clr(x) memset(x, 0, sizeof(x)) 15 | #define init(x, a) memset(x, a, sizeof(x)) 16 | ll inf = (ll)1e18; 17 | ll mod = 1e9 + 7; 18 | ll max1 = (ll)1e9; 19 | int main() { 20 | #ifndef ONLINE_JUDGE 21 | freopen("input.txt", "r", stdin); 22 | freopen("output.txt", "w", stdout); 23 | freopen("err.txt", "w", stderr); 24 | #endif 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> n; 27 | ll a[n]; ct = 0; 28 | for (i = 0; i < n; i++) cin >> a[i]; 29 | for (i = 1; i <= n - 2; i++) { 30 | if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) { 31 | a[i + 1] = 0; 32 | ct++; 33 | } 34 | } 35 | cout << ct << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1102/problem/B: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | int main(){ 5 | ll i,j,k,n,m,ct=0,t,ans=0; 6 | cin>>n>>m; 7 | unordered_map dict; 8 | ll a[n]; 9 | for(i=0;i>a[i]; 11 | if(dict.find(a[i])!=dict.end()) dict[a[i]]++; 12 | else dict.insert({a[i],1}); 13 | } 14 | ll max1=0,b[n]={0}; 15 | for(auto it=dict.begin();it!=dict.end();it++) max1=max(it->second,max1); 16 | if(max1<=m && n>=m){ 17 | cout<<"YES"<m) k=1; 21 | if(b[i]==0){b[i]=k;k++; 22 | } 23 | for(j=i+1;jm) k=1; 26 | //cout< 4 | using namespace std; 5 | typedef long long ll; 6 | ll inf=(ll)1e18; 7 | ll mod=(ll)1e9+7; 8 | ll max1=(ll)1e9; 9 | ll ways(ll a,ll b,ll c,ll d){ 10 | ll lower=max(c,d);; 11 | ll upper=min(a,b); 12 | ll val=upper-lower; 13 | if(val<0) return 0; 14 | else return val+1; 15 | } 16 | int main(){ 17 | ll i,j,k,n,m,ct=0,t,ans=1,a,b,c,d,flag=0; 18 | cin>>t;c=0;d=0; 19 | for(i=0;i>a>>b; 21 | if(c!=a || b!=d){ 22 | ans+=ways(a,b,c,d); 23 | if(c==d) ans--; 24 | } 25 | c=a;d=b; 26 | } 27 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define in insert 11 | #define mp make_pair 12 | #define F first 13 | #define S second 14 | //memset(x, 0, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | ll i, j, k, n, m, ct = 0, t, ans = 0; 27 | ll b, c, d; ll a[3]; 28 | cin >> a[0] >> a[1] >> a[2] >> d; 29 | sort(a, a + 3); 30 | if (a[1] - a[0] < d) { 31 | ans += abs(d - (a[1] - a[0])); 32 | } 33 | if (a[2] - a[1] < d) ans += abs(d - (a[2] - a[1])); 34 | cout << ans << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1191/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll;; 4 | ll inf=(ll)1e18; 5 | ll mod=(ll)1e9+7; 6 | ll max1 =(ll)1e9; 7 | int main(){ 8 | ll i,j,k,n,m,ct=0,t,ans=0,l; 9 | cin>>n>>m>>k; 10 | int sum=0; 11 | int now=1; 12 | ll a[m+1]; 13 | for(i=1;i<=m;i++) cin>>a[i]; 14 | while(now<=m) 15 | { 16 | ll r=((a[now]-sum-1)/k+1)*k+sum; 17 | while(now<=m&&a[now]<=r) 18 | { 19 | sum++; 20 | now++; 21 | } 22 | ans++; 23 | } 24 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | void swap(ll *x,ll *y){ 5 | ll j=*y; 6 | *y=*x; 7 | *x=j; 8 | } 9 | ll max1 =(ll)1e9; 10 | int main(){ 11 | ll i,j,k,n,m,ct=0,t,ans=0; 12 | cin>>t; 13 | while(t--){ 14 | cin>>n>>m; 15 | if((n+m)%3==0 && max(n,m)<=2*min(n,m)) cout<<"YES\n"; 16 | else cout<<"NO\n"; 17 | } 18 | return 0; 19 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/131/problem/C.cpp: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/131/problem/C 2 | 3 | #include 4 | using namespace std; 5 | typedef unsigned long long ll; 6 | ll inf = (ll)1e18; 7 | ll mod = 1000000001; 8 | ll ways(ll n, ll k) 9 | { 10 | ll C[n + 1][k + 1]; 11 | ll i, j; 12 | for (i = 0; i <= n; i++) 13 | { 14 | for (j = 0; j <= min(i, k); j++) 15 | { 16 | if (j == 0 || j == i) 17 | C[i][j] = 1; 18 | else 19 | C[i][j] = C[i - 1][j - 1] + 20 | C[i - 1][j]; 21 | } 22 | } 23 | 24 | return C[n][k]; 25 | } 26 | ll max1 = (ll)1e9; 27 | int main() { 28 | ll i, j, k, n, m, ct = 0, t; 29 | ll ans = 0; 30 | cin >> n >> m >> t; 31 | k = 4; 32 | while (k <=n && t - k >= 1) { 33 | if(t-k<=m) ans += ways(n, k) * ways(m, t - k); 34 | k++; 35 | } 36 | cout << ans << endl; 37 | return 0; 38 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1333/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | int n; cin >> n; 8 | vector prefix(n + 1, 0); 9 | for (int i = 0; i < n; ++i) { 10 | int x; cin >> x; 11 | prefix[i + 1] = prefix[i] + x; 12 | } 13 | int begin = 0, end = 0; 14 | long long ans = 0; 15 | set s = {0}; 16 | while(begin < n) { 17 | while(end < n && !s.count(prefix[end + 1])) { 18 | ++end; 19 | s.insert(prefix[end]); 20 | } 21 | ans += end - begin; 22 | s.erase(prefix[begin]); 23 | ++begin; 24 | } 25 | cout << ans << endl; 26 | } 27 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1350/problem/A.cpp: -------------------------------------------------------------------------------- 1 | //orac and factors 2 | //https://codeforces.com/contest/1350/problem/A 3 | 4 | #include 5 | using namespace std; 6 | typedef long long ll; 7 | ll max1 =(ll)1e9; 8 | ll calculate(ll n){ 9 | for(int i=2;i<=sqrt(n);i++){ 10 | if(n%i==0){ 11 | return i; 12 | } 13 | } 14 | return n; 15 | } 16 | int main(){ 17 | ll i,j,k,n,m,ct=0,t,ans=0; 18 | cin>>t; 19 | while(t--){ 20 | cin>>n>>k; 21 | ll final_val=n; 22 | i=0; 23 | while(final_val%2!=0){ 24 | final_val+=calculate(final_val); 25 | //cout< 4 | using namespace std; 5 | typedef long long ll; 6 | ll max1 =(ll)1e9; 7 | int main(){ 8 | ll i,j,k,n,m,ct=0,t,ans=0,flag=0; 9 | cin>>t; 10 | while(t--){ 11 | cin>>n; 12 | ll a[n+1]={0},b[n+1];flag=0; 13 | b[0]=0; 14 | for(i=1;i<=n;i++) b[i]=1; 15 | for(i=1;i<=n;i++) cin>>a[i]; 16 | for(i=(n)/2;i>=2;i--){ 17 | flag=0; 18 | for(j=2*i;j<=n;j+=i){ 19 | if(a[j]>a[i]){ 20 | flag=1; 21 | b[i]=max(b[j],b[i]);} 22 | } 23 | if(flag==1) b[i]+=1; 24 | }flag=0; 25 | for(i=2;i<=n;i++){ 26 | if(a[i]>a[1]){ 27 | b[1]=max(b[i],b[1]);flag=1; 28 | } 29 | } 30 | max1=0; 31 | if((flag==1)) b[1]+=1; 32 | for(i=1;i<=n;i++) max1=max(b[i],max1); 33 | cout< 3 | using namespace std; 4 | typedef long long ll; 5 | ll inf = (ll)1e18; 6 | ll mod = 1000000001; 7 | ll gcd(ll a, ll b) { 8 | if (a % b==0) return b; 9 | else return gcd(b, a % b); 10 | } 11 | ll max1 = (ll)1e9; 12 | int main() { 13 | ll i, j, k, n, m, ct = 0, t, ans = 0; 14 | cin >> n; 15 | ll a[n], b[n]; 16 | for (i = 0; i < n; i++) cin >> a[i]; 17 | ll x = a[n - 1]; 18 | for (i = n - 1; i >= 0; i--) { 19 | ll c = gcd(a[i], x); 20 | x = c; 21 | b[i] = c; 22 | } 23 | ll c[n]; 24 | for(i=0;i<=n-1;i++){ 25 | if(i==n-1) c[i]=b[n-1]; 26 | else c[i]=(a[i]*b[i+1])/b[i]; 27 | } 28 | for(int x:c) cout< 2 | using namespace std; 3 | typedef long long ll; 4 | int check_prime(int n){ 5 | for(int i=2;i*i<=n;i++) if(n%i==0) return 0; 6 | return 1; 7 | } 8 | ll max1 =(ll)1e9; 9 | int main(){ 10 | ll i,j,k,n,m,ct=0,t,ans=0,moves=max1; 11 | set dict; 12 | for(i=2;i<=200000;i++){ 13 | if(check_prime(i)==1) dict.insert(i); 14 | } 15 | cin>>n>>m; 16 | int a[n][m]; 17 | for(i=0;i>a[i][j]; 19 | } 20 | //checking row wuth min no of non prime numbers 21 | for(i=0;i 2 | using namespace std; 3 | typedef long long ll; 4 | ll max1 =(ll)1e9; 5 | int main(){ 6 | ll i,j,k,n,m,ct=0,t,ans=0; 7 | cin>>t; 8 | while(t--){ 9 | cin>>n; 10 | i=0;j=n; 11 | vector vec; 12 | while(j!=0){ 13 | k=j%10; 14 | j=j/10; 15 | m=k*pow(10,i); 16 | if(m!=0) vec.push_back(m); 17 | i++; 18 | } 19 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | ll max1 =(ll)1e9; 5 | int main(){ 6 | ll i,j,k,n,m,ct=0,t,ans=0,flag=0; 7 | cin>>t; 8 | while(t--){ 9 | cin>>n; 10 | int a[n]; 11 | for(i=0;i>a[i]; 12 | int prev_left=0,prev_right=0,left_sum=0,right_sum=0; 13 | j=0;k=n-1;i=0; 14 | while(j<=k){ 15 | if(i%2==0){ 16 | ll sum=0; 17 | while(prev_right>=sum && j<=k){ 18 | sum+=a[j];j++; 19 | } 20 | left_sum+=sum; 21 | prev_left=sum; 22 | } 23 | else{ 24 | ll sum=0; 25 | while(prev_left>=sum && j<=k){ 26 | sum+=a[k];k--; 27 | } 28 | right_sum+=sum; 29 | prev_right=sum; 30 | } 31 | i++; 32 | } 33 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | ll max1 =(ll)1e9; 5 | int main(){ 6 | ll i,j,k,n,m,ct=0,t,ans=0; 7 | cin>>t; 8 | while(t--){ 9 | cin>>n; 10 | vector vec; 11 | if(n%2==0) k=n-1; 12 | else k=n; 13 | if(n==4) cout<<"2 4 1 3\n"; 14 | else if(k>=5){ 15 | while(k>=1){ 16 | vec.push_back(k); 17 | k-=2;} 18 | vec.push_back(4);vec.push_back(2);k=6; 19 | while(k<=n){ 20 | vec.push_back(k);k+=2; 21 | } 22 | for(int x:vec) cout< 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf = (ll)1e18; 5 | ll mod = 1000000001; 6 | ll gcd(ll a, ll b) { 7 | if (a % b) return b; 8 | else return gcd(b, a % b); 9 | } 10 | ll max1 = (ll)1e9; 11 | int main() { 12 | #ifndef ONLINE_JUDGE 13 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 14 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 15 | //freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 16 | #endif 17 | ll i, j, k, n, m, ct = 0, t, ans = 0; 18 | cin >> t; 19 | while (t--) { 20 | cin >> n >> m; 21 | ll lamps = 0; 22 | lamps += m / 2 * n; 23 | if (m % 2 != 0) lamps += (n + 2 - 1) / 2; 24 | cout << lamps << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1358/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf = (ll)1e18; 5 | ll mod = 1000000001; 6 | ll gcd(ll a, ll b) { 7 | if (a % b) return b; 8 | else return gcd(b, a % b); 9 | } 10 | ll max1 = (ll)1e9; 11 | int main() { 12 | #ifndef ONLINE_JUDGE 13 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 14 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 15 | // freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 16 | #endif 17 | ll i, j, k, n, m, ct = 0, t, ans = 0; 18 | cin >> t; 19 | while (t--) { 20 | cin >> n; 21 | vector vec, vec2; 22 | vec.push_back(0); 23 | for (i = 1; i <= n; i++) { 24 | cin >> k; 25 | vec.push_back(k); 26 | } 27 | sort(vec.begin(), vec.end()); 28 | ct = 1; ll kt = 1; 29 | ll l = 0; j = 0; i = 0; 30 | for (i = 1; i <= n; i++) { 31 | if (ct + i - l > vec[i]) { 32 | ct += i - l; 33 | l = i; 34 | } 35 | } 36 | cout << ct << endl; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1358/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf = (ll)1e18; 5 | ll mod = 1000000001; 6 | ll gcd(ll a, ll b) { 7 | if (a % b) return b; 8 | else return gcd(b, a % b); 9 | } 10 | ll max1 = (ll)1e9; 11 | int main() { 12 | #ifndef ONLINE_JUDGE 13 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 14 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 15 | //freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 16 | #endif 17 | ll i, j, k, n, m, ct = 0, t, ans = 0; 18 | cin >> t; 19 | while (t--) { 20 | ll x1, x2, y1, y2; 21 | cin >> x1 >> y1 >> x2 >> y2; 22 | cout << abs(x1 - x2)*abs(y1 - y2) + 1 << endl; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1360/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf = (ll)1e18; 5 | ll mod = 1000000001; 6 | //ll max1 = (ll)1e9; 7 | int main() { 8 | #ifndef ONLINE_JUDGE 9 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 10 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 11 | //freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 12 | #endif 13 | ll i, j, k, n, m, ct = 0, t, ans = 0; 14 | cin >> t; 15 | while (t--) { 16 | ll a, b; cin >> a >> b; 17 | ll min1 = min(a, b); 18 | ll max1 = max(a, b); 19 | if (2 * min1 >= max1) cout << (2 * min1)*(2 * min1) << endl; 20 | else cout << (max1)*(max1) << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1360/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf = (ll)1e18; 5 | ll mod = 1000000001; 6 | ll gcd(ll a, ll b) { 7 | if (a % b) return b; 8 | else return gcd(b, a % b); 9 | } 10 | //ll max1 =(ll)1e9; 11 | int main() { 12 | #ifndef ONLINE_JUDGE 13 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 14 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 15 | //freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 16 | #endif 17 | ll i, j, k, n, m, ct = 0, t, ans = 0; 18 | cin >> t; 19 | while (t--) { 20 | cin >> n; 21 | ll a[n], diff = 0, min1 = inf; 22 | for (i = 0; i < n; i++) cin >> a[i]; 23 | sort(a, a + n); 24 | for (i = 1; i < n; i++) { 25 | diff = abs(a[i] - a[i - 1]); 26 | min1 = min(min1, diff); 27 | } 28 | cout << min1 << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1360/problem/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf = (ll)1e18; 5 | ll mod = 1000000001; 6 | ll val(ll n, ll k) { 7 | ll max1 = 1; 8 | for (ll i = 1; i <= sqrt(n); i++) { 9 | if (n % i == 0 && i <= k) { 10 | max1 = max(max1, i); 11 | if (n / i <= k) max1 = max(n / i, max1); 12 | } 13 | } 14 | return max1; 15 | } 16 | //ll max1 =(ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 20 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 21 | // freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 22 | #endif 23 | ll i, j, k, n, m, ct = 0, t, ans = 0; 24 | cin >> t; 25 | while (t--) { 26 | cin >> n >> k; 27 | m = val(n, k); 28 | cout << n / m << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1362/problem/D.cpp: -------------------------------------------------------------------------------- 1 | include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n,m; cin>>n>>m; 8 | vectoradj[n+1]; 9 | int t[n+1]; 10 | while(m--) { 11 | int a,b; cin>>a>>b; 12 | adj[a].push_back(b); 13 | adj[b].push_back(a); 14 | } 15 | for(int i=1; i<=n; i++) { 16 | cin>>t[i]; 17 | } 18 | for(int i=1; i<=n; i++) { 19 | set s; 20 | for(int j: adj[i]) { 21 | if(t[j]==t[i]) { 22 | cout << -1; 23 | return 0; 24 | } else if(t[j]>p; 34 | for(int i=0; i 2 | using namespace std; 3 | typedef long long ll; 4 | using namespace std; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1000000001; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/input.txt", "r", stdin); 21 | freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/output.txt", "w", stdout); 22 | //freopen("C:/Users/Utkarsh Kumar Singh/Desktop/ALL CODE/err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | cin >> i >> j; 28 | ll n = min(i, j); 29 | ll m = max(i, j); 30 | if (n <= m / 2) cout << n << endl; 31 | else cout << (i + j) / 3 << endl; 32 | 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1366/problem/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int frq[10000007],ans[500005]; 5 | 6 | int main() 7 | { 8 | int n,x;scanf("%d",&n); 9 | long long sum; 10 | for(int i=2; i<10000001; i++){ 11 | for(int j=i; j<10000001; j+=i){ 12 | if(!frq[j]) 13 | frq[j]=i; 14 | } 15 | } 16 | for(int i=0; i 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | ll a, b, c; 28 | cin >> a >> b >> c; 29 | ll c1 = c; 30 | ll x = 1; 31 | ll c2 = a; 32 | if (c2 < c1) { 33 | cout << x << " "; 34 | } 35 | else cout << "-1 "; 36 | x = b; 37 | c1 = c; c2 = b * a; 38 | if (c2 > c1) { 39 | cout << b << " "; 40 | } 41 | else cout << "-1 "; 42 | cout << endl; 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1373/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | string str; 28 | cin >> str; 29 | ct = 0; ll kt = 0; 30 | for (i = 0; i < str.size(); i++) { 31 | if (str[i] == '0') ct++; 32 | else kt++; 33 | } 34 | ll min1 = min(ct, kt); 35 | if (min1 % 2 != 0) cout << "DA\n"; 36 | else cout << "NET\n"; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1385/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | ll a[3]; 28 | for (i = 0; i < 3; i++) cin >> a[i]; 29 | sort(a, a + 3); 30 | if (a[2] == a[1]) { 31 | cout << "YES\n"; 32 | if (a[1] == a[0]) cout << a[0] << " " << a[0] << " " << a[0] << "\n"; 33 | else cout << a[2] << " " << a[0] << " " << a[0] << "\n"; 34 | } 35 | else cout << "NO\n"; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1388/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | cin >> n; 28 | ll quo = (n + 4 - 1) / 4; 29 | j = n - quo; 30 | for (i = 1; i <= j; i++) cout << "9"; 31 | //if (m != 0) cout << "8"; 32 | for (i = 1; i <= quo; i++) cout << "8"; 33 | cout << "\n"; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1389/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ll i, j, k, n, m, ct = 0, t, ans = 0; 25 | cin >> t; 26 | while (t--) { 27 | cin >> i >> j; 28 | k = 2 * i; 29 | if (2 * i > i && 2 * i <= j) cout << i << " " << 2 * i << "\n"; 30 | else cout << "-1 -1\n"; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1391/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; 29 | for (i = 1; i <= n; i++) cout << i << " "; 30 | cout << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1391/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n >> m; ct = 0; 29 | char a[n][m]; 30 | for (i = 0; i < n; i++) { 31 | for (j = 0; j < m; j++) cin >> a[i][j]; 32 | } 33 | for (i = 0; i < n; i++) { 34 | if (a[i][m - 1] == 'R') ct++; 35 | } 36 | for (j = 0; j < m; j++) { 37 | if (a[n - 1][j] == 'D') ct++; 38 | } 39 | cout << ct << endl; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1391/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll powerLL(ll x, ll n) 18 | { 19 | ll result = 1; 20 | while (n) { 21 | if (n & 1) 22 | result = result * x % mod; 23 | n = n / 2; 24 | x = x * x % mod; 25 | } 26 | return result; 27 | } 28 | 29 | int main() { 30 | #ifndef ONLINE_JUDGE 31 | freopen("input.txt", "r", stdin); 32 | freopen("output.txt", "w", stdout); 33 | freopen("err.txt", "w", stderr); 34 | #endif 35 | ios_base::sync_with_stdio(false); 36 | cin.tie(NULL); 37 | ll i, j, k, n, m, ct = 0, t, ans = 0; 38 | cin >> n; 39 | ll res = 1; 40 | for (i = 1; i <= n; i++) res = (res * i) % mod; 41 | cout << (mod + (res - powerLL(2, n - 1))) % mod << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1397/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; ll flag = 0; 29 | ll a[26] = {0}; 30 | for (i = 0; i < n; i++) { 31 | string str; 32 | cin >> str; 33 | for (j = 0; j < str.size(); j++) a[str[j] - 'a']++; 34 | } 35 | for (i = 0; i < 26; i++) { 36 | if (a[i] % n != 0) flag = 1; 37 | } 38 | if (flag == 1) cout << "NO\n"; 39 | else cout << "YES\n"; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1399/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; 29 | ll a[n]; ll flag = 0; 30 | for (i = 0; i < n; i++) cin >> a[i]; 31 | sort(a, a + n); 32 | for (i = 1; i < n; i++) { 33 | ll diff = a[i] - a[i - 1]; 34 | if (diff <= 1) continue; 35 | else {flag = 1; break;} 36 | } 37 | if (flag == 1) cout << "NO\n"; 38 | else cout << "YES\n"; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1401/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | main(){ 4 | long long int a,n,k,t; 5 | cin>>t; 6 | while(t--){ 7 | cin>>n>>k; 8 | if(k>=n)cout<<(k-n)< 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(NULL); 20 | ll i, j, k, n, m, ct = 0, t, ans = 0; 21 | cin >> n; 22 | ll a[n+1]; ll max1 = 1; 23 | for (i = 2; i <= n; i++) { 24 | cout << "? " << max1 << " " << i << endl; 25 | cin >> k; 26 | cout << "? " << i << " " << max1 << endl; 27 | cin >> j; 28 | if (k < j) { 29 | a[i]=j; 30 | } 31 | else { 32 | a[max1]=k; 33 | max1 = i; 34 | } 35 | } 36 | a[max1]=n; 37 | cout << "! "; 38 | for (i = 1; i <= n; i++) cout << a[i] << " "; 39 | cout << endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1409/problem/A.cpp: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/1409/problem/A 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define in insert 11 | #define mp make_pair 12 | #define F first 13 | #define S second 14 | //memset(x, 0, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | ll i, j, k, n, m, ct = 0, t, ans = 0; 27 | cin >> t; 28 | while (t--) { 29 | ll a, b; 30 | cin >> a >> b; 31 | ll diff = abs(a - b); 32 | ll num2 = diff % 10, num1 = diff / 10; 33 | if (num2 != 0) num2 = 1; 34 | cout << num1 + num2 << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1419/problem/D1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> n; 27 | ll a[n]; 28 | for (i = 0; i < n; i++) cin >> a[i]; 29 | sort(a, a + n); 30 | vector vect; 31 | for (i = 0; i <= n - 2; i += 2) { 32 | k = a[i]; 33 | j = a[i + 1]; 34 | a[i + 1] = k; 35 | a[i] = j; 36 | } 37 | cout << (n - 1) / 2 << endl; 38 | for (i = 0; i < n; i++) cout << a[i] << " "; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1421/problem/A.cpp: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/1421/problem/A 2 | #include 3 | using namespace std; 4 | typedef int ll; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define in insert 11 | #define mp make_pair 12 | #define F first 13 | #define S second 14 | //memset(x, 0, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | ll i, j, k, n, m, ct = 0, t, ans = 0; 27 | cin >> t; 28 | while (t--) { 29 | ll a, b; 30 | m = a; n = b; 31 | cin >> a >> b; 32 | k = a xor b; 33 | cout << k << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1421/problem/C.cpp: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/contest/1421/problem/C 2 | #include 3 | using namespace std; 4 | typedef long long ll; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define in insert 11 | #define mp make_pair 12 | #define F first 13 | #define S second 14 | //memset(x, 0, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1e9 + 7; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | ll i, j, k, n, m, ct = 0, t, ans = 0; 27 | string str; 28 | cin >> str; 29 | n = str.size(); 30 | k = n - 2; 31 | cout << 4 << endl; 32 | cout << "R " << " " << 2 << endl; 33 | cout << "L " << " " << 2 << endl; 34 | cout << "R " << " " << 2 << endl; 35 | cout << "R " << " " << 4 * n - 4 - 1 << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1422/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | ll a[3]; 29 | cin >> a[0] >> a[1] >> a[2]; 30 | sort(a, a + 3); 31 | if (a[2] == a[1]) cout << a[0] << endl; 32 | else if (a[0] == a[1]) cout << a[2] << endl; 33 | else if (a[0] == a[1] && a[1] == a[2]) cout << a[2] << endl; 34 | else cout << a[2] + 1 << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1430/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | ans = 0; 29 | cin >> n >> k; 30 | ll b[n]; 31 | for (i = 0; i < n; i++) cin >> b[i]; 32 | sort(b, b + n); 33 | for (ll i = n - 2; i >= 0 && k > 0; i--) 34 | { 35 | k--; 36 | b[n - 1] += b[i]; 37 | } 38 | ans = b[n - 1]; 39 | cout << ans << endl; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1430/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; 29 | k = n; j = n - 1; 30 | cout << "2\n"; 31 | for (i = n - 1; i >= 1; i--) { 32 | ll avg = (k + j + 1) / 2; 33 | cout << j << " " << k << endl; 34 | j--; 35 | k = avg; 36 | } 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1430/problem/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int T,n,s[200010],cnt,now,ans; 4 | char a[200010]; 5 | int main(){ 6 | scanf("%d",&T); 7 | while(T--){ 8 | now=1,cnt=0,ans=0; 9 | scanf("%d",&n); 10 | scanf("%s",a+1); 11 | for(int i=1;i<=n;i++){ 12 | if(a[i]==a[i-1])s[cnt]++; 13 | else s[++cnt]=1; 14 | } 15 | for(int i=1;i<=cnt;i++){ 16 | while(now<=cnt&&s[now]==1)now++; 17 | if(now<=cnt)s[now]--; 18 | if(now>cnt)i++; 19 | if(i==now)now++; 20 | ans++; 21 | } 22 | printf("%d\n",ans); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1433/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; 29 | k = n; ct = 0; 30 | while (k != 0) { 31 | k = k / 10; 32 | ct++; 33 | } 34 | m = n % 10; 35 | ans = 0; 36 | ll temp = 0; 37 | if (ct == 1) temp = 1; 38 | else if (ct == 2) temp = 3; 39 | else if (ct == 3) temp = 6; 40 | else temp = 10; 41 | ans += (m - 1) * 10 + temp; 42 | cout << ans << endl; 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1433/problem/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | ll a[75][75]; 18 | int main() { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | freopen("err.txt", "w", stderr); 23 | #endif 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | ll i, j, k, n, m, ct = 0, t; 27 | 28 | cin >> n; 29 | ll ans = 1; 30 | for ( i = 2; i < n; i++) { 31 | if (i != n / 2) { 32 | ans = ans * i; 33 | } 34 | 35 | } 36 | 37 | cout << ans << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1435/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; 29 | ll a[n], b[n]; 30 | for (i = 0; i < n; i++) cin >> a[i]; 31 | for (i = 1; i < n; i += 2) { 32 | ll v1 = a[i]; 33 | ll v2 = a[i - 1]; 34 | b[i] = -a[i - 1]; 35 | b[i - 1] = a[i]; 36 | } 37 | for (i = 0; i < n; i++) cout << b[i] << " "; 38 | cout << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1436/problem/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int64_t mod=1e9+7; 4 | int main () { 5 | ios::sync_with_stdio(0); 6 | cin.tie(0); 7 | int n,x,pos; 8 | cin>>n>>x>>pos; 9 | int64_t numl=x-1,numr=n-x,sol=1,cnt=0; 10 | int l=0,r=n; 11 | while (lpos){ 20 | sol=(sol*numr)%mod; 21 | numr--; 22 | cnt++; 23 | r=mid; 24 | } 25 | else l=mid+1; 26 | } 27 | for (int i=1;i<=n-cnt-1;i++) sol=(sol*i)%mod; 28 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | ll left, right; 29 | cin >> left >> right; 30 | if (right < 2 * left) cout << "YES\n"; 31 | else cout << "NO\n"; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1437/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | ll ct1 = 0, ct2 = 0; i = 1; 29 | cin >> n; 30 | string str; 31 | cin >> str; 32 | while (i < n) { 33 | if (str[i] == str[i - 1] && str[i] == '1') ct1++; 34 | else if (str[i] == str[i - 1] && str[0] == '1') ct2++; 35 | i++; 36 | } 37 | ans = max(ct1, ct2); 38 | cout << ans << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1445/problem/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | //vector vect; 29 | ll a, b, c, d; 30 | cin >> a >> b >> c >> d; 31 | ll arr[3]; 32 | arr[0] = a + b; arr[1] = c + d; arr[2] = a + c; 33 | sort(arr, arr + 3); 34 | cout << arr[1] << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/1465/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 0; 26 | cin >> t; 27 | while (t--) { 28 | cin >> n; 29 | string s; cin >> s; 30 | ll c = 0; 31 | for (ll i = n - 1; i >= 0; i--) 32 | { 33 | if (s[i] == ')')c++; 34 | else break; 35 | } 36 | if (c > (n - c))cout << "Yes" << endl; 37 | else cout << "No" << endl; 38 | 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /https:/codeforces.com/contest/192/problem/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll inf=(ll)1e18; 5 | ll mod=(ll)1e9+7; 6 | ll max1 =(ll)1e9; 7 | ll gcd(ll a,ll b){ 8 | if(a%b==0) return b; 9 | else return gcd(b,a%b); 10 | } 11 | int main(){ 12 | ll i,j,k,n,m,ct=0,t,ans=0,flag=0; 13 | cin>>n; 14 | set dict; 15 | for(i=1;i<=100000;i++) dict.insert(i*(i+1)/2); 16 | auto it=dict.begin(); 17 | while(*it 2 | using namespace std; 3 | const int maxn=100005; 4 | int n,m,k; 5 | long long a[maxn],l[maxn],r[maxn],d[maxn],op[maxn],aux[maxn],x,y; 6 | int main() 7 | { 8 | cin>>n>>m>>k; 9 | for(int i=1;i<=n;i++) 10 | cin>>a[i]; 11 | for(int i=1;i<=m;i++) 12 | cin>>l[i]>>r[i]>>d[i]; 13 | for(int i=1;i<=k;i++) 14 | cin>>x>>y,op[x]++,op[y+1]--; 15 | for(int i=1;i<=m;i++) 16 | op[i]+=op[i-1],aux[l[i]]+=d[i]*op[i],aux[r[i]+1]-=d[i]*op[i]; 17 | for(int i=1;i<=n;i++) 18 | aux[i]+=aux[i-1],cout< 4 | using namespace std; 5 | typedef long long ll; 6 | struct arr{ 7 | ll first,second; 8 | }; 9 | bool comp(struct arr c,struct arr d){ 10 | if(c.first>n>>d; 19 | struct arr *a = (struct arr*)malloc(sizeof(struct arr)*n); 20 | for(i=0;i>a[i].first>>a[i].second; 21 | sort(a,a+n,comp); 22 | i=0;j=0;ll diff=0;ll max1=0; 23 | while(j 2 | using namespace std; 3 | typedef long long ll; 4 | ll max1 =(ll)1e9; 5 | int main(){ 6 | ll i,j,k,n,m,ct=0,t,ans=0,a,b,c,d; 7 | cin>>n>>a>>b>>c>>d; 8 | for(i=1;i<=n;i++){ 9 | vector vec; 10 | vec.push_back(a+b+i);vec.push_back(b+d+i); 11 | vec.push_back(d+c+i);vec.push_back(a+c+i); 12 | sort(vec.begin(),vec.end()); 13 | ll max_sum=vec[0]+n; 14 | ll min_sum=vec[3]+1; 15 | if(max_sum>=min_sum) ans+=(max_sum-min_sum)+1; 16 | else ans+=0; 17 | } 18 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | ll max1 =(ll)1e9; 5 | int main(){ 6 | ll i,j,k,n,m,ct=0,t,ans=0,flag=0; 7 | cin>>n>>m; 8 | set dict; 9 | for(i=1;i<=sqrt(n);i++){ 10 | if(n%i==0){ 11 | dict.insert(i); 12 | dict.insert(n/i); 13 | } 14 | } 15 | k=1; 16 | for(auto it=dict.begin();it!=dict.end();it++){ 17 | if(k==m){ 18 | cout<<*it< 2 | using namespace std; 3 | typedef long long ll; 4 | using namespace std; 5 | #define fo(i,n) for(i=0;i 8 | #define vec vector 9 | #define pb push_back 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | #define clr(x) memset(x, 0, sizeof(x)) 14 | #define init(x, a) memset(x, a, sizeof(x)) 15 | ll inf = (ll)1e18; 16 | ll mod = 1000000001; 17 | ll max1 = (ll)1e9; 18 | int main() { 19 | ll i, j, k, n, m, ct = 0, t, ans = 0, a, b; 20 | cin >> n >> k >> a >> b; 21 | j = n; 22 | if (k == 1) cout << (n-1)*a; 23 | else { 24 | while (j > 1) { 25 | if (j % k != 0) { 26 | if(j 2 | using namespace std; 3 | typedef long long ll; 4 | #define fo(i,n) for(i=0;i 7 | #define vec vector 8 | #define pb push_back 9 | #define in insert 10 | #define mp make_pair 11 | #define F first 12 | #define S second 13 | //memset(x, 0, sizeof(x)) 14 | ll inf = (ll)1e18; 15 | ll mod = 1e9 + 7; 16 | ll max1 = (ll)1e9; 17 | int main() { 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | freopen("err.txt", "w", stderr); 22 | #endif 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | ll i, j, k, n, m, ct = 0, t, ans = 1; 26 | cin >> n >> m; 27 | ll a[n], b[1005] = {0}; 28 | map dict; 29 | for (i = 0; i < n; i++) { 30 | cin >> a[i]; 31 | } 32 | if (n <= m) { 33 | for (i = 0; i < n; i++) { 34 | for (j = i + 1; j < n; j++) { 35 | ll sum = abs(a[j] - a[i]); 36 | ans = (ans * sum) % m; 37 | } 38 | } 39 | cout << ans % m; 40 | } 41 | else cout << "0"; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /https:/www.codechef.com/COOK119B/problems/CACHEHIT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | int main() { 6 | 7 | ll i, j, k, n, m, ct = 0, t, ans = 0; 8 | cin >> t; 9 | while (t--) { 10 | ll b; 11 | cin >> n >> b >> m; 12 | k = (n + b - 1) / b; 13 | j = 0; 14 | ct = 0; 15 | ll bl = -1; 16 | for (i = 0; i < m; i++) { 17 | cin >> j; 18 | ll curr_block = j / b + 1; 19 | if (curr_block != bl) { 20 | ct++; 21 | bl = curr_block; 22 | } 23 | } 24 | cout << ct << "\n"; 25 | } 26 | return 0; 27 | } 28 | --------------------------------------------------------------------------------