├── Array ├── Array Maths │ ├── 2D Array Multiplication │ │ ├── Sol_by_Pandz18.cpp │ │ └── readme.md │ ├── Product Of Two Arrays │ │ └── Sol.cpp │ ├── Sum of Two Arrays │ │ └── sol.cpp │ └── Sum of elements │ │ └── Sol.cpp ├── Array Modification │ ├── Deletion │ │ └── Solution_1.cpp │ ├── Insert element │ │ └── Sol.cpp │ ├── Is Sorted │ │ └── Sol.cpp │ ├── Merge two sorted arrays │ │ └── merge.cpp │ ├── Moving All Zeros To The End │ │ └── Sol.cpp │ ├── Remove Duplicate Elements │ │ └── Sol.cpp │ └── Reverse Array │ │ └── Sol.cpp ├── Average of elements │ ├── Avg.cpp │ └── average_of_elements_of_an_array.cpp ├── Best Time to Buy and Sell Stock │ ├── README.md │ └── Solution_1.cpp ├── Count Frequency Of All Elements │ └── Sol.cpp ├── Largest Element │ └── Sol.cpp ├── Leaders In Array │ └── Sol.cpp ├── Maximum Consecutive 1's in a Binary Array │ └── Sol.cpp ├── Rotating an Array │ ├── Cyclically rotate an array by one │ │ ├── README.md │ │ └── Solution_1.cpp │ ├── Left Rotate Array By d │ │ └── Sol.cpp │ ├── Left Rotate By One │ │ └── Sol.cpp │ └── Right Rotation By d │ │ └── Sol.cpp ├── Searching │ ├── Binary Search │ │ └── Sol.cpp │ ├── Linear search │ │ └── Sol.cpp │ └── Max Number In Array │ │ ├── Solution.cpp │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md ├── Sorting │ ├── Bubble Sort │ │ └── Sol.cpp │ ├── Insertion Sort │ │ ├── Sol.cpp │ │ ├── Solution_1.cpp │ │ └── Solution_By_Shourya.cpp │ ├── Merge_sort.cpp │ ├── Solution_By_Yashasvi.cpp │ ├── array sorting.cpp │ ├── pr8.cpp │ ├── quick_sort.cpp │ └── sorting.cpp ├── Swapping │ └── Solution_by_Pandz18.md └── Trapping Rain Water │ ├── README.md │ └── Solution_1.cpp ├── Backtracking ├── N-Queen Problem │ ├── README.md │ └── Solution_1.cpp ├── Palindromic Partitions │ ├── README.md │ └── Solution_1.cpp ├── Print all permutations of a given string │ └── Solution_1.cpp ├── Rat In A Maze.cpp ├── Word Break Problem │ ├── README.md │ └── Solution_1.cpp └── permutations.cpp ├── Basic Math ├── Basic factorials │ └── Sol.cpp ├── Digit Counting │ └── Count_the_digits.cpp ├── Distance Between Two points │ └── distance_between_2_points.cpp ├── Increment and Decrement │ └── Sol_by_Pandz.cpp ├── Modulus Average │ └── Avg.cpp ├── Modulus Division │ └── Dividing.cpp ├── Product Of Two Numbers │ └── Sol.cpp └── Subtraction │ └── Sub.cpp ├── Bill_With_Discount ├── README.md └── solution.cpp ├── Binary Index Tree ├── Inversion Count.cpp ├── Prefix Max Element Query.cpp └── Range Sum Query.cpp ├── Binary Search ├── Calculating n-th real root using binary search │ ├── README.md │ └── Solution.cpp ├── Implementation.cpp ├── Problem 1 │ ├── README.md │ └── Solution_1.cpp └── Problem 2 │ ├── README.md │ └── Solution_1.cpp ├── Binary Tree ├── Construction of a Binary Tree from Inorder and Preorder traversal.cpp ├── Height_of_Tree.cpp ├── Inorder_Traversal.cpp ├── LCA.cpp ├── Postorder_Traversal.cpp ├── Preorder_Build_And_Print.cpp ├── Print_Node_At_Distance_K.cpp ├── Tree Traversal In Spiral Form.cpp ├── preorder_traversal.cpp └── serialize_binarytree.cpp ├── Bit_Manipulation ├── Calculate square of a number │ ├── README.md │ └── Solution_1.cpp ├── Clear Bit │ ├── README.md │ └── Solution_1.cpp ├── Clear Range I to J.cpp ├── Clear Range Of Bit │ └── Solution_1.cpp ├── Copy set bits in a range │ ├── README.md │ └── Solution_1.cpp ├── Count number of bits to be flipped to convert A to B │ ├── README.md │ └── Solution_1.cpp ├── Decimal_To_Binary │ └── Solution_1.cpp ├── Fast Exponential.cpp ├── Find position of the only set bit │ ├── README.md │ └── Solution_1.cpp ├── Find the two non-repeating elements in an array of repeating elements │ ├── README.md │ └── Solution_1`.cpp ├── Find_Unique_Number │ ├── README.md │ └── Solution_1.cpp ├── Inserting m into n such that m starts at bit j and ends at bit i │ ├── README.md │ └── Solution_1.cpp ├── Modify a bit at a given position │ ├── README.md │ └── Solution_1.cpp ├── Program to find whether a no is power of two │ ├── README.md │ └── Solution_1.cpp └── Set Bit │ ├── README.md │ └── Soltuion_1.cpp ├── CONTRIBUTING.md ├── Class ├── BIG CLASS │ └── solution_by_Pandz18.cpp ├── Calculator │ └── Sol.cpp └── Maximum and Minimum │ └── Sol.cpp ├── CodeChef └── Practice Solutions │ ├── Ambiguous Permutations │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Beginner │ ├── A Good Set │ │ ├── Sol.cpp │ │ └── readme.md │ ├── ATM │ │ ├── Solution_By_Shourya.cpp │ │ ├── Solution_by_Pandz18.cpp │ │ └── question.md │ ├── Ada King │ │ ├── readme.md │ │ └── solution.cpp │ ├── Add Two Numbers │ │ ├── Solution_By_Yashasvi.cpp │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Ads and Dishes │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Chain Reaction │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Chef And Coloring │ │ ├── readme.md │ │ └── sol_by_Pandz18.cpp │ ├── Chef And Operators │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Chef and Dolls │ │ ├── Sol_by_Pandz18.cpp │ │ └── readme.md │ ├── Chef and Glove │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Chef and Remissness │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Chef and Steps │ │ ├── Sol_by_Pandz18.cpp │ │ └── readme.md │ ├── Cutting Recipies │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Decrement OR Increment │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Easy Math │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Enormous Input Test │ │ ├── Solution_by_Pandz.cpp │ │ └── readme.md │ ├── Even Differences │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Event │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Fancy Quotes │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Find Remainder │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Finding Square Root │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── First and Last Digit │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Forgotten Language │ │ ├── Sol_by_Pandz18.cpp │ │ └── readme.md │ ├── Hard Cash │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Helping Chef │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── How many cuts │ │ └── readme.md │ ├── Id and Ship │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Jumping in the hills │ │ ├── Sol.cpp │ │ └── readme.md │ ├── K Fibonnaci │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Lapindrome │ │ ├── Sol_by_Pandz18.cpp │ │ └── readme.md │ ├── Lucky Four │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Mahasena │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Nobel Prize │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Number Mirror │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Packaging Cupcakes │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Pair Me │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Piece Of Cake │ │ ├── Sol_by_Pandz18.cpp │ │ └── readme.md │ ├── Playing with Strings │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Primality Test │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Reduce to One │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Reverse The Number │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Safe Robots │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Second Largest │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Small Factorial │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Small Factorials │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Smallest Numbers of Notes │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Smart Phone │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Smart Strategy │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Split Stones │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Sum OR Difference │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Sum of Digits │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Tanu and Head-Bob │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── The Block Game │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── The Promised Land │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Three Way communications │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Turbo Sort │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Two Numbers │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Valid Triangles │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ └── Weapon Value │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Fit Squares in Triangle │ ├── Solution_By_deenu-1001 │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── GCD and LCM │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Grade The Steel │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Gross Salary │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Lapindromes │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Puppy and Sum │ ├── Solution_By_deenu-1001 │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Simple Statistics │ ├── Problem.md │ └── Sol.cpp │ ├── Sums in a Triangle │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ └── Total Expenses │ ├── Solution_by_Pandz18.cpp │ └── readme.md ├── Daily Coding Problem ├── Day 1 │ ├── Sol.cpp │ └── problem.md ├── Day 10 │ └── Problem.md ├── Day 11 │ └── Problem.md ├── Day 131 │ └── Problem.md ├── Day 132 │ └── Problem.md ├── Day 133 │ └── Problem.md ├── Day 134 │ └── Problem.md ├── Day 135 │ └── Problem.md ├── Day 136 │ └── Problem.md ├── Day 137 │ └── Problem.md ├── Day 138 │ └── Problem.md ├── Day 139 │ └── Problem.md ├── Day 140 │ └── Problem.md ├── Day 142 │ └── Problem.md ├── Day 2 │ ├── problem.md │ └── sol.cpp ├── Day 3 │ └── problem.md ├── Day 4 │ ├── problem.md │ └── sol.cpp ├── Day 5 │ └── problem.md ├── Day 6 │ └── problem.md ├── Day 7 │ └── problem.md ├── Day 8 │ └── problem.md ├── Day 9 │ └── Problem.md ├── Day12 │ └── Problem.md ├── Day13 │ └── Problem.md ├── Day14 │ └── Problem.md ├── Day143 │ └── Problem.md ├── Day144 │ └── Problem.md ├── Day145 │ └── Problem.md ├── Day146 │ └── Problem.md ├── Day147 │ └── Problem.md ├── Day148 │ └── Problem.md ├── Day149 │ └── Problem.md ├── Day15 │ └── Problem.md ├── Day150 │ └── Problem.md ├── Day151 │ └── Problem.md ├── Day16 │ └── Problem.md ├── Day17 │ └── Problem.md ├── Day18 │ └── Problem.md ├── Day19 │ └── Problem.md ├── Day20 │ └── Problem.md └── Day21 │ └── Problem.md ├── Daily Interview Pro ├── Add two numbers as linked list │ └── problem.md ├── Create a Simple Calculator │ └── Problem.md ├── Edit Distance │ └── Problem.md ├── Find Cycles in a Graph │ └── Problem.md ├── Find Pythagorean Triplets │ └── Problem.md ├── Find the non-duplicate number │ └── Problem.md ├── First and Last indices of an element in a sorted array │ └── problem.md ├── Floor and Ceiling of a Binary Search Tree │ └── Problem.md ├── Invert a Binary Tree │ └── Problem.md ├── Longest Palindromic Substring │ └── problem.md ├── Longest Substring Without Repeating Characters │ └── problem.md ├── Maximum In A Stack │ └── Problem.md ├── Non-decreasing Array with Single Modification │ └── Problem.md ├── Number of Ways to Climb Stairs │ └── Problem.md ├── Reverse A Linked List │ └── problem.md ├── Sorting a list with 3 unique numbers │ └── Problem.md ├── Two-Sum │ └── Problem.md ├── Validate Balanced Parantheses │ └── problem.md └── Word Search │ └── Problem.md ├── Dynamic Programming ├── Array Jump Problem │ ├── README.md │ └── Solution_1.cpp ├── Box Stacking │ ├── README.md │ └── Solution_1.cpp ├── Coin Change Problem │ ├── Bottom Up Approach.cpp │ ├── README.md │ └── Top Down Approach.cpp ├── Count Tree │ ├── Bottom Up Approach.cpp │ ├── README.md │ └── Top Down Approach.cpp ├── Fibonacci │ ├── Bottom Up Approach With Space optimisation.cpp │ ├── Bottom Up Approach.cpp │ └── Top Down Approach.cpp ├── Frog's Min Cost │ ├── README.md │ └── Solution_1.cpp ├── Knapsack Problem │ ├── Bottom Up Solution.cpp │ └── README.md ├── Ladders Problem │ ├── Bottom Up Approach.cpp │ ├── Optimised Approach.cpp │ ├── README.md │ └── Top Down Approach.cpp ├── Longest Common Subsequence │ ├── README.md │ └── Solution_1.cpp ├── Longest Increasing Sequence │ ├── README.md │ └── Solution_1.cpp ├── Max Non Adjacent Sum │ ├── README.md │ └── Solution_1.cpp ├── Rod Cutting Problem │ ├── README.md │ └── Solution_1.cpp ├── Selling Wines │ ├── Bottom Up Approach.cpp │ ├── README.md │ └── Top Down Approach.cpp ├── Subsequence Count │ ├── Bottom Up Approach.cpp │ ├── README.md │ └── Top Down Approach.cpp └── Subset Sum Problem │ ├── README.md │ └── SubsetSum.cpp ├── GeekforGeeks ├── 12 hour clock addition │ └── Problem.md ├── Addition of Two Numbers │ ├── Problem.md │ └── Sol.cpp ├── Area of Rectange, Right Angled Triangle and Circle │ ├── Problem.md │ └── Sol.cpp ├── Armstrong Numbers │ ├── Problem.md │ └── Sol.cpp ├── Array │ ├── Leaders In Array │ │ ├── README.md │ │ └── Solution_1.cpp │ └── Minimize the Heights II │ │ ├── README.md │ │ └── Solution_1.cpp ├── Check if a number is power of another number │ ├── Problem.md │ └── Sol.cpp ├── Closest Number │ └── Problem.md ├── Count Squares │ ├── Problem.md │ └── Sol.cpp ├── Cube root of a number │ └── Problem.md ├── Factorial of Two Large Numbers.cpp ├── Factorial │ ├── Problem.md │ └── Sol.cpp ├── Find Nth root of M │ ├── Problem.md │ └── Sol.cpp ├── Find n-th term of series 1, 3, 6, 10, 15, 21 │ └── Problem.md ├── Find the smallest and second smallest elements in an array │ ├── Problem.md │ └── Sol.cpp ├── GCD of two numbers │ └── Problem.md ├── Greatest of three numbers │ ├── Problem.md │ └── Sol.cpp ├── Largest Element │ └── Sol.cpp ├── Largest prime factor │ └── Problem.md ├── Mean │ ├── Readme.md │ └── Sol.cpp ├── Multiplication Table │ ├── Problem.md │ └── Sol.cpp ├── Multiply Two Matrices │ └── Sol.cpp ├── Odd or Even │ ├── Problem.md │ └── Sol.cpp ├── Pair of Sum │ └── Sol.cpp ├── Parallel or Perpendicular? │ └── Problem.md ├── Perfect Numbers │ ├── Problem.md │ └── Sol.cpp ├── Power of Pow | Even Number │ ├── Problem.md │ └── Sol.cpp ├── Prime Number │ ├── Problem.md │ └── Sol.cpp ├── Print the Kth Digit │ ├── Problem.md │ └── Sol.cpp ├── Reverse Digits │ ├── Problem.md │ └── Sol.cpp ├── Reverse an Array │ ├── Problem.md │ └── Sol.cpp ├── Search an element in a sorted and rotated array │ ├── Problem.md │ └── Sol.cpp ├── Series AP │ ├── Problem.md │ └── Sol.cpp ├── Series GP │ ├── Problem.md │ └── Sol.cpp ├── Sieve of Eratosthenes │ ├── Problem.md │ └── Sol.cpp ├── Small Factorial │ ├── Problem.md │ └── Sol.cpp ├── Sum of Array Elements │ ├── Problem.md │ └── Sol.cpp ├── Sum of Digit is Pallindrome or not │ └── Problem.md ├── Sum of all prime numbers between 1 and N │ ├── Problem.md │ └── Sol.cpp ├── Sum of an AP │ ├── Problem.md │ └── Sol.cpp ├── Sum of elements │ └── Sol.cpp ├── Sum of odd and even elements │ ├── Problem.md │ └── Sol.cpp ├── Swap two numbers │ ├── Problem.md │ └── Sol.cpp ├── The dice problem │ ├── Problem.md │ └── Sol.cpp ├── Tower Of Hanoi │ ├── README.md │ └── Solution_1.cpp ├── nCr │ ├── Problem.md │ └── Sol.cpp ├── nPr │ ├── Problem.md │ └── Sol.cpp └── problem.md ├── Graph ├── Bellman Ford Algorithm.cpp ├── Bipartite Graph Check.cpp ├── Check If Unidirected Graph Is A Tree Or Not.cpp ├── Connected Components Count.cpp ├── Cycle Detectin Undirected Graph Using DFS.cpp ├── Cycle Detection Directed Graph Using DFS.cpp ├── Cycle Detection In Undirected Graph Using DSU.cpp ├── Dijkstra's Algorithm.cpp ├── Flood Fill Algorithm.cpp ├── Implementation.cpp ├── Minimum Spanning Tree │ ├── Kruskal's Algorithm │ │ └── Solution_1.cpp │ └── Prim's Algorithm │ │ └── Solution_1.cpp ├── Pairing Problem │ ├── README.md │ └── Solution_1.cpp ├── Path Compression Optimisation.cpp ├── Shortest Cycle in an Undirected Unweighted Graph │ ├── README.md │ └── Solution_1.cpp ├── Single_Source_Shortest_Path.cpp ├── Snake_And_ladder │ └── Solution_1.cpp ├── Topological Sort Using BFS.cpp ├── Topological Sort Using DFS.cpp ├── Traversal │ ├── BFS.cpp │ └── DFS.cpp └── Union By Rank Optimisation.cpp ├── Greedy Algorithmns ├── Activity_Selection_Problem │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Connecting_Wires │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Defense of a Kingdom │ ├── Problem_Statement.md │ └── Solution_1.cpp └── Load Balancing │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── LICENSE ├── LeetCode ├── Easy │ ├── Fibonacci_Number │ │ ├── Problem_Statement.md │ │ └── Solution_1.cpp │ ├── Longest Common Prefix │ │ └── readme.md │ ├── Majority Element │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Palindrome │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Plus One │ │ └── Sol.cpp │ ├── Reversed Integer │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Roman to Integer │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Search Insert Position │ │ └── Sol.cpp │ ├── Sqrt(x) │ │ └── Sol.cpp │ ├── Two Sum II - Input array is sorted │ │ ├── Problem.md │ │ └── Sol.cpp │ └── Two Sum │ │ ├── Sol.cpp │ │ └── readme.md ├── Hard │ ├── Distinct Subsequences │ │ ├── README.md │ │ └── Solution_1.cpp │ ├── Maximum Height by Stacking Cuboids │ │ ├── README.md │ │ └── Solution_1.cpp │ ├── N-Queens │ │ ├── README.md │ │ └── Solution_1.cpp │ └── Orderly Queue │ │ ├── Orderly Queue.cpp │ │ └── README.md └── Medium │ ├── Clumsy Factorial │ ├── README.md │ └── Solution_1.cpp │ ├── Coin Change DP Problem │ ├── README.md │ └── Solution_1.cpp │ ├── Global and Local Inversions │ ├── README.md │ └── Solution_1.cpp │ ├── House Robber │ ├── README.md │ └── Solution_1.cpp │ ├── Is Graph Bipartite? │ ├── README.md │ └── Solution_1.cpp │ ├── Jump Game II │ ├── README.md │ └── Solution_1.cpp │ ├── Longest Common Subsequence │ ├── README.md │ └── Solution_1.cpp │ ├── Longest Increasing Subsequence │ ├── README.md │ └── Solution_1.cpp │ ├── Longest Substring Without repeating characters │ ├── Longest Substring Without repeating characters.cpp │ └── README.md │ ├── Repeated String Match │ ├── README.md │ └── SolutionByMashiyat.cpp │ ├── Unique Binary Search Trees │ ├── README.md │ └── Solution_1.cpp │ └── Valid Triangle Number │ ├── README.md │ └── Valid Triangle Number.CPP ├── Linked List ├── Circular Linked List │ ├── Delete_Head │ │ ├── Efficient_Solution.cpp │ │ └── Naive_Solution.cpp │ ├── Delete_Kth_Node.cpp │ ├── Implementation.cpp │ ├── Insert_At_Begin │ │ ├── Efficient_Solution.cpp │ │ └── Naive_Solution.cpp │ ├── Insert_At_End │ │ ├── Efficient_Solution.cpp │ │ └── Naive_Solution.cpp │ └── Traversal │ │ ├── Do_while_method.cpp │ │ └── For_Loop_method.cpp ├── Doubly Linked List │ ├── Delete_Head.cpp │ ├── Delete_Last.cpp │ ├── Implementation.cpp │ ├── Insert_At_Begin.cpp │ └── Insert_At_End.cpp └── Singly_Linked_List │ ├── Delete_First_Node.cpp │ ├── Delete_Last_Node.cpp │ ├── DetectLoopInLinkedList.cpp │ ├── Implementation.cpp │ ├── Insert_At_End.cpp │ ├── Insert_At_begin.cpp │ ├── Merge Two Sorted Linked List.cpp │ ├── Middle of the Linked List │ ├── README.md │ └── Solution_1.cpp │ ├── Multiply two numbers represented by Linked Lists │ ├── README.md │ └── Solution_1.cpp │ ├── Program for n’th node from the end of a Linked List │ └── Solution_1.cpp │ ├── Recursive_Traversal.cpp │ ├── Remove duplicate element from sorted Linked List │ ├── README.md │ └── Solution_1.cpp │ ├── Reverse Linked List │ ├── Iterative Method.cpp │ └── Recursive.cpp │ ├── Reverse a Linked List in groups of given size │ ├── README.md │ └── Solutiopn_1.cpp │ ├── Searching │ ├── Iterative_method.cpp │ └── Recursive_method.cpp │ ├── Singly Linked List.cpp │ └── Traverse.cpp ├── Math ├── Big Integers │ └── Big Factorial.cpp └── Birthday Paradox Problem.cpp ├── Problem Statement ├── Problem_Statement 4 │ ├── Palindrome.md │ └── Solution_by_Pandz18.cpp ├── Problem_Statement_1 │ ├── README.md │ └── Solution_1.cpp ├── Problem_Statement_11 │ ├── Sol_by_Pandz18.cpp │ └── readme.md ├── Problem_Statement_12 │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Problem_Statement_13 │ ├── README.md │ └── Solution_1.cpp ├── Problem_Statement_2 │ ├── README.md │ └── Solution_1.cpp ├── Problem_Statement_3 │ ├── README.md │ └── Solution_1.cpp ├── Problem_Statement_6 │ ├── Divisibility.md │ └── Solution_By_Pandz18.cpp ├── Problem_Statement_7 │ ├── Maximum_Consecutive_1s.md │ └── Solution_1.cpp ├── Problem_Statement_8 │ ├── Lucas Numbers.md │ └── Solution_by_Pandz18.cpp ├── Problem_Statement_9 │ ├── PadovanSequence.md │ └── Solution_by_Pandz18.cpp ├── Problem_Statment_5 │ ├── PrimeNumbers.md │ └── Solution_by_Pandz18.cpp └── Problem_statement_10 │ ├── EvenOrOdd.md │ └── Solution_by_Pandz18.cpp ├── README.md ├── Recursion ├── 0 1 Knapsack Problem.cpp ├── Array Is Sorted Or Not.cpp ├── Decreasing Order.cpp ├── Factorial │ ├── Factorial.md │ └── Solution_by_Pandz18.cpp ├── Fibonacci │ └── Solution_1.cpp ├── First Occurrence │ ├── README.md │ └── Solution_1.cpp ├── GCD │ ├── GCD.md │ └── Solution_by_Pandz18.cpp ├── Generate Balanced Brackets.cpp ├── Increasing Order.cpp ├── Infinite Series │ ├── Sol.cpp │ └── infinite.cpp ├── Ladders Problem.cpp ├── Number to Spelling.cpp ├── Place The Tiles.cpp ├── Power of number.cpp ├── Print_1_to_N_using_recursion │ └── Solution_1.cpp ├── Print_N_to_1_Using_recursion │ └── Solution_1.cpp ├── Series │ └── Series.cpp ├── String To Integers.cpp ├── Subsequence Generation.cpp ├── Sum_Of_Digits │ ├── README.md │ └── Solution.cpp ├── Tower of Hanoi │ ├── README.md │ └── Solution_1.cpp └── last Occurrence │ ├── README.md │ └── Solution_1.cpp ├── STL ├── Binary Search.cpp ├── Find Function.cpp └── Strings in STL.cpp ├── Searching ├── Count_1s_in_a_Binary_Array │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Interpolation Search │ ├── Interpolation.cpp │ └── Problem_Statement.md └── Search_In_Infinite_Sized_Array │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Trie ├── Implementation.cpp └── Problem 1 │ ├── README.md │ └── Solution_1.cpp └── azure-pipelines.yml /Array/Array Maths/Product Of Two Arrays/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, A[100] , B[100]; 7 | cin>>n; 8 | for(int i=0 ; i>A[i]; 11 | } 12 | for(int j=0 ; j>B[j]; 15 | } 16 | 17 | for(int k=0 ; k 2 | using namespace std; 3 | 4 | int search(int n, int x, int a[]){ 5 | for(int i =0;i>n; 14 | int arr[n]; 15 | for(int i=0;i>arr[i]; 17 | } 18 | int x; 19 | cin>>x; 20 | cout< 2 | using namespace std; 3 | 4 | int isSorted(int arr[], int n){ 5 | for(int i=1;i 2 | using namespace std; 3 | 4 | void MoveZeros(int arr[], int n){ 5 | // Count of non-zero elements 6 | int count = 0; 7 | // Traverse the array. If arr[i] is non-zero, then 8 | // swap the element at index 'count' with the 9 | // element at index 'i' 10 | for(int i=0;i 2 | using namespace std; 3 | 4 | int removeDup(int arr[], int n){ 5 | int res = 1; 6 | for(int i=1;i 2 | using namespace std; 3 | 4 | void reverseArray(int arr[], int n){ 5 | int low = 0,high = n-1; 6 | while(low& prices) { 4 | 5 | if (prices.size() == 0) return 0; 6 | 7 | int mn = INT_MAX; 8 | int ans = 0; 9 | 10 | for (int i = 0; i < prices.size(); i++) { 11 | if (prices[i] < mn) mn = prices[i]; 12 | ans = max(ans, prices[i]-mn); 13 | } 14 | 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Array/Largest Element/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int largest_number(int arr[], int n){ 5 | int res= 0; 6 | for(int i=1;iarr[res]) 8 | res = i; 9 | } 10 | return res; 11 | } 12 | 13 | 14 | 15 | int main(){ 16 | int arr[]={71,42,13,4,95}; 17 | int n = sizeof(arr)/sizeof(arr[0]); 18 | cout< 5 | using namespace std; 6 | 7 | void Leadersinaray(int arr[], int n){ 8 | int current_ldr = arr[n-1]; 9 | cout<=0;i--){ 11 | if(current_ldr 5 | using namespace std; 6 | 7 | int maxConOnes(bool arr[], int n){ 8 | int res=0,curr=0; 9 | for(int i =0;i0;i--) 5 | arr[i] = arr[i-1]; 6 | arr[0] = temp; 7 | } 8 | -------------------------------------------------------------------------------- /Array/Rotating an Array/Left Rotate Array By d/Sol.cpp: -------------------------------------------------------------------------------- 1 | // IP:- 1,2,3,4,5 2 | // k =2 3 | // OP:- 3 4 5 1 2 4 | 5 | 6 | #include 7 | using namespace std; 8 | 9 | // left rotate by d 10 | void lrotateByD(int arr[], int n, int d){ 11 | int temp[d]; 12 | for(int i = 0; i 7 | using namespace std; 8 | 9 | // left rotate by one 10 | void lrotatebyOne(int arr[], int n){ 11 | int temp = arr[0]; 12 | for(int i =1; i 2 | using namespace std; 3 | 4 | int search(int n, int x, int a[]){ 5 | for(int i =0;i>n; 14 | int arr[n]; 15 | for(int i=0;i>arr[i]; 17 | } 18 | int x; 19 | cin>>x; 20 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Get the array 7 | int arr[] = { 1, 45, 54, 71, 76, 12 }; 8 | 9 | // Compute the sizes 10 | int n = sizeof(arr) / sizeof(arr[0]); 11 | 12 | // Print the array 13 | cout << "Array: "; 14 | for (int i = 0; i < n; i++) 15 | cout << arr[i] << " "; 16 | 17 | // Find the maximum element 18 | cout << "\nMax Element = " 19 | << *max_element(arr, arr + n); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Array/Sorting/Bubble Sort/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main () 4 | { 5 | int i, j, n,temp=0; 6 | int a[100]; 7 | cin>>n; 8 | 9 | for(i = 0; i>a[i]; 12 | } 13 | 14 | for(i = 0; i 2 | using namespace std; 3 | 4 | void insertionSort(int arr[], int n) 5 | { 6 | for(int i=1;i=0 && arr[j]>key) 11 | { 12 | arr[j+1]=arr[j]; 13 | j--; 14 | } 15 | arr[j+1]=key; 16 | } 17 | } 18 | 19 | 20 | int main() { 21 | 22 | int arr[]={50,20,40,60,10,30}; 23 | 24 | int n=sizeof(arr)/sizeof(arr[0]); 25 | insertionSort(arr,n); 26 | 27 | for(auto x: arr) 28 | cout< 2 | using namespace std; 3 | void insert_sort(int a[], int n){ 4 | int temp, k; 5 | for (int i = 1; i < n; i++){ 6 | k = i; 7 | temp = a[k]; 8 | for (int j = i - 1; j >= 0; j--){ 9 | if (a[j] > temp){ 10 | a[k] = a[j]; 11 | k--; 12 | } 13 | } 14 | a[k] = temp; 15 | } 16 | for (int i = 0; i < n; i++){ 17 | cout << a[i] << " "; 18 | } 19 | } 20 | int main(){ 21 | int n; 22 | cin >> n; 23 | int a[n]; 24 | for (int i = 0; i < n; i++){ 25 | cin >> a[i]; 26 | } 27 | insert_sort(a, n); 28 | } 29 | -------------------------------------------------------------------------------- /Array/Trapping Rain Water/README.md: -------------------------------------------------------------------------------- 1 | Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. 2 | 3 | Examples: 4 | ``` 5 | Input: arr[] = {2, 0, 2} 6 | Output: 2 7 | ``` 8 | ``` 9 | Input: arr[] = {3, 0, 2, 0, 4} 10 | Output: 7 11 | ``` 12 | -------------------------------------------------------------------------------- /Backtracking/Palindromic Partitions/README.md: -------------------------------------------------------------------------------- 1 | Given a string, find all possible palindromic partitions of given string. 2 | 3 | Example: 4 | ``` 5 | I/P:- nitin 6 | O/P:- n i t i n 7 | n iti n 8 | nitin 9 | ``` 10 | 11 | ``` 12 | I/P:- geeks 13 | O/P:- g e e k s 14 | g ee k s 15 | ``` 16 | 17 | [Problem Link](https://www.geeksforgeeks.org/given-a-string-print-all-possible-palindromic-partition/) 18 | -------------------------------------------------------------------------------- /Backtracking/Print all permutations of a given string/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | ``` 2 | I/P:- ABC 3 | O/P:- ABC 4 | ACB 5 | BAC 6 | BCA 7 | CBA 8 | CAB 9 | ``` 10 | 11 | #include 12 | using namespace std; 13 | void permute(string a, int l, int r){ 14 | // base case 15 | if(l==r){ 16 | cout< 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int a , i , fact=1; 7 | cin>>a; 8 | 9 | for(i=1;i<=a;i++) 10 | { 11 | fact=fact*i; 12 | } 13 | 14 | cout<<"Fact : "<<" "< 2 | using namespace std; 3 | 4 | 5 | int countDigit(long n){ 6 | int count=0; 7 | while(n!=0){ 8 | n=n/10; 9 | count++; 10 | } 11 | return count; 12 | } 13 | 14 | int main(){ 15 | int long a,b; 16 | cout<<"Enter the number"<>a; 18 | b=countDigit(a); 19 | cout< 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int n; 7 | cin>>n; 8 | cout< 3 | using namespace std; 4 | int main() 5 | { 6 | int a,b, sum=0 , avg=0; 7 | cout<<"Enter two numbers:"; 8 | cin>>a>>b; 9 | sum=a+b; 10 | avg=mod(sum/2); 11 | 12 | cout<<"Modulus average is "< 2 | using namespace std; 3 | int main() 4 | { 5 | int a,b,div; 6 | cout<<"Enter two numbers:"; 7 | cin>>a>>b; 8 | div=mod(a/b); 9 | cout<<"Modulus Division is "< 2 | using namespace std; 3 | int main() 4 | { 5 | int n1,n2,pr; 6 | cout<<"Enter two numbers:"; 7 | cin>>n1>>n2; 8 | pr=n1*n2; 9 | cout<<"Product of "<<" "< 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a,b,dif; 8 | cout<<"Enter two numbers:"; 9 | cin>>a>>b; 10 | dif=a+b; 11 | cout<<"Subtraction is "<= 100 and less than 500 then 10% discount. 6 | ### If bill amount >= 500 then 20% discount. 7 | -------------------------------------------------------------------------------- /Bill_With_Discount/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | float billamount; 5 | float discount=0.0; 6 | 7 | cout<<"Enter the bill amount: "; 8 | cin>>billamount; 9 | if(billamount>=500) 10 | discount=billamount*20/100; 11 | else if (billamount>=100 && billamount<=500) 12 | { 13 | discount=billamount*10/100; 14 | } 15 | cout<<"Bill amount is "<e){ 5 | return NULL; 6 | } 7 | //Rec Case 8 | node *root = new node(pre[i]); 9 | 10 | int index=-1; 11 | for(int j=s;s<=e;j++){ 12 | if(in[j]==pre[i]){ 13 | index = j; 14 | break; 15 | } 16 | } 17 | 18 | i++; 19 | root->left = createTreeFromTrav(in,pre,s,index-1); 20 | root->right = createTreeFromTrav(in,pre,index+1,e); 21 | return root; 22 | } 23 | -------------------------------------------------------------------------------- /Binary Tree/Height_of_Tree.cpp: -------------------------------------------------------------------------------- 1 | int height(node*root){ 2 | if(root == NULL){ 3 | return 0; 4 | } 5 | int ls = height(root->left); 6 | int rs = height(root->right); 7 | return max(ls,rs) + 1; 8 | } 9 | -------------------------------------------------------------------------------- /Binary Tree/Inorder_Traversal.cpp: -------------------------------------------------------------------------------- 1 | void inorder(node *root){ 2 | if(root != NULL){ 3 | inorder(root->left); 4 | cout<<(root->key)<<" "; 5 | inorder(root->right); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Binary Tree/Postorder_Traversal.cpp: -------------------------------------------------------------------------------- 1 | void postorder(Node *root){ 2 | if(root != NULL){ 3 | postorder(root->left); 4 | postorder(root->right); 5 | cout<<(root->key)<<" "; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Binary Tree/Print_Node_At_Distance_K.cpp: -------------------------------------------------------------------------------- 1 | void printkDist(Node*root, int k){ 2 | if(root == NULL) 3 | return; 4 | if(k==0){ 5 | cout<<(root->key)<<" "; 6 | } 7 | else{ 8 | printkDist(root->left,k-1); 9 | printkDist(root->right,k-1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Binary Tree/preorder_traversal.cpp: -------------------------------------------------------------------------------- 1 | void printPost(node*root){ 2 | if (root == NULL){ 3 | return; 4 | } 5 | printPost(root->left); 6 | printPost(root->right); 7 | cout<data<<" "; 8 | } 9 | -------------------------------------------------------------------------------- /Bit_Manipulation/Calculate square of a number/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int square(int n){ 5 | // base case 6 | if(n==0){ 7 | return 0; 8 | } 9 | if(n<0){ 10 | n = -n; 11 | } 12 | int x = n>>1; 13 | // if it is odd 14 | if(n&1){ 15 | return ((square(x)<<2) + (x<<2) + 1); 16 | } 17 | else{ 18 | return (square(x)<<2); 19 | } 20 | } 21 | int main(){ 22 | int n; 23 | cin>>n; 24 | //for (int n = 1; n <= 5; n++) 25 | cout << "n = " << n << ", n^2 = " << square(n)<< endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Bit_Manipulation/Clear Bit/README.md: -------------------------------------------------------------------------------- 1 | Clearing a bit means that if K-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged. 2 | 3 | ``` 4 | Input: N = 5, K = 1 5 | Output: Clearing Kth bit: 4 6 | ``` 7 | -------------------------------------------------------------------------------- /Bit_Manipulation/Clear Bit/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void clearBit(int &n, int i){ 5 | int mask = ~(1<>n>>i; 11 | clearBit(n,i); 12 | cout< 2 | using namespace std; 3 | 4 | int clearRangeItoJ(int n, int i, int j){ 5 | int ones = (~0); 6 | int a = ones<<(i+j); 7 | int b = (1<>n>>i>>j; 15 | cout< 2 | using namespace std; 3 | 4 | int clearRange(int n, int i){ 5 | int mask = (-1<>n>>i; 11 | cout< 2 | using namespace std; 3 | 4 | void copySetBits(unsigned &x, unsigned y, unsigned l, unsigned r){ 5 | // base case 6 | if(l<1 || r>32){ 7 | return; 8 | } 9 | for(int i=l;i<=r;i++){ 10 | int mask = 1<<(i-1); 11 | if(y&mask){ 12 | x = x | mask; 13 | } 14 | } 15 | } 16 | int main(){ 17 | unsigned x = 10, y = 13, l = 2, r = 3; 18 | copySetBits(x, y, l, r); 19 | cout<< x; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Bit_Manipulation/Count number of bits to be flipped to convert A to B/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countsetBits(int n){ 5 | int count = 0; 6 | while(n>0){ 7 | n &=(n-1); 8 | count++; 9 | } 10 | return count; 11 | } 12 | int FlippedCount(int a, int b){ 13 | return countsetBits(a^b); 14 | } 15 | 16 | 17 | int main(){ 18 | int a = 7; 19 | int b = 10; 20 | cout << FlippedCount(a, b)< 2 | using namespace std; 3 | 4 | int decimaltoBinary(int n){ 5 | int ans =0; 6 | int p=1; // 10^0 =1 7 | 8 | while(n>0){ 9 | int last_bit = (n&1); 10 | ans +=p*last_bit; 11 | p=p*10; // updating power of p 12 | n=n>>1; 13 | } 14 | 15 | return ans; 16 | } 17 | 18 | int main(){ 19 | int n; 20 | cin>>n; 21 | cout< 2 | using namespace std; 3 | 4 | int isPowerTwo(unsigned n){ 5 | return n && (!(n&(n-1))); 6 | } 7 | int findPosition(unsigned n){ 8 | if(!isPowerTwo(n)){ 9 | return -1; 10 | } 11 | unsigned i=1,pos=1; 12 | while((i & n) == 0){ 13 | i = i<<1; 14 | pos++; 15 | } 16 | return pos; 17 | } 18 | int main(){ 19 | int n = 128; 20 | int pos = findPosition(n); 21 | cout <<" Position " << pos << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Bit_Manipulation/Find the two non-repeating elements in an array of repeating elements/README.md: -------------------------------------------------------------------------------- 1 | Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. 2 | 3 | Example 1: 4 | ``` 5 | Input: 6 | N = 2 7 | arr[] = {1, 2, 3, 2, 1, 4} 8 | Output: 9 | 3 4 10 | Explanation: 11 | 3 and 4 occur exactly once. 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: 18 | N = 1 19 | arr[] = {2, 1, 3, 2} 20 | Output: 21 | 1 3 22 | Explanation: 23 | 1 3 occur exactly once. 24 | ``` 25 | 26 | Expected Time Complexity: O(N) 27 | 28 | Expected Space Complexity: O(1) 29 | -------------------------------------------------------------------------------- /Bit_Manipulation/Find_Unique_Number/README.md: -------------------------------------------------------------------------------- 1 | ### I/P 2 | 7 3 | 3,2,3,5,6,5,2 4 | ### O/P:- 5 | 6 6 | -------------------------------------------------------------------------------- /Bit_Manipulation/Find_Unique_Number/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | 8 | int no; 9 | int ans=0; 10 | for(int i=0;i>no; 12 | ans=ans^no; 13 | } 14 | cout< 2 | using namespace std; 3 | 4 | void updateBit(int &n, int i, int v){ 5 | int mask = ~(1<>n>>i>>v; 12 | updateBit(n,i,v); 13 | cout< 2 | using namespace std; 3 | 4 | void countsetBits(int n){ 5 | int count = 0; 6 | while(n>0){ 7 | n &= (n-1); 8 | count++; 9 | } 10 | if(count==1){ 11 | cout<<"Yes"; 12 | } 13 | else{ 14 | cout<<"No"; 15 | } 16 | return; 17 | } 18 | 19 | 20 | int main(){ 21 | int n; 22 | cin>>n; 23 | countsetBits(n); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Bit_Manipulation/Set Bit/README.md: -------------------------------------------------------------------------------- 1 | Setting a Nth bit means that if Nth bit is 0 then set it to 1 and if it is 1 then leave it. 2 | 3 | ``` 4 | I/P:- 5 5 | 0 6 | 7 | O/P:- 7 8 | -------------------------------------------------------------------------------- /Bit_Manipulation/Set Bit/Soltuion_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int setBit(int n, int i){ 5 | int mask = (1<>n>>i; 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | while(n>0) 8 | { 9 | int a[100000],f=0; 10 | for(int i=1;i<=n;i++) 11 | { 12 | cin>>a[i]; 13 | } 14 | for(int i=1;i<=n;i++) 15 | { 16 | if(a[a[i]]!=i) 17 | { 18 | f=1; 19 | } 20 | } 21 | if(f==0) 22 | { 23 | cout<<"ambiguous"<>n; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/A Good Set/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int a=n; 13 | for(int i=0;i 4 | using namespace std; 5 | 6 | int main() { 7 | int amt; 8 | float bal; 9 | cin>>amt; 10 | cin>>bal; 11 | if((bal>=(amt+0.50)) && (amt%5 == 0)){ 12 | printf("%.2f\n", bal-amt-0.50); 13 | } 14 | else{ 15 | printf("%.2f\n", bal); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/ATM/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int Withdrawal ; 7 | double Original , Current , charge=0.50; 8 | 9 | cin>>Withdrawal; 10 | cin>>Original; 11 | 12 | if(((Withdrawal +charge) <=Original) && (Withdrawal%5==0)) 13 | { 14 | Current=Original-(Withdrawal+charge); 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int r, c, k, x=1, y=1; 11 | cin>>r>>c>>k; 12 | --r; --c; 13 | for(int i=1; i<=k; i++) 14 | { 15 | if((r+i)<8) 16 | ++y; 17 | 18 | if((r-i)>=0) 19 | ++y; 20 | 21 | if((c+i)<8) 22 | ++x; 23 | 24 | if((c-i)>=0) 25 | ++x; 26 | } 27 | cout< 3 | using namespace std; 4 | int main() 5 | {int t; 6 | cin>>t; 7 | while(t--) 8 | {int a,b; 9 | cin>>a>>b; 10 | cout< 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int a , b, n ; 7 | cin>>n; 8 | 9 | for(int i=0;i>a>>b; 12 | int sum=a+b; 13 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n; 12 | cin>>n; 13 | int a[n]; 14 | for(int i=0;i>a[i]; 17 | } 18 | sort(a,a+n,greater ()); 19 | int b1=0; 20 | int b2=0; 21 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | for(int i=0;i>n; 15 | char col[n+1]; 16 | for(int j=0;j>col[j]; 19 | if(col[j]=='R')C1++; 20 | else if(col[j]=='B')C2++; 21 | else C3++; 22 | } 23 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t , a , b; 7 | cin>>t; 8 | while(t--) 9 | { 10 | cin>>a>>b; 11 | if(a>b) 12 | { 13 | cout<<" > "< 2 | using namespace std; 3 | 4 | int main() { 5 | long int a; 6 | cin>>a; 7 | while(a--) 8 | { 9 | long long int k,l; 10 | cin>>k>>l; 11 | long long int arr[k]; 12 | long long int arr2[k]; 13 | for(int i=0;i>arr[i]; 16 | if(arr[i]%l==0) 17 | { 18 | arr2[i]=1; 19 | } 20 | else 21 | arr2[i]=0; 22 | } 23 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int gcd(int a, int b){ 6 | for (;;){ 7 | if (a == 0) return b; 8 | b %= a; 9 | if (b == 0) return a; 10 | a %= b; 11 | } 12 | } 13 | 14 | int main(){ 15 | int t; 16 | cin>>t; 17 | 18 | while(t--){ 19 | int n; 20 | cin>>n; 21 | int arr[n]; 22 | cin>>arr[0]>>arr[1]; 23 | int l = gcd(arr[0],arr[1]); 24 | 25 | for(int i=2;i>arr[i]; 27 | l = gcd(l,arr[i]); 28 | } 29 | 30 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | 8 | cin>>n; 9 | if(n%4==0) 10 | { 11 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , a , k , count=0 , i; 7 | cin>>n>>k; 8 | 9 | for(i=0;i>a; 12 | if(a%k==0) 13 | { 14 | count++; 15 | } 16 | else 17 | { 18 | count=count+0; 19 | } 20 | 21 | } 22 | cout< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int test,n; 7 | cin>>test; 8 | while(test--) 9 | { 10 | cin>>n; 11 | int a[n],c=0,d=0; 12 | for(int i=0;i>a[i]; 15 | if(a[i]%2==1) 16 | c++; 17 | else 18 | d++; 19 | } 20 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | string a; 12 | cin>>a; 13 | while(t--) 14 | { 15 | string s; 16 | getline(cin,s); 17 | int flag=0; 18 | stringstream str(s); 19 | string word; 20 | while(str >> word) 21 | { 22 | if(word == "not"){ 23 | flag = 1; 24 | break; 25 | } 26 | } 27 | 28 | if(flag==1){ 29 | cout<<"Real Fancy"< 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int n ; 7 | cin>>n; 8 | 9 | for(int i=0;i>a>>b; 12 | int rem; 13 | 14 | if(a 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin>>n; 8 | while(n--){ 9 | float number; 10 | cin>>number; 11 | float sq= sqrt(number); 12 | int sol = abs(sq); 13 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | 9 | while (t--) { 10 | 11 | long n; 12 | cin>>n; 13 | 14 | int last, first; 15 | 16 | last = n%10; 17 | 18 | while (n > 0) { 19 | 20 | first = n%10; 21 | 22 | n /= 10; 23 | } 24 | 25 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,k; 12 | cin>>n>>k; 13 | long long sum=0; 14 | for(int i=0;i>ele; 18 | sum=sum+ele; 19 | 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t , n; 7 | cin>>t; 8 | while(t--) 9 | { 10 | cin>>n; 11 | if(n<10) 12 | { 13 | cout<<"Thanks for helping Chef!"< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t ; 7 | char c; 8 | cin>>t; 9 | while(t--) 10 | { 11 | cin>>c; 12 | if(c=='B'||c=='b') 13 | { 14 | cout<<"BattleShip"< 2 | #define ll long long 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ll n,k,s;cin>>n>>k; 8 | ll arr[200001]; 9 | for(ll i=0;i 2 | 3 | 4 | 5 | using namespace std; 6 | void solve() { 7 | int n; cin >> n; 8 | int temp=n; 9 | int count=0; 10 | while(temp!=0) { 11 | if(temp%10==4) count++; 12 | temp/=10; 13 | } 14 | 15 | cout << count << endl; 16 | 17 | } 18 | 19 | 20 | int main() { 21 | 22 | int t; cin >> t; 23 | while(t--) 24 | solve(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Mahasena/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t , n , count1=0 , count2=0; 7 | cin>>t; 8 | for(int i=1;i<=t;i++) 9 | { 10 | cin>>n; 11 | if(n%2==0) 12 | { 13 | count1++; 14 | } 15 | else 16 | { 17 | count2++; 18 | } 19 | } 20 | if(count1>count2) 21 | { 22 | cout<<"READY FOR BATTLE"< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin >> t; 9 | while (t--) 10 | { 11 | int n, m; 12 | cin >> n >> m; 13 | set s1; 14 | for (int i = 0; i < n; i++) 15 | { 16 | int x; 17 | cin >> x; 18 | s1.insert(x); 19 | } 20 | 21 | if (s1.size() == m) 22 | cout << "No" << endl; 23 | 24 | else 25 | cout << "Yes" << endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Number Mirror/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int n ; 7 | cin>>n; 8 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t , n; 6 | cin>>t; 7 | for(int i=0 ; i>n; 10 | cout<<(n/2)+1< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int a[3]; 12 | cin>>a[0]>>a[1]>>a[2]; 13 | sort(a,a+3); 14 | if(a[2]==a[1]+a[0]) cout<<"YES\n"; 15 | else cout<<"NO\n"; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Piece Of Cake/Sol_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string s; 11 | cin>>s; 12 | string output ="NO"; 13 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | string s,r; 13 | cin>>s>>r; 14 | int ans=0,ans_1=0; 15 | for(int i=0;i 2 | using namespace std; 3 | bool isPrime(int n) 4 | { 5 | if(n==0||n==1) 6 | return false; 7 | else 8 | { 9 | for(int j=2;j*j<=n;j++) 10 | { 11 | if(n%j==0) 12 | return false; 13 | } 14 | } 15 | return true; 16 | } 17 | int main() { 18 | // your code goes here 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n; 24 | cin>>n; 25 | if(isPrime(n)) 26 | cout<<"yes"< 2 | using namespace std; 3 | #define ll long long 4 | #define M 1000000007 5 | int main(){ 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | cout.tie(NULL); 9 | ll fact[1000002]; 10 | fact[0] = 1; 11 | for(int i=1;i<1000002;++i){ 12 | fact[i] = (fact[i-1]*i)%M; 13 | } 14 | ll t; 15 | cin>>t; 16 | while(t--){ 17 | ll n; 18 | cin>>n; 19 | ll ans = (fact[n+1]-1)%M; 20 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | while(n--) 8 | { 9 | int number; 10 | cin>>number; 11 | int rev=0; 12 | while(number>0) 13 | { 14 | int rem = number%10; 15 | rev=rev*10 +rem ; 16 | number=number/10; 17 | } 18 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | string s; 11 | cin>>s; 12 | int sa,sb,a,b,p; 13 | cin>>sa>>sb; 14 | for(int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | long long int T; 8 | cin>>T; 9 | while(T--) 10 | { 11 | long long int A,B,C ; 12 | cin>>A>>B>>C; 13 | if(A>=B&&A>=C) 14 | { 15 | if(B>=C) 16 | cout<=A&&B>=C) 21 | { 22 | if(A>=C) 23 | cout<=B) 30 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t , n; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int pr=1; 11 | cin>>n; 12 | for(int i =1; i<=n ;i++) 13 | { 14 | pr=pr*i; 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int t; 7 | cin>>t; 8 | while(t>0) 9 | { 10 | int a,sum=0; 11 | cin>>a; 12 | sum+=a/100; 13 | a%=100; 14 | sum+=a/50; 15 | a%=50; 16 | sum+=a/10; 17 | a%=10; 18 | sum+=a/5; 19 | a%=5; 20 | sum+=a/2; 21 | a%=2; 22 | sum+=a/1; 23 | a%=1; 24 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long int i,n; 6 | cin>>i; 7 | n=i; 8 | long int m=0; 9 | long int a[i]; 10 | long int a1[i]; 11 | for(int j=0;j>a[j]; 14 | } 15 | sort(a , a+ i); 16 | for(int j=0;j 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>n>>q; 10 | long long int d,a=1; 11 | for (long long int j=0;j>d; 13 | a*=d; 14 | } 15 | int x; 16 | for (long long int k=0;k>x; 18 | x=floor(x/a); 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int T; 8 | cin >> T; 9 | while(T--) { 10 | int a, b, c, x, y; 11 | cin >> a >> b >> c >> x >> y; 12 | int mini = a; 13 | if(b < mini) 14 | mini = b; 15 | if(c < mini) 16 | mini = c; 17 | if(min(x, y) < mini) 18 | cout << "NO"; 19 | else if(x + y != a + b + c) 20 | cout << "NO"; 21 | else 22 | cout << "YES"; 23 | cout << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Sum OR Difference/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a, b; 8 | cin>>a>>b; 9 | if(a>b) cout< 2 | using namespace std; 3 | 4 | int main () 5 | { 6 | int n ; 7 | cin>>n; 8 | while(n--) 9 | { 10 | int b , a, s=0; 11 | cin>>a; 12 | while (a>0) 13 | { 14 | b=a%10; 15 | s=s+b; 16 | a=a/10; 17 | 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t ; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n, num, digit, rev = 0; 11 | cin >> num; 12 | 13 | n = num; 14 | do 15 | { 16 | digit = num % 10; 17 | rev = (rev * 10) + digit; 18 | num = num / 10; 19 | } 20 | while (num != 0); 21 | 22 | 23 | if (n == rev) 24 | cout << " wins"; 25 | else 26 | cout << " loses"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Three Way communications/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool calc(int x,int y,int r){ 6 | return (x*x)+(y*y)<=r*r; 7 | } 8 | 9 | int main(){ 10 | int t; 11 | cin>>t; 12 | 13 | int x[3]; 14 | int y[3]; 15 | while(t--){ 16 | int r; 17 | cin>>r; 18 | int x1,y1,x2,y2,x3,y3; 19 | cin>>x1>>y1>>x2>>y2>>x3>>y3; 20 | 21 | int s=0; 22 | if(calc((x1-x2),(y1-y2),r)) 23 | s++; 24 | if(calc((x3-x2),(y3-y2),r)) 25 | s++; 26 | if(calc((x3-x1),(y3-y1),r)) 27 | s++; 28 | 29 | if(s>1) 30 | cout<<"yes\n"; 31 | else 32 | cout<<"no\n"; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Turbo Sort/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | int i,j; 10 | int a[n]; 11 | for(i=0;i> a[i]; 14 | 15 | } 16 | 17 | 18 | sort(a, a+n); 19 | for (int i = 0; i < n; ++i) 20 | cout << a[i] << "\n"; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Turbo Sort/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | 3 | Given the list of numbers, you are to sort them in non decreasing order. 4 | 5 | ## Input 6 | 7 | t – the number of numbers in list, then t lines follow [t <= 10^6]. 8 | 9 | Each line contains one integer: N [0 <= N <= 10^6] 10 | 11 | ## Output 12 | 13 | Output given numbers in non decreasing order. 14 | 15 | ## Example 16 | 17 | ## Input: 18 | 19 | 5 20 | 21 | 5 22 | 23 | 3 24 | 25 | 6 26 | 27 | 7 28 | 29 | 1 30 | 31 | ## Output: 32 | 33 | 1 34 | 35 | 3 36 | 37 | 5 38 | 39 | 6 40 | 41 | 7 42 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Two Numbers/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int a,b,n; 11 | cin>>a>>b>>n; 12 | if(n%2==1) 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | for(int i=0;i>a>>b>>c; 11 | sum=a+b+c; 12 | if(sum==180) 13 | { 14 | cout<<"YES"< 2 | using namespace std; 3 | int main() 4 | { 5 | int t,b,add,count; 6 | cin>>t; 7 | for(int j=0;j>b; 10 | add=1;count=0; 11 | 12 | if(b<4) 13 | cout<<"0"; 14 | else 15 | { 16 | for(int i=4;i 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | // code here 8 | int t; 9 | cin>>t; 10 | while(t--){ 11 | int b; 12 | cin>>b; 13 | b-=2; 14 | b/=2; 15 | cout<<(b*(b+1))/2< 2 | using namespace std; 3 | 4 | long long gcd(long long int a,long long int b){ 5 | if(b==0) 6 | return a; 7 | return gcd(b,a%b); 8 | } 9 | 10 | long long lcm(int a,int b){ 11 | return (a/gcd(a,b))*b; 12 | } 13 | 14 | int main() { 15 | // your code goes here 16 | int t; 17 | cin>>t; 18 | while(t--){ 19 | int a,b; 20 | cin>>a>>b; 21 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int a,b,c; 12 | cin>>a>>b>>c; 13 | if(a>50&&b<0.7&&c>5600) 14 | cout<<"10"<50&&b<0.7) 16 | cout<<"9"<5600) 18 | cout<<"8"<50&&c>5600) 20 | cout<<"7"<50||b<0.7||c>5600) 22 | cout<<"6"< 2 | #define ll long long int 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | 10 | while(t--){ 11 | string n; 12 | cin>>n; 13 | 14 | int s = n.length()/2; 15 | string a = n.substr(0,s); 16 | string b; 17 | 18 | if(n.length()%2 == 0){ 19 | b = n.substr(s, n.length()); 20 | }else{ 21 | b = n.substr(s+1, n.length()); 22 | } 23 | 24 | sort(a.begin(), a.end()); 25 | sort(b.begin(), b.end()); 26 | 27 | if(a == b){ 28 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,d,n,count=0,sum; 6 | cin>>t; 7 | for(int i=0;i>d>>n; 10 | for(int j=0;j 2 | using namespace std; 3 | 4 | int SUM (int a , int b) 5 | { 6 | int sm=0 , i; 7 | for(i=1;i<=b;i++) 8 | { 9 | sm=sm+i; 10 | } 11 | 12 | a--; 13 | if(a==0) 14 | return sm; 15 | else 16 | return SUM(a,sm); 17 | } 18 | 19 | 20 | 21 | int main() 22 | { 23 | int t , a , b; 24 | cin>>t; 25 | while(t--) 26 | { 27 | cin>>a>>b; 28 | int sum=0; 29 | sum=SUM(a,b); 30 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while(t--) 9 | { 10 | int N, K; 11 | cin >> N >> K; 12 | int A[N]; 13 | for(int i=0; i> A[i]; 15 | 16 | sort(A, A+N); 17 | 18 | double sum = 0; 19 | for(int i=K; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int quantity, price; 11 | cin>>quantity>>price; 12 | double cost = quantity*price; 13 | if(quantity > 1000){ 14 | std::cout << std::setprecision(6) << std::fixed; 15 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int k , i , j , n ,A[100]; 7 | 8 | cin>>n; 9 | for(i=0; i>A[i]; 12 | } 13 | 14 | cin>>k; 15 | 16 | for(i=0; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i , pr, j , n, A[100] , B[100]; 7 | 8 | cin>>n; 9 | for(i=1; i<=n ; i++) 10 | { 11 | cin>>A[i]; 12 | } 13 | 14 | for(i=1; i<=n ; i++) 15 | { 16 | pr=1; 17 | 18 | for(j=1; j<=n ; j++) 19 | { 20 | if(j!=i) 21 | { 22 | pr=pr*j; 23 | } 24 | 25 | } 26 | 27 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int A[100] , i , n , temp, res; 8 | cin>>n; 9 | 10 | for(i=0 ; i>A[100]; 13 | } 14 | 15 | 16 | for(i=0 ; i A[i+1]) 19 | { 20 | temp = A[i]; 21 | A[i] = A[i+1]; 22 | A[i+1] = temp; 23 | } 24 | } 25 | 26 | res = A[0]; 27 | 28 | for (int i = 1; i < n; i++) 29 | res = min(res, arr[i]); 30 | 31 | cout< 2 -> 3 -> 4, return 2 -> 1 -> 4 -> 3. 8 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day146/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This question was asked by BufferBox. 4 | 5 | Given a binary tree where all nodes are either 0 or 1, prune the tree so that subtrees containing all 0s are removed. 6 | 7 | For example, given the following tree: 8 | 9 | ``` 10 | 0 11 | / \ 12 | 1 0 13 | / \ 14 | 1 0 15 | / \ 16 | 0 0 17 | ``` 18 | 19 | should be pruned to: 20 | ``` 21 | 0 22 | / \ 23 | 1 0 24 | / 25 | 1 26 | ``` 27 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day147/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | Given a list, sort it using this method: reverse(lst, i, j), which reverses lst from i to j. 4 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day148/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Apple. 4 | 5 | Gray code is a binary code where each successive value differ 6 | in only one bit, as well as when wrapping around. Gray code is 7 | common in hardware so that we don't see temporary spurious values 8 | during transitions. 9 | 10 | Given a number of bits n, generate a possible gray code for it. 11 | 12 | For example, for n = 2, one gray code would be [00, 01, 11, 10]. 13 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day149/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Goldman Sachs. 4 | 5 | Given a list of numbers L, implement a method sum(i, j) which 6 | returns the sum from the sublist L[i:j] (including i, excluding j). 7 | 8 | For example, given L = [1, 2, 3, 4, 5], sum(1, 3) should return 9 | sum([2, 3]), which is 5. 10 | 11 | You can assume that you can do some pre-processing. sum() should be 12 | optimized over the pre-processing step. 13 | 14 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day15/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Facebook. 4 | 5 | Given a stream of elements too large to store in memory, pick a 6 | random element from the stream with uniform probability. 7 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day150/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by LinkedIn. 4 | 5 | Given a list of points, a central point, and an integer k, 6 | find the nearest k points from the central point. 7 | 8 | For example, given the list of points [(0, 0), (5, 4), (3, 1)], 9 | the central point (1, 2), and k = 2, return [(0, 0), (3, 1)]. 10 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day151/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | Given a 2-D matrix representing an image, a location of a pixel in the screen and a color C, 4 | replace the color of the given pixel and all adjacent same colored pixels with C. 5 | 6 | For example, given the following matrix, and location pixel of (2, 2), and 'G' for green: 7 | 8 | ``` 9 | B B W 10 | W W W 11 | W W W 12 | B B B 13 | ``` 14 | 15 | Becomes 16 | ``` 17 | B B G 18 | G G G 19 | G G G 20 | B B B 21 | ``` 22 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day16/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Twitter. 4 | 5 | You run an e-commerce website and want to record the last N order 6 | ids in a log. Implement a data structure to accomplish this, with 7 | the following API: 8 | 9 | record(order_id): adds the order_id to the log 10 | 11 | get_last(i): gets the ith last element from the log. 12 | 13 | i is guaranteed to be smaller than or equal to N. 14 | 15 | You should be as efficient with time and space as possible. 16 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day18/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Google. 4 | 5 | Given an array of integers and a number k, where 1 <= k <= length of the array, 6 | compute the maximum values of each subarray of length k. 7 | 8 | For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], 9 | since: 10 | 11 | 10 = max(10, 5, 2) 12 | 13 | 7 = max(5, 2, 7) 14 | 15 | 8 = max(2, 7, 8) 16 | 17 | 8 = max(7, 8, 7) 18 | 19 | Do this in O(n) time and O(k) space. You can modify the input array in-place and you do 20 | not need to store the results. You can simply print them out as you compute them. 21 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day19/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Facebook. 4 | 5 | A builder is looking to build a row of N houses that can be of K different colors. 6 | He has a goal of minimizing cost while ensuring that no two neighboring houses are 7 | of the same color. 8 | 9 | Given an N by K matrix where the nth row and kth column represents the cost to build 10 | the nth house with kth color, return the minimum cost which achieves this goal. 11 | 12 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day20/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Google. 4 | 5 | Given two singly linked lists that intersect at some point, 6 | find the intersecting node. The lists are non-cyclical. 7 | 8 | For example, given A = 3 -> 7 -> 8 -> 10 and B = 99 -> 1 -> 8 -> 10, 9 | return the node with value 8. 10 | 11 | In this example, assume nodes with the same value are the exact same node objects. 12 | 13 | Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space. 14 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day21/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Snapchat. 4 | 5 | Given an array of time intervals (start, end) for classroom lectures 6 | (possibly overlapping), find the minimum number of rooms required. 7 | 8 | For example, given [(30, 75), (0, 50), (60, 150)], you should return 2. 9 | -------------------------------------------------------------------------------- /Daily Interview Pro/Add two numbers as linked list/problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Microsoft: 2 | 3 | You are given two linked-lists representing two non-negative integers. 4 | The digits are stored in reverse order and each of their nodes contain a 5 | single digit. Add the two numbers and return it as a linked list. 6 | 7 | Example: 8 | 9 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 10 | 11 | Output: 7 -> 0 -> 8 12 | 13 | Explanation: 14 | 15 | 342 + 465 = 807. 16 | -------------------------------------------------------------------------------- /Daily Interview Pro/Create a Simple Calculator/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Google: 2 | 3 | Given a mathematical expression with just single digits, plus signs, 4 | negative signs, and brackets, evaluate the expression. Assume the expression 5 | is properly formed. 6 | 7 | Example: 8 | 9 | Input: - ( 3 + ( 2 - 1 ) ) 10 | 11 | Output: -4 12 | 13 | Here's the function signature: 14 | ``` 15 | def eval(expression): 16 | # Fill this in. 17 | 18 | print eval('- (3 + ( 2 - 1 ) )') 19 | # -4 20 | ``` 21 | -------------------------------------------------------------------------------- /Daily Interview Pro/Edit Distance/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by AirBNB: 2 | 3 | Given two strings, determine the edit distance between them. The edit distance 4 | is defined as the minimum number of edits (insertion, deletion, or substitution) 5 | needed to change one string to the other. 6 | 7 | For example, "biting" and "sitting" have an edit distance of 2 (substitute b for s, 8 | and insert a t). 9 | 10 | Here's the signature: 11 | ``` 12 | def distance(s1, s2): 13 | # Fill this in. 14 | 15 | print distance('biting', 'sitting') 16 | # 2 17 | ``` 18 | -------------------------------------------------------------------------------- /Daily Interview Pro/Find Cycles in a Graph/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Facebook: 2 | 3 | Given an undirected graph, determine if a cycle exists in the graph. 4 | 5 | Here is a function signature: 6 | ``` 7 | def find_cycle(graph): 8 | # Fill this in. 9 | 10 | graph = { 11 | 'a': {'a2':{}, 'a3':{} }, 12 | 'b': {'b2':{}}, 13 | 'c': {} 14 | } 15 | print find_cycle(graph) 16 | # False 17 | graph['c'] = graph 18 | print find_cycle(graph) 19 | # True 20 | ``` 21 | -------------------------------------------------------------------------------- /Daily Interview Pro/Find Pythagorean Triplets/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Uber: 2 | 3 | Given a list of numbers, find if there exists a pythagorean triplet in 4 | that list. A pythagorean triplet is 3 variables a, b, c where a2 + b2 = c2 5 | 6 | Example: 7 | 8 | Input: [3, 5, 12, 5, 13] 9 | 10 | Output: True 11 | 12 | Here, 52 + 122 = 132. 13 | 14 | def findPythagoreanTriplets(nums): 15 | 16 | #Fill this in. 17 | 18 | print findPythagoreanTriplets([3, 12, 5, 13]) 19 | 20 | #True 21 | -------------------------------------------------------------------------------- /Daily Interview Pro/Find the non-duplicate number/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Facebook: 2 | 3 | Given a list of numbers, where every number shows up twice except for one 4 | number, find that one number. 5 | 6 | Example: 7 | Input: [4, 3, 2, 4, 1, 3, 2] 8 | Output: 1 9 | Here's the function signature: 10 | 11 | def singleNumber(nums): 12 | #Fill this in. 13 | 14 | print singleNumber([4, 3, 2, 4, 1, 3, 2]) 15 | #1 16 | 17 | Challenge: Find a way to do this using O(1) memory. 18 | -------------------------------------------------------------------------------- /Daily Interview Pro/First and Last indices of an element in a sorted array/problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by AirBNB: 2 | 3 | Given a sorted array, A, with possibly duplicated elements, find the 4 | indices of the first and last occurrences of a target element, x. 5 | Return -1 if the target is not found. 6 | 7 | Example: 8 | 9 | Input: A = [1,3,3,5,7,8,9,9,9,15], target = 9 10 | 11 | Output: [6,8] 12 | 13 | Input: A = [100, 150, 150, 153], target = 150 14 | 15 | Output: [1,2] 16 | 17 | Input: A = [1,2,3,4,5,6,10], target = 9 18 | 19 | Output: [-1, -1] 20 | -------------------------------------------------------------------------------- /Daily Interview Pro/Longest Palindromic Substring/problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Twitter: 2 | 3 | A palindrome is a sequence of characters that reads the same backwards and 4 | forwards. Given a string, s, find the longest palindromic substring in s. 5 | 6 | Example: 7 | 8 | Input: "banana" 9 | 10 | Output: "anana" 11 | 12 | Input: "million" 13 | 14 | Output: "illi" 15 | -------------------------------------------------------------------------------- /Daily Interview Pro/Longest Substring Without Repeating Characters/problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Microsoft: 2 | 3 | Given a string, find the length of the longest substring without repeating characters. 4 | 5 | Here is an example solution in Python language. (Any language is OK to use in an interview, 6 | though we'd recommend Python as a generalist language utilized by companies like Google, 7 | Facebook, Netflix, Dropbox, Pinterest, Uber, etc.,) 8 | -------------------------------------------------------------------------------- /Daily Interview Pro/Number of Ways to Climb Stairs/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by LinkedIn: 2 | 3 | You are given a positive integer N which represents the number of steps in a 4 | staircase. You can either climb 1 or 2 steps at a time. Write a function that 5 | returns the number of unique ways to climb the stairs. 6 | 7 | ``` 8 | def staircase(n): 9 | # Fill this in. 10 | 11 | print staircase(4) 12 | # 5 13 | 14 | print staircase(5) 15 | # 8 16 | ``` 17 | 18 | Can you find a solution in O(n) time? 19 | -------------------------------------------------------------------------------- /Daily Interview Pro/Reverse A Linked List/problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Google: 2 | 3 | Given a singly-linked list, reverse the list. This can be done iteratively 4 | or recursively. Can you get both solutions? 5 | 6 | Example: 7 | 8 | Input: 4 -> 3 -> 2 -> 1 -> 0 -> NULL 9 | 10 | Output: 0 -> 1 -> 2 -> 3 -> 4 -> NULL 11 | -------------------------------------------------------------------------------- /Daily Interview Pro/Sorting a list with 3 unique numbers/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Google: 2 | 3 | Given a list of numbers with only 3 unique numbers (1, 2, 3), sort the 4 | list in O(n) time. 5 | 6 | Example 1: 7 | 8 | Input: [3, 3, 2, 1, 3, 2, 1] 9 | 10 | Output: [1, 1, 2, 2, 3, 3, 3] 11 | 12 | def sortNums(nums): 13 | 14 | #Fill this in. 15 | 16 | 17 | print sortNums([3, 3, 2, 1, 3, 2, 1]) 18 | 19 | #[1, 1, 2, 2, 3, 3, 3] 20 | -------------------------------------------------------------------------------- /Daily Interview Pro/Two-Sum/Problem.md: -------------------------------------------------------------------------------- 1 | Hi, here's your problem today. This problem was recently asked by Facebook: 2 | 3 | You are given a list of numbers, and a target number k. Return whether or 4 | not there are two numbers in the list that add up to k. 5 | 6 | Example: 7 | 8 | Given [4, 7, 1 , -3, 2] and k = 5, 9 | 10 | return true since 4 + 1 = 5. 11 | 12 | def two_sum(list, k): 13 | 14 | #Fill this in. 15 | 16 | print two_sum([4,7,1,-3,2], 5) 17 | 18 | #True 19 | 20 | Try to do it in a single pass of the list. 21 | -------------------------------------------------------------------------------- /Dynamic Programming/Array Jump Problem/README.md: -------------------------------------------------------------------------------- 1 | #### Given an array of positive integers, where each element represents the max number of steps you can jump forward in the array. 2 | #### Find the min jumps needed to reach the final index. 3 | 4 | ``` 5 | I/P: {3,4,2,1,2,3,7,1,1,1,2,5} 6 | 7 | O/P:- 4 8 | 9 | ``` 10 | 11 | -------------------------------------------------------------------------------- /Dynamic Programming/Box Stacking/README.md: -------------------------------------------------------------------------------- 1 | #### We are given an array of boxes. Each boxes is represented using 3 integers denoting height, width, depth of the box. Our goal is stack up the boxes and maximise the total height of the stack. 2 | #### you can't rotate any box i.e. integers in each box must represent [width,depth,height] at all times. 3 | #### A box must have strictly smaller width, depth, and height than any other box below it. 4 | 5 | ``` 6 | I/P:- [[2,1,2],[3,2,3],[2,2,8],[2,3,4],[2,2,1],[4,4,5] 7 | 8 | O/P:- 10 9 | ``` 10 | -------------------------------------------------------------------------------- /Dynamic Programming/Coin Change Problem/Bottom Up Approach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // bottom up approach 7 | int coinExhange(int m, vector denoms){ 8 | vectordp(m+1,0); // creating array 9 | dp[0] = 0; 10 | for(int i = 1; i<=m; i++){ 11 | dp[i]=INT_MAX; 12 | for(int c:denoms){ 13 | if(i-c>=0 and dp[i-c] != INT_MAX){ 14 | dp[i] = min(dp[i],dp[i-c]+1); 15 | } 16 | } 17 | } 18 | return dp[m]==INT_MAX? -1 : dp[m]; 19 | } 20 | int main(){ 21 | vectordenoms = {2}; // types 22 | int m; 23 | cin>>m; 24 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int countBSTBottomUp(int N){ 6 | vectordp(N+1,0); 7 | dp[0] = dp[1] = 1; 8 | for(int n=2;n<=N;n++){ 9 | for(int i=1;i<=n;i++){ 10 | dp[n] += (dp[i-1]*dp[n-i]); 11 | } 12 | } 13 | return dp[N]; 14 | } 15 | int main() { 16 | int n = 4; 17 | int dp[100] = {0}; 18 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int countBSTTopDown(int n, int dp[]){ 6 | if(n==0 or n==1){ 7 | return 1; 8 | } 9 | if(dp[n] !=0){ 10 | return dp[n]; 11 | } 12 | int ans = 0; 13 | for(int i=1;i<=n;i++){ 14 | int x = countBSTTopDown(i-1,dp); 15 | int y = countBSTTopDown(n-i,dp); 16 | ans += x*y; 17 | } 18 | return dp[n] = ans; 19 | } 20 | int main() { 21 | int n = 4; 22 | int dp[100] = {0}; 23 | cout< 2 | using namespace std; 3 | 4 | // bottom up approach with space optimisation 5 | int fibSpace(int n){ 6 | if(n==0 or n==1){ 7 | return n; 8 | } 9 | int a = 0; 10 | int b = 1; 11 | int c; 12 | for(int i = 2;i<=n;i++){ 13 | c = a+b; 14 | a=b; 15 | b=c; 16 | } 17 | return c; 18 | } 19 | int main(){ 20 | int n; 21 | cin>>n; 22 | cout< 2 | using namespace std; 3 | 4 | // bottom up approach 5 | int fibBU(int n){ 6 | int dp[100] = {0}; 7 | dp[1] = 1; 8 | 9 | for(int i =2;i<=n;i++){ 10 | dp[i]=dp[i-1] + dp[i-2]; 11 | } 12 | return dp[n]; 13 | } 14 | int main(){ 15 | int n; 16 | cin>>n; 17 | 18 | int dp[100]={0}; 19 | cout< 2 | using namespace std; 3 | 4 | // top down approach 5 | int fib(int n, int dp[]){ 6 | //base case 7 | if(n==0 or n==1){ 8 | return n; 9 | } 10 | // check if the state is already computed 11 | if(dp[n] != 0){ 12 | return dp[n]; 13 | } 14 | // otherwise compute the state and store it 15 | return dp[n]=fib(n-1,dp) + fib(n-2,dp); 16 | } 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | 21 | int dp[100]={0}; 22 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // top down approach 7 | int getMinCost(vectorstones){ 8 | int n = stones.size(); 9 | vectordp(n,0); 10 | dp[1]=abs(stones[1]-stones[0]); 11 | 12 | for(int i=2;i>n; 22 | vectorstones(n); 23 | for(int i=0;i>stones[i]; 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | // ladder bottom up 5 | int ladders_bottomup(int n, int k){ 6 | int dp[100] = {0}; 7 | dp[0] = 1; // start case 8 | for(int i = 1; i<=n; i++){ 9 | dp[i]=0; 10 | for(int j=1;j<=k;j++){ 11 | if(i-j>=0){ 12 | dp[i] += dp[i-j]; 13 | } 14 | } 15 | } 16 | return dp[n]; 17 | } 18 | int main(){ 19 | int n,k; 20 | cin>>n>>k; 21 | cout< 2 | using namespace std; 3 | 4 | // ladder bottom up optimised approach 5 | int ladders_optimised(int n, int k){ 6 | int dp[100] = {0}; 7 | dp[0] = dp[1] = 1; 8 | for(int i = 2; i<=k; i++){ 9 | dp[i]=2*dp[i-1]; 10 | } 11 | for(int i = k+1; i<=n; i++){ 12 | dp[i] = 2*dp[i-1] - dp[i-k-1]; 13 | } 14 | return dp[n]; 15 | } 16 | int main(){ 17 | int n,k; 18 | cin>>n>>k; 19 | cout< 2 | using namespace std; 3 | 4 | // ladder top down 5 | int ladders_topdown(int n, int k, int dp[]){ 6 | // base case 7 | if(n==0){ 8 | return 1; 9 | } 10 | // lookup 11 | if(dp[n] != 0){ 12 | return dp[n]; 13 | } 14 | // recursive case 15 | int ways = 0; 16 | for(int i = 1; i<=k; i++){ 17 | if(n-i>=0){ 18 | ways += ladders_topdown(n-i,k,dp); 19 | } 20 | } 21 | return dp[n] = ways; 22 | } 23 | int main(){ 24 | int n,k; 25 | cin>>n>>k; 26 | int dp[100] ={0}; 27 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // bottom up approach 7 | int lis(vectorarr){ 8 | int n = arr.size(); 9 | vectordp(n,1); 10 | int len = 1; 11 | for(int i = 1; iarr[j]){ 14 | dp[i] = max(dp[i], 1+dp[j]); 15 | len = max(len,dp[i]); 16 | } 17 | } 18 | } 19 | return len; 20 | } 21 | int main(){ 22 | int n; 23 | cin>>n; 24 | vectorarrr(n); 25 | for(int i=0;i>arrr[i]; 27 | } 28 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // bottom up approach 7 | int max_profit(int *prices, int n){ 8 | int dp[n+1]; 9 | dp[0] = 0; 10 | for(int len = 1; len<=n; len++){ 11 | int ans = INT_MIN; 12 | for(int i =0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int wines(int dp[][10], int prices[], int L, int R, int y){ 7 | // base case 8 | if(L>R){ 9 | return 0; 10 | } 11 | if(dp[L][R] != 0){ 12 | return dp[L][R]; 13 | } 14 | // rec case 15 | int pick_left = y*prices[L] + wines(dp,prices,L+1,R,y+1); 16 | int pick_right = y*prices[R] + wines(dp,prices,L,R-1,y+1); 17 | 18 | return dp[L][R] = max(pick_right,pick_left); 19 | } 20 | int main() { 21 | 22 | int a[] ={10, 22, 5, 75, 65, 80}; 23 | int n=6; 24 | int dp[10][10] = {0}; 25 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a , b; 7 | cin>>a>>b; 8 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double L , W , B , H , R; 8 | cin>>L>>W>>B>>H>>R; 9 | cout<<(L*W)<<(0.5*B*H)<<(3.14*(pow(R,2))); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /GeekforGeeks/Armstrong Numbers/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, a, rem, res = 0; 7 | cin >> n; 8 | a = n; 9 | 10 | while (a != 0) 11 | { 12 | rem = a % 10; 13 | res += rem * rem * rem; 14 | a /= 10; 15 | } 16 | 17 | if (res == n) 18 | cout << n << " is an Armstrong number"; 19 | else 20 | cout << n << " is not an Armstrong number"; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /GeekforGeeks/Array/Minimize the Heights II/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getMinDiff(int arr[], int n, int k) { 4 | // code here 5 | sort(arr,arr+n); 6 | int minEle, maxEle; 7 | int res = arr[n-1] - arr[0]; 8 | for(int i = 1; i= k){ 10 | maxEle = max(arr[i-1] + k, arr[n-1]-k); 11 | minEle = min(arr[0]+k, arr[i]-k); 12 | 13 | res = min(res, maxEle - minEle); 14 | } 15 | else continue; 16 | } 17 | return res; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /GeekforGeeks/Check if a number is power of another number/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int X , Y , flag; 9 | cin>>X>>Y; 10 | 11 | for(int i=1 ; i<=Y ; i++) 12 | { 13 | int p=pow(X,i); 14 | 15 | if(p==X) 16 | { 17 | flag=1; 18 | break; 19 | } 20 | } 21 | 22 | if(flag==1) 23 | { 24 | cout<<"1"; 25 | } 26 | else 27 | { 28 | cout<<"0"; 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /GeekforGeeks/Count Squares/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i , n ; 7 | cin>>n; 8 | int A[n]; 9 | 10 | for(i=1 ; i<=n ; i++) 11 | { 12 | for(int j=1 ; j<=i ; j++) 13 | { 14 | if((j*j)==i) 15 | { 16 | A[i]=j; 17 | } 18 | } 19 | } 20 | for(i=1 ; i<=n ; i++) 21 | { 22 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i , n , pr=1; 7 | cin>>n; 8 | 9 | for(i=1 ; i<=n ; i++) 10 | { 11 | pr=pr*i; 12 | } 13 | 14 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | float n , m; 8 | cin>>n>>m; 9 | 10 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , n , i , max , sm , temp; 7 | cin>>n; 8 | 9 | for(i=0; i<=n ; i++) 10 | { 11 | cin>>A[i]; 12 | } 13 | max=A[0]; 14 | 15 | for(i=0; i<=n ; i++) 16 | { 17 | if(A[i]>A[i+1]) 18 | { 19 | temp=A[i]; 20 | A[i]=A[i+1]; 21 | A[i+1]=temp; 22 | } 23 | } 24 | 25 | cout << "Largest number : " << max< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A , B , C; 7 | cin>>A>>B>>C; 8 | 9 | if(A>B & A>C) 10 | { 11 | cout<A & B>C) 14 | { 15 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int max, i , n , A[100]; 7 | 8 | cin>>n; 9 | 10 | for(i=0 ; i>A[i]; 13 | } 14 | 15 | max=A[0]; 16 | 17 | for(i=0 ; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , A[100] , sum=0; 7 | cin>>n; 8 | 9 | for(int i=0 ; i<=n ; i++) 10 | { 11 | cin>>A[i]; 12 | } 13 | 14 | for(int i=0 ; i<=n ; i++) 15 | { 16 | sum=sum+A[i]; 17 | } 18 | 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int n ; 8 | cin>>n; 9 | 10 | for(int i=1 ; i<=10 ; i++) 11 | { 12 | cout<<(n*i)<<" "; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /GeekforGeeks/Multiply Two Matrices/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , B[100] , C[100] , n , i; 7 | 8 | cin>>n; 9 | 10 | for(i=0;i>A[i]; 13 | } 14 | 15 | for(i=0;i>B[i]; 18 | } 19 | 20 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a; 7 | cin>>a; 8 | if(a%2==0) 9 | { 10 | cout<<"even"; 11 | } 12 | else 13 | { 14 | cout<<"odd"; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /GeekforGeeks/Parallel or Perpendicular?/Problem.md: -------------------------------------------------------------------------------- 1 | Given two force vectors, find out whether they are parallel, perpendicular or neither. 2 | Let the first vector be A = a1 i +a2 j + a3 k and the second vector be B = b1 i + b2 j + b3 k. 3 | 4 | A.B = a1 * b1 + a2 * b2 + a3 * b3 5 | 6 | A x B = (a2 * b3 - a3 * b2) i - (a1 * b3 - b1* a3) j + (a1 * b2 - a2 * b1) k 7 | 8 | |A|2 = a12 + a22 + a32 9 | 10 | If A.B = 0, then A and B are perpendicular. 11 | 12 | If |A X B|2 = 0, then A and B are parallel. 13 | 14 | Example 1: 15 | 16 | Input: A = 3i + 2j + k, B = 6i + 4j + 2k 17 | 18 | Output: 1 19 | 20 | Explanation: |A X B|2 = 0 21 | 22 | Example 2: 23 | 24 | Input: A = 4i + 6j + k, B = i - 1j + 2k 25 | 26 | Output: 2 27 | 28 | Explanation: A.B = 0 29 | -------------------------------------------------------------------------------- /GeekforGeeks/Perfect Numbers/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , i , sum=0; 7 | cin>>n; 8 | 9 | for(i=1 ; i 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,sum=0,c=2; 8 | cin>>n; 9 | 10 | for(int i=1 ; i<=n ; i++) 11 | { 12 | sum=sum+pow(c,2); 13 | c=c+2; 14 | } 15 | 16 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, count=0; 7 | cin>>n; 8 | 9 | for(int i=1; i<=N ; i++) 10 | { 11 | if(n%i==0) 12 | { 13 | count++; 14 | } 15 | } 16 | 17 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a , b , res, dig , n; 8 | cin>>a>>b; 9 | res = pow(a , b); 10 | cin>>n; 11 | 12 | for(int i=0 ; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a , dig, rev; 7 | cin>>a; 8 | 9 | while(a!=0) 10 | { 11 | dig=a%10; 12 | rev=rev*10 + dig; 13 | a=a/10; 14 | } 15 | 16 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , t , n ; 7 | cin>>t; 8 | 9 | while(t--) 10 | { 11 | cin>>n; 12 | for(int i=1 ; i<=n ; i++) 13 | { 14 | cin>>A[i]; 15 | } 16 | 17 | for(int j=n ; j>=1 ; j--) 18 | { 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , i , se , n; 7 | cin>>n; 8 | 9 | for(i=0; i<=n; i++) 10 | { 11 | cin>>A[i]; 12 | } 13 | 14 | cin>>se; 15 | 16 | for(i=0; i<=n; i++) 17 | { 18 | if(A[i]==se) 19 | { 20 | cout<<"Found at index"<<" "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a1 , a2 , n; 7 | cin>>a1>>a2>>n; 8 | 9 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int A , R , N; 8 | 9 | cin>>A>>R>>N; 10 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i , pr=1 , n; 7 | cin>>n; 8 | 9 | for(i=1 ; i<=n ; i++) 10 | { 11 | pr=pr*i; 12 | } 13 | 14 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , sum=0 , n; 7 | 8 | cin>>n; 9 | for(int i=0; i>A[i]; 11 | 12 | for(int j=0; j 3 | using namespace std; 4 | 5 | int sumOfPrimes(int n) 6 | { 7 | 8 | bool prime[n + 1]; 9 | memset(prime, true, n + 1); 10 | 11 | for (int p = 2; p * p <= n; p++) { 12 | 13 | if (prime[p] == true) { 14 | for (int i = p * 2; i <= n; i += p) 15 | prime[i] = false; 16 | } 17 | } 18 | 19 | int sum = 0; 20 | for (int i = 2; i <= n; i++) 21 | if (prime[i]) 22 | sum += i; 23 | return sum; 24 | } 25 | 26 | 27 | int main() 28 | { 29 | int n; 30 | cin>>n; 31 | cout << sumOfPrimes(n); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /GeekforGeeks/Sum of an AP/Problem.md: -------------------------------------------------------------------------------- 1 | Given n, a and d as the number of terms, first term and common difference respectively of an Arthimetic Series. 2 | Find the sum of the series upto nth term. 3 | 4 | Example 1: 5 | 6 | Input: 5 1 3 7 | 8 | Output: 35 9 | 10 | Explanation: Series upto 5th term is 1 4 7 10 13, so sum will be 35. 11 | 12 | Example 2: 13 | 14 | Input: 3 1 2 15 | 16 | Output: 9 17 | 18 | Example: Series upto 3rd term is 1 3 5, so sum will be 9. 19 | -------------------------------------------------------------------------------- /GeekforGeeks/Sum of an AP/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , a , d ; 7 | cin>>n>>a>>d; 8 | cout<<((n*((2*a) + (n-1)*d)/2); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /GeekforGeeks/Sum of elements/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100], i , n , sum=0; 7 | 8 | cin>>n; 9 | for(i=0;i>A[i]; 12 | } 13 | 14 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , s=0 , s2=0; 7 | cin>>n; 8 | for(int i=1 ; i<=n ; i++) 9 | { 10 | if(i%2==0) 11 | { 12 | s2++; 13 | } 14 | else 15 | { 16 | s++; 17 | } 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int a, b; 8 | cin>>a>>b; 9 | cout<<"Before swap a= "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , r , diff , sol1 , sol2 , sol3 , sol4 ; 7 | cin>>n>>r; 8 | 9 | for(int i=1 ; i<=n; i++) 10 | { 11 | sol1=sol1*i; 12 | } 13 | 14 | for(int j=1 ; j<=r; j++) 15 | { 16 | sol2=sol2*j; 17 | } 18 | 19 | diff=n-r; 20 | 21 | for(int k=1 ; k<=diff; k++) 22 | { 23 | sol3=sol3*k; 24 | } 25 | 26 | sol4=sol3*sol2; 27 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n , r , diff , sol1=1 , sol2=1, res; 7 | cin>>n>>r; 8 | 9 | diff=n-r; 10 | 11 | for(int i=1 ; i<=n ; i++) 12 | { 13 | sol1=sol1*i; 14 | } 15 | 16 | for(int j=1 ; j<=diff ; j++) 17 | { 18 | sol2=sol2*j; 19 | } 20 | 21 | res=sol1/sol2; 22 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(pairp1, pairp2){ 7 | return p1.second < p2.second; 8 | } 9 | int main() { 10 | int t,n,s,e; 11 | cin>>t; 12 | vector> v; 13 | while(t--){ 14 | cin>>n; 15 | for(int i=0;i>s>>e; 17 | v.push_back(make_pair(s,e)); 18 | } 19 | sort(v.begin(),v.end(),compare); 20 | 21 | int res=1; 22 | int fin=v[0].second; 23 | 24 | for(int i =1;i=fin){ 26 | fin=v[i].second; 27 | res++; 28 | } 29 | } 30 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int white[]={3,4,1}; 8 | int black[]={2,5,6}; 9 | int n=sizeof(white)/sizeof(white[0]); 10 | 11 | //sort the arrays 12 | sort(white,white+n); 13 | sort(black,black+n); 14 | 15 | int ans =0; 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[9000]; 6 | int n,i,val,diff; 7 | while(1){ 8 | int max_load =0, load =0; 9 | cin>>n; 10 | if(n==-1) 11 | break; 12 | for(int i=0;i>arr[i]; 14 | load += arr[i]; 15 | } 16 | if(load %n!=0){ 17 | cout<<-1< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, num, dig, rev = 0; 7 | 8 | cout << "Enter a positive number: "; 9 | cin >> num; 10 | 11 | n = num; 12 | 13 | do 14 | { 15 | dig = num % 10; 16 | rev = (rev * 10) + dig; 17 | num = num / 10; 18 | } while (num != 0); 19 | 20 | cout << " The reverse of the number is: " << rev << endl; 21 | 22 | if (n == rev) 23 | cout << " The number is a palindrome."; 24 | else 25 | cout << " The number is not a palindrome."; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/Easy/Plus One/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[3]; 7 | 8 | for(int i=0;i<3;i++) 9 | { 10 | cin>>A[i] 11 | } 12 | 13 | for(int i=0;i<3;i++) 14 | { 15 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, Rev = 0, rem; 7 | 8 | cout << "Enter an integer: "; 9 | cin >> n; 10 | 11 | while(n != 0) { 12 | rem = n%10; 13 | Rev = Rev*10 + rem; 14 | n /= 10; 15 | } 16 | 17 | cout << "Reversed Number = " << Rev; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Easy/Reversed Integer/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. 3 | 4 | Assume the environment does not allow you to store 64-bit integers (signed or unsigned). 5 | 6 | 7 | 8 | ### Example 1: 9 | 10 | Input: x = 123 11 | Output: 321 12 | Example 2: 13 | 14 | Input: x = -123 15 | Output: -321 16 | Example 3: 17 | 18 | Input: x = 120 19 | Output: 21 20 | Example 4: 21 | 22 | Input: x = 0 23 | Output: 0 24 | 25 | 26 | ### Constraints: 27 | 28 | -231 <= x <= 231 - 1 29 | -------------------------------------------------------------------------------- /LeetCode/Easy/Search Insert Position/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , se, pos; 7 | 8 | for(int i=0;i<=4;i++) 9 | cin>>A[i]; 10 | 11 | cin>>se; 12 | 13 | for(int i=0;i<=4;i++) 14 | { 15 | if(se==A[i]) 16 | { 17 | pos=i; 18 | } 19 | } 20 | 21 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int a , sq; 10 | 11 | cin>>a; 12 | sq= sqrt(a); 13 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , target, i , j , n; 7 | cout<<"Enter size"; 8 | cin>>n; 9 | cout<<"Enter values"; 10 | for(i=0 ; i>A[i]; 13 | } 14 | cout<<"Enter target"; 15 | cin>>target; 16 | 17 | for(i=0; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int A[100] , sum=0 , a , size , i; 7 | 8 | cout<<"Enter size"; 9 | cin>>size; 10 | 11 | cout<<"Enter array"; 12 | for(i=0;i>A[i]; 15 | } 16 | 17 | cout<<"Enter target"; 18 | cin>>a; 19 | 20 | for(i=0;i>dp(m+1,vector(n+1,0)); 7 | for(int i =0;i<=m;i++){ 8 | dp[i][0]=1; 9 | } 10 | for(int i=1;i<=m;i++){ 11 | for(int j=1;j<=n;j++){ 12 | if(s[i-1] == t[j-1]){ 13 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j]; 14 | } 15 | else{ 16 | dp[i][j] = dp[i-1][j]; 17 | } 18 | } 19 | } 20 | return dp[m][n]; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/Hard/N-Queens/README.md: -------------------------------------------------------------------------------- 1 | The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. 2 | 3 | Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order. 4 | 5 | Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively. 6 | 7 | ``` 8 | Input: n = 4 9 | Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 10 | ``` 11 | -------------------------------------------------------------------------------- /LeetCode/Hard/Orderly Queue/Orderly Queue.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string orderlyQueue(string s, int k) { 4 | if(k>1) 5 | { 6 | sort(s.begin(),s.end()); 7 | return s; 8 | } 9 | string ans = s; 10 | for(int i=1;i0;i-=4){ 6 | if(i>2){ 7 | int num = (i*(i-1)/(i-2)); 8 | sum += (i==N ? num : -num) + (i-3); 9 | } 10 | else{ 11 | int num = i; 12 | sum += (i==N ? num : -num) ; 13 | } 14 | } 15 | 16 | return sum; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/Medium/Coin Change DP Problem/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector& coins, int amount) { 4 | vector dp(amount + 1, INT_MAX); 5 | dp[0] = 0; 6 | for(int j = 1; j <= amount; j++){ 7 | for(int i = 0; i < coins.size(); i++){ 8 | if(coins[i] <= j){ 9 | if(dp[j - coins[i]] == -1) continue; 10 | dp[j] = min(dp[j], 1 + dp[j - coins[i]]); 11 | } 12 | } 13 | if(dp[j] == INT_MAX) dp[j] = -1; 14 | } 15 | return dp[amount]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/Medium/Global and Local Inversions/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIdealPermutation(vector& A) { 4 | for (int i = 0; i < A.size(); i++) 5 | if (abs(A[i] - i) > 1) 6 | return false; 7 | return true; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /LeetCode/Medium/House Robber/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums){ 4 | int n = nums.size(); 5 | vectordp(n+1,0); 6 | if(n==1){ 7 | return max(nums[0], 0); 8 | } 9 | else if(n==2){ 10 | return max(0,max(nums[0],nums[1])); 11 | } 12 | dp[0] = max(nums[0],0); 13 | dp[1] = max(0,max(nums[0],nums[1])); 14 | for(int i =2; iarr, int n, vectordp, int i){ 3 | // base case 4 | if(i==n-1){ 5 | return 0; 6 | } 7 | if(i>=n){ 8 | return INT_MAX; 9 | } 10 | // recursive case 11 | if(dp[i] != 0){ 12 | return dp[i]; 13 | } 14 | // assume 15 | int steps = INT_MAX; 16 | int max_jumps = arr[i]; 17 | for(int jumps = 1; jumps <= max_jumps; jumps++){ 18 | int next_cell = i+jumps; 19 | int subprob = min_jumps(arr,n,dp,next_cell); 20 | 21 | if(subprob != INT_MAX){ 22 | steps = min(steps,subprob+1); 23 | } 24 | } 25 | return dp[i]=steps; 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/Medium/Longest Common Subsequence/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2){ 4 | int n1 = text1.length(); 5 | int n2 = text2.length(); 6 | vector>dp(n1+1,vector(n2+1,0)); 7 | for(int i =1;i<=n1;i++){ 8 | for(int j = 1; j<=n2; j++){ 9 | if(text1[i-1] == text2[j-1]){ 10 | dp[i][j] = 1+ dp[i-1][j-1]; 11 | } 12 | else{ 13 | int op1 = dp[i-1][j]; 14 | int op2 = dp[i][j-1]; 15 | dp[i][j] = max(op1,op2); 16 | } 17 | } 18 | } 19 | return dp[n1][n2]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/Medium/Longest Increasing Subsequence/README.md: -------------------------------------------------------------------------------- 1 | #### Given an integer array nums, return the length of the longest strictly increasing subsequence. 2 | 3 | #### A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7]. 4 | 5 | ##### Example 1: 6 | ``` 7 | Input: nums = [10,9,2,5,3,7,101,18] 8 | Output: 4 9 | Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. 10 | ``` 11 | 12 | ##### Example 2: 13 | ``` 14 | Input: nums = [0,1,0,3,2,3] 15 | Output: 4 16 | ``` 17 | -------------------------------------------------------------------------------- /LeetCode/Medium/Longest Increasing Subsequence/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums){ 4 | int n = nums.size(); 5 | vectordp(n,1); 6 | int len = 1; 7 | for(int i = 1; inums[j]){ 10 | dp[i] = max(dp[i], 1+dp[j]); 11 | len = max(len,dp[i]); 12 | } 13 | } 14 | } 15 | return len; 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /LeetCode/Medium/Longest Substring Without repeating characters/Longest Substring Without repeating characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | if(s.length()==0||s.length()==1)return s.length(); 5 | unordered_map m; 6 | int i =0 , j=0 , count =0 ; 7 | while(jdp(N+1,0); 5 | dp[0] = dp[1] = 1; 6 | for(int n=2;n<=N;n++){ 7 | for(int i=1;i<=n;i++){ 8 | dp[n] += (dp[i-1]*dp[n-i]); 9 | } 10 | } 11 | return dp[N]; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/Medium/Valid Triangle Number/README.md: -------------------------------------------------------------------------------- 1 | Given an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: nums = [2,2,3,4] 8 | Output: 3 9 | Explanation: 10 | Valid combinations are: 11 | 2,3,4 (using the first 2) 12 | 2,3,4 (using the second 2) 13 | 2,2,3 14 | 15 | 16 | Example 2: 17 | 18 | Input: nums = [4,2,3,4] 19 | Output: 4 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= nums.length <= 1000 25 | 0 <= nums[i] <= 1000 26 | -------------------------------------------------------------------------------- /LeetCode/Medium/Valid Triangle Number/Valid Triangle Number.CPP: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int triangleNumber(vector& nums) { 5 | sort(nums.begin(),nums.end()); 6 | int cf=0; 7 | for(int i=nums.size()-1;i>=2;i--){ 8 | int l=0; 9 | int r=i-1; 10 | while(l 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int d){ 8 | data=d; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | int main() 14 | { 15 | Node *head=new Node(10); 16 | head->next=new Node(5); 17 | head->next->next=new Node(20); 18 | head->next->next->next=new Node(15); 19 | head->next->next->next->next=head; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Linked List/Circular Linked List/Traversal/Do_while_method.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int d){ 8 | data=d; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | void printlist(Node *head){ 14 | if(head==NULL)return; 15 | Node *p=head; 16 | do{ 17 | cout<data<<" "; 18 | p=p->next; 19 | }while(p!=head); 20 | } 21 | 22 | int main() 23 | { 24 | Node *head=new Node(10); 25 | head->next=new Node(5); 26 | head->next->next=new Node(20); 27 | head->next->next->next=new Node(15); 28 | head->next->next->next->next=head; 29 | printlist(head); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Linked List/Circular Linked List/Traversal/For_Loop_method.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int d){ 8 | data=d; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | void printlist(Node *head){ 14 | if(head==NULL)return; 15 | cout<data<<" "; 16 | for(Node *p=head->next;p!=head;p=p->next) 17 | cout<data<<" "; 18 | } 19 | 20 | int main() 21 | { 22 | Node *head=new Node(10); 23 | head->next=new Node(5); 24 | head->next->next=new Node(20); 25 | head->next->next->next=new Node(15); 26 | head->next->next->next->next=head; 27 | printlist(head); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Linked List/Doubly Linked List/Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* prev; 7 | Node* next; 8 | Node(int d){ 9 | data=d; 10 | prev=NULL; 11 | next=NULL; 12 | } 13 | }; 14 | 15 | void printlist(Node *head){ 16 | Node *curr=head; 17 | while(curr!=NULL){ 18 | cout<data<<" "; 19 | curr=curr->next; 20 | }cout<next=temp1; 29 | temp1->prev=head; 30 | temp1->next=temp2; 31 | temp2->prev=temp1; 32 | printlist(head); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Linked List/Singly_Linked_List/Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data=x; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | int main() 14 | { 15 | Node *head=new Node(10); 16 | Node *temp1=new Node(20); 17 | Node *temp2=new Node(30); 18 | head->next=temp1; 19 | temp1->next=temp2; 20 | cout<data<<"-->"<data<<"-->"<data; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Linked List/Singly_Linked_List/Insert_At_begin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data=x; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | Node *insertBegin(Node *head,int x){ 14 | Node *temp=new Node(x); 15 | temp->next=head; 16 | return temp; 17 | 18 | } 19 | 20 | void printlist(Node *head){ 21 | Node *curr=head; 22 | while(curr!=NULL){ 23 | cout<data<<" "; 24 | curr=curr->next; 25 | } 26 | } 27 | int main() 28 | { 29 | Node *head=NULL; 30 | head=insertBegin(head,30); 31 | head=insertBegin(head,20); 32 | head=insertBegin(head,10); 33 | printlist(head); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Linked List/Singly_Linked_List/Middle of the Linked List/README.md: -------------------------------------------------------------------------------- 1 | Given the head of a singly linked list, return the middle node of the linked list. 2 | 3 | If there are two middle nodes, return the second middle node. 4 | 5 | 6 | Example 1: 7 | ``` 8 | Input: head = [1,2,3,4,5] 9 | Output: [3,4,5] 10 | Explanation: The middle node of the list is node 3. 11 | ``` 12 | 13 | Example 2: 14 | ``` 15 | Input: head = [1,2,3,4,5,6] 16 | Output: [4,5,6] 17 | Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. 18 | ``` 19 | -------------------------------------------------------------------------------- /Linked List/Singly_Linked_List/Recursive_Traversal.cpp: -------------------------------------------------------------------------------- 1 | void rprint(Node *head){ 2 | if(head==NULL) 3 | return; 4 | cout<<(head->data)<<" "; 5 | rprint(head->next); 6 | } 7 | -------------------------------------------------------------------------------- /Linked List/Singly_Linked_List/Reverse a Linked List in groups of given size/README.md: -------------------------------------------------------------------------------- 1 | Given a linked list, write a function to reverse every k nodes (where k is an input to the function). 2 | 3 | Example: 4 | ``` 5 | Input: 1->2->3->4->5->6->7->8->NULL, K = 3 6 | Output: 3->2->1->6->5->4->8->7->NULL 7 | Input: 1->2->3->4->5->6->7->8->NULL, K = 5 8 | Output: 5->4->3->2->1->8->7->6->NULL 9 | ``` 10 | -------------------------------------------------------------------------------- /Linked List/Singly_Linked_List/Traverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data=x; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | void printlist(Node *head){ 14 | Node *curr=head; 15 | while(curr!=NULL){ 16 | cout<data<<" "; 17 | curr=curr->next; 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | Node *head=new Node(10); 24 | head->next=new Node(20); 25 | head->next->next=new Node(30); 26 | head->next->next->next=new Node(40); 27 | printlist(head); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Math/Birthday Paradox Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | float x = 1.0; 6 | int people = 0; 7 | float num = 365; 8 | float denom = 365; 9 | 10 | float p; 11 | cin>>p; 12 | if(p==1.0){ 13 | cout<<"366"<1-p){ 17 | x=x*(num)/denom; 18 | num--; 19 | people++; 20 | cout<<"People"< 2 | using namespace std; 3 | int main() 4 | { 5 | int n , number , dig; 6 | cin>>number; 7 | int rev=0; 8 | if(number>0) 9 | { 10 | n=number; 11 | do 12 | { 13 | dig = number % 10; 14 | rev = (rev * 10) + dig; 15 | number= number / 10; 16 | } while (number != 0); 17 | 18 | if(n==rev) 19 | { 20 | cout<<"YES"; 21 | } 22 | else 23 | { 24 | cout<<"no"; 25 | } 26 | } 27 | else 28 | { 29 | cout<<"invalid"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statement_1/README.md: -------------------------------------------------------------------------------- 1 | # *Task* 2 | 3 | ### Given an integer, *_n_* , perform the following conditional actions: 4 | 1. If _n_ is odd, print Weird 5 | 2. If _n_ is even and in the inclusive range of _2_ to _5_, print Not Weird. 6 | 3. If _n_ is even and in the inclusive range of _6_ to _20_ , print Weird. 7 | 4. If _n_ is even and greater than _20_, print Not Weird. 8 | 9 | ## Input Fromat 10 | A single line containing a positive integer,_n_ . 11 | 12 | ## Output Format 13 | Print **Weird** if the number is weird; otherwise, print **Not Weird**. 14 | 15 | #### *Sample Input 1* 16 | >- 3 17 | #### *Sample Output 1* 18 | >- Weird 19 | 20 | 21 | 22 | #### *Sample Input 2* 23 | >- 24 24 | #### *Sample Output 2* 25 | >- Not Weird 26 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statement_1/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | int main() 8 | { 9 | int N; 10 | cin >> N; 11 | cin.ignore(numeric_limits::max(), '\n'); 12 | if (N%2!=0) { 13 | cout<<"Weird"<20){ 22 | cout<<"Not Weird"< 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | int main() { 7 | ll t; 8 | cin>>t; 9 | 10 | ll primes[]={2,3,5,7,11,13,17,19}; 11 | 12 | while(t--){ 13 | ll n; 14 | cin>>n; 15 | 16 | ll subsets = (1<<8)-1; 17 | ll ans =0; 18 | for(ll i=1;i<=subsets;i++){ 19 | ll denom =1ll; 20 | ll setBits =__builtin_popcount(i); 21 | for(ll j=0;j<=7;j++){ 22 | if(i& (1< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // top down approach 7 | int getMinCost(vectorstones){ 8 | int n = stones.size(); 9 | vectordp(n,0); 10 | dp[1]=abs(stones[1]-stones[0]); 11 | 12 | for(int i=2;i>n; 22 | vectorstones(n); 23 | for(int i=0;i>stones[i]; 25 | } 26 | cout< **I/P:- arr[ ]={4,3,4,4,4,5,5}** 6 | 7 | > **O/P:- 3** 8 | 9 | 10 | > **I/P:- arr[ ]={8,7,7,8,8}** 11 | 12 | > **O/P:- 8** 13 | 14 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statement_2/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findOddd(int arr[], int n) 5 | { 6 | int res=0; 7 | for(int i = 0; i < n; i++){ 8 | res=res^arr[i]; // using xor operator 9 | } 10 | return res; 11 | 12 | } 13 | 14 | 15 | 16 | int main() 17 | { 18 | int arr[]= {4, 3, 4, 4, 4, 5, 5,3,3}, n = 9; 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n1 , n2; 7 | cin>>n1>>n2; 8 | 9 | if(n1>0 && n2>0) 10 | { 11 | if(max(n1,n2)%min(n1,n2)==0) 12 | { 13 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int maxConsecutiveOnes(int arr[],int n){ 6 | int res=0, curr=0; 7 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int lucas(int n) 6 | { 7 | 8 | if (n == 0) 9 | return 2; 10 | if (n == 1) 11 | return 1; 12 | 13 | 14 | return lucas(n - 1) + 15 | lucas(n - 2); 16 | } 17 | 18 | 19 | int main() 20 | { 21 | int n ; 22 | cout<<"enter the position of the element of the series "; 23 | cin>>n; 24 | printf("%d", lucas(n)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statement_9/PadovanSequence.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | #### The Padovan sequence is the sequence of integers P(n) defined by the initial values. and the recurrence relation. 4 | ``` 5 | P(n) = P(n-2) + P(n-3) 6 | P(0) = P(1) = P(2) = 1 7 | 8 | ``` 9 | #### Given n, calculate P(n). 10 | 11 | **Example 1:** 12 | ``` 13 | Input: n = 0 14 | Output: 1 15 | Explanation: P(1) = P(2) = 0 + 1 = 1. 16 | ``` 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: n = 7 22 | Output: 2 23 | Explanation: P(7) = P(5) + P(4) = P(3)+P(2)+P(2)+P(1)=P(2)+P(1)+1+1+1 = 1+1+1+1+1 = 5 24 | ``` 25 | **Constraints:** 26 | ``` 27 | 0 <= n <= 10000 28 | 29 | ``` 30 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statement_9/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | int pad(int n) 2 | { 3 | 4 | int prevprev = 1, prev = 1, curr = 1, next = 1; 5 | 6 | for (int i=3; i<=n; i++) 7 | { 8 | next = prevprev + prev; 9 | prevprev = rev; 10 | rev = curr; 11 | curr = next; 12 | } 13 | 14 | return next; 15 | } 16 | 17 | 18 | int main() 19 | { 20 | cin>>"Enter the position "; 21 | cin>>n; 22 | cout << pad(n); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statment_5/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n, i, m=0, flag=0; 6 | cin >> n; 7 | m=n/2; 8 | if(n>1) 9 | { 10 | for(i = 2; i <= m; i++) 11 | { 12 | if(n % i == 0) 13 | { 14 | cout<<"not prime"< 2 | using namespace std; 3 | 4 | int knapsack(int N, int C, int*wt, int *prices){ 5 | if(N==0 || C==0){ 6 | return 0; 7 | } 8 | int ans = 0; 9 | int inc, exc; 10 | inc = exc = 0; 11 | 12 | // inc 13 | if(wt[N-1] <= C){ 14 | inc = prices[N-1] + knapsack(N-1, C-wt[N-1], wt, prices); 15 | } 16 | // exc 17 | exc = knapsack(N-1, C, wt, prices); 18 | 19 | ans = max(inc, exc); 20 | return ans; 21 | } 22 | 23 | int main(){ 24 | int N; 25 | cin>>N; 26 | int C; 27 | cin>>C; 28 | int weigths[] = {1,2,3,4,5}; 29 | int prices[] = {40,20,30,10,100}; 30 | cout< 2 | using namespace std; 3 | 4 | bool isSorted(int a[], int n){ 5 | // base case 6 | if(n==0 or n==1){ 7 | return true; 8 | } 9 | // recursive case 10 | if(a[0] 2 | using namespace std; 3 | 4 | void dec(int n){ 5 | // base case 6 | if(n==0){ 7 | return; 8 | } 9 | // recursiive case 10 | cout<>n; 16 | dec(n); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Recursion/Factorial/Factorial.md: -------------------------------------------------------------------------------- 1 | #### A factorial is a function that multiplies a number by every number below it. 2 | 3 | N! = N x (N-1) x (N-2) x (N-3) x ......x 1 4 | 5 | ``` 6 | 1! = 1 2!= 2 x 1 = 2 7 | 10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800 8 | ``` 9 | #### Given N, calculate N!. 10 | 11 | **Example 1:** 12 | ``` 13 | Input: 1 N= 1 14 | Output: 1! = 1 15 | Explanation: 1! = 1 = 1 16 | ``` 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: N = 3 22 | Output: 3! = 6 23 | Explanation: 3! = 3 x 2 x 1 = 6 24 | ``` 25 | 26 | **Example 3:** 27 | ``` 28 | Input: N = 4 29 | Output: 4! = 24 30 | Explanation: 4! = 4 x 3 x 2 x 1 31 | ``` 32 | 33 | **Constraints:** 34 | ``` 35 | 0 <= N <= 100000 36 | 37 | ``` 38 | -------------------------------------------------------------------------------- /Recursion/Factorial/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N , i , perm=1; 7 | cout<<"Input value of N"; 8 | cin>>N; 9 | 10 | for(i=N ; i>0 ; i--) 11 | { 12 | perm=perm*i; 13 | } 14 | cout< 2 | using namespace std; 3 | 4 | int fib(int n){ 5 | if(n==0) 6 | return 0; 7 | else if(n==1) 8 | return 1; 9 | else 10 | return fib(n-1)+fib(n-2); 11 | 12 | } 13 | 14 | int main(){ 15 | int n=3; 16 | cout< 2 | using namespace std; 3 | 4 | int firstOcc(int *a, int n, int key){ 5 | // base case 6 | if(n==0){ 7 | return 0; 8 | } 9 | // recursive case 10 | if(a[0]==key){ 11 | return 0; 12 | } 13 | int i = firstOcc(a+1,n-1,key); 14 | if(i==-1){ 15 | return -1; 16 | } 17 | return i+1; 18 | } 19 | int main(){ 20 | //int arr[] ={8,2,3,4,5}; 21 | int n; 22 | cin>>n; 23 | int arr[n]; 24 | for(int i=0; i>arr[i]; 26 | } 27 | int key; 28 | cin>>key; 29 | // int n = sizeof(arr)/sizeof(int); 30 | cout< 2 | using namespace std; 3 | 4 | void generate_brackets(char *out, int n, int idx, int open, int close){ 5 | // base case 6 | if(idx == 2*n){ 7 | out[idx] = '\0'; 8 | cout<>n; 24 | char out[1000]; 25 | int idx = 0; 26 | generate_brackets(out, n, 0,0,0); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Recursion/Increasing Order.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void dec(int n){ 5 | // base case 6 | if(n==0){ 7 | return; 8 | } 9 | // recursiive case 10 | dec(n-1); 11 | cout<>n; 16 | dec(n); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Recursion/Infinite Series/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | for(int i=1 ; i>0 ; i++) 8 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i ; 7 | for(i=0;i>=0;i++) 8 | { 9 | cout< 2 | using namespace std; 3 | 4 | int ladderProblem(int n, int k){ 5 | // base case 6 | if(n==0){ 7 | return 1; 8 | } 9 | if(n<0){ 10 | return 0; 11 | } 12 | int ans = 0; 13 | for(int i =1;i<=k;i++){ 14 | ans += ladderProblem(n-i,k); 15 | } 16 | return ans; 17 | } 18 | int main(){ 19 | int n,k; 20 | // says k =2 then steps can be 1,2. 21 | cin>>n>>k; 22 | cout< 7 | using namespace std; 8 | 9 | char words[][10]={"zero","one","two","three","four","five","six","seven", "eight","nine"}; 10 | 11 | void printSpelling(int n){ 12 | // base case 13 | if(n==0){ 14 | return; 15 | } 16 | // recursive case 17 | printSpelling(n/10); 18 | int digit = n%10; 19 | cout<>n; 25 | printSpelling(n); 26 | } 27 | -------------------------------------------------------------------------------- /Recursion/Place The Tiles.cpp: -------------------------------------------------------------------------------- 1 | ``` 2 | Given a wall of size of 4XN, and tiles of size (1,4) and (4,1). In how many ways you can build the wall? 3 | ``` 4 | 5 | #include 6 | using namespace std; 7 | 8 | int TilingProblem(int length){ 9 | // base case 10 | if(length<1){ 11 | return 0; 12 | } 13 | if(length<=3){ 14 | return 1; 15 | } 16 | if(length=4){ 17 | return 2; 18 | } 19 | int ans = TilingProblem(length-1) + TilingProblem(length-4); 20 | return ans; 21 | } 22 | int main(){ 23 | int length; 24 | cin>>length; 25 | cout< 2 | using namespace std; 3 | 4 | int fast_power(int a, int n){ 5 | // base case 6 | if(n==0){ 7 | return 1; 8 | } 9 | // recursive case 10 | int sub = fast_power(a,n/2); 11 | sub *=sub; 12 | 13 | // check if the number is odd 14 | if(n & 1){ 15 | return sub*a; 16 | } 17 | return sub; 18 | } 19 | 20 | int main(){ 21 | int a; 22 | cin>>a; 23 | int n; 24 | cin>>n; 25 | cout< 2 | using namespace std; 3 | 4 | void print1toN(int n) 5 | { 6 | if(n==0) 7 | return; 8 | print1toN(n-1); 9 | cout< 2 | using namespace std; 3 | 4 | void printNto1(int n){ 5 | if(n==0) 6 | return; 7 | else 8 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i , n; 7 | 8 | cin>>n; 9 | 10 | if(n>0) 11 | { 12 | if(n%2==0) 13 | { 14 | for(i=0;i<=n;i=i+2) 15 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int StringToInt(char *a, int n){ 6 | // base case 7 | if(n==0){ 8 | return 0; 9 | } 10 | // recursive case 11 | int digits = a[n-1] - '0'; 12 | int sub_problems = StringToInt(a, n-1); 13 | return sub_problems*10 + digits; 14 | } 15 | int main(){ 16 | char a[] = "123"; 17 | int len = strlen(a); 18 | int c = StringToInt(a, len); 19 | cout< 9 | using namespace std; 10 | 11 | void generator_subsequence(char *inn, char *out, int i, int j){ 12 | if(inn[i] == '\0'){ 13 | out[j] = '\0'; 14 | cout< Sample input and output:- 6 | 7 | ``` 8 | I/P:- n=253 9 | O/P:- 10 10 | ``` 11 | 12 | ``` 13 | I/P:- n=9987 14 | O/P:- 33 15 | ``` 16 | -------------------------------------------------------------------------------- /Recursion/Sum_Of_Digits/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getSum(int n){ 5 | if(n==0) 6 | return 0; 7 | else 8 | return getSum(n/10)+n%10; 9 | }; 10 | 11 | int main(){ 12 | int n=253; 13 | cout< 2 | using namespace std; 3 | 4 | void Tower(int n, char src, char helper, char dest){ 5 | if(n==0){ 6 | return; 7 | } 8 | Tower(n-1, src, dest, helper); 9 | cout<<"Shift disk "<>n; 15 | Tower(n,'A','B','C'); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Recursion/last Occurrence/README.md: -------------------------------------------------------------------------------- 1 | ``` 2 | I/P:- 1,2,7,3,7,6 3 | key:- 7 4 | 5 | O/P:- 4 6 | ``` 7 | -------------------------------------------------------------------------------- /Recursion/last Occurrence/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lastOcc(int *a, int n, int key){ 5 | // base case 6 | if(n==0){ 7 | return -1; 8 | } 9 | // recursive case 10 | int i = lastOcc(a+1,n-1,key); 11 | if(i==-1){ 12 | if(a[0]==key){ 13 | return 0; 14 | }else{ 15 | return -1; 16 | } 17 | } 18 | return i+1; 19 | } 20 | int main(){ 21 | //int arr[] ={8,2,3,4,5}; 22 | int n; 23 | cin>>n; 24 | int arr[n]; 25 | for(int i=0; i>arr[i]; 27 | } 28 | int key; 29 | cin>>key; 30 | // int n = sizeof(arr)/sizeof(int); 31 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[] = {1,2,3,4,5,6,7,8,9}; 6 | int n = sizeof(arr) / sizeof(arr[0]); 7 | // search 8 | int key; 9 | cin>>key; 10 | auto present = binary_search(arr, arr+n, key); 11 | if(present){ 12 | cout<<"Present"; 13 | } 14 | else{ 15 | cout<<"Not present"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /STL/Find Function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[] = {1,2,3,4,5,6,7,8,9}; 6 | int n = sizeof(arr) / sizeof(arr[0]); 7 | // search 8 | int key; 9 | cin>>key; 10 | auto it = find(arr,arr+n, key); 11 | int index = it - arr; 12 | if(index == n){ 13 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | string s0; 6 | string s1("Hello World"); 7 | string s2 = "Hello World"; 8 | string s3(s2); 9 | string s4 = s3; 10 | char a[] ={'a','b','c','\0'}; 11 | string s5(a); 12 | 13 | cout< 2 | using namespace std; 3 | 4 | int countOnes(int arr[], int n){ 5 | int low=0, high=n-1; 6 | while (low<=high) 7 | { 8 | int mid=(low+high)/2; 9 | if(arr[mid]==0) 10 | low=mid+1; 11 | else{ 12 | if(mid==0 || arr[mid-1]==0) 13 | return (n-mid); 14 | else 15 | high=mid-1; 16 | } 17 | } 18 | return 0; 19 | } 20 | 21 | int main(){ 22 | int arr[]= {0,0,0,1,1,1}, n = 6; 23 | cout<