├── Recursion ├── last Occurrence │ ├── README.md │ └── Solution_1.cpp ├── First Occurrence │ ├── README.md │ └── Solution_1.cpp ├── Infinite Series │ ├── Sol.cpp │ └── infinite.cpp ├── Sum_Of_Digits │ ├── README.md │ └── Solution.cpp ├── Print_1_to_N_using_recursion │ └── Solution_1.cpp ├── Print_N_to_1_Using_recursion │ └── Solution_1.cpp ├── Factorial │ ├── Solution_by_Pandz18.cpp │ └── Factorial.md ├── Increasing Order.cpp ├── Decreasing Order.cpp ├── Fibonacci │ └── Solution_1.cpp ├── Tower of Hanoi │ └── Solution_1.cpp ├── Array Is Sorted Or Not.cpp ├── Ladders Problem.cpp ├── Power of number.cpp ├── Number to Spelling.cpp ├── String To Integers.cpp ├── Subsequence Generation.cpp ├── Place The Tiles.cpp ├── Series │ └── Series.cpp ├── Generate Balanced Brackets.cpp └── 0 1 Knapsack Problem.cpp ├── Bit_Manipulation ├── Find_Unique_Number │ ├── README.md │ └── Solution_1.cpp ├── Set Bit │ ├── README.md │ └── Soltuion_1.cpp ├── Clear Bit │ ├── README.md │ └── Solution_1.cpp ├── Clear Range Of Bit │ └── Solution_1.cpp ├── Program to find whether a no is power of two │ ├── README.md │ └── Solution_1.cpp ├── Modify a bit at a given position │ ├── Solution_1.cpp │ └── README.md ├── Clear Range I to J.cpp ├── Count number of bits to be flipped to convert A to B │ └── Solution_1.cpp ├── Decimal_To_Binary │ └── Solution_1.cpp ├── Copy set bits in a range │ ├── Solution_1.cpp │ └── README.md ├── Find position of the only set bit │ └── Solution_1.cpp ├── Calculate square of a number │ └── Solution_1.cpp └── Find the two non-repeating elements in an array of repeating elements │ └── README.md ├── Linked List ├── Singly_Linked_List │ ├── Recursive_Traversal.cpp │ ├── Reverse a Linked List in groups of given size │ │ └── README.md │ ├── Implementation.cpp │ ├── Middle of the Linked List │ │ └── README.md │ ├── Traverse.cpp │ └── Insert_At_begin.cpp ├── Circular Linked List │ ├── Implementation.cpp │ └── Traversal │ │ ├── Do_while_method.cpp │ │ └── For_Loop_method.cpp └── Doubly Linked List │ └── Implementation.cpp ├── Binary Tree ├── Inorder_Traversal.cpp ├── Postorder_Traversal.cpp ├── preorder_traversal.cpp ├── Height_of_Tree.cpp ├── Print_Node_At_Distance_K.cpp └── Construction of a Binary Tree from Inorder and Preorder traversal.cpp ├── Binary Search └── Calculating n-th real root using binary search │ └── README.md ├── GeekforGeeks ├── Addition of Two Numbers │ ├── Sol.cpp │ └── Problem.md ├── Small Factorial │ ├── Problem.md │ └── Sol.cpp ├── Sum of an AP │ ├── Sol.cpp │ └── Problem.md ├── Find Nth root of M │ └── Sol.cpp ├── Series AP │ └── Sol.cpp ├── Factorial │ ├── Sol.cpp │ └── Problem.md ├── Series GP │ └── Sol.cpp ├── Multiplication Table │ └── Sol.cpp ├── Odd or Even │ ├── Sol.cpp │ └── Problem.md ├── problem.md ├── Area of Rectange, Right Angled Triangle and Circle │ └── Sol.cpp ├── Find the smallest and second smallest elements in an array │ ├── Problem.md │ └── Sol.cpp ├── Prime Number │ └── Sol.cpp ├── GCD of two numbers │ └── Problem.md ├── Perfect Numbers │ └── Sol.cpp ├── Reverse Digits │ ├── Sol.cpp │ └── Problem.md ├── Sum of Array Elements │ ├── Sol.cpp │ └── Problem.md ├── Power of Pow | Even Number │ ├── Sol.cpp │ └── Problem.md ├── Swap two numbers │ ├── Sol.cpp │ └── Problem.md ├── Print the Kth Digit │ └── Sol.cpp ├── Sum of elements │ └── Sol.cpp ├── Mean │ ├── Sol.cpp │ └── Readme.md ├── Sum of odd and even elements │ ├── Sol.cpp │ └── Problem.md ├── Greatest of three numbers │ ├── Sol.cpp │ └── Problem.md ├── Reverse an Array │ ├── Sol.cpp │ └── Problem.md ├── Tower Of Hanoi │ └── Solution_1.cpp ├── Largest Element │ └── Sol.cpp ├── nPr │ ├── Sol.cpp │ └── Problem.md ├── Cube root of a number │ └── Problem.md ├── Count Squares │ └── Sol.cpp ├── Find n-th term of series 1, 3, 6, 10, 15, 21 │ └── Problem.md ├── Search an element in a sorted and rotated array │ └── Sol.cpp ├── 12 hour clock addition │ └── Problem.md ├── Armstrong Numbers │ └── Sol.cpp ├── Multiply Two Matrices │ └── Sol.cpp ├── nCr │ ├── Sol.cpp │ └── Problem.md ├── Check if a number is power of another number │ └── Sol.cpp ├── The dice problem │ ├── Sol.cpp │ └── Problem.md ├── Array │ └── Minimize the Heights II │ │ └── Solution_1.cpp ├── Sum of all prime numbers between 1 and N │ └── Sol.cpp └── Parallel or Perpendicular? │ └── Problem.md ├── Daily Coding Problem ├── Day147 │ └── Problem.md ├── Day 10 │ └── Problem.md ├── Day15 │ └── Problem.md ├── Day145 │ └── Problem.md ├── Day14 │ └── Problem.md ├── Day 131 │ └── Problem.md ├── Day21 │ └── Problem.md ├── Day13 │ └── Problem.md ├── Day 1 │ ├── problem.md │ └── Sol.cpp ├── Day 142 │ └── Problem.md ├── Day150 │ └── Problem.md ├── Day 135 │ └── Problem.md ├── Day 9 │ └── Problem.md ├── Day 8 │ └── problem.md ├── Day 137 │ └── Problem.md ├── Day 138 │ └── Problem.md ├── Day 136 │ └── Problem.md ├── Day 133 │ └── Problem.md ├── Day 7 │ └── problem.md ├── Day 134 │ └── Problem.md ├── Day 140 │ └── Problem.md ├── Day 5 │ └── problem.md ├── Day148 │ └── Problem.md ├── Day149 │ └── Problem.md ├── Day146 │ └── Problem.md ├── Day151 │ └── Problem.md ├── Day19 │ └── Problem.md ├── Day 11 │ └── Problem.md ├── Day16 │ └── Problem.md ├── Day 4 │ ├── problem.md │ └── sol.cpp ├── Day 2 │ ├── problem.md │ └── sol.cpp ├── Day20 │ └── Problem.md ├── Day 132 │ └── Problem.md ├── Day143 │ └── Problem.md ├── Day144 │ └── Problem.md ├── Day18 │ └── Problem.md ├── Day 139 │ └── Problem.md └── Day 6 │ └── problem.md ├── Trie └── Problem 1 │ └── README.md ├── CodeChef └── Practice Solutions │ ├── Beginner │ ├── Number Mirror │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Add Two Numbers │ │ ├── Solution_By_Yashasvi.cpp │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Packaging Cupcakes │ │ └── Solution_by_Pandz18.cpp │ ├── Chef and Remissness │ │ └── Solution_by_Pandz18.cpp │ ├── Sum OR Difference │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Decrement OR Increment │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Finding Square Root │ │ └── Solution_by_Pandz18.cpp │ ├── Pair Me │ │ └── Sol.cpp │ ├── Small Factorial │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Sum of Digits │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Two Numbers │ │ └── Sol.cpp │ ├── ATM │ │ ├── Solution_By_Shourya.cpp │ │ └── Solution_by_Pandz18.cpp │ ├── A Good Set │ │ └── Sol.cpp │ ├── Chef and Dolls │ │ └── Sol_by_Pandz18.cpp │ ├── Helping Chef │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── K Fibonnaci │ │ └── Sol.cpp │ ├── Turbo Sort │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Valid Triangles │ │ └── Solution_by_Pandz18.cpp │ ├── Reverse The Number │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Lucky Four │ │ └── Solution_by_Pandz18.cpp │ ├── Even Differences │ │ └── Sol.cpp │ ├── Hard Cash │ │ └── Sol.cpp │ ├── Chef And Operators │ │ └── Solution_by_Pandz18.cpp │ ├── The Block Game │ │ └── Solution_by_Pandz18.cpp │ ├── Enormous Input Test │ │ └── Solution_by_Pandz.cpp │ ├── Find Remainder │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Split Stones │ │ └── Sol.cpp │ ├── Mahasena │ │ └── Solution_by_Pandz18.cpp │ ├── Safe Robots │ │ └── Sol.cpp │ ├── Piece Of Cake │ │ └── Sol_by_Pandz18.cpp │ ├── Smart Phone │ │ └── Sol.cpp │ ├── Smallest Numbers of Notes │ │ └── Solution_by_Pandz18.cpp │ ├── Small Factorials │ │ └── readme.md │ ├── Ada King │ │ └── solution.cpp │ ├── Reduce to One │ │ └── Sol.cpp │ ├── Chef And Coloring │ │ └── sol_by_Pandz18.cpp │ ├── Nobel Prize │ │ └── Sol.cpp │ ├── Fancy Quotes │ │ └── Sol.cpp │ ├── Smart Strategy │ │ └── Sol.cpp │ ├── First and Last Digit │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Primality Test │ │ └── Solution_by_Pandz18.cpp │ ├── Playing with Strings │ │ └── Sol.cpp │ ├── Second Largest │ │ ├── Solution_by_Pandz18.cpp │ │ └── readme.md │ ├── Three Way communications │ │ └── Solution_by_Pandz18.cpp │ ├── Ads and Dishes │ │ └── Sol.cpp │ ├── Id and Ship │ │ └── Solution_by_Pandz18.cpp │ ├── Chef and Steps │ │ └── Sol_by_Pandz18.cpp │ └── Cutting Recipies │ │ └── Solution_by_Pandz18.cpp │ ├── Fit Squares in Triangle │ ├── Solution_by_Pandz18.cpp │ └── Solution_By_deenu-1001 │ ├── Puppy and Sum │ ├── Solution_By_deenu-1001 │ └── Solution_by_Pandz18.cpp │ ├── GCD and LCM │ ├── Solution_by_Pandz18.cpp │ └── readme.md │ ├── Simple Statistics │ └── Sol.cpp │ ├── Total Expenses │ └── Solution_by_Pandz18.cpp │ ├── Grade The Steel │ └── Solution_by_Pandz18.cpp │ ├── Ambiguous Permutations │ └── Solution_by_Pandz18.cpp │ └── Lapindromes │ └── Solution_by_Pandz18.cpp ├── Array ├── Rotating an Array │ ├── Cyclically rotate an array by one │ │ ├── Solution_1.cpp │ │ └── README.md │ ├── Left Rotate By One │ │ └── Sol.cpp │ └── Left Rotate Array By d │ │ └── Sol.cpp ├── Trapping Rain Water │ └── README.md ├── Array Maths │ ├── Sum of elements │ │ └── Sol.cpp │ ├── Product Of Two Arrays │ │ └── Sol.cpp │ └── Sum of Two Arrays │ │ └── sol.cpp ├── Array Modification │ ├── Is Sorted │ │ └── Sol.cpp │ ├── Insert element │ │ └── Sol.cpp │ ├── Reverse Array │ │ └── Sol.cpp │ ├── Remove Duplicate Elements │ │ └── Sol.cpp │ └── Moving All Zeros To The End │ │ └── Sol.cpp ├── Largest Element │ └── Sol.cpp ├── Searching │ ├── Linear search │ │ └── Sol.cpp │ └── Max Number In Array │ │ └── Solution.cpp ├── Best Time to Buy and Sell Stock │ └── Solution_1.cpp ├── Sorting │ ├── Bubble Sort │ │ └── Sol.cpp │ └── Insertion Sort │ │ ├── Solution_1.cpp │ │ └── Solution_By_Shourya.cpp ├── Leaders In Array │ └── Sol.cpp └── Maximum Consecutive 1's in a Binary Array │ └── Sol.cpp ├── Dynamic Programming ├── Max Non Adjacent Sum │ └── README.md ├── Count Tree │ ├── README.md │ ├── Bottom Up Approach.cpp │ └── Top Down Approach.cpp ├── Ladders Problem │ ├── README.md │ ├── Optimised Approach.cpp │ ├── Bottom Up Approach.cpp │ └── Top Down Approach.cpp ├── Subsequence Count │ └── README.md ├── Array Jump Problem │ └── README.md ├── Subset Sum Problem │ └── README.md ├── Knapsack Problem │ └── README.md ├── Fibonacci │ ├── Bottom Up Approach.cpp │ ├── Bottom Up Approach With Space optimisation.cpp │ └── Top Down Approach.cpp ├── Rod Cutting Problem │ ├── README.md │ └── Solution_1.cpp ├── Box Stacking │ └── README.md ├── Frog's Min Cost │ ├── README.md │ └── Solution_1.cpp ├── Longest Increasing Sequence │ ├── README.md │ └── Solution_1.cpp ├── Coin Change Problem │ ├── README.md │ └── Bottom Up Approach.cpp └── Selling Wines │ └── Top Down Approach.cpp ├── Problem Statement ├── Problem_Statement_2 │ ├── README.md │ └── Solution_1.cpp ├── Problem_Statement_3 │ └── Solution_1.cpp ├── Problem_Statement_7 │ ├── Maximum_Consecutive_1s.md │ └── Solution_1.cpp ├── Problem_Statement_9 │ ├── Solution_by_Pandz18.cpp │ └── PadovanSequence.md ├── Problem_Statement_8 │ └── Solution_by_Pandz18.cpp ├── Problem_Statement_1 │ ├── Solution_1.cpp │ └── README.md ├── Problem_Statment_5 │ └── Solution_by_Pandz18.cpp ├── Problem_Statement_6 │ └── Solution_By_Pandz18.cpp ├── Problem_Statement 4 │ └── Solution_by_Pandz18.cpp ├── Problem_Statement_12 │ └── Solution_1.cpp └── Problem_Statement_13 │ ├── README.md │ └── Solution_1.cpp ├── Basic Math ├── Subtraction │ └── Sub.cpp ├── Modulus Division │ └── Dividing.cpp ├── Basic factorials │ └── Sol.cpp ├── Increment and Decrement │ └── Sol_by_Pandz.cpp ├── Product Of Two Numbers │ └── Sol.cpp ├── Modulus Average │ └── Avg.cpp └── Digit Counting │ └── Count_the_digits.cpp ├── LeetCode ├── Easy │ ├── Sqrt(x) │ │ └── Sol.cpp │ ├── Fibonacci_Number │ │ └── Solution_1.cpp │ ├── Search Insert Position │ │ └── Sol.cpp │ ├── Reversed Integer │ │ ├── Sol.cpp │ │ └── readme.md │ ├── Plus One │ │ └── Sol.cpp │ ├── Two Sum │ │ └── Sol.cpp │ ├── Majority Element │ │ └── readme.md │ ├── Longest Common Prefix │ │ └── readme.md │ ├── Palindrome │ │ └── Sol.cpp │ └── Two Sum II - Input array is sorted │ │ └── Sol.cpp ├── Medium │ ├── Global and Local Inversions │ │ └── Solution_1.cpp │ ├── Unique Binary Search Trees │ │ ├── Solution_1.cpp │ │ └── README.md │ ├── Clumsy Factorial │ │ └── Solution_1.cpp │ ├── Longest Substring Without repeating characters │ │ ├── README.md │ │ └── Longest Substring Without repeating characters.cpp │ ├── Longest Increasing Subsequence │ │ ├── Solution_1.cpp │ │ └── README.md │ ├── Valid Triangle Number │ │ ├── README.md │ │ └── Valid Triangle Number.CPP │ ├── Coin Change DP Problem │ │ └── Solution_1.cpp │ ├── House Robber │ │ └── Solution_1.cpp │ ├── Jump Game II │ │ └── Solution_1.cpp │ └── Longest Common Subsequence │ │ └── Solution_1.cpp └── Hard │ ├── Orderly Queue │ └── Orderly Queue.cpp │ ├── N-Queens │ └── README.md │ └── Distinct Subsequences │ ├── README.md │ └── Solution_1.cpp ├── Bill_With_Discount ├── README.md └── solution.cpp ├── Searching ├── Interpolation Search │ └── Problem_Statement.md ├── Search_In_Infinite_Sized_Array │ └── Problem_Statement.md └── Count_1s_in_a_Binary_Array │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Daily Interview Pro ├── Reverse A Linked List │ └── problem.md ├── Longest Palindromic Substring │ └── problem.md ├── Sorting a list with 3 unique numbers │ └── Problem.md ├── Longest Substring Without Repeating Characters │ └── problem.md ├── Add two numbers as linked list │ └── problem.md ├── Find Cycles in a Graph │ └── Problem.md ├── Two-Sum │ └── Problem.md ├── Find the non-duplicate number │ └── Problem.md ├── Find Pythagorean Triplets │ └── Problem.md ├── Create a Simple Calculator │ └── Problem.md ├── Number of Ways to Climb Stairs │ └── Problem.md ├── First and Last indices of an element in a sorted array │ └── problem.md └── Edit Distance │ └── Problem.md ├── Greedy Algorithmns ├── Connecting_Wires │ ├── Problem_Statement.md │ └── Solution_1.cpp ├── Load Balancing │ └── Solution_1.cpp └── Activity_Selection_Problem │ └── Solution_1.cpp ├── Graph └── Shortest Cycle in an Undirected Unweighted Graph │ └── README.md ├── STL ├── Binary Search.cpp ├── Strings in STL.cpp └── Find Function.cpp ├── Backtracking ├── Palindromic Partitions │ └── README.md └── Print all permutations of a given string │ └── Solution_1.cpp ├── Math └── Birthday Paradox Problem.cpp └── azure-pipelines.yml /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Recursion/First Occurrence/README.md: -------------------------------------------------------------------------------- 1 | Check the first occurrence of number in a given array. 2 | 3 | ``` 4 | I/P:- 1,2,3,7,4,5,6,7,10 5 | key:- 7 6 | 7 | O/P:- 3 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Binary Search/Calculating n-th real root using binary search/README.md: -------------------------------------------------------------------------------- 1 | ``` 2 | Input : 5 2 3 | Output : 2.2360679768025875 4 | 5 | Input : x = 5, n = 3 6 | Output : 1.70997594668 7 | 8 | ``` 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /GeekforGeeks/Addition of Two Numbers/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a , b; 7 | cin>>a>>b; 8 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | for(int i=1 ; i>0 ; i++) 8 | cout<left); 6 | printPost(root->right); 7 | cout<data<<" "; 8 | } 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Trie/Problem 1/README.md: -------------------------------------------------------------------------------- 1 | ## ***Unique Prefix Array*** 2 | 3 | 4 | ### Input:- 5 | ``` 6 | ["cobra","dog","dove","duck"] 7 | ``` 8 | 9 | ### Output:- 10 | ``` 11 | ["c","dog","dov","du"] 12 | ``` 13 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Recursion/Infinite Series/infinite.cpp: -------------------------------------------------------------------------------- 1 | #include 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 main () 5 | { 6 | int n ; 7 | cin>>n; 8 | cout<0;i--) 5 | arr[i] = arr[i-1]; 6 | arr[0] = temp; 7 | } 8 | -------------------------------------------------------------------------------- /Dynamic Programming/Max Non Adjacent Sum/README.md: -------------------------------------------------------------------------------- 1 | #### Given an array of positivecintegers. Find the maximum sum of adjacent elements in the array. 2 | 3 | ``` 4 | I/P:- 6,10,12,7,9,14 5 | 6 | O/P:- 32 (6+12+14=32) 7 | 8 | ``` 9 | -------------------------------------------------------------------------------- /Recursion/Sum_Of_Digits/README.md: -------------------------------------------------------------------------------- 1 | ### Sum of digits using recursion 2 | 3 | 4 | 5 | > 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 | -------------------------------------------------------------------------------- /GeekforGeeks/Find Nth root of M/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 a1 , a2 , n; 7 | cin>>a1>>a2>>n; 8 | 9 | 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 | int A , R , N; 8 | 9 | cin>>A>>R>>N; 10 | 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 | -------------------------------------------------------------------------------- /Basic Math/Subtraction/Sub.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 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 "< 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 | 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 | -------------------------------------------------------------------------------- /LeetCode/Easy/Sqrt(x)/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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<= 100 and less than 500 then 10% discount. 6 | ### If bill amount >= 500 then 20% discount. 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Dynamic Programming/Subsequence Count/README.md: -------------------------------------------------------------------------------- 1 | ### Problem Statement 2 | Given two strings, find the number of times the second string occurs as a subsequence in the first string. 3 | 4 | ``` 5 | I/P:- s1 = "ABCDCE" 6 | s2 = "ABC" 7 | 8 | O/P:- 2 9 | ``` 10 | -------------------------------------------------------------------------------- /Basic Math/Modulus Division/Dividing.cpp: -------------------------------------------------------------------------------- 1 | #include 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 "<key)<<" "; 6 | } 7 | else{ 8 | printkDist(root->left,k-1); 9 | printkDist(root->right,k-1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /GeekforGeeks/Odd or Even/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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/Small Factorial/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 , i , fact=1; 7 | cin>>a; 8 | 9 | for(i=1;i<=a;i++) 10 | { 11 | fact=fact*i; 12 | } 13 | 14 | cout<<"Fact : "<<" "<& 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 | -------------------------------------------------------------------------------- /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 clearRange(int n, int i){ 5 | int mask = (-1<>n>>i; 11 | cout< 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 | { 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 -> 3 -> 4, return 2 -> 1 -> 4 -> 3. 8 | -------------------------------------------------------------------------------- /GeekforGeeks/Area of Rectange, Right Angled Triangle and Circle/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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/Find the smallest and second smallest elements in an array/Problem.md: -------------------------------------------------------------------------------- 1 | Write an efficient program to find smallest and second smallest element in an array. 2 | 3 | Example: 4 | 5 | Input: arr[] = {12, 13, 1, 10, 34, 1} 6 | 7 | Output: The smallest element is 1 and second Smallest element is 10 8 | -------------------------------------------------------------------------------- /Basic Math/Product Of Two Numbers/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 "<<" "< 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 | int main() { 5 | int t ; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int a,b; 10 | cin>>a>>b; 11 | 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< 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 | 4 | void print1toN(int n) 5 | { 6 | if(n==0) 7 | return; 8 | print1toN(n-1); 9 | cout< 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 | #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 , i , sum=0; 7 | cin>>n; 8 | 9 | for(i=1 ; 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] , sum=0 , n; 7 | 8 | cin>>n; 9 | for(int i=0; i>A[i]; 11 | 12 | for(int j=0; j 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 | void printNto1(int n){ 5 | if(n==0) 6 | return; 7 | else 8 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | 7 | int a, b; 8 | cin>>a>>b; 9 | cout<<"Before swap a= "<>N; 9 | 10 | for(i=N ; i>0 ; i--) 11 | { 12 | perm=perm*i; 13 | } 14 | 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[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 | 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 | -------------------------------------------------------------------------------- /Dynamic Programming/Knapsack Problem/README.md: -------------------------------------------------------------------------------- 1 | Given weights and prices of n items, we need put a subset of items in a bag of capacity W such that we get the max. total value in the bag, also known as knapsack problem. 2 | 3 | ``` 4 | I/P:- N = 4, W = 11 5 | wts = {2,7,3,4} 6 | prices = {5,20,20,10} 7 | 8 | O/P:- 40 9 | ``` 10 | -------------------------------------------------------------------------------- /Recursion/Decreasing 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 | cout<>n; 16 | dec(n); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Recursion/Fibonacci/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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 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 | 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 | 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 | 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 | 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 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 | #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 | 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Searching/Interpolation Search/Problem_Statement.md: -------------------------------------------------------------------------------- 1 | ### Problem Statement 2 | 3 | ### Given an array, find the index of the element to be searched using Interpolation Search 4 | 5 | ### Sample Input & Output 6 | 7 | ``` 8 | I/P:- arr[] = { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47 }, x=18 9 | 10 | O/P:- Element found at index 4 11 | ``` 12 | 13 | -------------------------------------------------------------------------------- /Bit_Manipulation/Modify a bit at a given position/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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< 3 -> 2 -> 1 -> 0 -> NULL 9 | 10 | Output: 0 -> 1 -> 2 -> 3 -> 4 -> NULL 11 | -------------------------------------------------------------------------------- /Greedy Algorithmns/Connecting_Wires/Problem_Statement.md: -------------------------------------------------------------------------------- 1 | ### There are n white dots and n black dots, equally spaced in a line. You want to connect each white dot with some one black dot, with a minimum total length of wire. Find the total length of the wire needed. 2 | 3 | #### Input:- 4 | ``` 5 | 3 4 1 6 | 2 5 6 7 | ``` 8 | 9 | ### Output:- 10 | ``` 11 | 5 12 | ``` 13 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Small Factorial/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 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 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 | int main() 4 | { 5 | int A[7]={1,2,3,4,5,6,7} , i , n=7,sum=0; 6 | 7 | for (i = 0; i < 7; i++) // using for each loop write for(int x:A) 8 | { 9 | sum=sum+A[i]; // and here sum=sum+x 10 | } 11 | cout<<"Sum is "<dp(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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Number Mirror/readme.md: -------------------------------------------------------------------------------- 1 | ## Problem Statement 2 | Write a program that accepts a number, n, and outputs the same. 3 | 4 | ## Input 5 | The only line contains a single integer. 6 | 7 | ## Output 8 | Output the answer in a single line. 9 | 10 | ## Constraints 11 | 0 ≤ n ≤ 105 12 | 13 | ## Sample Input 14 | 123 15 | 16 | ## Sample Output 17 | 123 18 | -------------------------------------------------------------------------------- /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 isSorted(int arr[], int n){ 5 | for(int i=1;i 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 | -------------------------------------------------------------------------------- /Bit_Manipulation/Clear Range I to J.cpp: -------------------------------------------------------------------------------- 1 | #include 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< 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 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 1/problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was recently asked by Google. 4 | 5 | Given a list of numbers and a number k, return whether any two 6 | numbers from the list add up to k. 7 | 8 | For example, given [10, 15, 3, 7] and k of 17, return true since 9 | 10 + 7 is 17. 10 | 11 | Bonus: Can you do this in one pass? 12 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 142/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Microsoft. 4 | 5 | Implement 3 stacks using a single list: 6 | 7 | class Stack: 8 | def __init__(self): 9 | self.list = [] 10 | 11 | def pop(self, stack_number): 12 | pass 13 | 14 | def push(self, item, stack_number): 15 | pass 16 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 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 t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,p=0; 11 | cin>>n; 12 | int arr[n]; 13 | for(int i=0;i>arr[i]; 16 | p^=arr[i]; 17 | } 18 | 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 | 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/K Fibonnaci/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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 , 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 | 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<1) 5 | { 6 | sort(s.begin(),s.end()); 7 | return s; 8 | } 9 | string ans = s; 10 | for(int i=1;i 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 | 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 | 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 | #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 | -------------------------------------------------------------------------------- /GeekforGeeks/Cube root of a number/Problem.md: -------------------------------------------------------------------------------- 1 | Given a number N, find the cube root of N. 2 | 3 | Note: We need to print the floor value of the result. 4 | 5 | Example 1: 6 | 7 | Input: 8 | 9 | N = 3 10 | 11 | Output: 12 | 13 | 1 14 | 15 | Explanation: 16 | 17 | Cube root of 3 is 1.442 = 1 18 | 19 | Example 2: 20 | 21 | Input: 22 | 23 | N = 8 24 | 25 | Output: 26 | 27 | 2 28 | 29 | Explanation: 30 | 31 | Cube root of 8 is 2 32 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 8/problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Google. 4 | 5 | A unival tree (which stands for "universal value") is a tree 6 | where all nodes under it have the same value. 7 | 8 | Given the root to a binary tree, count the number of unival subtrees. 9 | 10 | ``` 11 | 0 12 | / \ 13 | 1 0 14 | / \ 15 | 1 0 16 | / \ 17 | 1 1 18 | ``` 19 | -------------------------------------------------------------------------------- /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< 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 | 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 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 | 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 | -------------------------------------------------------------------------------- /STL/Strings in STL.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | // 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 | 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 | 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 | 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 | 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 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 | 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 | -------------------------------------------------------------------------------- /GeekforGeeks/12 hour clock addition/Problem.md: -------------------------------------------------------------------------------- 1 | Given two positive integers num1 and num2, the task is to find the sum of the two numbers on a 12 hour clock 2 | rather than a number line. 3 | 4 | Example 1: 5 | 6 | Input: num1 = 5, num2 = 7 7 | 8 | Output: 0 9 | 10 | Explaination: 5+7=12, which in 12 hour clock format is 0. 11 | 12 | Example 2: 13 | 14 | Input: num1 = 3, num2 = 5 15 | 16 | Output: 8 17 | 18 | Explaination: 3+5=8, which is 12 hour format is intact. 19 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 7/problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Facebook. 4 | 5 | Given the mapping a = 1, b = 2, ... z = 26, and an encoded 6 | message, count the number of ways it can be decoded. 7 | 8 | For example, the message '111' would give 3, since it could 9 | be decoded as 'aaa', 'ka', and 'ak'. 10 | 11 | You can assume that the messages are decodable. For example, 12 | '001' is not allowed. 13 | -------------------------------------------------------------------------------- /Linked List/Circular Linked List/Implementation.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 | 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Dynamic Programming/Rod Cutting Problem/README.md: -------------------------------------------------------------------------------- 1 | ##### You are given an integer N, which represents the length of a rod, and an array of integers, which represents the prices of rod pieces of length varying from 1 to N. 2 | ##### You have to find the maximum value that can be obtained by selling the rod. 3 | ##### You can sell it in pieces or as a whole. 4 | 5 | #### Sample Input:- 6 | 5 7 | 8 8 | 9 9 | 10 10 | 17 11 | 17 12 | 20 13 | 14 | #### Sample Output: 15 | 22 16 | -------------------------------------------------------------------------------- /Bit_Manipulation/Program to find whether a no is power of two/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Even Differences/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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 | 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 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"<& 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 | -------------------------------------------------------------------------------- /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 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 | { 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/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 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 | 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 | 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<0;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/Unique Binary Search Trees/README.md: -------------------------------------------------------------------------------- 1 | #### Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n. 2 | 3 | ##### Example 1: 4 | 5 | ![LEETCODE](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg) 6 | ``` 7 | Input: n = 3 8 | Output: 5 9 | ``` 10 | 11 | ##### Example 2: 12 | ``` 13 | Input: n = 1 14 | Output: 1 15 | ``` 16 | 17 | ##### Constraints: 18 | ``` 19 | 1 <= n <= 19 20 | ``` 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 140/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Facebook. 4 | 5 | Given an array of integers in which two elements appear 6 | exactly once and all other elements appear exactly twice, 7 | find the two elements that appear only once. 8 | 9 | For example, given the array [2, 4, 6, 8, 10, 2, 6, 10], 10 | return 4 and 8. The order does not matter. 11 | 12 | Follow-up: Can you do this in linear time and constant space? 13 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 5/problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Jane Street. 4 | 5 | cons(a, b) constructs a pair, and car(pair) and cdr(pair) 6 | returns the first and last element of that pair. For example, 7 | car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. 8 | 9 | Given this implementation of cons: 10 | 11 | def cons(a, b): 12 | def pair(f): 13 | return f(a, b) 14 | return pair 15 | Implement car and cdr. 16 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Array/Searching/Max Number In Array/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Dynamic Programming/Count Tree/Bottom Up Approach.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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>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 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 | 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 | #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>"Enter the position "; 21 | cin>>n; 22 | cout << pad(n); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /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/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/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 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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Split Stones/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 11/Problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Twitter. 4 | 5 | Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix. 6 | 7 | For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal]. 8 | 9 | Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries. 10 | 11 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Bit_Manipulation/Copy set bits in a range/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /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 , 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 | 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 | #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 | #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 | -------------------------------------------------------------------------------- /Recursion/Ladders Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 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< 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 | 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 | 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 | 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<& 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 | -------------------------------------------------------------------------------- /azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | # Starter pipeline 2 | # Start with a minimal pipeline that you can customize to build and deploy your code. 3 | # Add steps that build, run tests, deploy, and more: 4 | # https://aka.ms/yaml 5 | 6 | trigger: 7 | - main 8 | 9 | pool: 10 | vmImage: ubuntu-latest 11 | 12 | steps: 13 | - script: echo Hello, world! 14 | displayName: 'Run a one-line script' 15 | 16 | - script: | 17 | echo Add other tasks to build, test, and deploy your project. 18 | echo See https://aka.ms/yaml 19 | displayName: 'Run a multi-line script' 20 | -------------------------------------------------------------------------------- /Bit_Manipulation/Find position of the only set bit/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Fit Squares in Triangle/Solution_By_deenu-1001: -------------------------------------------------------------------------------- 1 | #include 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 | { 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 | 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 | 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 | 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"< 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 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 2/problem.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Uber. 4 | 5 | Given an array of integers, return a new array such that each 6 | element at index i of the new array is the product of all the 7 | numbers in the original array except the one at i. 8 | 9 | For example, if our input was [1, 2, 3, 4, 5], the expected 10 | output would be [120, 60, 40, 30, 24]. If our input was 11 | [3, 2, 1], the expected output would be [2, 3, 6]. 12 | 13 | Follow-up: what if you can't use division? 14 | -------------------------------------------------------------------------------- /Dynamic Programming/Ladders Problem/Optimised Approach.cpp: -------------------------------------------------------------------------------- 1 | #include 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< 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 | -------------------------------------------------------------------------------- /LeetCode/Easy/Majority Element/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | 3 | Given an array nums of size n, return the majority element. 4 | 5 | The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 6 | 7 | 8 | 9 | ### Example 1: 10 | 11 | Input: nums = [3,2,3] 12 | 13 | Output: 3 14 | 15 | ### Example 2: 16 | 17 | Input: nums = [2,2,1,1,1,2,2] 18 | Output: 2 19 | 20 | 21 | ### Constraints: 22 | 23 | n == nums.length 24 | 25 | 1 <= n <= 5 * 104 26 | 27 | -231 <= nums[i] <= 231 - 1 28 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Smallest Numbers of Notes/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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< 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 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 "< 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< 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 | 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 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 | // 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 | 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 | using namespace std; 3 | 4 | void reverseArray(int arr[], int n){ 5 | int low = 0,high = n-1; 6 | while(low 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 | // 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 | 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< 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 | int main() 5 | { 6 | int n; 7 | cin>>n; 8 | 9 | if(n==1) 10 | { 11 | cout<<"6"; 12 | } 13 | else if(n==2) 14 | { 15 | cout<<"5"; 16 | } 17 | else if(n==3) 18 | { 19 | cout<<"4"; 20 | } 21 | else if(n==4) 22 | { 23 | cout<<"3"; 24 | } 25 | else if(n==5) 26 | { 27 | cout<<"2"; 28 | } 29 | else if(n==6) 30 | { 31 | cout<<"1"; 32 | } 33 | else 34 | { 35 | cout<<"Invalid"; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Recursion/String To Integers.cpp: -------------------------------------------------------------------------------- 1 | #include 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< 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 | 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 | #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/Grade The Steel/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 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"< 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< 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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Fancy Quotes/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | #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< 5 | using namespace std; 6 | 7 | int maxConOnes(bool arr[], int n){ 8 | int res=0,curr=0; 9 | for(int i =0;ie){ 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 | -------------------------------------------------------------------------------- /Dynamic Programming/Frog's Min Cost/README.md: -------------------------------------------------------------------------------- 1 | #### There are N stones, numbered 1,2,…,N. For each i (1≤i≤N), the height of Stone i is hi.There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:If the frog is currently on Stone i, jump to Stone i+1 or Stone i+2. Here, a cost of |hi−hj| is incurred, where j is the stone to land on.Find the minimum possible total cost incurred before the frog reaches Stone N. 2 | 3 | ``` 4 | I/P:- 2 5 | 10 10 6 | 7 | O/P:- 0 8 | ``` 9 | 10 | ``` 11 | I/P:- 6 12 | 30 10 60 10 60 50 13 | 14 | O/P:- 40 15 | 16 | ``` 17 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Find Remainder/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | Write a program to find the remainder when an integer A is divided by an integer B. 3 | 4 | ## Input 5 | The first line contains an integer T, the total number of test cases. Then T lines follow, each line contains two Integers A and B. 6 | 7 | ## Output 8 | For each test case, find the remainder when A is divided by B, and display it in a new line. 9 | 10 | ## Constraints 11 | 1 ≤ T ≤ 1000 12 | 13 | 1 ≤ A,B ≤ 10000 14 | 15 | ## Example 16 | 17 | ## Input 18 | 3 19 | 1 2 20 | 100 200 21 | 40 15 22 | 23 | ## Output 24 | 1 25 | 100 26 | 10 27 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/First and Last Digit/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | #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<= 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 | -------------------------------------------------------------------------------- /Greedy Algorithmns/Load Balancing/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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<& 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Primality Test/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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 | -------------------------------------------------------------------------------- /GeekforGeeks/Sum of Array Elements/Problem.md: -------------------------------------------------------------------------------- 1 | Given an integer array arr of size n, you need to sum the elements of arr. 2 | 3 | Example 1: 4 | 5 | Input: 6 | 7 | n = 3 8 | 9 | arr[] = {3 2 1} 10 | 11 | Output: 6 12 | 13 | Example 2: 14 | 15 | Input: 16 | 17 | n = 4 18 | 19 | arr[] = {1 2 3 4} 20 | 21 | Output: 10 22 | 23 | Your Task: 24 | You need to complete the function sumElement() that takes arr and n and returns 25 | the sum. The printing is done by the driver code. 26 | 27 | Expected Time Complexity: O(n). 28 | 29 | Expected Auxiliary Space: O(1). 30 | 31 | Constraints: 32 | 33 | 1 <= n <= 103 34 | 35 | 1 <= arri <= 104 36 | -------------------------------------------------------------------------------- /LeetCode/Easy/Longest Common Prefix/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | 3 | Write a function to find the longest common prefix string amongst an array of strings. 4 | 5 | If there is no common prefix, return an empty string "". 6 | 7 | 8 | 9 | ### Example 1: 10 | 11 | Input: strs = ["flower","flow","flight"] 12 | Output: "fl" 13 | 14 | ### Example 2: 15 | 16 | Input: strs = ["dog","racecar","car"] 17 | Output: "" 18 | Explanation: There is no common prefix among the input strings. 19 | 20 | 21 | ### Constraints: 22 | 23 | 0 <= strs.length <= 200 24 | 0 <= strs[i].length <= 200 25 | strs[i] consists of only lower-case English letters. 26 | -------------------------------------------------------------------------------- /Array/Rotating an Array/Cyclically rotate an array by one/README.md: -------------------------------------------------------------------------------- 1 | Given an array, rotate the array by one position in clock-wise direction. 2 | 3 | Example 1: 4 | ``` 5 | Input: 6 | N = 5 7 | A[] = {1, 2, 3, 4, 5} 8 | Output: 9 | 5 1 2 3 4 10 | ``` 11 | 12 | Example 2: 13 | ``` 14 | Input: 15 | N = 8 16 | A[] = {9, 8, 7, 6, 4, 2, 1, 3} 17 | Output: 18 | 3 9 8 7 6 4 2 1 19 | ``` 20 | 21 | Your Task: 22 | You don't need to read input or print anything. Your task is to complete the function rotate() which takes the array A[] and its size N as inputs and modify the array. 23 | 24 | Expected Time Complexity: O(N) 25 | Expected Auxiliary Space: O(1) 26 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day144/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 numbers and an index i, return the index of 6 | the nearest larger number of the number at index i, where distance 7 | is measured in array indices. 8 | 9 | For example, given [4, 1, 3, 5, 6] and index 0, you should return 3. 10 | 11 | If two distances to larger numbers are the equal, then return any one of them. 12 | If the array at i doesn't have a nearest larger integer, then return null. 13 | 14 | Follow-up: If you can preprocess the array, can you do this in constant time? 15 | -------------------------------------------------------------------------------- /LeetCode/Easy/Palindrome/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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/Hard/Distinct Subsequences/README.md: -------------------------------------------------------------------------------- 1 | Given two strings s and t, return the number of distinct subsequences of s which equals t. 2 | 3 | A string's subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining characters' relative positions. (i.e., "ACE" is a subsequence of "ABCDE" while "AEC" is not). 4 | 5 | It is guaranteed the answer fits on a 32-bit signed integer. 6 | 7 | 8 | **Example 1:** 9 | ``` 10 | Input: s = "rabbbit", t = "rabbit" 11 | Output: 3 12 | ``` 13 | 14 | **Example 2:** 15 | ``` 16 | Input: s = "babgbag", t = "bag" 17 | Output: 5 18 | ``` 19 | -------------------------------------------------------------------------------- /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; i 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< 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< 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 | 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 | #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<& 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 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[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 m; 6 | int i =0 , j=0 , count =0 ; 7 | while(j 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 | -------------------------------------------------------------------------------- /Recursion/Series/Series.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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 | 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 | -------------------------------------------------------------------------------- /GeekforGeeks/nPr/Problem.md: -------------------------------------------------------------------------------- 1 | Write a program to calculate nPr. nPr represents n permutation r and value of nPr is (n!) / (n-r)!. 2 | 3 | Example 1: 4 | 5 | Input: n = 2, r = 1 6 | 7 | Output: 2 8 | 9 | Explaination: 2!/(2-1)! = 2!/1! = (2*1)/1 = 2. 10 | 11 | Example 2: 12 | 13 | Input: n = 3, r = 3 14 | 15 | Output: 6 16 | 17 | Explaination: 3!/(3-3)! = 3!/0! = 6/1 = 6. 18 | 19 | Your Task: 20 | You do not need to read input or print anything. Your task is to complete the function nPr() which 21 | takes n and r as input parameters and returns nPr . 22 | 23 | Expected Time Complexity: O(n) 24 | 25 | Expected Auxiliary Space: O(1) 26 | 27 | Constraints: 28 | 29 | 1 ≤ n, r ≤ 20 30 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Ads and Dishes/Sol.cpp: -------------------------------------------------------------------------------- 1 | #include 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 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Problem Statement/Problem_Statement_13/README.md: -------------------------------------------------------------------------------- 1 | #### There are N stones, numbered 1,2,…,N. For each i (1≤i≤N), the height of Stone i is hi.There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:If the frog is currently on Stone i, jump to Stone i+1 or Stone i+2. Here, a cost of |hi−hj| is incurred, where j is the stone to land on.Find the minimum possible total cost incurred before the frog reaches Stone N. 2 | 3 | ``` 4 | I/P:- 2 5 | 10 10 6 | 7 | O/P:- 0 8 | ``` 9 | 10 | ``` 11 | I/P:- 6 12 | 30 10 60 10 60 50 13 | 14 | O/P:- 40 15 | 16 | ``` 17 | 18 | [Problem Link:- AtCoder](https://atcoder.jp/contests/dp/tasks/dp_a) 19 | -------------------------------------------------------------------------------- /Array/Sorting/Insertion Sort/Solution_By_Shourya.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /Daily Coding Problem/Day 139/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 iterator with methods next() and hasNext(), create 6 | a wrapper iterator, PeekableInterface, which also implements 7 | peek(). peek shows the next element that would be returned 8 | on next(). 9 | 10 | Here is the interface: 11 | ``` 12 | class PeekableInterface(object): 13 | 14 | def __init__(self, iterator): 15 | 16 | pass 17 | 18 | def peek(self): 19 | 20 | pass 21 | 22 | def next(self): 23 | 24 | pass 25 | 26 | def hasNext(self): 27 | 28 | pass 29 | ``` 30 | -------------------------------------------------------------------------------- /Dynamic Programming/Ladders Problem/Top Down Approach.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | 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 | 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 | -------------------------------------------------------------------------------- /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 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;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 | -------------------------------------------------------------------------------- /Recursion/First Occurrence/Solution_1.cpp: -------------------------------------------------------------------------------- 1 | #include 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 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 | 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Bit_Manipulation/Copy set bits in a range/README.md: -------------------------------------------------------------------------------- 1 | Given two numbers x and y, and a range [l, r] where 1 <= l, r <= 32. The task is consider set bits of y in range [l, r] and set these bits in x also. Examples : 2 | 3 | Input : x = 10, y = 13, l = 2, r = 3 4 | Output : x = 14 5 | Binary representation of 10 is 1010 and 6 | that of y is 1101. There is one set bit 7 | in y at 3’rd position (in given range). 8 | After we copy this bit to x, x becomes 1110 9 | which is binary representation of 14. 10 | 11 | Input : x = 8, y = 7, l = 1, r = 2 12 | Output : x = 11 13 | 14 | 15 | We can one by one find set bits of y by traversing given range. For every set bit, we OR it to existing bit of x, so that the becomes set in x, if it was not set. 16 | -------------------------------------------------------------------------------- /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 | #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<>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 | -------------------------------------------------------------------------------- /Recursion/0 1 Knapsack Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 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 | #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 | 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 | #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 | // 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 | #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- 3 17 | #### *Sample Output 1* 18 | >- Weird 19 | 20 | 21 | 22 | #### *Sample Input 2* 23 | >- 24 24 | #### *Sample Output 2* 25 | >- Not Weird 26 | -------------------------------------------------------------------------------- /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 | 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 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Beginner/Helping Chef/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | 3 | Write a program, which takes an integer N and if the number is less than 10 then display "Thanks for helping Chef!" otherwise print "-1". 4 | 5 | ### Input 6 | 7 | The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N. 8 | 9 | ### Output 10 | 11 | For each test case, output the given string or -1 depending on conditions, in a new line. 12 | 13 | ### Constraints 14 | 15 | 1 ≤ T ≤ 1000 16 | 17 | -20 ≤ N ≤ 20 18 | 19 | ### Example 20 | 21 | ### Input 22 | 23 | 3 24 | 25 | 1 26 | 27 | 12 28 | 29 | -5 30 | 31 | ### Output 32 | 33 | Thanks for helping Chef! 34 | 35 | -1 36 | 37 | Thanks for helping Chef! 38 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/GCD and LCM/readme.md: -------------------------------------------------------------------------------- 1 | ## PROBLEM STATEMENT 2 | 3 | Two integers A and B are the inputs. Write a program to find GCD and LCM of A and B. 4 | 5 | ## Input 6 | 7 | The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer A and B. 8 | 9 | ## Output 10 | 11 | Display the GCD and LCM of A and B separated by space respectively. The answer for each test case must be displayed in a new line. 12 | 13 | ## Constraints 14 | 15 | 1 ≤ T ≤ 1000 16 | 17 | 1 ≤ A,B ≤ 1000000 18 | 19 | ## Example 20 | 21 | ## Input 22 | 23 | 3 24 | 25 | 120 140 26 | 27 | 10213 312 28 | 29 | 10 30 30 | 31 | ## Output 32 | 33 | 20 840 34 | 35 | 1 3186456 36 | 37 | 10 30 38 | -------------------------------------------------------------------------------- /CodeChef/Practice Solutions/Lapindromes/Solution_by_Pandz18.cpp: -------------------------------------------------------------------------------- 1 | #include 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"<>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 | --------------------------------------------------------------------------------