├── .DS_Store ├── .vscode ├── settings.json └── tasks.json ├── A) Hello World ├── Addition of two numbers.cpp ├── Calculator using c++.cpp ├── Checking size of variables.cpp ├── Division of two numbers.cpp ├── Hello World.cpp ├── Multiplication of two numbers.cpp ├── Substraction of 2 numbers.cpp └── sum of two numbers.cpp ├── B) Ifelse Statement ├── 2.1 find the greatest number.cpp ├── 2.2 even And odd.cpp ├── 2.3 Eligible to vote or not using if else.cpp ├── 2.3 Eligible to vote or not.cpp ├── 2.4 Find the greatest number using else if.cpp ├── 2.4 Find the greatest number.cpp ├── 2.5 Find the greatest number usind Nested If.cpp ├── 2.5 Find the greatest number usind Nested If.exe ├── 2.6 Find the Minimum And the Maximum.cpp ├── 2.6 Find the Minimum And the Maximum.exe ├── 2.7 Check if a tirangle is scalen, isoscelens or equilateral.cpp └── 2.8 Check If an Alphabet Is a vowel or a consonant.cpp ├── C) Loops in C++ ├── 3.1 for loop.cpp ├── 3.10 prime or not.cpp ├── 3.11 prime no in a given range.cpp ├── 3.12 Switch Statement.cpp ├── 3.13 Simple Calculator.cpp ├── 3.14 Vowel or Consonant.cpp ├── 3.2 while loop.cpp ├── 3.3 do while loop.cpp ├── 3.4 Program to find the sum of nutral numbers till n.cpp ├── 3.5 prigram to display the multiplication table upto 10.cpp ├── 3.6 Program to add only positive number.cpp ├── 3.7 countinue statement.cpp ├── 3.8 Break statement.cpp ├── 3.9 Neha's Problem.cpp └── Write a program to print all odd number till n.cpp ├── D) pattern ├── 4.1 solid rectangle.cpp ├── 4.10 Floyd's Triangle.cpp ├── 4.11 print 01 pattern.cpp ├── 4.12 palindromic pattern.cpp ├── 4.13 Anagram.cpp ├── 4.2 Adv pattern │ ├── 4.2.1 dimond using starts vs.cpp │ └── 4.2.1 dimond using starts.cpp ├── 4.2 hollow rectangle.cpp ├── 4.3 Half pyramid.cpp ├── 4.4 inverted half pyramid.cpp ├── 4.5 half pyramid after 180 degree rotation.cpp ├── 4.6 half pyramid using numbers.cpp ├── 4.7 Inverted half pyramid using number.cpp ├── 4.8 half pyramid using number 2.cpp ├── 4.9 inverted hlaf pyramid 2.cpp └── pascals triangle.cpp ├── E) Funtion ├── 6.1 write a program to add two numbers using funtion.cpp ├── 6.10 Swapping using funtion.cpp ├── 6.11 Voting Eligibility.cpp ├── 6.2 prime numbers between 2 numbers using funtion.cpp ├── 6.3 Fibonacci Sequence using funtion.cpp ├── 6.4 factorial Number.cpp ├── 6.5 Binary Coefficient nCr.cpp ├── 6.6 pascal triangle.cpp ├── 6.7 Given number even or odd using funtion.cpp ├── 6.8 Given character Alphabet or not using funtion.cpp ├── 6.9 Maximum Numbers from three given numbers using funtion.cpp └── funtion Placement Question │ ├── 6.1.1 Sum of n nutral numbers VS.cpp │ ├── 6.1.1 Sum of n nutral numbers.cpp │ ├── 6.1.17 DecimalToHexadecimal.cpp │ ├── 6.1.2 Pythagorean Triplet.cpp │ ├── 6.1.3 BInaryToDecimal.cpp │ ├── 6.1.4 OctalToDecimal.cpp │ ├── 6.1.5 HexadecimalToDecimal.cpp │ ├── 6.1.6 DecimalToBinary.cpp │ └── 6.1.8 DecimalToOctal.cpp ├── F) Array ├── 1.Search.cpp ├── 10. Remove Duplicate soln 2.cpp ├── 11. Move Zero's to End.cpp ├── 12. Left Rotate an Array by one.cpp ├── 13. Left Rotate an Array by d place.cpp ├── 14. Left Rotate an Array by d place soln 2.cpp ├── 1480. Running Sum of 1d Array.cpp ├── 15. Left Rotate an Array by d place soln 3.cpp ├── 16. Leader in an Array O(n^2).cpp ├── 1672. Richest Customer Wealth.cpp ├── 17. Leader in an Array O(n).cpp ├── 18. Maximum Difference O(n).cpp ├── 19. Maximum Difference O(n^2).cpp ├── 2.Insertion.cpp ├── 20. Product of Array elements except itself.cpp ├── 21. Second Largest Element in an Array.cpp ├── 22.Count Distinct Element in Every K Size Window.cpp ├── 23. Maximum Consecutive Ones.cpp ├── 2D array │ ├── 1.2D array declaration.cpp │ ├── 2. Searching a element in a matrix.cpp │ ├── 3. Sprial order Matrix traversal.cpp │ ├── 4.Transpose of a matrix.cpp │ ├── 5.Matrix Multiplication.cpp │ ├── 6.Set_Matrix_Zero.cpp │ └── Search_2D_Matrix.cpp ├── 3. Delete.cpp ├── 4. Largest ln Array O(n^2).cpp ├── 5. Largest ln Array O(n).cpp ├── 6. Check Array Sorted or not.cpp ├── 7. Check Array Sorted or not O(n).cpp ├── 8. Reverse an array.cpp ├── 8.1 Find the min and the max term.cpp ├── 8.2 Sum Of arrays.cpp ├── 8.3 Greater NUmber.cpp ├── 8.4 Max Till i.cpp ├── 9. Remove Duplicate from sorted array.cpp ├── Character Array │ ├── 1.Declaration.cpp │ ├── 2.Character Pallindrome.cpp │ └── 3.largest word in the sentence.cpp ├── Combination Sum II │ ├── README.md │ └── combination-sum-ii.cpp ├── Searching in an Array │ ├── 8.1.1 Linear Search.cpp │ └── 8.1.2 Binary Search.cpp ├── Subarray │ ├── 8.2.1 Sum of each subarray in given array.cpp │ ├── 8.2.2 Subarray with given sum.cpp │ ├── 8.2.3 Smallest Postivite Number.cpp │ ├── 8.2.4 Subarray Using brute Force Approach.cpp │ ├── 8.2.5 sum of maximum subarray in a given array.cpp │ ├── 8.2.6Longest Subarray Zero Sum.cpp │ └── Google kick start │ │ ├── 1.Longest Arithmetic Array.cpp │ │ ├── 2.2 Record breaking day.cpp │ │ ├── 2.Record breaking day.cpp │ │ ├── 3.3 First repeating element.cpp │ │ └── 3.First repeating element.cpp └── Union of Two Sorted Arrays │ ├── README.md │ └── union-of-two-sorted-arrays.cpp ├── G) Searching & Sorting ├── 1. sorting using Sort function.cpp ├── 2. Sorting using Vector.cpp ├── 3. Bubble Sort.cpp ├── 4. Selection Sort.cpp ├── 5. Insertion Sort.cpp ├── Comparisons & Visualization │ ├── Comaprison.cpp │ └── Reults.py ├── HeapSort.cpp ├── MergeSort.cpp ├── QuickSort.cpp ├── RadixSort.cpp ├── Searching │ ├── BinarySearch.cpp │ └── linerSearch.cpp ├── Sort 0's and 1's in an Array.cpp ├── TopologicalSort.cpp └── mergesort.c++ ├── H) Pointer ├── 1.Declaration.cpp ├── 2.Pointer array.cpp ├── 3.pointer to funrtion.cpp ├── 4.call by value and referance.cpp └── 5.Heap memory allocation.cpp ├── I) String ├── 1.Declaration string.cpp ├── 10.Convert Upper Case to Lower case.cpp ├── 11.Form the biggest number from the gicen number.cpp ├── 12.Output the character which has maximum number of presence.cpp ├── 13.Compressing a string.cpp ├── 14.Group_Anagrams.cpp ├── 15.Valid_Palindrome.cpp ├── 2.Different types of string funtion.cpp ├── 3.Empty Funtion.cpp ├── 4.Erase Funtion.cpp ├── 5.Search ,find funtion.cpp ├── 6.insert funtion.cpp ├── 7.Substring.cpp ├── 8.String to numeric Funtion.cpp └── 9.Sorting a String.cpp ├── J) Bit Magic ├── 1.Get ith Bit.cpp ├── 11.Power of Two.cpp ├── 12.Program two to count numbers of one in a binary representstion.cpp ├── 13.Subsets Of posssible Set.cpp ├── 14.XOR funtion (unique number in a array present twice except one).cpp ├── 15.XOR funtion ( 2 unique number in a array present twice except one).cpp ├── 16.XOR funtion (unique number in a array present trice except one).cpp ├── 2.Set bit.cpp ├── 3.Clear bit.cpp ├── 4.UpdateBIt.cpp ├── 5. ToggleBit.cpp ├── 6.Right and Left Shift.cpp ├── 7. And Or Xor Nor.cpp ├── 8. Check if K-th bit is set.cpp ├── Maximum Product of Word Lengths.cpp └── Number of 1 Bits │ ├── Number of 1 Bits.docx │ └── source code.cpp ├── K) Sives Of Eratosthenes ├── 1. Prime Number in a given Range Using Sives.cpp └── 2.Prime factorization using Sives.cpp ├── L) Principle Of Inclusion and Exclusion ├── 1.Find the Number of number in interval 1,n which are divisible by a or b.cpp └── 2.Greatest Common Divisor or HCF.cpp ├── M) Recursion ├── 1. Get the sum of the first n number using recursion.cpp ├── 10.Tower Of Hanoi.cpp ├── 11.Remove all duplicates from the string.cpp ├── 12.Move all X to the end of the string using recursion.cpp ├── 13.Generate all substring of a string using recursion.cpp ├── 14.Generate substring with ASCII number using recursion.cpp ├── 15.Print all possible words from phone digits using recursion.cpp ├── 16.Spell the given digit using recursion.cpp ├── 17.Permutation Print all the possible permutation of a string.cpp ├── 18.Count the number of path possible from start point to end point in gameboard.cpp ├── 19.Count the number of path possible in a maze.cpp ├── 2.Calculate n raised to power of p.cpp ├── 20.SortAStackUsingRecurssion ├── 20.SortAStackUsingRecurssion.cpp ├── 3.Find the factorial of a number n!.cpp ├── 4.FIbonacci of nth number.cpp ├── 5.Sorted array.cpp ├── 6.print increasing and decreasing from 1 to n.cpp ├── 7. Find the firts and the last occurrence of an element.cpp ├── 8. Print the string in reverse order using recursion.cpp ├── 9.Replace all pie with 3.14.cpp ├── Count Digit using recursion.cpp ├── N-Queen.cpp ├── QuickSort.cpp ├── Sudoko_Solver.cpp └── Word_Search.cpp ├── N) Maths ├── 1. Find the number of digit in a number.cpp ├── 10. Prime Number.cpp ├── 11. Prime O(root n).cpp ├── 12.Prime No for large N O(n).cpp ├── 13. Prime Factor O(nxn logn).cpp ├── 14. Prime Factor O(root n).cpp ├── 15. Prime Factor O(root n) more effective.cpp ├── 16. All Divisor of a Number.cpp ├── 17. All Divisor of a Number O(root n).cpp ├── 18. Prime Number between a given number.cpp ├── 2. Palindrome Number.cpp ├── 20. Prime Number between a given n O(n log log n).cpp ├── 21. Computing Power 0(n).cpp ├── 22. Computing Power 0(log n).cpp ├── 23. Modular Exponentiation.cpp ├── 24. Maximum Swap ├── 25. Sqaures_Of_A_Sorted_Array.cpp ├── 26. Ugly_Number_2.cpp ├── 27.Fibonacci in logn.cpp ├── 3. Factorial of a Number.cpp ├── 4. Trailing Zeros in Factorial.cpp ├── 5. Trailing Zeros in Factorial over 20!.cpp ├── 6.GCD O(min(a,b)).cpp ├── 7. GCD using Euclidean Algo O(log(min a,b)).cpp ├── 8.GCD recursive soln.cpp ├── 9. LCM.cpp ├── 9.1. LCM using Logarithmic approach.cpp ├── Aramstrong number 2.cpp └── readme.md ├── O) Stack ├── BasicCalculator.cpp ├── Evaluating_Prefix_expression ├── Evaluating_postfix_expression ├── ImplementStackUsingQueue.cpp ├── NextGreaterEelementInArrayUsingStack.cpp ├── infix_to_postfix.cpp ├── infix_to_prefix ├── largestareainhistogram.cpp ├── minimum-add-valid.cpp ├── reverseAstack.cpp ├── stackImplementation.cpp └── valid_paranthesis.cpp ├── P) Binary Search ├── Aggresive_cows.cpp ├── Binary Search.cpp ├── BinarySearch.cpp ├── Floor_and_Ceil_in_Sorted_Array.cpp ├── SquareRootOfNumberUsingBinarySearch.cpp ├── koko-eating-bananas.cpp ├── minimum-in-rotated-sorted-array.cpp ├── peak-element-in-array.cpp ├── search-element-in-a-rotated-sorted-array.cpp ├── search-in-2D-matrix.cpp └── smallest_missing_element.cpp ├── Q) Linked List ├── CircularLinkedList.cpp ├── DoubleEndedLinkedList.cpp ├── DoubleEndedLinkedList.exe ├── Insert at Starts of Singly Linked List using C# ├── Insert at Starts of Singly Linked List using C++ ├── MergeKSortedLists.cpp ├── Readme.md ├── ReverseNodesInKGroup.cpp ├── Traversal of Linked List using C# ├── Traversal of Linked List using c++ ├── linked-list-reversal.mermaid └── linkedlist.cpp ├── R) Intervals ├── merge-intervals.cpp ├── nested-intervals-check.cpp └── summary-ranges.cpp ├── README.md ├── S) Tree ├── 1. Binary Tree implement.cpp ├── 2. Inorder Traversal.cpp ├── 3. Preorder Traversal.cpp ├── 4. Postorder Traversal.cpp ├── 5. Binary Tree Maximum Path Sum.cpp ├── 6.levelOrderTraversal.cpp ├── Binary Search Tree.cpp ├── CheckingBalancedBST ├── HeightOfABinaryTree.cpp ├── Level of Nodes │ ├── Level of Nodes.docx │ └── source_code.cpp ├── SerializeAndDeserializeBinaryTree.cpp ├── Zig-ZagTraversal.cpp ├── lowestCommonAncestor.cpp ├── successor_predecessor.cpp └── verticalTraversal.cpp ├── T) Deque ├── 239.Sliding Window Maximum.cpp ├── 641. Design Circular Deque.cpp └── Readme.md ├── U) Trie ├── .DS_Store └── Trie Implementation.cpp ├── V) Dynamic Programming ├── Best-time-to-buy-stock.cpp ├── Coin Change.cpp ├── Coin Change.exe ├── Distinct Subsequences.cpp ├── Longest Common Subsequesnce.cpp ├── Longest Common Subsequesnce.exe ├── LongestIncreasingSubsequence.cpp └── edit-distance.cpp └── W) Graph ├── breadth_first_search.cpp ├── depth_first_search.cpp ├── dijkstras.cpp ├── floyd_warshall.cpp ├── kahns_algo.cpp ├── number_of_islands.cpp └── surrounded_regions.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitMajee/DataStructureInCpp/4c2d971587b183bee22e5d007b32d590138440f8/.DS_Store -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "vector": "cpp", 4 | "iostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc.exe build active file", 6 | "command": "C:\\MinGW\\bin\\gcc.exe", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /A) Hello World/Addition of two numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int a,b,sum=0; 5 | cout<<"Enter two numbers="; 6 | cin>>a>>b; 7 | sum=a+b; 8 | cout<<"sum="<< sum; 9 | 10 | 11 | return 0; 12 | } 13 | 14 | /*int a,b,ssum=0; 15 | cout<<"enter two number="; 16 | cin>>a>>b; 17 | sum=a+b; 18 | cout<<"sum"< 2 | using namespace std; 3 | int main(){ 4 | int a; 5 | //a=12; 6 | cout<<"Enter any int="; 7 | cin>>a; 8 | cout<<"size of int="< 2 | using namespace std; 3 | int main(){ 4 | int a,b,mul=0; 5 | cout<<"Enter two numbers="; 6 | cin>>a>>b; 7 | mul=a*b; 8 | cout<<"Mul="< 2 | using namespace std; 3 | int main(){ 4 | int a,b,sub=0; 5 | cout<<"Enter two numbers="; 6 | cin>>a>>b; 7 | sub=a-b; 8 | cout<<"sub="<< sub; 9 | 10 | //cout<< sum; 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /A) Hello World/sum of two numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int amount1; 5 | cin>> amount1; 6 | int amount2; 7 | cin>>amount2; 8 | int sum =amount1+ amount2; 9 | cout<<"sum\n"<< sum; 10 | //cout<< sum; 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.1 find the greatest number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int a,b,c; 5 | cin>>a>>b>>c; 6 | 7 | if(a>c){ 8 | if(a>b){ 9 | cout<b){ 17 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | if (n%2==0){ 7 | cout<<"Even"< 2 | using namespace std; 3 | int main(){ 4 | int age; 5 | cin>>age; 6 | if(age>=18){ 7 | cout<<"You can vote."; 8 | } 9 | else{ 10 | cout<<"Not eligible for voting."; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.3 Eligible to vote or not.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int age; 5 | cin>>age; 6 | if(age>=18){ 7 | cout<<"You can vote."; 8 | } 9 | else{ 10 | cout<<"Not eligible for voting."; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.4 Find the greatest number using else if.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int x,y; 5 | cin>>x>>y; 6 | if(x==y){ 7 | cout<<"Both the number are equal"; 8 | } 9 | else if(x>y){ 10 | cout<<"X is greater than Y"; 11 | } 12 | else{ 13 | cout<<"Y is greater than X"; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.4 Find the greatest number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int x,y; 5 | cin>>x>>y; 6 | if(x==y){ 7 | cout<<"Both the number are equal"; 8 | } 9 | else if(x>y){ 10 | cout<<"X is greater than Y"; 11 | } 12 | else{ 13 | cout<<"Y is greater than X"; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.5 Find the greatest number usind Nested If.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int x,y; 5 | cin>>x>>y; 6 | if(x==y){ 7 | cout<<"Both the numbers are equal"; 8 | } 9 | else{ 10 | if(x>y){ 11 | cout<<"X is Greater than Y"; 12 | } 13 | else{ 14 | cout<<"Y is Greater than X"; 15 | } 16 | } 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.5 Find the greatest number usind Nested If.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitMajee/DataStructureInCpp/4c2d971587b183bee22e5d007b32d590138440f8/B) Ifelse Statement/2.5 Find the greatest number usind Nested If.exe -------------------------------------------------------------------------------- /B) Ifelse Statement/2.6 Find the Minimum And the Maximum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n1,n2,max,min; 5 | cin>>n1>>n2; 6 | //int max,min; 7 | if(n1>n2){ 8 | max=n1; 9 | min=n2; 10 | } 11 | else{ 12 | max=n2; 13 | min=n1; 14 | } 15 | cout<<"Max="<>sidea>>sideb>>sidec; 7 | if(sidea==sideb && sideb==sidec){ 8 | cout<<"This Triangle is an equilateral Triangle with three equal sides.\n"; 9 | } 10 | else if(sidea==sideb|| sidea==sidec|| sideb==sidec){ 11 | cout<<"This is an isosceles Triangle with two equal sides.\n"; 12 | } 13 | else{ 14 | cout<<"This is a Scalene Triangle with no equal sides.\n"; 15 | } 16 | 17 | return 0; 18 | } 19 | /* 20 | #include 21 | using namespace std; 22 | int main(){ 23 | 24 | return 0; 25 | } 26 | */ 27 | -------------------------------------------------------------------------------- /B) Ifelse Statement/2.8 Check If an Alphabet Is a vowel or a consonant.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | char c; 5 | int lvowel,uvowel; 6 | cout<<"Enter an Alphabet="; 7 | cin>>c; 8 | lvowel=(c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u'); 9 | uvowel=(c=='A'|| c=='E'|| c=='I'|| c=='O'|| c=='U'); 10 | if(lvowel||uvowel){ 11 | cout< 2 | using namespace std; 3 | int main(){ 4 | for(int i=1;i<=5;i++){ 5 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter a Number:"; 6 | cin>>n; 7 | int i; 8 | for(i=2;i<=n;i++){ 9 | if(n%i==0){ 10 | cout<<"Not a Prime"< 2 | using namespace std; 3 | int main(){ 4 | int a,b; 5 | cout<<"Enter 2 numbers:"; 6 | cin>>a>>b; 7 | for(int num=a;num<=b;num++){ 8 | int i; 9 | for(i=2; i 2 | using namespace std; 3 | int main(){ 4 | char button; 5 | cout<<"Enter a Character(a-f):"; 6 | cin>>button; 7 | switch(button){ 8 | case'a': 9 | cout<<"Hello"< 2 | using namespace std; 3 | int main(){ 4 | int n1,n2; 5 | char op; 6 | cout<<"Enter 1st number:"; 7 | cin>>n1; 8 | cout<<"Enter operator:"; 9 | cin>>op; 10 | cout<<"Enter 2nd number:"; 11 | cin>>n2; 12 | switch(op){ 13 | case'+': 14 | cout< 2 | using namespace std; 3 | int main(){ 4 | char c; 5 | cout<<"Enter a alphabet:"; 6 | cin>>c; 7 | switch(c){ 8 | case'a': 9 | cout<<"Its Vowel"< 2 | using namespace std; 3 | int main(){ 4 | int i=1; 5 | while(i<=5){ 6 | cout< 2 | using namespace std; 3 | int main(){ 4 | int i=1; 5 | 6 | do{ 7 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int sum=0; 7 | for(int counter=1; counter<=n;counter++){ 8 | sum=sum+counter; 9 | } 10 | cout< 15 | using namespace std; 16 | int main(){ 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C) Loops in C++/3.5 prigram to display the multiplication table upto 10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n,sum=0; 5 | cout<<"Enter a positive interger:"; 6 | cin>>n; 7 | for(int i=1; i<=10; ++i){ 8 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n,sum=0; 5 | cout<<"Enter a number:"; 6 | cin>>n; 7 | while(n>=0){ 8 | sum+=n; //sum=sum+n; both are same 9 | cout<<"Enter a number:"; 10 | cin>>n; 11 | } 12 | cout<<"\n The sum is"< 2 | using namespace std; 3 | int main(){ 4 | int i; 5 | for(i=1;i<=20;i++){ //Except the number divisible by 3 all others are printed so continue skippes the if() statement 6 | if(i%3==0){ 7 | continue; 8 | } 9 | cout< 2 | using namespace std; 3 | int main(){ 4 | int i; 5 | for(i=1;i<=20;i++){ 6 | if(i==11){ 7 | break; 8 | } 9 | cout< 2 | using namespace std; 3 | int main(){ 4 | int date, pocketmoney=3000; 5 | for(date=1;date<=30;date++){ 6 | if(date%2==0){ 7 | continue; 8 | } 9 | if(pocketmoney==0) 10 | break; 11 | } 12 | cout<<"Go out Today!"< 2 | using namespace std; 3 | int main(){ 4 | int n,i; 5 | cout<<"Enter a number:"; 6 | cin>>n; 7 | 8 | for(i=1;i<=n;i++){ 9 | if(i%2==0){ 10 | continue; 11 | } 12 | cout< 2 | using namespace std; 3 | int main(){ 4 | int row,columns; 5 | cout<<"Enter the numbers of rows:-\n"; 6 | cin>>row; 7 | cout<<"Enter the numbers of columns:-\n"; 8 | cin>>columns; 9 | for(int i=1;i<=row;i++){ 10 | for(int j=1;j<=columns;j++){ 11 | cout<<"*"; 12 | } 13 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | int number=1; 8 | for(int i=1; i<=n; i++){ 9 | for(int j=1; j<=i; j++){ 10 | cout< 2 | using namespace std; 3 | int main(){ 4 | int i,j,r; 5 | cout<<"Enter Size:-"; 6 | cin>>r; 7 | for(i=1;i<=r;i++){ 8 | for(j=1;j<=i;j++){ 9 | if((i+j)%2==0) 10 | cout<<" 1"; 11 | else 12 | cout<<" 0"; 13 | } 14 | cout< 2 | using namespace std; 3 | int main(){ 4 | int i,j,r; 5 | cout<<"Enter Size:- "; 6 | cin>>r; 7 | for(i=1; i<=r; i++){ 8 | int k=i; 9 | for(j=1; j<=(r-i);j++){ 10 | cout<<" "; 11 | } 12 | for(; j<=r;j++){ 13 | cout< 22 | using namespace std; 23 | 24 | 25 | bool isAnagram(string& s1, string& s2) { 26 | 27 | // Your code here 28 | unordered_map ump1; 29 | unordered_map ump2; 30 | 31 | for(auto c1 : s1) 32 | ump1[c1]++; 33 | for (auto c2 : s2 ) 34 | ump2[c2]++; 35 | if(ump1==ump2) 36 | return true; 37 | return false; 38 | 39 | } 40 | 41 | 42 | int main() 43 | { 44 | string s1, s2; 45 | cout<<"Enter two strings s1 and s2"; 46 | cin>>s1>>s2; 47 | bool ans = isAnagram (s1,s2); 48 | if(ans==true) 49 | cout<<"The entered two strings are anagrams"; 50 | else 51 | cout<<"The entered two strings are not anagrams"; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /D) pattern/4.2 Adv pattern/4.2.1 dimond using starts vs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int space=(2*n-1)/2; 8 | for(int i=1 ; i<=n ;i++){ 9 | for( int j=1; j<=space;j++){ 10 | 11 | cout<<" "; 12 | } 13 | for( int j=1; j<=2*j-1; j++){ 14 | cout<<"*"; 15 | } 16 | cout<=1;i--){ 21 | for(int j=1; j<=space;j++){ 22 | cout<<" "; 23 | } 24 | 25 | for(int j=1;j<=(2*i-1);j++){ 26 | cout<<"*"; 27 | } 28 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int space=(2*n-1)/2; 8 | for(int i=1 ; i<=n ;i++){ 9 | for( int j=1; j<=space;j++){ 10 | 11 | cout<<" "; 12 | } 13 | for( int j=1; j<=2*j-1; j++){ 14 | cout<<"*"; 15 | } 16 | cout<=1;i--){ 21 | for(int j=1; j<=space;j++){ 22 | cout<<" "; 23 | } 24 | 25 | for(int j=1;j<=(2*i-1);j++){ 26 | cout<<"*"; 27 | } 28 | cout< 2 | using namespace std; 3 | int main(){ int row,columns; 4 | cout<<"Enter the numbers of rows:-\n"; 5 | cin>>row; 6 | cout<<"Enter the numbers of columns:-\n"; 7 | cin>>columns; 8 | for(int i=1;i<=row;i++){ 9 | for(int j=1;j<=columns;j++){ 10 | if(i==1|| i==row ||j== 1|| j==columns){ 11 | 12 | cout<<"*"; 13 | } 14 | else{ 15 | cout<<" "; 16 | } 17 | } 18 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=i;j++){ 9 | cout<<"* "; 10 | } 11 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | for(int i=n;i>=1;i--){ 8 | for(int j=1;j<=i;j++){ 9 | cout<<"* "; 10 | } 11 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter Size :-"; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | if(j<=n-i){ 10 | 11 | cout<<" "; 12 | } 13 | else{ 14 | cout<<"* "; 15 | } 16 | } 17 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | for(int i=1; i<=n; ++i){ 8 | for(int j=1; j<=i; ++j){ 9 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | for(int i=n; i>=1; --i){ 8 | for(int j=1; j<=i; ++j){ 9 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | for(int i=1; i<=n; ++i){ 8 | for(int j=1; j<=i; ++j){ 9 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cout<<"Enter size:-"; 6 | cin>>n; 7 | for(int i=1; i<=n; ++i){ 8 | for(int j=1; j<=n-i+1; ++j){ 9 | cout< 3 | using namespace std; 4 | 5 | int nCr(int n, int r) { 6 | long long res = 1; 7 | 8 | // calculating nCr: 9 | for (int i = 0; i < r; i++) { 10 | res = res * (n - i); 11 | res = res / (i + 1); 12 | } 13 | return (int)(res); 14 | } 15 | 16 | vector> pascalTriangle(int n) { 17 | vector> ans; 18 | 19 | //Store the entire pascal's triangle: 20 | for (int row = 1; row <= n; row++) { 21 | vector tempLst; // temporary list 22 | for (int col = 1; col <= row; col++) { 23 | tempLst.push_back(nCr(row - 1, col - 1)); 24 | } 25 | ans.push_back(tempLst); 26 | } 27 | return ans; 28 | } 29 | 30 | int main() 31 | { 32 | int n = 5; 33 | vector> ans = pascalTriangle(n); 34 | for (auto it : ans) { 35 | for (auto ele : it) { 36 | cout << ele << " "; 37 | } 38 | cout << "n"; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /E) Funtion/6.1 write a program to add two numbers using funtion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int add(int num1,int num2){ 4 | int sum=num1+num2; 5 | return sum; 6 | 7 | } 8 | 9 | int main(){ 10 | int a,b; 11 | cin>>a>>b; 12 | cout< 2 | using namespace std; 3 | void swapped(int n1,int n2){ 4 | int temp; 5 | 6 | temp=n1; 7 | n1=n2; 8 | n2=temp; 9 | cout<<"The Swapped Value is"<>n1>>n2; 15 | swapped(n1,n2); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /E) Funtion/6.11 Voting Eligibility.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void voteEligibility(int age){ 4 | if(age>=18){ 5 | cout<<"You Can Vote"; 6 | } 7 | else{ 8 | cout<<"Not eligible For Voting"; 9 | } 10 | } 11 | 12 | 13 | 14 | int main(){ 15 | int age; 16 | cout<<"Enter your age"; 17 | cin>>age; 18 | voteEligibility(age); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /E) Funtion/6.2 prime numbers between 2 numbers using funtion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | bool prime(int num){ 10 | for(int i=2; i<=sqrt(num);i++){ 11 | if(num%i==0){ 12 | return false; 13 | } 14 | } 15 | return true; 16 | } 17 | int main(){ 18 | int a,b; 19 | cin>>a>>b; 20 | for( int i=a;i<=b;i++){ 21 | if(prime(i)){ 22 | cout< 2 | using namespace std; 3 | void fib(int n){ 4 | int t1=0,t2=1,nextTerm; 5 | for( int i=1; i<=n;i++){ 6 | cout<>n; 16 | fib(n); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /E) Funtion/6.4 factorial Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int fact(int n){ 4 | int factorial=1; 5 | for(int i=2; i<=n;i++){ 6 | factorial*=i; 7 | } 8 | return factorial; 9 | } 10 | 11 | int main(){ 12 | int n; 13 | cin>>n; 14 | int ans=fact(n); 15 | 16 | cout< 2 | using namespace std; 3 | int fact(int m){ 4 | int factorial=1; 5 | for(int i=2; i<=m;i++){ 6 | factorial*=i; 7 | } 8 | return factorial; 9 | } 10 | int main(){ 11 | int n,r; 12 | cin>>n>>r; 13 | int ans=fact(n)/(fact(r)*fact(n-r)); 14 | cout< 2 | #include 3 | using namespace std; 4 | int fact(int n){ 5 | int factorial=1; 6 | for(int i=2; i<=n;i++){ 7 | factorial*=i; 8 | } 9 | return factorial; 10 | } 11 | int main(){ 12 | int n; 13 | cin>>n; 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | int oddeven(int n){ 5 | if(n%2==0){ 6 | cout<<"Even"; 7 | } 8 | else{ 9 | cout<<"Odd"; 10 | } 11 | return 0; 12 | } 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cout< 2 | using namespace std; 3 | 4 | void alphabet(char c){ 5 | if(c>='A'&& c<='z'|| c>='a'&& c<='z') 6 | cout<<"Alphabet"; 7 | else 8 | cout<<"Invalid Input !"; 9 | 10 | } 11 | int main(){ 12 | char c; 13 | cout<<"Enter a Character:-"; 14 | cin>>c; 15 | alphabet(c); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /E) Funtion/6.9 Maximum Numbers from three given numbers using funtion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void max(int x,int y,int z){ 4 | if(x>y){ 5 | if(x>z) 6 | cout<<"X is Maximum\n"; 7 | else 8 | cout<<"Z is Maximum\n"; 9 | } 10 | else if(y>z){ 11 | cout<<"Y is Maximun\n"; 12 | } 13 | else{ 14 | cout<<"Z is Maximum\n"; 15 | } 16 | 17 | } 18 | void min(int x,int y,int z){ 19 | if(x>x>>y>>z; 37 | max(x,y,z); 38 | min(x,y,z); 39 | return 0 40 | ; 41 | } 42 | -------------------------------------------------------------------------------- /E) Funtion/funtion Placement Question/6.1.1 Sum of n nutral numbers VS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int sum(int n){ 4 | int ans=0; 5 | for(int i=0;i<=n;i++){ 6 | ans+=1; 7 | } 8 | return ans; 9 | } 10 | int sum(int n){ 11 | return(n*(n+1))/2; 12 | } 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cout< 2 | using namespace std; 3 | intsum(int n){ 4 | int ans=0; 5 | for(int i=0;i<=n;i++){ 6 | ans+=1; 7 | } 8 | return ans; 9 | } 10 | int sum(int n){ 11 | return(n*(n+1))/2; 12 | } 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cout< 2 | using namespace std; 3 | string DecimalToHexadecimal(int n){ 4 | int x=1; 5 | string ans=" "; 6 | while(x0){ 11 | int lastdigit=n/x; 12 | n-=lastdigit*x; 13 | x/=16; 14 | if(lastdigit<=9){ 15 | ans=ans+to_string(lastdigit); 16 | } 17 | else{ 18 | char c='A'+lastdigit-10; 19 | ans.push_back(c); 20 | } 21 | } 22 | return ans; 23 | } 24 | int main(){ 25 | int n; 26 | cin>>n; 27 | cout< 2 | using namespace std; 3 | 4 | bool check(int x,int y,int z){ 5 | int a=max(x,max(y,z)); 6 | int b,c; 7 | if(a==x){ 8 | b=y; 9 | c=2; 10 | } 11 | else if(a==y){ 12 | b=x; 13 | c=2; 14 | } 15 | else{ 16 | b=x; 17 | c=y; 18 | } 19 | if(a*a==b*b+c*c) 20 | return true; 21 | else 22 | return false; 23 | } 24 | 25 | 26 | 27 | int main(){ 28 | int x,y,z; 29 | cin>>x>>y>>z; 30 | if(check(x,y,z)){ 31 | cout<<"Pythagorean Triplet"; 32 | } 33 | else{ 34 | cout<<"Not a Pythagorean Triplet"; 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /E) Funtion/funtion Placement Question/6.1.3 BInaryToDecimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int BinaryToDecimal(int n){ 4 | int ans=0; 5 | int x=1; 6 | while(n>0){ 7 | int y=n%10; 8 | ans+=x*y; 9 | x*=2; 10 | n/=10; 11 | } 12 | return ans; 13 | } 14 | int main(){ 15 | int n; 16 | cin>>n; 17 | cout< 2 | using namespace std; 3 | int OctalToDecimal(int n){ 4 | int ans=0; 5 | int x=1; 6 | while(n>0){ 7 | int y=n%10; 8 | ans+=x*y; 9 | x*=8; 10 | n/=10; 11 | } 12 | return ans; 13 | } 14 | int main(){ 15 | int n; 16 | cin>>n; 17 | cout< 2 | #include 3 | using namespace std; 4 | int HexadecimalToDecimal(string n){ 5 | int ans=0; 6 | int x=1; 7 | int s=n.size(); 8 | for(int i=s-1; i>=0;i--){ 9 | if(n[i]>='0'&& n[i]<='9'){ 10 | ans+=x*(n[i]-'0'); 11 | } 12 | else if( n[i]>='A'&& n[i]<='F'){ 13 | ans+=x*(n[i]-'A'+10); 14 | } 15 | x*=16; 16 | } 17 | return ans; 18 | } 19 | int main(){ 20 | string n; 21 | cin>>n; 22 | cout< 2 | using namespace std; 3 | using namespace std; 4 | int DecimalToBinary(int n){ 5 | int x=1; 6 | int ans=0; 7 | while(x<=n){ 8 | x*=2; 9 | } 10 | x/=2; 11 | while(x>0){ 12 | int lastDigit=n/x; 13 | n-=lastDigit*x; 14 | x/=2; 15 | ans=ans*10+lastDigit; 16 | } 17 | return ans; 18 | } 19 | int main(){ 20 | int n; 21 | cin>>n; 22 | cout< 2 | using namespace std; 3 | int DecimalToOctal(int n){ 4 | int x=1; 5 | int ans=0; 6 | while(x<=n){ 7 | x*=8; 8 | } 9 | x/=8; 10 | while(x>0){ 11 | int lastDigit=n/x; 12 | n-=lastDigit*x; 13 | x/=8; 14 | ans=ans*10+lastDigit; 15 | } 16 | return ans; 17 | } 18 | int main(){ 19 | int n; 20 | cin>>n; 21 | cout< 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int search(int arr[],int n,int x){ 11 | for(int i=0;i Editor Option > Snippets >Default Source Code 32 | 33 | -------------------------------------------------------------------------------- /F) Array/10. Remove Duplicate soln 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | /********************************************** TC->O(n) SC-> O(1) ******************************************************/ 12 | 13 | int revDup(int arr[],int n){ 14 | int res=1; 15 | for(int i=1;i>n; 29 | int arr[n]; 30 | for(int i=0;i>arr[i]; 32 | } 33 | 34 | revDup(arr,n); 35 | 36 | for(int i=0;i Editor Option > Snippets >Default Source Code 50 | 51 | -------------------------------------------------------------------------------- /F) Array/11. Move Zero's to End.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | 12 | /********************************************** TC->O(n^2)******************************************************/ 13 | 14 | int moveToEnd(int arr[],int n){ 15 | for(int i=0;i>n; 29 | int arr[n]; 30 | for(int i=0;i>arr[i]; 32 | } 33 | 34 | moveToEnd(arr,n); 35 | 36 | for(int i=0;i Editor Option > Snippets >Default Source Code 50 | 51 | -------------------------------------------------------------------------------- /F) Array/12. Left Rotate an Array by one.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | /********************************************** TC->O(n) SC-> O(1)******************************************************/ 12 | 13 | int LRotateOne(int arr[],int n){ 14 | int temp=arr[0]; 15 | for(int i=1;i>n; 27 | int arr[n]; 28 | for(int i=0;i>arr[i]; 30 | } 31 | 32 | LRotateOne(arr,n); 33 | 34 | for(int i=0;i Editor Option > Snippets >Default Source Code 48 | 49 | -------------------------------------------------------------------------------- /F) Array/13. Left Rotate an Array by d place.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | /********************************************** TC->O(nd) SC-> O(1)******************************************************/ 12 | 13 | 14 | int LRotateOne(int arr[],int n){ 15 | int temp=arr[0]; 16 | for(int i=1;i>n>>d; 32 | int arr[n]; 33 | for(int i=0;i>arr[i]; 35 | } 36 | 37 | LRotateOne(arr,n,d); 38 | 39 | for(int i=0;i Editor Option > Snippets >Default Source Code 54 | 55 | -------------------------------------------------------------------------------- /F) Array/14. Left Rotate an Array by d place soln 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | /********************************************** TC->O(n) Auxl.S-> O(d)******************************************************/ 12 | 13 | 14 | int LRotateOne(int arr[],int n,int d){ 15 | int temp[d]; 16 | for(int i=0;i O(d) 17 | temp[i]=arr[i]; 18 | for(int i=d;i O(n-d) 19 | arr[i-d]=arr[i]; 20 | for(int i=0;iO(d) 21 | arr[n-d+i]=temp[i]; 22 | 23 | } 24 | int main() { 25 | ios::sync_with_stdio(false); 26 | cin.tie(0); 27 | int n,d; 28 | cin>>n>>d; 29 | int arr[n]; 30 | for(int i=0;i>arr[i]; 32 | } 33 | 34 | LRotateOne(arr,n,d); 35 | 36 | for(int i=0;i Editor Option > Snippets >Default Source Code 50 | 51 | -------------------------------------------------------------------------------- /F) Array/1480. Running Sum of 1d Array.cpp: -------------------------------------------------------------------------------- 1 | //Dev Wadhwa 2 | 3 | class Solution { 4 | public: 5 | vector runningSum(vector& nums) { 6 | int n=nums.size(),sum=0; 7 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int reverse(int arr[],int low,int high){ 12 | while(low>n>>d; 28 | int arr[n]; 29 | for(int i=0;i>arr[i]; 31 | } 32 | 33 | LRotateOne(arr,n,d); 34 | 35 | for(int i=0;i Editor Option > Snippets >Default Source Code 64 | 65 | -------------------------------------------------------------------------------- /F) Array/16. Leader in an Array O(n^2).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | /********************************************** TC->O(n^2)******************************************************/ 12 | 13 | 14 | int leader(int arr[],int n){ 15 | for(int i=0;i>n; 34 | int arr[n]; 35 | for(int i=0;i>arr[i]; 37 | } 38 | 39 | leader(arr,n); 40 | 41 | 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | //Goto tools > Editor Option > Snippets >Default Source Code 51 | 52 | -------------------------------------------------------------------------------- /F) Array/1672. Richest Customer Wealth.cpp: -------------------------------------------------------------------------------- 1 | //Saksham Dewan 2 | class Solution { 3 | public: 4 | int maximumWealth(vector>& accounts) { 5 | int max=0, m=accounts.size(), n=accounts[0].size(); 6 | vector wealth; 7 | for(int i=0;imax) max=sum; 15 | } 16 | return max; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /F) Array/17. Leader in an Array O(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int leader(int arr[],int n){ 12 | int curr_ldr=arr[n-1]; 13 | cout<0;i--){ 15 | if(curr_ldr>n; 27 | int arr[n]; 28 | for(int i=0;i>arr[i]; 30 | } 31 | 32 | leader(arr,n); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | //Goto tools > Editor Option > Snippets >Default Source Code 43 | 44 | -------------------------------------------------------------------------------- /F) Array/18. Maximum Difference O(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int maxDiff(int arr[],int n){ 12 | int res=arr[1]-arr[0], minVal=arr[0]; 13 | 14 | for(int j=1;j>n; 27 | int arr[n]; 28 | for(int i=0;i>arr[i]; 30 | } 31 | cout< Editor Option > Snippets >Default Source Code 41 | 42 | -------------------------------------------------------------------------------- /F) Array/19. Maximum Difference O(n^2).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int maxDiff(int arr[],int n){ 12 | int res=arr[1]-arr[0]; 13 | for(int i=0;i>n; 26 | int arr[n]; 27 | for(int i=0;i>arr[i]; 29 | } 30 | cout< Editor Option > Snippets >Default Source Code 39 | 40 | -------------------------------------------------------------------------------- /F) Array/2.Insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int insert(int arr[],int n,int x,int cap,int pos){ 11 | if(n==cap) 12 | return n; 13 | 14 | int idx=pos-1; 15 | for(int i=n-1;i>=idx;i--) 16 | arr[i+1]=arr[i]; 17 | 18 | arr[idx]=x; 19 | return(n+1); 20 | } 21 | int main() { 22 | ios::sync_with_stdio(false); 23 | cin.tie(0); 24 | int n; 25 | cin>>n; 26 | int arr[n]; 27 | for(int i=0;i>arr[i]; 29 | } 30 | int x,cap,pos; 31 | cin>>x>>pos; 32 | cout< Editor Option > Snippets >Default Source Code 43 | 44 | -------------------------------------------------------------------------------- /F) Array/20. Product of Array elements except itself.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector productExceptSelf(const vector& nums) { 6 | int n = nums.size(); 7 | vector result(n, 1); // Initialize the result array with 1 8 | 9 | // Step 1: Calculate left products 10 | int leftProduct = 1; 11 | for (int i = 0; i < n; ++i) { 12 | result[i] = leftProduct; 13 | leftProduct *= nums[i]; 14 | } 15 | 16 | // Step 2: Calculate right products and multiply with left products 17 | int rightProduct = 1; 18 | for (int i = n - 1; i >= 0; --i) { 19 | result[i] *= rightProduct; 20 | rightProduct *= nums[i]; 21 | } 22 | 23 | return result; 24 | } 25 | 26 | int main() { 27 | vector nums = {1, 2, 3, 4}; 28 | vector result = productExceptSelf(nums); 29 | 30 | // Output the result 31 | for (int value : result) { 32 | cout << value << " "; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /F) Array/21. Second Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findSecondLargest(int arr[], int n) { 5 | int largest = INT_MIN, secondLargest = INT_MIN; 6 | 7 | for (int i = 0; i < n; i++) { 8 | if (arr[i] > largest) { 9 | secondLargest = largest; 10 | largest = arr[i]; 11 | } else if (arr[i] > secondLargest && arr[i] != largest) { 12 | secondLargest = arr[i]; 13 | } 14 | } 15 | 16 | return secondLargest; 17 | } 18 | 19 | int main() { 20 | int arr[] = {12, 35, 1, 10, 34, 1}; 21 | int n = sizeof(arr) / sizeof(arr[0]); 22 | int secondLargest = findSecondLargest(arr, n); 23 | 24 | if (secondLargest == INT_MIN) { 25 | cout << "No second largest element exists." << endl; 26 | } else { 27 | cout << "Second largest element is: " << secondLargest << endl; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /F) Array/22.Count Distinct Element in Every K Size Window.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector countDistinctElements(vector &arr, int k) { 5 | vector ans; 6 | unordered_map mp; 7 | 8 | for (int i = 0; i < k; i++) { 9 | mp[arr[i]]++; 10 | } 11 | 12 | ans.push_back(mp.size()); 13 | 14 | for (int i = k; i < arr.size(); i++) { 15 | if (mp[arr[i - k]] == 1) { 16 | mp.erase(arr[i - k]); 17 | } else { 18 | mp[arr[i - k]]--; 19 | } 20 | 21 | mp[arr[i]]++; 22 | ans.push_back(mp.size()); 23 | } 24 | 25 | return ans; 26 | } 27 | 28 | int main() { 29 | vector arr = {1, 2, 1, 3, 4, 2, 3}; 30 | int k = 4; 31 | vector result = countDistinctElements(arr, k); 32 | 33 | for (int val : result) { 34 | cout << val << " "; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /F) Array/23. Maximum Consecutive Ones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longestSubSeg(vector &arr, int n, int k) { 5 | int i = 0, j = 0, zerocount = 0, ans = 0; 6 | 7 | while (j < n) { 8 | if (arr[j] == 0) { 9 | zerocount++; 10 | } 11 | 12 | while (zerocount > k) { 13 | if (arr[i] == 0) zerocount--; 14 | i++; 15 | } 16 | 17 | ans = max(ans, j - i + 1); 18 | j++; 19 | } 20 | return ans; 21 | } 22 | 23 | int main() { 24 | vector arr = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1}; 25 | int k = 2; 26 | int n = arr.size(); 27 | cout << "Length of longest subsegment with at most " << k << " zeros: " 28 | << longestSubSeg(arr, n, k) << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /F) Array/2D array/1.2D array declaration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n,m; 5 | cin>>n>>m; 6 | int arr[n][m]; 7 | for(int i=0;i>arr[i][j]; 10 | } 11 | } 12 | cout<<"matrixis:\n"; 13 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int n,m; 5 | cin>>n>>m; 6 | int arr[n][m]; 7 | for(int i=0;i>arr[i][j]; 10 | } 11 | } 12 | int x; 13 | cin>>x; 14 | bool flag=0; 15 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int n,m; 5 | cin>>n>>m; 6 | int a[n][m]; 7 | for(int i=0;i>a[i][j]; 10 | } 11 | } 12 | int row_start=0,row_end=n-1,column_start=0,column_end=m-1; 13 | while(row_start<=row_end && column_start<=column_end){ 14 | 15 | //for row_start 16 | for(int col=column_start;col<=column_end;col++) 17 | cout<=column_start;col--) 29 | cout<=row_start;row--) 35 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n=0,m=0; 5 | cin>>n>>m; 6 | int a[n][m]; 7 | for(int i=0;i>a[i][j]; 10 | int temp=a[i][j]; 11 | a[i][j]=a[j][i]; 12 | a[j][i]=temp; 13 | } 14 | } 15 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int n1,n2,n3; 5 | cin>>n1>>n2>>n3; 6 | int m1[n1][n2]; 7 | int m2[n2][n3]; 8 | for(int i=0;i>m1[i][j]; 11 | } 12 | for(int i=0;i>m2[i][j]; 15 | } 16 | int ans[n1][n3]; 17 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | bool searchMatrix(vector>& matrix, int target) { 6 | int m = matrix.size(); 7 | if (m == 0) return false; 8 | int n = matrix[0].size(); 9 | if (n == 0) return false; 10 | 11 | for (int i = 0; i < m; i++) { 12 | if (matrix[i][0] <= target && (i == m - 1 || target < matrix[i + 1][0])) { 13 | for (int j = 0; j < n; j++) { 14 | if (matrix[i][j] == target) return true; 15 | } 16 | } 17 | } 18 | return false; 19 | } 20 | 21 | int main() { 22 | int rows, cols; 23 | cout << "Enter number of rows and columns: "; 24 | cin >> rows >> cols; 25 | 26 | vector> matrix(rows, vector(cols)); 27 | cout << "Enter the elements of the matrix:" << endl; 28 | for (int i = 0; i < rows; i++) { 29 | for (int j = 0; j < cols; j++) { 30 | cin >> matrix[i][j]; 31 | } 32 | } 33 | 34 | int target; 35 | cout << "Enter the target value to search: "; 36 | cin >> target; 37 | 38 | bool found = searchMatrix(matrix, target); 39 | if (found) { 40 | cout << "Target found in the matrix." << endl; 41 | } else { 42 | cout << "Target not found in the matrix." << endl; 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /F) Array/3. Delete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int deleteEle(int arr[],int n,int x){ 11 | int i; 12 | for(i=0;i>n; 29 | int arr[n]; 30 | for(int i=0;i>arr[i]; 32 | } 33 | cin>>x; 34 | for(int i=0;i Editor Option > Snippets >Default Source Code 48 | 49 | -------------------------------------------------------------------------------- /F) Array/4. Largest ln Array O(n^2).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int getlargest(int arr[],int n){ 11 | for(int i=0;iarr[i]){ 15 | flag=false; 16 | break; 17 | } 18 | } 19 | if(flag==true) 20 | return i; 21 | } 22 | return -1; 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(false); 27 | cin.tie(0); 28 | int arr[]={5,6,3,2,3}; 29 | cout< Editor Option > Snippets >Default Source Code 41 | 42 | -------------------------------------------------------------------------------- /F) Array/5. Largest ln Array O(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int getLargest(int arr[],int n){ 11 | int res=0; 12 | for(int i=1;iarr[res]) 14 | res=i; 15 | 16 | return res; 17 | } 18 | int main() { 19 | ios::sync_with_stdio(false); 20 | cin.tie(0); 21 | int arr[]={5,6,3,2,3}; 22 | cout< Editor Option > Snippets >Default Source Code 33 | 34 | -------------------------------------------------------------------------------- /F) Array/6. Check Array Sorted or not.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | // TC O(n^2) 12 | 13 | bool isSorted(int arr[],int n){ 14 | for(int i=0;i Editor Option > Snippets >Default Source Code 37 | 38 | -------------------------------------------------------------------------------- /F) Array/7. Check Array Sorted or not O(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | bool isSorted(int arr[],int n){ 12 | for(int i=1;i Editor Option > Snippets >Default Source Code 34 | 35 | -------------------------------------------------------------------------------- /F) Array/8. Reverse an array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int rev(int arr[],int n){ 11 | int low=0, high=n-1; 12 | while(low>n; 27 | for(int i=0;i>arr[i]; 29 | } 30 | 31 | rev(arr,n); 32 | 33 | for(int i=0;i Editor Option > Snippets >Default Source Code 47 | 48 | -------------------------------------------------------------------------------- /F) Array/8.1 Find the min and the max term.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int arr[n]; 9 | for(int i=0;i>arr[i]; 11 | } 12 | int maxNO = INT_MIN; 13 | int minNO =INT_MAX; 14 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int n,sum=0; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0; i>arr[i]; 9 | } 10 | for(int i=0; i 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0;i>arr[i]; 9 | } 10 | int maxNO =0; 11 | 12 | for(int i=0;i 2 | /*Given an array a[] of size n. for every i from 0 to n-1 max(a[0],a[1].....a[i]) 3 | Example:- 1 0 5 4 6 8 4 | max(1 1 5 5 6 8) */ 5 | using namespace std; 6 | int main(){ 7 | int mx=-19999999; 8 | int n; 9 | cin>>n; 10 | int a[n]; 11 | for(int i=0;i>a[i]; 13 | } 14 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | /********************************************** TC->O(n) SC-> O(n) ******************************************************/ 12 | int revDup(int arr[],int n){ 13 | int temp[n]; 14 | temp[0]=arr[0]; 15 | int res=1; 16 | for(int i=1;i>n; 33 | int arr[n]; 34 | for(int i=0;i>arr[i]; 36 | } 37 | 38 | revDup(arr,n); 39 | 40 | for(int i=0;i Editor Option > Snippets >Default Source Code 54 | 55 | -------------------------------------------------------------------------------- /F) Array/Character Array/1.Declaration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | char arr[100]; 7 | cin>>arr; 8 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | char arr[n]; 9 | cin>>arr; 10 | bool check=1; 11 | for(int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | cin.ignore(); 9 | char arr[n+1]; 10 | cin.getline(arr,n); 11 | cin.ignore(); 12 | int i=0; 13 | int currlen=0,maxlen=0; 14 | int st=0,maxst=0; 15 | while(1){ 16 | if(arr[i]==' '||arr[i]=='\0'){ 17 | if(currlen>maxlen){ 18 | maxlen=currlen; 19 | maxst=st; 20 | } 21 | currlen=0; 22 | st=i+1; 23 | } 24 | else 25 | currlen++; 26 | if(arr[i]=='\0') 27 | break; 28 | i++; 29 | } 30 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | void helper(int ind, vector &arr, int target, vector &temp, 13 | vector> &ans){ 14 | // base case 15 | if(target==0){ 16 | ans.push_back(temp); 17 | return; 18 | } 19 | 20 | // recursive case 21 | for(int i=ind; iind and arr[i]==arr[i-1]) continue; 23 | if(arr[i]>target) break; 24 | temp.push_back(arr[i]); 25 | helper(i+1, arr, target-arr[i], temp, ans); 26 | temp.pop_back(); 27 | } 28 | return; 29 | } 30 | vector> CombinationSum2(vector arr,int n,int target) 31 | { 32 | //code here 33 | sort(arr.begin(), arr.end()); 34 | vector temp; 35 | vector> ans; 36 | helper(0, arr, target, temp, ans); 37 | return ans; 38 | } 39 | }; 40 | 41 | //{ Driver Code Starts. 42 | 43 | 44 | int main() 45 | { 46 | int t; 47 | cin>>t; 48 | while(t--) 49 | { 50 | int n,k; 51 | cin>>n>>k; 52 | vector arr(n); 53 | for(int i=0;i>arr[i]; 56 | } 57 | Solution ob; 58 | auto obj=ob.CombinationSum2(arr,n,k); 59 | for(int i=0;i 2 | using namespace std; 3 | int linearSearch(int arr[],int n,int key){ 4 | for(int i=0; i>n; 15 | int arr[n]; 16 | for(int i=0; i>arr[i]; 18 | } 19 | int key; 20 | cin>>key; 21 | cout< 2 | using namespace std; 3 | int BinarySearch(int arr[],int n,int key){ 4 | int s=0; 5 | int e=n; 6 | while(s<=e){ 7 | int mid=(s+e)/2; 8 | if(arr[mid]==key){ 9 | return mid; 10 | } 11 | else if(arr[mid]>key){ 12 | e=mid-1; 13 | } 14 | else{ 15 | s=mid+1; 16 | } 17 | } 18 | return -1; 19 | } 20 | 21 | int main(){ 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 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0; i>arr[i]; 9 | } 10 | int curr=0; 11 | for(int i=0; i 2 | using namespace std; 3 | int main(){ 4 | int n,s; 5 | cin>>n>>s; 6 | int a[n]; 7 | for(int i=0; i>a[i]; 9 | 10 | } 11 | int i=0,j=0,st=-1,en=-1,sum=0; 12 | while(js){ 23 | sum-=a[i]; 24 | i++; 25 | } 26 | if(sum==s){ 27 | st=i+1; 28 | en=j+1; 29 | break; 30 | } 31 | j++; 32 | } 33 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0; i>a[i]; 10 | 11 | } 12 | int N=1e6+2; 13 | bool check[N]; 14 | for(int i=0; i0){ 19 | check[a[i]]=1; 20 | } 21 | } 22 | int ans=-1; 23 | for(int i=1; i 2 | using namespace std; 3 | int main(){ 4 | 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0; i>a[i]; 10 | } 11 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0; i>a[i]; 10 | } 11 | int maxsum=INT_MIN; 12 | for(int i=0; i 2 | using namespace std; 3 | 4 | int LongestSubsetWithZeroSum(vector arr) { 5 | int n = arr.size(); 6 | int cnt = 0; 7 | int sum = 0; 8 | 9 | unordered_map map; 10 | map[0] = -1; 11 | 12 | for (int i = 0; i < n; i++) { 13 | sum += arr[i]; 14 | if (map.find(sum) != map.end()) { 15 | cnt = max(cnt, i - map[sum]); 16 | } else { 17 | map[sum] = i; 18 | } 19 | } 20 | 21 | return cnt; 22 | } 23 | 24 | int main() { 25 | vector arr = {6, 1, -1, 2, -1, 4, -5, -5}; 26 | cout << "Length of the longest subarray with zero sum: " 27 | << LongestSubsetWithZeroSum(arr) << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /F) Array/Subarray/Google kick start/1.Longest Arithmetic Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int a[n]; 7 | for(int i=0;i>a[i]; 9 | 10 | int ans=2; 11 | int d=a[1]-a[0]; 12 | int j=2; 13 | int curr=2; 14 | 15 | while(j 2 | #include 3 | using namespace std; 4 | void recordbreakers(){ 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 10 | } 11 | a[n]=-1; 12 | if(n==1){ 13 | cout<<"1"<mx && a[i]>a[i+1]) 20 | ans++; 21 | mx=max(mx,a[i]); 22 | } 23 | cout< 2 | #include 3 | using namespace std; 4 | void recordbreakers(){ 5 | int n; 6 | cin>>n; 7 | int a[n]; 8 | for(int i=0;i>a[i]; 10 | } 11 | a[n]=-1; 12 | if(n==1){ 13 | cout<<"1"<mx && a[i]>a[i+1]) 20 | ans++; 21 | mx=max(mx,a[i]); 22 | } 23 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int a[n]; 9 | for(int i=0;i>a[i]; 11 | } 12 | const int N=1e6+2; 13 | int idx(N); 14 | for(int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | int a[n]; 9 | for(int i=0;i>a[i]; 11 | } 12 | const int N=1e6+2; 13 | int idx[N]; 14 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | int arr[]={10,20,30,5,7}; 9 | sort(arr,arr+5); 10 | for(int x:arr) cout<()); 12 | cout< Editor Option > Snippets >Default Source Code 23 | 24 | -------------------------------------------------------------------------------- /G) Searching & Sorting/2. Sorting using Vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int main() { 12 | ios::sync_with_stdio(false); 13 | cin.tie(0); cout.tie(0); 14 | vector v={50,27,19,90,30}; 15 | sort(v.begin(),v.end()); 16 | for(int x:v){ 17 | cout<()); 21 | for(int x:v){ 22 | cout< Editor Option > Snippets >Default Source Code 34 | -------------------------------------------------------------------------------- /G) Searching & Sorting/3. Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n,temp; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0; i>arr[i]; 9 | } 10 | int counter=1; 11 | while(counterarr[i+1]){ 14 | int temp=arr[i]; 15 | arr[i]=arr[i+1]; 16 | arr[i+1]=temp; 17 | } 18 | } 19 | counter++; 20 | } 21 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int n,temp; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0; i>arr[i]; 9 | } 10 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int n,temp; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0; i>arr[i]; 9 | } 10 | for(int i=1;icurrent&&j>=0){ 14 | arr[j+1]=arr[j]; 15 | j--; 16 | } 17 | arr[j+1]=current; 18 | } 19 | for(int i=0; i 2 | using namespace std; 3 | 4 | void heapify(vector& arr, int n, int i) { 5 | int largest = i; 6 | int l = 2 * i + 1; 7 | int r = 2 * i + 2; 8 | if (l < n && arr[l] > arr[largest]) 9 | largest = l; 10 | if (r < n && arr[r] > arr[largest]) 11 | largest = r; 12 | if (largest != i) { 13 | swap(arr[i], arr[largest]); 14 | heapify(arr, n, largest); 15 | } 16 | } 17 | 18 | void heapSort(vector& arr) { 19 | int n = arr.size(); 20 | for (int i = n / 2 - 1; i >= 0; i--) 21 | heapify(arr, n, i); 22 | for (int i = n - 1; i > 0; i--) { 23 | swap(arr[0], arr[i]); 24 | heapify(arr, i, 0); 25 | } 26 | } 27 | 28 | void printArray(vector& arr) { 29 | for (int i = 0; i < arr.size(); ++i) 30 | cout << arr[i] << " "; 31 | cout << "\n"; 32 | } 33 | 34 | int main() { 35 | vector arr = { 9, 4, 3, 8, 10, 2, 5 }; 36 | heapSort(arr); 37 | cout << "Sorted array is \n"; 38 | printArray(arr); 39 | } 40 | -------------------------------------------------------------------------------- /G) Searching & Sorting/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int partition(vector& arr, int low, int high) { 5 | int pivot = arr[high]; 6 | int i = low - 1; 7 | for (int j = low; j <= high - 1; j++) { 8 | if (arr[j] < pivot) { 9 | i++; 10 | swap(arr[i], arr[j]); 11 | } 12 | } 13 | swap(arr[i + 1], arr[high]); 14 | return i + 1; 15 | } 16 | 17 | void quickSort(vector& arr, int low, int high) { 18 | if (low < high) { 19 | int pi = partition(arr, low, high); 20 | quickSort(arr, low, pi - 1); 21 | quickSort(arr, pi + 1, high); 22 | } 23 | } 24 | 25 | int main() { 26 | vector arr = {10, 7, 8, 9, 1, 5}; 27 | int n = arr.size(); 28 | quickSort(arr, 0, n - 1); 29 | for (int i = 0; i < n; i++) { 30 | cout << arr[i] << " "; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /G) Searching & Sorting/Searching/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Question 4 | 5 | Given a sorted array arr and an integer k, find the position(0-based indexing) at which k is present in the array using binary search. 6 | 7 | Constraints: 8 | 1 <= arr.size() <= 105 9 | 1 <= arr[i] <= 106 10 | 1 <= k <= 106 11 | 12 | Input: k = 4, arr= [1, 2, 3, 4, 5] 13 | Output: 3 14 | Explanation: 4 appears at index 3. 15 | 16 | 17 | Input: k = 445, arr= [11, 22, 33, 44, 55] 18 | Output: -1 19 | Explanation: 445 is not present. */ 20 | 21 | 22 | # include 23 | using namespace std; 24 | 25 | int binarysearch(vector &arr, int k) { 26 | // code here 27 | int low = 0; 28 | int high = arr.size()-1; 29 | while(low<=high){ 30 | int mid = (low+high)/2; 31 | if(arr[mid] == k) 32 | return mid; 33 | else if(arr[mid]>n; 47 | vector v; 48 | for(int i=0;i>num; 52 | v.push_back(num); 53 | } 54 | int element; 55 | cout<<"Enter the search element"; 56 | cin>>element; 57 | int ans = binarysearch(v,element); 58 | cout<<"The"< 4 | using namespace std; 5 | 6 | int linearSearch(vector& arr, int size, int target){ 7 | for(int i=0;i>size; 19 | 20 | vector arr; 21 | cout<<"Enter element of Array:"; 22 | for (int i=0;i> element; 25 | arr.push_back(element); 26 | } 27 | 28 | int target; 29 | cout<<"Enter the Target element:"; 30 | cin>>target; 31 | 32 | int result = linearSearch(arr, size, target); 33 | if(result != -1) { 34 | cout << "Target found at index: " << result << endl; 35 | } else { 36 | cout << "Target not found!" << endl; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /G) Searching & Sorting/Sort 0's and 1's in an Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to segregate 0s and 1s 5 | void segregate0and1(int arr[], int n) 6 | { 7 | int count = 0; 8 | 9 | for (int i = 0; i < n; i++) 10 | if (arr[i] == 0) 11 | count++; 12 | 13 | for (int i = 0; i < count; i++) 14 | arr[i] = 0; 15 | 16 | for (int i = count; i < n; i++) 17 | arr[i] = 1; 18 | } 19 | 20 | void print(int arr[], int n) 21 | { 22 | cout << "Array after segregation is "; 23 | 24 | for (int i = 0; i < n; i++) 25 | cout << arr[i] << " "; 26 | } 27 | 28 | int main() 29 | { 30 | int arr[] = { 0, 1, 0, 1, 1, 1 }; 31 | int n = sizeof(arr) / sizeof(arr[0]); 32 | 33 | segregate0and1(arr, n); 34 | print(arr, n); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /G) Searching & Sorting/mergesort.c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int A[], int l, int mid, int h) { 5 | int i = l, j = mid + 1, k = l; 6 | int B[100]; // Assuming the array size won't exceed 100 7 | 8 | while (i <= mid && j <= h) { 9 | if (A[i] < A[j]) 10 | B[k++] = A[i++]; 11 | else 12 | B[k++] = A[j++]; 13 | } 14 | 15 | for (; i <= mid; i++) 16 | B[k++] = A[i]; 17 | for (; j <= h; j++) 18 | B[k++] = A[j]; 19 | for (i = l; i <= h; i++) 20 | A[i] = B[i]; 21 | } 22 | 23 | void mergesort(int A[], int l, int h) { 24 | if (l < h) { 25 | int mid = (l + h) / 2; 26 | mergesort(A, l, mid); 27 | mergesort(A, mid + 1, h); 28 | merge(A, l, mid, h); 29 | } 30 | } 31 | 32 | int main() { 33 | int n; 34 | cout << "Enter the value of n: "; 35 | cin >> n; 36 | int a[n]; 37 | cout << "Enter " << n << " integers:" << endl; 38 | for (int i = 0; i < n; i++) { 39 | cin >> a[i]; 40 | } 41 | 42 | mergesort(a, 0, n - 1); 43 | 44 | cout << "Sorted array:" << endl; 45 | for (int i = 0; i < n; i++) { 46 | cout << a[i] << " "; 47 | } 48 | cout << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /H) Pointer/1.Declaration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int a=10; 5 | int *aptr=&a; 6 | cout<<*aptr< 2 | using namespace std; 3 | int main(){ 4 | int arr[]={10,20,30,40}; 5 | cout<<*arr< 2 | using namespace std; 3 | void increment(int b){ 4 | b++; 5 | } 6 | void incrementplus(int *a){ 7 | for(int i=0;i<*a;i++) 8 | cout< 2 | using namespace std; 3 | 4 | void swap(int *a,int *b){ 5 | int temp=*a; 6 | *a=*b; 7 | *b=temp; 8 | } 9 | int main(){ 10 | int a=2; 11 | int b=4; 12 | // int *aptr=&a,*bptr=&b; 13 | // swap(aptr,bptr); 14 | cout< 2 | using namespace std; 3 | int main(){ 4 | int a=10; 5 | int *p=new int();//allocating new memory space; 6 | *p=10; 7 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | cout<<"*****************"<>str; 15 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s="xfgt ADADDS"; 13 | string s1="ASDFF fhgfh"; 14 | //upper case 15 | /* for(int i=0; i='a'|| s[i]<='z') 18 | s[i]-=32; 19 | } 20 | //lower case 21 | for(int i=0; i='A'|| s[i]<='Z') 24 | s1[i]+=32; 25 | } 26 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s="88237830090"; 13 | sort(s.begin(),s.end(),greater()); 14 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s="sajakldgghgjftyfvbbjgfjhtukvjsl"; 13 | int a[26]; 14 | for(int i=0;i<26;i++){ 15 | a[i]=0; 16 | } 17 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s="asaasdddfff"; 13 | int n=s.size(); 14 | char ans[100]; 15 | ans[0]=s[0]; 16 | int cnt=1; 17 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | string alpha_numeric(string str) { 8 | string newstr; 9 | for (char ch : str) { 10 | if (isalnum(ch)) { 11 | newstr += tolower(ch); 12 | } 13 | } 14 | return newstr; 15 | } 16 | 17 | bool isPalindrome(string s) { 18 | string str = alpha_numeric(s); 19 | int n = str.size(); 20 | int low = 0; 21 | int high = n - 1; 22 | 23 | while (low < high) { 24 | if (str[low] != str[high]) { 25 | return false; 26 | } 27 | low++; 28 | high--; 29 | } 30 | return true; 31 | } 32 | 33 | int main() { 34 | string input; 35 | cout << "Enter a string: "; 36 | getline(cin, input); 37 | 38 | if (isPalindrome(input)) { 39 | cout << "The string is a palindrome." << endl; 40 | } else { 41 | cout << "The string is not a palindrome." << endl; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /I) String/2.Different types of string funtion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s1="abc"; 13 | string s2="xyz"; 14 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s1; 13 | cin>>s1; 14 | s1.clear(); 15 | if(s1.empty()){ 16 | cout<<"String is empty"; 17 | } 18 | return 0; 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /I) String/4.Erase Funtion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | 13 | string s3="nincompoop"; 14 | s3.erase(3,3); 15 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s1="nincompoop"; 13 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s1="nincompoop"; 13 | s1.insert(2,"lol"); 14 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s1="abcdefghijklmnopqrstuvwxyz"; 13 | string s2=s1.substr(6,19); 14 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){string s1="786"; 12 | int x=stoi(s1); 13 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int main(){ 12 | string s1="gdjasgjASHJuqi+sksa-k%#&2^5/*\\*/"; 13 | sort(s1.begin(),s1.end()); 14 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int getbit(int n,int pos){ 12 | return((n&(1<>pos(rightshift) 13 | } 14 | 15 | 16 | int main(){ 17 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | bool poweroftwo(int n){ 12 | return(n && !(n&n-1)); 13 | } 14 | int main(){ 15 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int NmofOne(int n){ 12 | int count=0; 13 | while(n){ 14 | n= n & (n-1); 15 | count++; 16 | } 17 | return count; 18 | } 19 | int main(){ 20 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | void subsets(int arr[],int n){ 12 | for(int i=0;i<(1< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include // XOR funtion 9 | 10 | using namespace std; 11 | int unique(int arr[],int n){ 12 | int xorsum=0; 13 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int setBit(int n,int pos){ 12 | return ((n & (1<>1; 26 | } 27 | int newxor=0; 28 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | bool getBit(int n, int pos){ 11 | return((n &(1< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int setbit(int n,int pos){ 12 | return(n | (1<>pos(rightshift) 13 | } 14 | 15 | 16 | int main(){ 17 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int clearbit(int n,int pos){ 12 | int mask= ~(1<>pos(rightshift) 14 | } 15 | 16 | 17 | int main(){ 18 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int updatebit(int n,int pos,int value){ 12 | int mask= ~(1<>pos(rightshift) 15 | } 16 | 17 | 18 | int main(){ 19 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | int togglebit(int n,int pos){ 12 | return (n xor(1< 3 | 4 | using namespace std; 5 | int main(){ 6 | int x=33; 7 | // Rigth Shift >> 8 | cout<<(x>>1)<<"\n"; 9 | cout<<(x>>2)<<"\n"; 10 | 11 | // Left Shift << 12 | cout<<(x<<1)<<"\n"; 13 | cout<<(x<<2)<<"\n"; 14 | 15 | 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /J) Bit Magic/7. And Or Xor Nor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main(){ 5 | int x=3,y=6; 6 | // AND of two Numbers & 7 | cout<<"AND ="<<(x&y)< 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int KthBitleftShift(int n,int k){ 11 | if(n & (1<<(k-1))!=0) cout<<"Yes"; 12 | else cout<<"No"; 13 | } 14 | int KthBitRightShift(int n,int k){ 15 | if(((n>>(k-1)) & 1)==1)cout<<"Yes"; 16 | else cout<<"No"; 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(false); 21 | cin.tie(0); 22 | int a,b; 23 | cin>>a>>b; 24 | cout< Editor Option > Snippets >Default Source Code 36 | 37 | -------------------------------------------------------------------------------- /J) Bit Magic/Maximum Product of Word Lengths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& words) { 4 | int n = words.size(); 5 | vector masks; 6 | 7 | for (auto word: words) { 8 | vector f(26); 9 | for (auto c: word) f[c-'a'] = 1; 10 | 11 | int mask = 0, prod = 1; 12 | for (int i=0; i<26; i++) { 13 | if (f[i]) mask += (prod*2); 14 | prod *= 2; 15 | } 16 | 17 | masks.push_back(mask); 18 | } 19 | 20 | int ans = 0; 21 | for (int i=0; i> 1; 10 | } 11 | return count; 12 | } 13 | }; -------------------------------------------------------------------------------- /K) Sives Of Eratosthenes/1. Prime Number in a given Range Using Sives.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void primeSives(int n){ 11 | int prime[100]={0}; 12 | for(int i=2;i<=n;i++){ 13 | if(prime[i]==0){ 14 | for(int j=i*i;j<=n;j+=i){ 15 | prime[j]=1; 16 | } 17 | } 18 | } 19 | for(int i=2;i<=n;i++){ 20 | if(prime[i]==0){ 21 | cout<>n; 29 | primeSives(n); 30 | return 0; 31 | return 0; 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /K) Sives Of Eratosthenes/2.Prime factorization using Sives.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void primefactor(int n){ 11 | int spf[100]={0}; 12 | 13 | for(int i=2;i<=n;i++){ 14 | spf[i]=1; 15 | } 16 | 17 | for(int i=2;i<=n;i++){ 18 | 19 | if(spf[i]==i){ 20 | for(int j=i*i;j<=n;j+=i){ 21 | if(spf[j]==j){ 22 | spf[j]=i; 23 | } 24 | } 25 | } 26 | } 27 | while(n!=1){ 28 | cout<>n; 35 | primefactor(n); 36 | return 0; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /L) Principle Of Inclusion and Exclusion/1.Find the Number of number in interval 1,n which are divisible by a or b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int divisible(int n,int a,int b){ 11 | int c1=n/a; 12 | int c2=n/b; 13 | int c3=n/(a*b); 14 | 15 | return c1+c2-c3; 16 | } 17 | int main(){ 18 | int n,a,b; 19 | cin>>n>>a>>b; 20 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int gcd(int a,int b){ 11 | while(b!=0){ 12 | int rem=a%b; 13 | a=b; 14 | b=rem; 15 | } 16 | return a; 17 | } 18 | int main(){ 19 | int a,b; 20 | cin>>a>>b; 21 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int sum(int n){ 11 | if(n==0){ 12 | return 0; 13 | } 14 | int prevSum=sum(n-1); 15 | return n+prevSum; 16 | } 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void towerofhanoi(int n,char src,char dest,char helper){ 11 | if(n==0){ 12 | return; 13 | } 14 | towerofhanoi(n-1,src,helper,dest); 15 | cout<<"Move From "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | string removeDup(string s){ 11 | if(s.length()==0){ 12 | return"";} 13 | char ch=s[0]; 14 | string ans=removeDup(s.substr(1)); 15 | if(ch==ans[0]){ 16 | return ans; 17 | } 18 | return(ch+ans); 19 | } 20 | 21 | int main(){ 22 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | string moveallx(string s){ 11 | if(s.length()==0){ 12 | return""; 13 | } 14 | char ch=s[0]; 15 | string ans=moveallx(s.substr(1)); 16 | if(ch=='x'){ 17 | return ans+ch; 18 | } 19 | return ch+ans; 20 | } 21 | int main(){ 22 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void subseq(string s,string ans=""){ 11 | if(s.length()==0){ 12 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void subseq(string s, string ans){ 11 | if(s.length()==0){ 12 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | string keypadArr[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 11 | void keypad(string s, string ans){ 12 | if(s.length()==0){ 13 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void printspell(int n,string str[]){ 11 | if(n==0) 12 | return; 13 | printspell(n/10,str); 14 | int number=n%10; 15 | cout<>n; 21 | string str[10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; 22 | printspell(n,str); 23 | return 0; 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /M) Recursion/17.Permutation Print all the possible permutation of a string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void permutation(string s,string ans){ 11 | if(s.length()==0){ 12 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int countpath(int s,int e){ 11 | if(s==e){ 12 | return 1; 13 | } 14 | if(s>e){ 15 | return 0; 16 | } 17 | int count=0; 18 | for(int i=1;i<=6;i++){ 19 | count+=countpath(s+i,e); 20 | } 21 | return count; 22 | } 23 | int main(){ 24 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | //Given a 2D maze find the all the path to complete the path where 2D maze=n*n matrix 9 | 10 | using namespace std; 11 | int countpathmaze(int n,int i,int j){ 12 | if(i==n-1 && j==n-1){ 13 | return 1; 14 | } 15 | if(i>=n|| j>=n){ 16 | return 0; 17 | } 18 | return countpathmaze(n,i+1,j)+countpathmaze(n,i,j+1); 19 | } 20 | int main(){ 21 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int power(int n,int p){ 11 | if(p==0){ 12 | return 1; 13 | } 14 | int prevpower=power(n,p-1); 15 | return n* prevpower; 16 | 17 | } 18 | int main(){ 19 | int n,p; 20 | cin>>n>>p; 21 | cout< 2 | 3 | using namespace std; 4 | 5 | void insert(stack& v, int temp) 6 | { 7 | if(v.empty() || v.top() >= temp) 8 | { 9 | v.push(temp); 10 | return; 11 | } 12 | int val = v.top(); 13 | v.pop(); 14 | insert(v,temp); 15 | v.push(val); 16 | } 17 | 18 | void sortS(stack& v) 19 | { 20 | if(v.empty()) 21 | { 22 | return; 23 | } 24 | int d = v.top(); 25 | v.pop(); 26 | sortS(v); 27 | insert(v,d); 28 | return; 29 | } 30 | 31 | int main() 32 | { 33 | stack v; 34 | v.push(15); 35 | v.push(31); 36 | v.push(4); 37 | v.push(3); 38 | v.push(1); 39 | v.push(31); 40 | v.push(21); 41 | 42 | sortS(v); 43 | 44 | while(!v.empty()) 45 | { 46 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int factorial(int n){ 11 | if(n==0){ 12 | return 1; 13 | } 14 | 15 | int prevfact=factorial(n-1); 16 | 17 | 18 | return n* prevfact; 19 | } 20 | int main(){ 21 | int n; 22 | cin>>n; 23 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int fib(long long n){ 11 | if(n==0 || n==1){ 12 | return n; 13 | } 14 | return fib(n-1)+fib(n-2); 15 | } 16 | int main(){ 17 | long long n; 18 | cin>>n; 19 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | bool sorted (int arr[],int n){ 11 | if(n==1){ 12 | return true; 13 | } 14 | bool restArray=sorted(arr+1,n-1); 15 | return (arr[0] 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void dec(int n){ 11 | if(n==1 || n==0){ 12 | 13 | cout<<"1"<>n; 34 | dec(n); 35 | inc(n); 36 | 37 | return 0; 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /M) Recursion/7. Find the firts and the last occurrence of an element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | int firstocc(int arr[],int n,int i,int key){ 11 | if(i==n){ 12 | return -1; 13 | } 14 | if(arr[i]==key){ 15 | return i; 16 | } 17 | return firstocc(arr,n,i+1,key); 18 | } 19 | int lastocc(int arr[],int n,int i,int key){ 20 | if(i==n){ 21 | return -1; 22 | } 23 | int restarray=lastocc(arr,n,i+1,key); 24 | if(restarray!=-1){ 25 | return restarray; 26 | } 27 | if(arr[i]==key){ 28 | return i; 29 | } 30 | return -1; 31 | } 32 | int main(){ 33 | int arr[]={4,2,3,2,4,5,2}; 34 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void reverse(string s){ 11 | if(s.length()==0){//base case 12 | return; 13 | } 14 | string ros=s.substr(1); 15 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | //Goto Tools > Editor Option > Snippets > Default Source Code 8 | 9 | using namespace std; 10 | void replacepi(string s){ 11 | if(s.length()==0){ 12 | return; 13 | } 14 | if( s[0]=='p' && s[1]=='i'){ 15 | cout<<"3.14"; 16 | replacepi(s.substr(2)); 17 | } 18 | else{ 19 | cout< 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int countDigit(long n){ 12 | if(n==0) 13 | return 0; 14 | return 1+countDigit(n/10); 15 | 16 | } 17 | int main() { 18 | ios::sync_with_stdio(false); 19 | cin.tie(0); 20 | long n; 21 | cin>>n; 22 | cout< Editor Option > Snippets >Default Source Code 32 | 33 | -------------------------------------------------------------------------------- /M) Recursion/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void quicksort(int *arr, int start, int end) { 6 | if (start >= end) { 7 | return; 8 | } 9 | 10 | int pivot = arr[start]; 11 | int left = start + 1; 12 | int right = end; 13 | 14 | while (left <= right) { 15 | while (left <= end && arr[left] <= pivot) { 16 | left++; 17 | } 18 | 19 | while (right > start && arr[right] >= pivot) { 20 | right--; 21 | } 22 | 23 | if (left < right) { 24 | swap(arr[left], arr[right]); 25 | } 26 | } 27 | 28 | 29 | swap(arr[start], arr[right]); 30 | 31 | quicksort(arr, start, right - 1); 32 | quicksort(arr, right + 1, end); 33 | } 34 | 35 | 36 | int main(){ 37 | int arr[] = {1,3,2,54,4}; 38 | int n=5; 39 | quicksort(arr,0,n); 40 | for(auto x:arr) cout< 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int countDigit(int n){ 11 | int count=0; 12 | while(n!=0){ 13 | n=n/10; 14 | ++count; 15 | } 16 | return count; 17 | } 18 | int main() { 19 | ios::sync_with_stdio(false); 20 | cin.tie(0); 21 | cout.tie(0); 22 | int n; 23 | cin>>n; 24 | cout< Editor Option > Snippets >Default Source Code 35 | 36 | -------------------------------------------------------------------------------- /N) Maths/10. Prime Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | bool isPrime(int n){ 11 | if(n==1) return false; 12 | 13 | for(int i=2;i>n; 24 | cout< Editor Option > Snippets >Default Source Code 34 | 35 | -------------------------------------------------------------------------------- /N) Maths/11. Prime O(root n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | bool isPrime(int n){ 12 | if(n==1) return false; 13 | 14 | for(int i=2;i*i<=n;i++){ 15 | if(n%i==0) 16 | return false; 17 | } 18 | return true; 19 | } 20 | int main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(0); 23 | int n; 24 | cin>>n; 25 | cout< Editor Option > Snippets >Default Source Code 36 | 37 | -------------------------------------------------------------------------------- /N) Maths/12.Prime No for large N O(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | bool isPrime(int n){ 11 | if(n==1) return false; 12 | if(n==2|| n==3) return true; 13 | if(n%2==0|| n%3==0) return false; 14 | 15 | for(int i=5;i*i<=n;i+=6){ 16 | if(n%i==0 || n%(i+2)==0) 17 | return false; 18 | } 19 | return true; 20 | } 21 | 22 | int main() { 23 | ios::sync_with_stdio(false); 24 | cin.tie(0); 25 | int n; 26 | cin>>n; 27 | cout< Editor Option > Snippets >Default Source Code 37 | 38 | -------------------------------------------------------------------------------- /N) Maths/13. Prime Factor O(nxn logn).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | bool isPrime(int n){ 11 | if(n==1) return false; 12 | if(n==2|| n==3) return true; 13 | if(n%2==0|| n%3==0) return false; 14 | 15 | for(int i=5;i*i<=n;i+=6){ 16 | if(n%i==0 || n%(i+2)==0) 17 | return false; 18 | } 19 | return true; 20 | } 21 | int primeFactor(int n){ 22 | for(int i=2;i>n; 38 | return primeFactor(n); 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | //Goto tools > Editor Option > Snippets >Default Source Code 47 | 48 | -------------------------------------------------------------------------------- /N) Maths/14. Prime Factor O(root n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int printPrimeFactor(int n){ 12 | if(n<=1) return 0; 13 | for(int i=2;i*i<=n;i++){ 14 | while(n%i == 0){ 15 | cout<1) cout<>n; 28 | return printPrimeFactor(n); 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | //Goto tools > Editor Option > Snippets >Default Source Code 37 | 38 | -------------------------------------------------------------------------------- /N) Maths/15. Prime Factor O(root n) more effective.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int printPrimeFactor(int n){ 12 | if(n<=1) return 0; 13 | while(n%2 ==0){ 14 | cout<<"2 "; 15 | n=n/2; 16 | } 17 | while(n%3== 0){ 18 | cout<<"3 "; 19 | n=n/3; 20 | } 21 | 22 | for(int i=5;i*i<=n; i=i+6){ 23 | while(n%i==0){ 24 | cout<3) cout<>n; 40 | return printPrimeFactor(n); 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | //Goto tools > Editor Option > Snippets >Default Source Code 49 | 50 | -------------------------------------------------------------------------------- /N) Maths/16. All Divisor of a Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | // TC-> 0(n) 12 | 13 | int printDivisor(int n){ 14 | for(int i=1;i>n; 24 | cout< Editor Option > Snippets >Default Source Code 35 | 36 | -------------------------------------------------------------------------------- /N) Maths/17. All Divisor of a Number O(root n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | int printDivisor(int n){ 12 | for(int i=1;i*i<=n;i++) 13 | if(n%i ==0){ 14 | cout<>n; 24 | 25 | return printDivisor(n); 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | //Goto tools > Editor Option > Snippets >Default Source Code 34 | 35 | -------------------------------------------------------------------------------- /N) Maths/18. Prime Number between a given number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | bool isPrime(int n){ 11 | if(n==1) return false; 12 | 13 | for(int i=2;i*i<=n;i++){ 14 | if(n%i==0) 15 | return false; 16 | } 17 | return true; 18 | } 19 | int printPrime(int n){ 20 | for(int i=2;i<=n;i++){ 21 | if(isPrime(i)) 22 | cout<>n; 30 | printPrime(n); 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | //Goto tools > Editor Option > Snippets >Default Source Code 41 | 42 | -------------------------------------------------------------------------------- /N) Maths/2. Palindrome Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | 12 | bool isPal(int n){ 13 | int rev=0; 14 | int temp=n; 15 | while(temp!=0){ 16 | int ld=temp%10; 17 | rev=rev*10+ld; 18 | temp=temp/10; 19 | } 20 | return(rev==n); 21 | } 22 | int main() { 23 | ios::sync_with_stdio(false); 24 | cin.tie(0); 25 | int n; 26 | cin>>n; 27 | cout< Editor Option > Snippets >Default Source Code 38 | 39 | -------------------------------------------------------------------------------- /N) Maths/20. Prime Number between a given n O(n log log n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | bool isPrime(int n){ 12 | if(n==1) return false; 13 | 14 | for(int i=2;i*i<=n;i++){ 15 | if(n%i==0) 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | int sieve(int n){ 22 | vector isPrime(n+1,true); 23 | for(int i=2;i<=n;i++) 24 | if(isPrime[i]) { 25 | cout<>n; 35 | sieve(n); 36 | return 0; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | //Goto tools > Editor Option > Snippets >Default Source Code 46 | 47 | -------------------------------------------------------------------------------- /N) Maths/21. Computing Power 0(n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int power(int x,int y){ 11 | int res=1; 12 | for(int i=0;i>x>>n; 21 | cout< Editor Option > Snippets >Default Source Code 32 | 33 | -------------------------------------------------------------------------------- /N) Maths/22. Computing Power 0(log n).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | // tc O(log n) and Space Complexity is O(log n) 11 | int power(int x,int n){ 12 | if(n==0) return 1; 13 | int temp =power(x,n/2); 14 | 15 | temp=temp*temp; 16 | if(n%2 ==0) return temp; 17 | else return temp*x; 18 | } 19 | int main() { 20 | ios::sync_with_stdio(false); 21 | cin.tie(0); 22 | int a,b; 23 | cin>>a>>b; 24 | cout< Editor Option > Snippets >Default Source Code 35 | 36 | -------------------------------------------------------------------------------- /N) Maths/23. Modular Exponentiation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Modular Exponentiation 4 | Given three numbers x, y and p, compute (x^y)%p 5 | 6 | Constraints : 7 | 1 ≤ x, y, p ≤ 10^9 8 | 9 | Time Complexity : O(log(n)) 10 | Space Complexity : O(1) 11 | 12 | Practice Problem Link : https://practice.geeksforgeeks.org/problems/modular-exponentiation-for-large-numbers5537/1 13 | 14 | */ 15 | 16 | 17 | #include 18 | using namespace std; 19 | 20 | struct Exponentiation{ 21 | 22 | long long mod_expo(long long a, long long b, long long mod){ 23 | if (b == 0 || b == 1) return a; 24 | else if (b & 1){ 25 | long long ans = mod_expo(a, b / 2, mod) % mod; 26 | return (((1LL * ans * ans) % mod) * a) % mod; 27 | } 28 | else{ 29 | long long ans = mod_expo(a, b / 2, mod) % mod; 30 | return (1LL * ans * ans) % mod; 31 | } 32 | } 33 | }; 34 | 35 | int32_t main() 36 | { 37 | long long x,y,p; 38 | cin >> x >> y >> p; 39 | struct Exponentiation E; 40 | cout << E.mod_expo(x,y,p) << "\n"; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /N) Maths/24. Maximum Swap: -------------------------------------------------------------------------------- 1 | // LeetCode 670. Maximum Swap 2 | 3 | class Solution { 4 | public: 5 | int maximumSwap(int num) { 6 | // Convert the number to a string for easier digit manipulation 7 | string numStr = to_string(num); 8 | int n = numStr.size(); 9 | 10 | // Store the last occurrence of each digit (0-9) 11 | vector last(10, -1); 12 | for (int i = 0; i < n; ++i) { 13 | last[numStr[i] - '0'] = i; 14 | } 15 | 16 | // Traverse each digit from left to right 17 | for (int i = 0; i < n; ++i) { 18 | // Check if there's a larger digit appearing later 19 | for (int d = 9; d > numStr[i] - '0'; --d) { 20 | if (last[d] > i) { 21 | // Swap the current digit with the larger one 22 | swap(numStr[i], numStr[last[d]]); 23 | return stoi(numStr); // Convert back to integer and return 24 | } 25 | } 26 | } 27 | 28 | // If no swap was made, return the original number 29 | return num; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /N) Maths/26. Ugly_Number_2.cpp: -------------------------------------------------------------------------------- 1 | // Ugly Number 2 2 | 3 | // Given an integer n, return the nth unique Ugly Number. An Ugly Number is defined as having prime factors of only 2,3 and 5. 4 | 5 | // Input 6 | // n - a number from 1 to 1690 7 | 8 | // Output 9 | // Return the nth Ugly Number 10 | 11 | // Intuition 12 | // So a cool thing here is that an ugly number can be used to generate other ugly numbers 13 | // so if you have a ugly number, lets say 2, you can generate 3 other ugly numbers by 14 | // 2 * 2 = 4 15 | // 2 * 3 = 6 16 | // 2 * 5 = 10 17 | 18 | // so we basically keeep doing this for n times, each time we take the minimum value so far using a priority queue 19 | 20 | // then at the end we have the nth Ugly Number 21 | // To ensure taht duplicate values are not added, we also use a set and check if it has been added previously or not 22 | 23 | 24 | // Solution 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | using namespace std; 32 | 33 | class Solution { 34 | public: 35 | int nthUglyNumber(int n) { 36 | 37 | priority_queue,greater<>> pq; 38 | // minheap 39 | 40 | pq.push(1); 41 | long long curr = 1; 42 | unordered_set visit = {1}; 43 | 44 | for (int i = 0 ; i < n ; i++){ 45 | curr = pq.top(); 46 | pq.pop(); 47 | 48 | if (visit.find(2 * curr) == visit.end()){ 49 | visit.insert(2 * curr); 50 | pq.push(2 * curr); 51 | } 52 | if (visit.find(3 * curr) == visit.end()){ 53 | visit.insert(3 * curr); 54 | pq.push(3 * curr); 55 | } 56 | if (visit.find(5 * curr) == visit.end()){ 57 | visit.insert(5 * curr); 58 | pq.push(5 * curr); 59 | } 60 | } 61 | 62 | return (int)curr; 63 | } 64 | }; 65 | 66 | 67 | int main() 68 | { 69 | Solution sol; 70 | int n = 10; 71 | 72 | int result = sol.nthUglyNumber(n); 73 | 74 | cout << "Result : " << result < 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | #define int long long int 11 | int fact(int n){ 12 | int res=1; 13 | for(int i=2;i<=n;i++){ 14 | res=res*i; 15 | } 16 | return res; 17 | } 18 | signed main() { 19 | ios::sync_with_stdio(false); 20 | cin.tie(0); 21 | int n; 22 | cin>>n; 23 | cout< Editor Option > Snippets >Default Source Code 34 | 35 | -------------------------------------------------------------------------------- /N) Maths/4. Trailing Zeros in Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | #define int long long int 11 | int countZero(int n){ 12 | int fact=1; 13 | for(int i=2;i<=n;i++){ 14 | fact=fact*i; 15 | } 16 | int res=0; 17 | while(fact%10==0){ 18 | res++; 19 | fact=fact/10; 20 | } 21 | return res; 22 | } 23 | 24 | signed main() { 25 | ios::sync_with_stdio(false); 26 | cin.tie(0); 27 | int n; 28 | cin>>n; 29 | cout< Editor Option > Snippets >Default Source Code 40 | 41 | -------------------------------------------------------------------------------- /N) Maths/5. Trailing Zeros in Factorial over 20!.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | // TC 0(log n) for number greater than 20! 12 | 13 | int countZero(int n){ 14 | int res=0; 15 | 16 | for(int i=5;i<=n;i=i*5){ 17 | res=res+(n/i); 18 | } 19 | return res; 20 | } 21 | int main() { 22 | ios::sync_with_stdio(false); 23 | cin.tie(0); 24 | cout.tie(0); 25 | int n; 26 | cin>>n; 27 | cout< Editor Option > Snippets >Default Source Code 37 | 38 | -------------------------------------------------------------------------------- /N) Maths/6.GCD O(min(a,b)).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | // TC-> 0(min(a,b)) 12 | int gcd(int a,int b){ 13 | int res=min(a,b); 14 | while(res>0){ 15 | if(a%res==0 && b%res==0){ 16 | break; 17 | } 18 | res--; 19 | } 20 | return res; 21 | } 22 | int main() { 23 | ios::sync_with_stdio(false); 24 | cin.tie(0); 25 | int a,b; 26 | cin>>a>>b; 27 | cout< Editor Option > Snippets >Default Source Code 38 | 39 | -------------------------------------------------------------------------------- /N) Maths/7. GCD using Euclidean Algo O(log(min a,b)).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int gcd(int a,int b){ 11 | while(a!=b){ 12 | if(a>b) a=a-b; 13 | else b=b-a; 14 | } 15 | return a; 16 | } 17 | int main() { 18 | ios::sync_with_stdio(false); 19 | cin.tie(0); 20 | int a,b; 21 | cin>>a>>b; 22 | cout< Editor Option > Snippets >Default Source Code 33 | 34 | -------------------------------------------------------------------------------- /N) Maths/8.GCD recursive soln.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int gcd(int a,int b){ 11 | if(b==0) return a; 12 | else return gcd(b,a%b); 13 | } 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(0); 17 | int a,b; 18 | cin>>a>>b; 19 | cout< Editor Option > Snippets >Default Source Code 30 | 31 | -------------------------------------------------------------------------------- /N) Maths/9. LCM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | int lcm(int a,int b){ 11 | int res=max(a,b); 12 | while(true){ 13 | if(res%a==0 && res%b==0) return res; 14 | res++; 15 | } 16 | return res; 17 | } 18 | int main() { 19 | ios::sync_with_stdio(false); 20 | cin.tie(0); 21 | int a,b; 22 | cin>>a>>b; 23 | cout< Editor Option > Snippets >Default Source Code 34 | 35 | -------------------------------------------------------------------------------- /N) Maths/9.1. LCM using Logarithmic approach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | 11 | //TC -> 0(log(min(a,b))) 12 | 13 | int gcd(int a,int b){ // 0(log(min(a,b))) 14 | while(a!=b){ 15 | if(a>b) a=a-b; 16 | else b=b-a; 17 | } 18 | return a; 19 | } 20 | 21 | int lcm (int a,int b){ // 0(1) 22 | return (a*b)/gcd(a,b); 23 | } 24 | int main() { 25 | ios::sync_with_stdio(false); 26 | cin.tie(0); 27 | int a,b; 28 | cin>>a>>b; 29 | cout< Editor Option > Snippets >Default Source Code 40 | 41 | -------------------------------------------------------------------------------- /N) Maths/Aramstrong number 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int n,t,temp=0,digit=0,r,l=0,sum=0; 8 | 9 | cout<<"Enter a Number:-"; 10 | cin>>n; 11 | t=n; 12 | temp=n; 13 | 14 | while(n!=0){ 15 | digit=n%10; 16 | l=l+1; 17 | n=n/10; 18 | } 19 | while(t!=0){ 20 | r=t%10; 21 | sum=sum+pow(r,l); 22 | t=t/10; 23 | } 24 | if(sum==temp) 25 | cout<<"Aramstrong NUmber"; 26 | else 27 | cout<<"Not a Aramstrong Number"; 28 | return 0; 29 | } 30 | 31 | // 1634=(1^4+6^4+3^4+4^4) 32 | // 153,9474 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /N) Maths/readme.md: -------------------------------------------------------------------------------- 1 | ## TC-> 0(log n) 2 | -------------------------------------------------------------------------------- /O) Stack/Evaluating_Prefix_expression: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int evaluatePrefix(const string &expression) { 6 | stack s; 7 | for (int i = expression.length() - 1; i >= 0; i--) { 8 | char ch = expression[i]; 9 | if (ch >= '0' && ch <= '9') { 10 | s.push(ch - '0'); 11 | } else if (ch == ' ') { 12 | continue; 13 | } else { 14 | int operand1 = s.top(); s.pop(); 15 | int operand2 = s.top(); s.pop(); 16 | 17 | switch (ch) { 18 | case '+': s.push(operand1 + operand2); break; 19 | case '-': s.push(operand1 - operand2); break; 20 | case '*': s.push(operand1 * operand2); break; 21 | case '/': s.push(operand1 / operand2); break; 22 | default: 23 | cerr << "Invalid operator: " << ch << endl; 24 | return -1; 25 | } 26 | } 27 | } 28 | return s.top(); 29 | } 30 | 31 | int main() { 32 | string expression; 33 | cout << "Enter a prefix expression (e.g., -+53*82): "; 34 | getline(cin, expression); 35 | cout << "The result of the expression is: " << evaluatePrefix(expression) << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /O) Stack/Evaluating_postfix_expression: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int evaluatePostfix(const string &expression) { 6 | stack stack; 7 | for (char ch : expression) { 8 | if (ch >= '0' && ch <= '9') { 9 | stack.push(ch - '0'); 10 | } else if (ch == ' ') { 11 | continue; 12 | } else { 13 | int operand2 = stack.top(); stack.pop(); 14 | int operand1 = stack.top(); stack.pop(); 15 | 16 | switch (ch) { 17 | case '+': stack.push(operand1 + operand2); break; 18 | case '-': stack.push(operand1 - operand2); break; 19 | case '*': stack.push(operand1 * operand2); break; 20 | case '/': stack.push(operand1 / operand2); break; 21 | default: 22 | cerr << "Invalid operator: " << ch << endl; 23 | return -1; 24 | } 25 | } 26 | } 27 | return stack.top(); 28 | } 29 | 30 | int main() { 31 | string expression; 32 | cout << "Enter a postfix expression (e.g., 53+82-*): "; 33 | getline(cin, expression); 34 | int result = evaluatePostfix(expression); 35 | if (result != -1) { 36 | cout << "The result of the expression is: " << result << endl; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /O) Stack/ImplementStackUsingQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Problem Statement: 7 | // Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). 8 | 9 | // Implement the MyStack class: 10 | 11 | // void push(int x) Pushes element x to the top of the stack. 12 | // int pop() Removes the element on the top of the stack and returns it. 13 | // int top() Returns the element on the top of the stack. 14 | // boolean empty() Returns true if the stack is empty, false otherwise. 15 | // Notes: 16 | 17 | // You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid. 18 | // Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations. 19 | 20 | class Stack{ 21 | public: 22 | queue q; 23 | void push(int val){ 24 | q.push(val); 25 | } 26 | int pop(){ 27 | int i = q.size()-1; 28 | while (i!=1){ 29 | int temp = q.front(); 30 | q.push(temp); 31 | q.pop(); 32 | i--; 33 | } 34 | int temp = q.front(); 35 | q.pop(); 36 | return temp; 37 | } 38 | int top(){ 39 | return q.back(); 40 | } 41 | bool empty(){ 42 | return q.empty(); 43 | } 44 | }; 45 | 46 | int main(){ 47 | Stack* st = new Stack(); 48 | st->push(10); 49 | st->push(20); 50 | st->push(30); 51 | st->push(40); 52 | cout<pop()<top()< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int precedence(char op) { 7 | if (op == '+' || op == '-') { 8 | return 1; 9 | } 10 | if (op == '*' || op == '/') { 11 | return 2; 12 | } 13 | if (op == '^') { 14 | return 3; 15 | } 16 | return 0; 17 | } 18 | 19 | bool isOperand(char ch) { 20 | return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'); 21 | } 22 | 23 | string infixToPostfix(string infix) { 24 | stack s; 25 | string postfix = ""; 26 | 27 | for (int i = 0; i < infix.length(); i++) { 28 | char ch = infix[i]; 29 | if (isOperand(ch)) { 30 | postfix += ch; 31 | } 32 | else if (ch == '(') { 33 | s.push(ch); 34 | } 35 | else if (ch == ')') { 36 | while (!s.empty() && s.top() != '(') { 37 | postfix += s.top(); 38 | s.pop(); 39 | } 40 | s.pop(); 41 | } 42 | else { 43 | while (!s.empty() && precedence(s.top()) >= precedence(ch)) { 44 | postfix += s.top(); 45 | s.pop(); 46 | } 47 | s.push(ch); 48 | } 49 | } 50 | while (!s.empty()) { 51 | postfix += s.top(); 52 | s.pop(); 53 | } 54 | return postfix; 55 | } 56 | int main() { 57 | string infix; 58 | cout << "Enter an infix expression: "; 59 | cin >> infix; 60 | string postfix = infixToPostfix(infix); 61 | cout << "Postfix expression: " << postfix << endl; 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /O) Stack/largestareainhistogram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Function to calculate the largest rectangle area in a histogram 7 | int largestRectangleArea(vector& heights) { 8 | stack st; 9 | int maxArea = 0; 10 | int n = heights.size(); 11 | 12 | for (int i = 0; i < n; ++i) { 13 | // Pop elements from stack while the current bar is shorter than the stack's top bar 14 | while (!st.empty() && heights[i] < heights[st.top()]) { 15 | int h = heights[st.top()]; 16 | st.pop(); 17 | int width = st.empty() ? i : i - st.top() - 1; 18 | maxArea = max(maxArea, h * width); 19 | } 20 | st.push(i); 21 | } 22 | 23 | // Process remaining elements in the stack 24 | while (!st.empty()) { 25 | int h = heights[st.top()]; 26 | st.pop(); 27 | int width = st.empty() ? n : n - st.top() - 1; 28 | maxArea = max(maxArea, h * width); 29 | } 30 | 31 | return maxArea; 32 | } 33 | 34 | int main() { 35 | vector heights = {2, 1, 5, 6, 2, 3}; 36 | cout << "Largest Rectangle Area: " << largestRectangleArea(heights) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /O) Stack/reverseAstack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Q: Reverse a stack using recursion. 3 | Level : Medium 4 | Reference : https://www.geeksforgeeks.org/problems/reverse-a-stack/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=reverse-a-stack 5 | */ 6 | class Solution{ 7 | public: 8 | void Reverse(stack &St){ 9 | // Base case: if stack is empty, return 10 | if (St.empty()) { 11 | return; 12 | } 13 | 14 | // Remove the top element from the stack 15 | int num = St.top(); 16 | St.pop(); 17 | 18 | // Recursively call Reverse for the remaining elements 19 | Reverse(St); 20 | 21 | // Place the removed element at the bottom of the stack 22 | insertAtBottom(St, num); 23 | } 24 | 25 | private: 26 | // Helper function to place an element at the bottom of the stack 27 | void insertAtBottom(stack& St, int num) { 28 | // Base case: if stack is empty, push the element 29 | if (St.empty()) { 30 | St.push(num); 31 | return; 32 | } 33 | 34 | // Remove the top element 35 | int top = St.top(); 36 | St.pop(); 37 | 38 | // Recursively call insertAtBottom to reach the base 39 | insertAtBottom(St, num); 40 | 41 | // Push the top element back 42 | St.push(top); 43 | } 44 | }; -------------------------------------------------------------------------------- /O) Stack/valid_paranthesis.cpp: -------------------------------------------------------------------------------- 1 | // Valid paranthesis 2 | 3 | // Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 4 | 5 | // An input string is valid if: 6 | 7 | // 1. Open brackets must be closed by the same type of brackets. 8 | // 2. Open brackets must be closed in the correct order. 9 | // 3. Every close bracket has a corresponding open bracket of the same type. 10 | 11 | // Input 12 | // s - a string that only contains '(','[','{' and ')',']','}' 13 | 14 | // Output 15 | // Returns whether the string is valid or not 16 | 17 | // Intuition 18 | // we have a two cases when the string is invalid 19 | // 1. if when we encounter an opening bracket, the correspongind closing bracket is not at the top of the stack 20 | // 2. if after processing the whole string, there are elements still in the stack 21 | 22 | 23 | // Solution 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | class Solution { 32 | public: 33 | bool isValid(string temp) { 34 | stack s; 35 | unordered_map relation = {{')' , '('} , {']' , '['} , {'}' , '{'}}; 36 | 37 | for (auto value:temp){ 38 | // this is the case of opening brackets 39 | if (relation.find(value) == relation.end()){ 40 | s.push(value); 41 | } 42 | else{ 43 | char required = relation[value]; 44 | if (s.empty() || s.top() != required){ 45 | return false; 46 | } 47 | s.pop(); 48 | } 49 | } 50 | 51 | return (s.empty()); 52 | } 53 | }; 54 | 55 | int main() 56 | { 57 | Solution sol; 58 | string s = "()[]{}"; 59 | 60 | bool result = sol.isValid(s); 61 | cout << "Result : " << result << endl; 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /P) Binary Search/Binary Search.cpp: -------------------------------------------------------------------------------- 1 | //You are given an array b of size n and an integer d. 2 | //For each integer value vl in the range [-10,000, 10,000], the function check(vl, b, n, d) 3 | //calculates the total adjustment cost of transforming all elements in the array to the value vl. 4 | //The adjustment cost is defined as twice the sum of the absolute differences between each element of b and vl. 5 | 6 | //The function returns true if the total cost is less than or equal to d, and false otherwise. 7 | //Your task is to determine how many valid values of vl satisfy this condition. 8 | 9 | //Input: 10 | //The first line contains two integers n (the size of the array) and d (the maximum allowable cost). 11 | //The second line contains n integers, the elements of the array b. 12 | //Output: 13 | //Output a single integer, the count of valid values of vl in the range [-10,000, 10,000] such that the total cost of transforming the array to vl does not exceed d. 14 | 15 | 16 | 17 | #include 18 | using namespace std; 19 | 20 | const int MAX_N = 1000; 21 | 22 | int n, d; 23 | int b[MAX_N]; 24 | 25 | bool check(int vl, int b[], int n, int d) { 26 | int cost = 0; 27 | for (int i = 0; i < n; i++) { 28 | cost += std::abs(b[i] - vl); 29 | } 30 | 31 | cost *= 2; 32 | if (cost <= d) { 33 | return true; 34 | } 35 | return false; 36 | } 37 | 38 | int main() { 39 | int low = -1e4; 40 | int high = 1e4; 41 | int count = 0; 42 | 43 | int n, d; 44 | cin>>n>>d; 45 | 46 | for(int i=0;i>b[i]; 49 | 50 | } 51 | 52 | // Assuming you've set n, d, and filled the array b[n] somewhere before this point 53 | 54 | for (int i = low; i <= high; i++) { 55 | if (check(i, b, n, d)) { 56 | count++; 57 | } 58 | } 59 | 60 | cout << count << std::endl; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /P) Binary Search/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int binarySearch(vector& arr,int left,int right,int target){ 7 | while(left<=right){ 8 | int mid=left+(right-left)/2; 9 | if(arr[mid]==target){ 10 | return mid; 11 | } 12 | if(arr[mid]>n; 25 | vector arr(n); 26 | cout<<"Enter the elements of the array:\n"; 27 | for(int i=0;i>arr[i]; 29 | } 30 | sort(arr.begin(),arr.end()); 31 | cout<<"Enter the element to search: "; 32 | cin>>target; 33 | int result=binarySearch(arr,0,n-1,target); 34 | if(result!=-1){ 35 | cout<<"Element found at index: "< 2 | using namespace std; 3 | 4 | int findFloor(int arr[], int n, int x) { 5 | int low = 0, high = n - 1; 6 | int ans = -1; 7 | 8 | while (low <= high) { 9 | int mid = (low + high) / 2; 10 | // maybe an answer 11 | if (arr[mid] <= x) { 12 | ans = arr[mid]; 13 | low = mid + 1; 14 | } 15 | else { 16 | high = mid - 1; 17 | } 18 | } 19 | return ans; 20 | } 21 | 22 | int findCeil(int arr[], int n, int x) { 23 | int low = 0, high = n - 1; 24 | int ans = -1; 25 | 26 | while (low <= high) { 27 | int mid = (low + high) / 2; 28 | // maybe an answer 29 | if (arr[mid] >= x) { 30 | ans = arr[mid]; 31 | high = mid - 1; 32 | } 33 | else { 34 | low = mid + 1; 35 | } 36 | } 37 | return ans; 38 | } 39 | 40 | pair getFloorAndCeil(int arr[], int n, int x) { 41 | int f = findFloor(arr, n, x); 42 | int c = findCeil(arr, n, x); 43 | return make_pair(f, c); 44 | } 45 | 46 | int main() { 47 | int arr[] = {3, 4, 4, 7, 8, 10}; 48 | int n = 6, x = 5; 49 | pair ans = getFloorAndCeil(arr, n, x); 50 | cout << "The floor and ceil are: " << ans.first 51 | << " " << ans.second << endl; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /P) Binary Search/SquareRootOfNumberUsingBinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Problem Statement: 6 | // Given int x return the sqaure root of the integer as integer 7 | 8 | int SquareRoot(int x){ 9 | int ans = 0, l = 0, h = x; 10 | while (l<=h){ 11 | int mid = (h+l)/2; 12 | if (mid*mid<=x){ 13 | ans = mid; 14 | l = mid + 1; 15 | } 16 | else { 17 | h = mid - 1; 18 | } 19 | } 20 | return ans; 21 | } 22 | 23 | int main(){ 24 | cout< 2 | using namespace std; 3 | 4 | int findMin(vector& arr) { 5 | int low = 0, high = arr.size() - 1; 6 | int ans = INT_MAX; 7 | while (low <= high) { 8 | int mid = (low + high) / 2; 9 | if (arr[low] <= arr[high]) { 10 | ans = min(ans, arr[low]); 11 | break; 12 | } 13 | if (arr[low] <= arr[mid]) { 14 | ans = min(ans, arr[low]); 15 | low = mid + 1; 16 | } 17 | else { 18 | ans = min(ans, arr[mid]); 19 | high = mid - 1; 20 | } 21 | } 22 | return ans; 23 | } 24 | 25 | int main() 26 | { 27 | vector arr = {4, 5, 6, 7, 0, 1, 2, 3}; 28 | int ans = findMin(arr); 29 | cout << "The minimum element is: " << ans << "\n"; 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /P) Binary Search/peak-element-in-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findPeakElement(vector &arr) { 5 | int n = arr.size(); 6 | if (n == 1) return 0; 7 | if (arr[0] > arr[1]) return 0; 8 | if (arr[n - 1] > arr[n - 2]) return n - 1; 9 | 10 | int low = 1, high = n - 2; 11 | while (low <= high) { 12 | int mid = (low + high) / 2; 13 | if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1]) 14 | return mid; 15 | if (arr[mid] > arr[mid - 1]) low = mid + 1; 16 | else high = mid - 1; 17 | } 18 | return -1; 19 | } 20 | 21 | int main() 22 | { 23 | vector arr = {1, 2, 3, 4, 5, 6, 7, 8, 5, 1}; 24 | int ans = findPeakElement(arr); 25 | cout << "The peak is at index: " << ans << "\n"; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /P) Binary Search/search-element-in-a-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int search(vector& arr, int n, int k) { 6 | int low = 0, high = n - 1; 7 | while (low <= high) { 8 | int mid = (low + high) / 2; 9 | if (arr[mid] == k) return mid; 10 | if (arr[low] <= arr[mid]) { 11 | if (arr[low] <= k && k <= arr[mid]) { 12 | high = mid - 1; 13 | } 14 | else { 15 | low = mid + 1; 16 | } 17 | } 18 | else { 19 | if (arr[mid] <= k && k <= arr[high]) { 20 | low = mid + 1; 21 | } 22 | else { 23 | high = mid - 1; 24 | } 25 | } 26 | } 27 | return -1; 28 | } 29 | 30 | int main() 31 | { 32 | vector arr = {7, 8, 9, 1, 2, 3, 4, 5, 6}; 33 | int n = 9, k = 1; 34 | int ans = search(arr, n, k); 35 | if (ans == -1) 36 | cout << "Target is not present.\n"; 37 | else 38 | cout << "The index is: " << ans << "\n"; 39 | return 0; 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /P) Binary Search/smallest_missing_element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Function to find the smallest missing element using binary search 6 | int findSmallestMissingElement(const vector& arr, int start, int end) { 7 | if (start > end) { 8 | return start; 9 | } 10 | 11 | int mid = start + (end - start) / 2; 12 | 13 | // Check if the mid element is equal to its index 14 | if (arr[mid] == mid) { 15 | // Search in the right half 16 | return findSmallestMissingElement(arr, mid + 1, end); 17 | } else { 18 | // Search in the left half 19 | return findSmallestMissingElement(arr, start, mid - 1); 20 | } 21 | } 22 | 23 | int main() { 24 | vector arr = {0, 1, 2, 6, 9, 11, 15}; 25 | int n = arr.size(); 26 | 27 | int smallestMissing = findSmallestMissingElement(arr, 0, n - 1); 28 | cout << "The smallest missing element is: " << smallestMissing << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Q) Linked List/CircularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | // Implementation of Circular Linked List 2 | #include 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | 11 | Node(int d) 12 | { 13 | this->data = d; 14 | this->next = NULL; 15 | } 16 | }; 17 | void InsertNode(Node *&tail, int element, int d) 18 | { 19 | 20 | if (tail == NULL) 21 | { 22 | Node *newnode = new Node(d); 23 | tail = newnode; 24 | newnode->next = newnode; 25 | } 26 | else 27 | { 28 | Node *curr = tail; 29 | Node *newnode = new Node(d); 30 | while (curr->data != element) 31 | { 32 | curr = curr->next; 33 | } 34 | newnode->next = curr->next; 35 | curr->next = newnode; 36 | } 37 | } 38 | void print(Node *tail) 39 | { 40 | Node *temp = tail; 41 | if (temp == NULL) 42 | { 43 | cout << "List is Empty" << endl; 44 | return; 45 | } 46 | do 47 | { 48 | cout << tail->data << "->"; 49 | tail = tail->next; 50 | } while (tail != temp); 51 | cout << endl; 52 | } 53 | 54 | void Delete(Node *tail, int element) 55 | { 56 | Node *curr = tail->next; 57 | Node *prev = tail; 58 | while (curr->data != element) 59 | { 60 | prev = curr; 61 | curr = curr->next; 62 | } 63 | prev->next = curr->next; 64 | if (curr == prev) 65 | { 66 | tail = NULL; 67 | } 68 | if (tail == curr) 69 | { 70 | tail = prev; 71 | } 72 | delete (curr); 73 | } 74 | int main() 75 | { 76 | Node *tail = NULL; 77 | 78 | InsertNode(tail, 5, 3); 79 | print(tail); 80 | InsertNode(tail, 3, 5); 81 | print(tail); 82 | InsertNode(tail, 5, 7); 83 | print(tail); 84 | InsertNode(tail, 7, 9); 85 | print(tail); 86 | InsertNode(tail, 5, 6); 87 | print(tail); 88 | InsertNode(tail, 9, 10); 89 | print(tail); 90 | InsertNode(tail, 3, 4); 91 | print(tail); 92 | Delete(tail, 10); 93 | print(tail); 94 | Delete(tail, 6); 95 | print(tail); 96 | 97 | return 0; 98 | } -------------------------------------------------------------------------------- /Q) Linked List/DoubleEndedLinkedList.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitMajee/DataStructureInCpp/4c2d971587b183bee22e5d007b32d590138440f8/Q) Linked List/DoubleEndedLinkedList.exe -------------------------------------------------------------------------------- /Q) Linked List/Insert at Starts of Singly Linked List using C#: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Node 4 | { 5 | public int data; 6 | public Node next; 7 | public Node(int data) 8 | { 9 | this.data = data; 10 | next = null; 11 | } 12 | } 13 | 14 | class LinkedList 15 | { 16 | Node head; 17 | public void insertAtBegin(int data) 18 | { 19 | Node newNode = new Node(data); 20 | newNode.next = head; 21 | head = newNode; 22 | } 23 | 24 | public void printList() 25 | { 26 | Node temp = head; 27 | while (temp != null) 28 | { 29 | Console.Write(temp.data + " "); 30 | temp = temp.next; 31 | } 32 | } 33 | } 34 | 35 | class Program 36 | { 37 | public static void Main(string[] args) 38 | { 39 | LinkedList list = new LinkedList(); 40 | list.insertAtBegin(1); 41 | list.insertAtBegin(2); 42 | list.insertAtBegin(3); 43 | list.insertAtBegin(4); 44 | list.insertAtBegin(5); 45 | Console.Write("Linked List: "); 46 | list.printList(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Q) Linked List/Insert at Starts of Singly Linked List using C++: -------------------------------------------------------------------------------- 1 | I/P: 10->20->30->40 element=5 2 | O/P: 5->10->20->30->40 3 | 4 | I/P: Null element=50 5 | O/P: 50 6 | 7 | #include 8 | using namespace std; 9 | 10 | struct Node { 11 | int data; 12 | Node* next; 13 | }; 14 | 15 | Node* head = NULL; 16 | 17 | void insertAtBegin(int data) { 18 | Node* newNode = new Node(); 19 | newNode->data = data; 20 | newNode->next = head; 21 | head = newNode; 22 | } 23 | 24 | void printList() { 25 | Node* temp = head; 26 | while (temp != NULL) { 27 | cout << temp->data << " "; 28 | temp = temp->next; 29 | } 30 | } 31 | 32 | int main() { 33 | insertAtBegin(1); 34 | insertAtBegin(2); 35 | insertAtBegin(3); 36 | insertAtBegin(4); 37 | insertAtBegin(5); 38 | cout << "Linked List: "; 39 | printList(); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Q) Linked List/MergeKSortedLists.cpp: -------------------------------------------------------------------------------- 1 | // You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. 2 | // Merge all the linked-lists into one sorted linked-list and return it. 3 | 4 | // Example 1: 5 | // Input: lists = [[1,4,5],[1,3,4],[2,6]] 6 | // Output: [1,1,2,3,4,4,5,6] 7 | // Explanation: The linked-lists are: 8 | // [ 9 | // 1->4->5, 10 | // 1->3->4, 11 | // 2->6 12 | // ] 13 | // merging them into one sorted list: 14 | // 1->1->2->3->4->4->5->6 15 | // Example 2: 16 | // Input: lists = [] 17 | // Output: [] 18 | // Example 3: 19 | // Input: lists = [[]] 20 | // Output: [] 21 | 22 | struct compare { 23 | bool operator()(const ListNode* l, const ListNode* r) { 24 | return l->val > r->val; 25 | } 26 | }; 27 | ListNode *mergeKLists(vector &lists) { //priority_queue 28 | priority_queue, compare> q; 29 | for(auto l : lists) { 30 | if(l) q.push(l); 31 | } 32 | if(q.empty()) return NULL; 33 | 34 | ListNode* result = q.top(); 35 | q.pop(); 36 | if(result->next) q.push(result->next); 37 | ListNode* tail = result; 38 | while(!q.empty()) { 39 | tail->next = q.top(); 40 | q.pop(); 41 | tail = tail->next; 42 | if(tail->next) q.push(tail->next); 43 | } 44 | return result; 45 | } 46 | -------------------------------------------------------------------------------- /Q) Linked List/Readme.md: -------------------------------------------------------------------------------- 1 | # Linked List Reversal Visualization 2 | 3 | This README demonstrates the process of reversing a linked list using recursion. 4 | 5 | ## Visualization 6 | 7 | The following diagram illustrates the steps of the recursive linked list reversal: 8 | 9 | ```mermaid 10 | graph TD 11 | subgraph "Initial List" 12 | A1[1] --> B1[2] 13 | B1 --> C1[3] 14 | C1 --> D1[4] 15 | end 16 | subgraph "First Recursive Call" 17 | A2[1] --> B2[2] 18 | B2 --> C2[3] 19 | C2 --> D2[4] 20 | D2 -.-> C2 21 | end 22 | subgraph "Second Recursive Call" 23 | A3[1] --> B3[2] 24 | B3 --> C3[3] 25 | C3 --> D3[4] 26 | D3 --> C3 27 | C3 -.-> B3 28 | end 29 | subgraph "Third Recursive Call" 30 | A4[1] --> B4[2] 31 | B4 --> C4[3] 32 | C4 --> D4[4] 33 | D4 --> C4 34 | C4 --> B4 35 | B4 -.-> A4 36 | end 37 | subgraph "Final Reversed List" 38 | A5[1] --> B5[2] 39 | B5 --> C5[3] 40 | C5 --> D5[4] 41 | D5 --> C5 42 | C5 --> B5 43 | B5 --> A5 44 | end 45 | Initial --> First 46 | First --> Second 47 | Second --> Third 48 | Third --> Final 49 | ``` 50 | 51 | This diagram shows how the linked list is reversed step by step through recursive calls. 52 | 53 | ## Explanation 54 | 55 | The diagram illustrates the following steps: 56 | 57 | 1. Initial List: The original linked list (1 -> 2 -> 3 -> 4) 58 | 2. First Recursive Call: The last node (4) is identified as the new head 59 | 3. Second Recursive Call: Node 3 is connected to node 4 60 | 4. Third Recursive Call: Node 2 is connected to node 3 61 | 5. Final Reversed List: Node 1 is connected to node 2, completing the reversal 62 | 63 | For more details on the implementation, please refer to the code in this repository. 64 | -------------------------------------------------------------------------------- /Q) Linked List/ReverseNodesInKGroup.cpp: -------------------------------------------------------------------------------- 1 | // Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. 2 | // k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. 3 | // You may not alter the values in the list's nodes, only nodes themselves may be changed. 4 | 5 | // Example 1: 6 | // Input: head = [1,2,3,4,5], k = 2 7 | // Output: [2,1,4,3,5] 8 | // Example 2: 9 | // Input: head = [1,2,3,4,5], k = 3 10 | // Output: [3,2,1,4,5] 11 | 12 | class Solution { 13 | public: 14 | ListNode* reverseKGroup(ListNode* head, int k) { 15 | dummy -> next = head; 16 | int len = length(head); 17 | for (int i = 0; i < len / k; i++) { 18 | for (int j = 1; j < k; j++) { 19 | ListNode* temp = pre -> next; 20 | pre -> next = head -> next; 21 | head -> next = head -> next -> next; 22 | pre -> next -> next = temp; 23 | } 24 | pre = head; 25 | head = head -> next; 26 | } 27 | return dummy -> next; 28 | } 29 | private: 30 | ListNode *dummy = new ListNode(0), *pre = dummy; 31 | int length(ListNode* head) { 32 | int len = 0; 33 | while (head) { 34 | len++; 35 | head = head -> next; 36 | } 37 | return len; 38 | } 39 | }; 40 | 41 | /** 42 | * Definition for singly-linked list. 43 | * public class ListNode { 44 | * int val; 45 | * ListNode next; 46 | * ListNode() {} 47 | * ListNode(int val) { this.val = val; } 48 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 49 | * } 50 | */ 51 | -------------------------------------------------------------------------------- /Q) Linked List/Traversal of Linked List using C#: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Node 4 | { 5 | public int data; 6 | public Node next; 7 | public Node(int x) 8 | { 9 | data = x; 10 | next = null; 11 | } 12 | } 13 | 14 | class MainClass 15 | { 16 | public static void PrintList(Node head) 17 | { 18 | Node curr = head; 19 | while (curr != null) 20 | { 21 | Console.Write(curr.data + " "); 22 | curr = curr.next; 23 | } 24 | } 25 | 26 | public static void Main() 27 | { 28 | Node head = new Node(10); 29 | head.next = new Node(20); 30 | head.next.next = new Node(30); 31 | head.next.next.next = new Node(40); 32 | PrintList(head); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Q) Linked List/Traversal of Linked List using c++: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Q) Linked List/linked-list-reversal.mermaid: -------------------------------------------------------------------------------- 1 | graph TD 2 | subgraph "Initial List" 3 | A1[1] --> B1[2] 4 | B1 --> C1[3] 5 | C1 --> D1[4] 6 | end 7 | subgraph "First Recursive Call" 8 | A2[1] --> B2[2] 9 | B2 --> C2[3] 10 | C2 --> D2[4] 11 | D2 -.-> C2 12 | end 13 | subgraph "Second Recursive Call" 14 | A3[1] --> B3[2] 15 | B3 --> C3[3] 16 | C3 --> D3[4] 17 | D3 --> C3 18 | C3 -.-> B3 19 | end 20 | subgraph "Third Recursive Call" 21 | A4[1] --> B4[2] 22 | B4 --> C4[3] 23 | C4 --> D4[4] 24 | D4 --> C4 25 | C4 --> B4 26 | B4 -.-> A4 27 | end 28 | subgraph "Final Reversed List" 29 | A5[1] --> B5[2] 30 | B5 --> C5[3] 31 | C5 --> D5[4] 32 | D5 --> C5 33 | C5 --> B5 34 | B5 --> A5 35 | end 36 | Initial --> First 37 | First --> Second 38 | Second --> Third 39 | Third --> Final 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # DSA in CPP Beginner Friendly 3 | 4 | A Beginner Friendly repo to get started with DSA in CPP with questions asked in interviews and Online Assessments. 5 | 1. Contribute to all interviews questions you came up with solutions. 6 | 2. Write the questions and solutions in a single .cpp file 7 | 3. Write the file name as the question asked avoid using fanncy name 8 | e.g `Watermelon ,two sum,Way Too Long Words` 9 | 10 | ## Usage/Examples 11 | 12 | ```javascript 13 | //Question Details 14 | //Constraints 15 | //Input 16 | //Output 17 | 18 | #include <> //whats actually required avoid #include 19 | using namespace std; 20 | int main() 21 | { 22 | //code goes here 23 | return 0; 24 | } 25 | ``` 26 | 27 | 28 | ## Authors 29 | 30 | - [@AnkitMajee](https://www.github.com/AnkitMajee) 31 | 32 | 33 | ## Installation 34 | 35 | Fork and clone the repo or Drag and drop your code 36 | 37 | ```bash 38 | gh repo clone AnkitMajee/CPP 39 | ``` 40 | 41 | ## Awesome Contributors 42 | 43 |
44 |

Thank you for contributing to our repository


45 |

46 | 47 | 48 | 49 |

50 |
51 | -------------------------------------------------------------------------------- /S) Tree/1. Binary Tree implement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | // struct node 11 | // { 12 | // int data; 13 | // struct node *left, *right; 14 | // }; 15 | // 16 | // struct node *create() 17 | //{ 18 | // struct node *temp; 19 | // int data,choice; 20 | // temp = (struct node *)malloc(sizeof(struct node)); 21 | // printf("Press 0 to exit"); 22 | // printf("\nPress 1 for new node"); 23 | // printf("Enter your choice : "); 24 | // scanf("%d", &choice); 25 | // if(choice==0) 26 | //{ 27 | //return 0; 28 | //} 29 | //else 30 | //{ 31 | // printf("Enter the data:"); 32 | // scanf("%d", &data); 33 | // temp->data = data; 34 | // printf("Enter the left child of %d", data); 35 | // temp->left = create(); 36 | //printf("Enter the right child of %d", data); 37 | //temp->right = create(); 38 | //return temp; 39 | //} 40 | //} 41 | // 42 | // int main() 43 | // { 44 | // struct node *root; 45 | // root = create(); 46 | // } 47 | // 48 | struct Node{ 49 | int key; 50 | Node *left; 51 | Node *right; 52 | Node(int k){ 53 | key=k; 54 | left=right=NULL; 55 | } 56 | }; 57 | 58 | int main() { 59 | ios::sync_with_stdio(false); 60 | cin.tie(0); 61 | Node *root=new Node(10); 62 | root ->left= new Node(20); 63 | root ->right= new Node(30); 64 | root ->left-> left= new Node(40); 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | //Goto tools > Editor Option > Snippets >Default Source Code 75 | 76 | -------------------------------------------------------------------------------- /S) Tree/2. Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Define the node structure for the binary tree 6 | struct Node { 7 | int data; 8 | Node* left; 9 | Node* right; 10 | }; 11 | 12 | // Function to create a new node with given data 13 | Node* newNode(int data) { 14 | Node* node = new Node; 15 | node->data = data; 16 | node->left = NULL; 17 | node->right = NULL; 18 | return node; 19 | } 20 | 21 | // Function to insert a node in the binary tree 22 | Node* insert(Node* root, int data) { 23 | if (root == NULL) { 24 | return newNode(data); 25 | } else { 26 | if (data <= root->data) { 27 | //cout<data<left = insert(root->left, data); 29 | } else { 30 | root->right = insert(root->right, data); 31 | } 32 | return root; 33 | } 34 | } 35 | 36 | // Function to print the binary tree in inorder traversal 37 | void inorder(Node* root) { 38 | if (root != NULL) { 39 | inorder(root->left); 40 | cout << root->data << " "; 41 | inorder(root->right); 42 | } 43 | } 44 | 45 | int main() { 46 | // Create a new binary tree 47 | Node* root = NULL; 48 | root = insert(root, 5); 49 | root = insert(root, 3); 50 | root = insert(root, 7); 51 | root = insert(root, 1); 52 | root = insert(root, 4); 53 | root = insert(root, 6); 54 | root = insert(root, 8); 55 | 56 | // Print the binary tree in inorder traversal 57 | inorder(root); 58 | cout << endl; 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | //Goto tools > Editor Option > Snippets >Default Source Code 68 | 69 | -------------------------------------------------------------------------------- /S) Tree/3. Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Define the node structure for the binary tree 6 | struct Node { 7 | int data; 8 | Node* left; 9 | Node* right; 10 | }; 11 | 12 | // Function to create a new node with given data 13 | Node* newNode(int data) { 14 | Node* node = new Node; 15 | node->data = data; 16 | node->left = NULL; 17 | node->right = NULL; 18 | return node; 19 | } 20 | 21 | // Function to insert a node in the binary tree 22 | Node* insert(Node* root, int data) { 23 | if (root == NULL) { 24 | return newNode(data); 25 | } else { 26 | if (data <= root->data) { 27 | //cout<data<left = insert(root->left, data); 29 | } else { 30 | root->right = insert(root->right, data); 31 | } 32 | return root; 33 | } 34 | } 35 | 36 | // Function to print the binary tree in preorder traversal 37 | void preorder(Node* root) { 38 | if (root != NULL) { 39 | cout << root->data << " "; 40 | preorder(root->left); 41 | preorder(root->right); 42 | } 43 | } 44 | 45 | int main() { 46 | // Create a new binary tree 47 | Node* root = NULL; 48 | root = insert(root, 5); 49 | root = insert(root, 3); 50 | root = insert(root, 7); 51 | root = insert(root, 1); 52 | root = insert(root, 4); 53 | root = insert(root, 6); 54 | root = insert(root, 8); 55 | 56 | // Print the binary tree in preorder traversal 57 | preorder(root); 58 | cout << endl; 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | //Goto tools > Editor Option > Snippets >Default Source Code 68 | 69 | -------------------------------------------------------------------------------- /S) Tree/4. Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Define the node structure for the binary tree 6 | struct Node { 7 | int data; 8 | Node* left; 9 | Node* right; 10 | }; 11 | 12 | // Function to create a new node with given data 13 | Node* newNode(int data) { 14 | Node* node = new Node; 15 | node->data = data; 16 | node->left = NULL; 17 | node->right = NULL; 18 | return node; 19 | } 20 | 21 | // Function to insert a node in the binary tree 22 | Node* insert(Node* root, int data) { 23 | if (root == NULL) { 24 | return newNode(data); 25 | } else { 26 | if (data <= root->data) { 27 | //cout<data<left = insert(root->left, data); 29 | } else { 30 | root->right = insert(root->right, data); 31 | } 32 | return root; 33 | } 34 | } 35 | 36 | // Function to print the binary tree in postorder traversal 37 | void postorder(Node* root) { 38 | if (root != NULL) { 39 | postorder(root->left); 40 | postorder(root->right); 41 | cout << root->data << " "; 42 | } 43 | } 44 | 45 | int main() { 46 | // Create a new binary tree 47 | Node* root = NULL; 48 | root = insert(root, 5); 49 | root = insert(root, 3); 50 | root = insert(root, 7); 51 | root = insert(root, 1); 52 | root = insert(root, 4); 53 | root = insert(root, 6); 54 | root = insert(root, 8); 55 | 56 | // Print the binary tree in postorder traversal 57 | postorder(root); 58 | cout << endl; 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | //Goto tools > Editor Option > Snippets >Default Source Code 68 | 69 | -------------------------------------------------------------------------------- /S) Tree/6.levelOrderTraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Define the node structure for the binary tree 8 | class Node { 9 | public: 10 | int data; 11 | Node* left; 12 | Node* right; 13 | Node(int data){ 14 | this->data = data; 15 | this->left = NULL; 16 | this->right = NULL; 17 | } 18 | }; 19 | 20 | // Function to create a new node with given data 21 | Node* newNode(int data) { 22 | Node* node = new Node(data); 23 | return node; 24 | } 25 | 26 | // Function to insert a node in the binary tree 27 | Node* insert(Node* root, int data) { 28 | if (root == NULL) { 29 | return newNode(data); 30 | } else { 31 | if (data <= root->data) { 32 | root->left = insert(root->left, data); 33 | } else { 34 | root->right = insert(root->right, data); 35 | } 36 | return root; 37 | } 38 | } 39 | 40 | // Function to print the binary tree in postorder traversal 41 | void levelOrder(Node* root) { 42 | queue q; 43 | q.push(root); 44 | q.push(NULL); 45 | while (!q.empty()){ 46 | Node* front = q.front(); 47 | q.pop(); 48 | if (front == NULL){ 49 | if (!q.empty()){ 50 | cout<data<<" "; 56 | if (front->left!=NULL){ 57 | q.push(front->left); 58 | } 59 | if (front->right!=NULL){ 60 | q.push(front->right); 61 | } 62 | } 63 | } 64 | } 65 | 66 | int main() { 67 | // Create a new binary tree 68 | Node* root = NULL; 69 | root = insert(root, 5); 70 | root = insert(root, 3); 71 | root = insert(root, 7); 72 | root = insert(root, 1); 73 | root = insert(root, 4); 74 | root = insert(root, 6); 75 | root = insert(root, 8); 76 | 77 | // Print the binary tree in postorder traversal 78 | levelOrder(root); 79 | cout << endl; 80 | 81 | return 0; 82 | } 83 | 84 | -------------------------------------------------------------------------------- /S) Tree/HeightOfABinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Problem Statement: 6 | // Given the root of a binary tree, return its maximum depth. 7 | // A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 8 | 9 | class Node { 10 | public: 11 | int data; 12 | Node* left; 13 | Node* right; 14 | Node(int data){ 15 | this->data = data; 16 | this->left = NULL; 17 | this->right = NULL; 18 | } 19 | }; 20 | 21 | // Function to create a new node with given data 22 | Node* newNode(int data) { 23 | Node* node = new Node(data); 24 | return node; 25 | } 26 | 27 | // Function to insert a node in the binary tree 28 | Node* insert(Node* root, int data) { 29 | if (root == NULL) { 30 | return newNode(data); 31 | } else { 32 | if (data <= root->data) { 33 | root->left = insert(root->left, data); 34 | } else { 35 | root->right = insert(root->right, data); 36 | } 37 | return root; 38 | } 39 | } 40 | 41 | int maxDepth(Node* root){ 42 | if (root == NULL) return 0; 43 | int leftHeight = maxDepth(root->left); 44 | int rightHeight = maxDepth(root->right); 45 | return max(leftHeight, rightHeight)+1; 46 | } 47 | 48 | int main(){ 49 | // Create a new binary tree 50 | Node* root = NULL; 51 | root = insert(root, 5); 52 | root = insert(root, 3); 53 | root = insert(root, 7); 54 | root = insert(root, 1); 55 | root = insert(root, 4); 56 | root = insert(root, 6); 57 | root = insert(root, 8); 58 | 59 | cout< adj[], int X) 6 | { 7 | // code here 8 | vector vis(V+1, false); 9 | int level = 0; 10 | queue q; 11 | q.push(0); 12 | vis[q.front()] = true; 13 | while(q.size()){ 14 | ++level; 15 | int count = q.size(); 16 | while(count--){ 17 | int front = q.front(); 18 | q.pop(); 19 | for(auto &i : adj[front]){ 20 | if(!vis[i]){ 21 | vis[i] = true; 22 | if(i == X) return level; 23 | q.push(i); 24 | } 25 | } 26 | } 27 | } 28 | return -1; 29 | } 30 | }; -------------------------------------------------------------------------------- /S) Tree/lowestCommonAncestor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | Node* left; 7 | Node* right; 8 | 9 | Node(int val) : data(val), left(nullptr), right(nullptr) {} 10 | }; 11 | 12 | Node* insert(Node* root, int data) { 13 | if (root == nullptr) 14 | return new Node(data); 15 | if (data < root->data) 16 | root->left = insert(root->left, data); 17 | else if (data > root->data) 18 | root->right = insert(root->right, data); 19 | return root; 20 | } 21 | 22 | Node* findLCA(Node* root, int n1, int n2) { 23 | while (root != nullptr) { 24 | if (root->data > n1 && root->data > n2) 25 | root = root->left; 26 | else if (root->data < n1 && root->data < n2) 27 | root = root->right; 28 | else 29 | return root; 30 | } 31 | return nullptr; 32 | } 33 | 34 | int main() { 35 | Node* root = nullptr; 36 | int n, data, n1, n2; 37 | 38 | cout << "Enter number of nodes in the BST: "; 39 | cin >> n; 40 | cout << "Enter node values:\n"; 41 | for (int i = 0; i < n; ++i) { 42 | cin >> data; 43 | root = insert(root, data); 44 | } 45 | 46 | cout << "Enter two node values to find their LCA: "; 47 | cin >> n1 >> n2; 48 | Node* lca = findLCA(root, n1, n2); 49 | 50 | if (lca != nullptr) 51 | cout << "LCA of " << n1 << " and " << n2 << ": " << lca->data << endl; 52 | else 53 | cout << "LCA not found" << endl; 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /T) Deque/239.Sliding Window Maximum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | vector maxSlidingWindow(vector& nums, int k) { 10 | deque dq; // Deque to store indices of useful elements 11 | vector result; // Resultant vector to store maximums of each window 12 | 13 | for (int i = 0; i < nums.size(); i++) { 14 | // Remove indices that are out of the current window 15 | if (!dq.empty() && dq.front() == i - k) { 16 | dq.pop_front(); // Remove from front if it's out of the window 17 | } 18 | 19 | // Remove elements from the back of the deque that are less than the current element 20 | while (!dq.empty() && nums[dq.back()] < nums[i]) { 21 | dq.pop_back(); // Maintain decreasing order in deque 22 | } 23 | 24 | // Add the current element's index to the deque 25 | dq.push_back(i); 26 | 27 | // Start adding results to the output once the first window is completed 28 | if (i >= k - 1) { 29 | result.push_back(nums[dq.front()]); // The front of the deque is the maximum for the window 30 | } 31 | } 32 | 33 | return result; // Return the results containing the maximums of each sliding window 34 | } 35 | }; 36 | 37 | // Driver code to test the function 38 | int main() { 39 | Solution sol; 40 | vector nums = {1, 3, -1, -3, 5, 3, 6, 7}; // Example input 41 | int k = 3; // Size of the sliding window 42 | 43 | vector result = sol.maxSlidingWindow(nums, k); 44 | 45 | // Output the maximum values of each sliding window 46 | cout << "Sliding Window Maximum: "; 47 | for (int i : result) { 48 | cout << i << " "; 49 | } 50 | cout << endl; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /T) Deque/Readme.md: -------------------------------------------------------------------------------- 1 | # Deque Implementation in C++ 2 | 3 | ### Overview 4 | 5 | A **deque** (double-ended queue) is a dynamic data structure similar to arrays, but with added flexibility to insert and remove elements from both the front and the back in amortized constant time. This implementation avoids using the C++ STL, ensuring a hands-on approach to understanding the internal workings of a deque. 6 | 7 | ### Features 8 | 9 | - **Dynamic Resizing**: The deque dynamically resizes itself to accommodate more elements as needed. 10 | - **Efficient Operations**: 11 | - Insertion and deletion at both ends in **O(1)** amortized time. 12 | - Access to elements via index in **O(1)** time. 13 | - Middle insertion and deletion in **O(n)** time. 14 | 15 | ### Supported Operations 16 | 17 | 1. `deque()` – Initializes an empty deque. 18 | 2. `deque(n, x)` – Initializes a deque of size `n` with all values set to `x`. 19 | 3. `push_back(x)` – Appends the value `x` to the back of the deque. 20 | 4. `pop_back()` – Removes the value at the back of the deque. 21 | 5. `push_front(x)` – Appends the value `x` to the front of the deque. 22 | 6. `pop_front()` – Removes the value at the front of the deque. 23 | 7. `front()` – Returns the value at the front of the deque. 24 | 8. `back()` – Returns the value at the back of the deque. 25 | 9. `empty()` – Returns `true` if the deque is empty, otherwise `false`. 26 | 10. `size()` – Returns the current size of the deque. 27 | 11. `resize(x, d)` – Resizes the deque to `x` elements, filling new spaces with `d` if expanded. 28 | 12. `clear()` – Clears all elements from the deque. 29 | 13. `D[n]` – Returns the nth element of the deque using the bracket operator `[]`. 30 | 31 | ### Example Usage 32 | 33 | ```cpp 34 | Deque dq; 35 | dq.push_back(10); 36 | dq.push_front(20); 37 | dq.push_back(30); 38 | 39 | cout << "Front: " << dq.front() << endl; // Output: 20 40 | cout << "Back: " << dq.back() << endl; // Output: 30 41 | 42 | dq.pop_front(); 43 | cout << "After pop_front, Front: " << dq.front() << endl; // Output: 10 44 | 45 | dq.pop_back(); 46 | cout << "After pop_back, Back: " << dq.back() << endl; // Output: 10 47 | -------------------------------------------------------------------------------- /U) Trie/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitMajee/DataStructureInCpp/4c2d971587b183bee22e5d007b32d590138440f8/U) Trie/.DS_Store -------------------------------------------------------------------------------- /V) Dynamic Programming/Best-time-to-buy-stock.cpp: -------------------------------------------------------------------------------- 1 | // Problem link:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int maxProfit(vector&prices){ 9 | int buy=prices[0]; 10 | int profit=0; 11 | for(int i=1;i 13 | 14 | using namespace std; 15 | 16 | // Normal overlapping Recursion 17 | int getCount(vectorcoins,int n,int sum){ 18 | if(sum==0) return 1; 19 | if(n==0) return 0; 20 | int res=getCount(coins,n-1,sum); 21 | if(coins[n-1]<=sum){ 22 | res=res+getCount(coins,n,sum-coins[n-1]); 23 | } 24 | return res; 25 | } 26 | int main() { 27 | int n,sum; 28 | cin>>n>>sum; 29 | vectorarr(n); 30 | for(int i=0;i>arr[i]; 32 | } 33 | cout< 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | class Solution { 25 | public: 26 | int f(int i, int j, string& s, string& t, vector>& dp) { 27 | // Base cases 28 | if (j == t.length()) return 1; // Found a match for the entire string t 29 | if (i >= s.length()) return 0; // Ran out of characters in s to match 30 | 31 | // Memoization check 32 | if (dp[i][j] != -1) return dp[i][j]; 33 | 34 | // Recursive calls 35 | int notMatch = f(i + 1, j, s, t, dp); // Skip current character in s 36 | int match = 0; 37 | if (s[i] == t[j]) { // If characters match, move both pointers 38 | match = f(i + 1, j + 1, s, t, dp); 39 | } 40 | 41 | // Store the result in dp array and return 42 | return dp[i][j] = match + notMatch; 43 | } 44 | 45 | int numDistinct(string s, string t) { 46 | vector> dp(s.length() + 1, vector(t.length() + 1, -1)); 47 | return f(0, 0, s, t, dp); 48 | } 49 | }; 50 | 51 | // Function to test the solution 52 | void testSolution(string s, string t, int expected) { 53 | Solution sol; 54 | int result = sol.numDistinct(s, t); 55 | cout << "Input: s = \"" << s << "\", t = \"" << t << "\"" << endl; 56 | cout << "Output: " << result << endl; 57 | cout << "Expected: " << expected << endl; 58 | cout << (result == expected ? "Test passed!" : "Test failed.") << endl << endl; 59 | } 60 | 61 | int main() { 62 | // Test cases 63 | testSolution("rabbbit", "rabbit", 3); 64 | testSolution("babgbag", "bag", 5); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /V) Dynamic Programming/Longest Common Subsequesnce.cpp: -------------------------------------------------------------------------------- 1 | //Find the longest common subsequence for the given two string 2 | 3 | //input:- S1="ABCDGH" S2="AEDFHR" 4 | //output:- 3 5 | 6 | //Explaination:- S1="A__D_H" S2="A_D_H_" 7 | //common sub sequence length is 3 (ADH) 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | //Normal Resursion Approach 16 | int lcs(string s1,string s2, int m,int n){ 17 | //base case 18 | if(m==0||n==0){ 19 | return 0; 20 | } 21 | //when both last character same 22 | if(s1[m-1]==s2[n-1]){ 23 | return 1+lcs(s1,s2,m-1,n-1); 24 | } 25 | //when last character not same 26 | else{ 27 | return max(lcs(s1,s2,m-1,n),lcs(s1,s2,m,n-1)); 28 | } 29 | } 30 | 31 | // DP approach for Memoization 32 | int lcsMemoization(string s1,string s2, int m,int n){ 33 | vector> memo(m + 1, vector(n + 1, -1)); 34 | 35 | //base case 36 | if(memo[m][n]!=-1){ 37 | return memo[m][n]; 38 | } 39 | 40 | //when both last character same 41 | if(m==0||n==0){ 42 | memo[m][n]=0; 43 | } 44 | //when last character not same 45 | else{ 46 | if(s1[m-1]==s2[n-1]){ 47 | memo[m][n]=1+lcsMemoization(s1,s2,m-1,n-1); 48 | }else{ 49 | memo[m][n]=max(lcsMemoization(s1,s2,m-1,n),lcsMemoization(s1,s2,m,n-1)); 50 | } 51 | } 52 | return memo[m][n]; 53 | } 54 | 55 | int main() { 56 | string s1,s2; 57 | cin>>s1>>s2; 58 | int m=s1.length(),n=s2.length(); 59 | int res1=lcs(s1,s2,m,n); 60 | int res2=lcsMemoization(s1,s2,m,n); 61 | cout<<"Normal Resursion Approach:- "< Editor Option > Snippets >Default Source Code 72 | 73 | -------------------------------------------------------------------------------- /V) Dynamic Programming/Longest Common Subsequesnce.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitMajee/DataStructureInCpp/4c2d971587b183bee22e5d007b32d590138440f8/V) Dynamic Programming/Longest Common Subsequesnce.exe -------------------------------------------------------------------------------- /V) Dynamic Programming/LongestIncreasingSubsequence.cpp: -------------------------------------------------------------------------------- 1 | // Given an integer array nums, find the length of the longest strictly increasing subsequence. 2 | 3 | // A subsequence is a sequence derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3, 6, 2, 7] is a subsequence of [0, 3, 1, 6, 2, 2, 7]. 4 | 5 | 6 | 7 | // Code: 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int lengthOfLIS(vector& nums) { 16 | int n = nums.size(); 17 | if (n == 0) return 0; 18 | 19 | // Initialize a dp array to store the LIS length ending at each index 20 | vector dp(n, 1); 21 | 22 | for (int i = 1; i < n; ++i) { 23 | for (int j = 0; j < i; ++j) { 24 | if (nums[i] > nums[j]) { // Check if nums[i] can extend the sequence ending at nums[j] 25 | dp[i] = max(dp[i], dp[j] + 1); 26 | } 27 | } 28 | } 29 | 30 | // The longest increasing subsequence length is the max value in dp array 31 | return *max_element(dp.begin(), dp.end()); 32 | } 33 | 34 | int main() { 35 | vector nums = {10, 9, 2, 5, 3, 7, 101, 18}; 36 | cout << "Length of Longest Increasing Subsequence: " << lengthOfLIS(nums) << endl; // Output should be 4 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /W) Graph/depth_first_search.cpp: -------------------------------------------------------------------------------- 1 | // Depth First Search 2 | 3 | // Given a graph, return the order of traversal of the graph starting from node 0 4 | 5 | // Intuition 6 | // This algorithm explores as far as possible along each branch before backtracking. 7 | // This is another way of traversing the graph 8 | // For example, when we start at 0, we consider neighbor 1 9 | // then for 1, we consider its neighbor 3 10 | // suppose 3 doesnt have any neighbors 11 | // so we backtrack and go for the next neighbor of 1 12 | // then when 1's neighbors are finished, we go for the next neighbor of 1 13 | // and so on 14 | 15 | // Nodes are marked as "visited" to prevent cycles. 16 | 17 | // Very useful algo for a lot of problems. 18 | // The other way to do it is BFS. 19 | 20 | // Solution 21 | 22 | #include 23 | #include 24 | #include 25 | using namespace std; 26 | 27 | class Solution { 28 | public: 29 | void dfs(int node, vector& traversalOrder, unordered_set& visited, vector>& adjList) { 30 | // Mark the current node as visited 31 | visited.insert(node); 32 | traversalOrder.push_back(node); 33 | 34 | // Visit all neighbors 35 | for (int neighbor : adjList[node]) { 36 | if (visited.find(neighbor) == visited.end()) { 37 | dfs(neighbor, traversalOrder, visited, adjList); 38 | } 39 | } 40 | } 41 | 42 | vector solve(vector>& adjList, int n) { 43 | vector traversalOrder; 44 | unordered_set visited; 45 | 46 | // Start DFS from node 0 47 | dfs(0, traversalOrder, visited, adjList); 48 | 49 | return traversalOrder; 50 | } 51 | }; 52 | 53 | int main() { 54 | Solution sol; 55 | // adjacency list representation of graph 56 | vector> adjList = {{1, 2}, {0, 3, 4}, {0, 4}, {1, 5}, {1, 2, 5}, {3, 4} 57 | }; 58 | 59 | int n = adjList.size(); 60 | vector result = sol.solve(adjList, n); 61 | 62 | cout << "Result : "; 63 | for (int node : result) { 64 | cout << node << " "; 65 | } 66 | cout << endl; 67 | 68 | return 0; 69 | } 70 | --------------------------------------------------------------------------------