├── Level-1 ├── 14. LinkedList │ ├── Remove-At.cpp │ ├── Remove-Last.cpp │ └── Remove-First.cpp ├── 12. Dynamic-Prgramming-and-Greedy │ ├── Paint-fence.cpp │ ├── Count-Encodings.cpp │ ├── Tiling-2_1-Tiles.cpp │ ├── Tiling-M_1-Tiles.cpp │ ├── Zero-One-Knapsack.cpp │ ├── Count-Binary-Strings.cpp │ ├── Unbounded-Knapsack.cpp │ ├── Count_A+b+c_Subsequence.cpp │ ├── Buy_and_Sell_Stocks_Infinite-Transactions.cpp │ ├── Buy_and_Sell_Stocks_Infinite-Transactions(Transaction Fee).cpp │ ├── Buy_and_Sell_Stocks_One-Transaction.cpp │ ├── Friends-Pairing.cpp │ ├── Fibonacci-dp.cpp │ ├── Maximum-Sum-Non-Adjacent-Elements.cpp │ ├── Arrange-Buildings.cpp │ ├── Coin-Change-Combination.cpp │ ├── Coin-Change-Permutation.cpp │ ├── Buy_and_Sell_Stocks_K-Transactions.cpp │ ├── Climb-Stairs.cpp │ ├── Buy_and_Sell_Stocks_Infinite-Transactions(Cooldown).cpp │ ├── Partition-into-Subsets.cpp │ ├── Climb-stairs-with-variable-jumps.cpp │ └── Target-Sum-Subsets-dp.cpp ├── 3. Functions and Number System │ ├── Any_Base_Multiplication.cpp │ ├── Digit_Frequency.cpp │ ├── Decimal_To_Any_Base.cpp │ ├── Any_Base_to_Decimal.cpp │ ├── Digit_frequency.java │ ├── Any_Base_Addition.cpp │ ├── Any_Base_Substraction.cpp │ └── Any_Base_to_Any_Base.cpp ├── 2. Patterns │ ├── cpp │ │ ├── Pattern-3.cpp │ │ ├── Pattern-2.cpp │ │ ├── Pattern-11.cpp │ │ ├── Pattern-14.cpp │ │ ├── Pattern-1.cpp │ │ ├── Pattern-4.cpp │ │ ├── Pattern-12.cpp │ │ ├── Pattern-7.cpp │ │ ├── Pattern-9.cpp │ │ ├── Pattern-8.cpp │ │ ├── Pattern-5.cpp │ │ ├── Pattern-6.cpp │ │ ├── Pattern-20.cpp │ │ ├── Pattern-13.cpp │ │ ├── Pattern-16.cpp │ │ ├── Pattern-17.cpp │ │ ├── Pattern-19.cpp │ │ ├── Pattern-15.cpp │ │ ├── Pattern-18.cpp │ │ └── Pattern-10.cpp │ └── java │ │ ├── Pattern-1.java │ │ ├── Pattern-2.java │ │ ├── Pattern-11.java │ │ ├── Pattern-14.java │ │ ├── Pattern-12.java │ │ ├── Pattern-7.java │ │ ├── Pattern-4.java │ │ ├── Pattern-8.java │ │ ├── Pattern-3.java │ │ ├── Pattern-9.java │ │ ├── Pattern-5.java │ │ ├── Pattern-6.java │ │ ├── Pattern-20.java │ │ ├── Pattern-13.java │ │ ├── Pattern-16.java │ │ ├── Pattern-17.java │ │ └── Pattern-19.java ├── 4. Arrays │ ├── cpp │ │ ├── Reverse_an_array.cpp │ │ ├── Span_of_Array.cpp │ │ ├── Find_element_in_array.cpp │ │ ├── Bar_Chart.cpp │ │ ├── Broken_Economy.cpp │ │ ├── Subarray_Problem.cpp │ │ ├── Inverse-of-an-Array.cpp │ │ ├── Rotate_an_array.cpp │ │ ├── MaximumSubarray.cpp │ │ ├── First-and-Last-Index.cpp │ │ ├── Subsets_of_an_Array.cpp │ │ └── Sum_of_Two_Arrays.cpp │ └── java │ │ ├── First_Index_Last_Index.java │ │ ├── Subarray_problem.java │ │ └── Inverse_of_an_Array.java ├── 1. Getting Started │ ├── cpp │ │ ├── TheCuriousCaseOfBenjaminBulbs.cpp │ │ ├── ReverseANumber.cpp │ │ ├── CountDigitsInANumber.cpp │ │ ├── PrintZ.cpp │ │ ├── PythagoreanTriplet.cpp │ │ ├── GcdAndLcm.cpp │ │ ├── PrintFibonacciTillN.cpp │ │ ├── PrimeFactorisationOfANumber.cpp │ │ ├── InverseOfANumber.cpp │ │ ├── PrintAllPrimeTillN.cpp │ │ ├── DigitsOfANumber.cpp │ │ ├── IsANumberPrime.cpp │ │ ├── GradingSystem.cpp │ │ └── RotateANumber.cpp │ └── java │ │ ├── PrintZ.java │ │ ├── ReverseANumber.java │ │ ├── TheCuriousCaseOfBenjaminBulbs.java │ │ ├── CountDigitsInANumber.java │ │ ├── PrimeFactorisationOfANumber.java │ │ ├── PythagoreanTriplet.java │ │ ├── PrintFibonacciTillN.java │ │ ├── GcdAndLcm.java │ │ ├── InverseOfANumber.java │ │ ├── PrintAllPrimeTillN.java │ │ ├── DigitsOfANumber.java │ │ ├── GradingSystem.java │ │ ├── IsANumberPrime.java │ │ └── RotateANumber.java ├── 6. Introduction-to-Recursion │ ├── cpp │ │ ├── Factorial.cpp │ │ ├── Print_Decreasing.cpp │ │ ├── Print_Decreasing_Increasing.cpp │ │ ├── Print_ZigZag.cpp │ │ ├── Print_Increasing.cpp │ │ ├── Power_Linear.cpp │ │ ├── Power_Logarithmic.cpp │ │ └── Tower_of_Hanoi.cpp │ └── java │ │ ├── Factorial.java │ │ ├── Print_Deacreasing_Increasing.java │ │ ├── Print_Decreasing.java │ │ ├── Print_Increasing.java │ │ ├── Print_ZigZag.java │ │ ├── Power_Linear.java │ │ ├── Power_Logorithmic.java │ │ └── Tower_of_Hanoi.java ├── 19. HashMap and Heaps │ ├── Highest Frequency Character.cpp │ ├── Get Common Elements - 2.cpp │ ├── K Largest Elements.cpp │ ├── Sort K Sorted Array.cpp │ ├── Get Common Elements - 1.cpp │ └── Longest Consecutive Sequence Of Elements.cpp ├── 7. Recursion-with-Arrays │ ├── cpp │ │ ├── Display_Array.cpp │ │ ├── Display_Array_in_Reverse.cpp │ │ ├── Max_of_an_Array.cpp │ │ ├── First_index.cpp │ │ ├── Last_Index.cpp │ │ └── All_Indices_of_Array.cpp │ └── java │ │ ├── Display_Array.java │ │ ├── Display_Array_in_Reverse.java │ │ ├── First_Index.java │ │ ├── Max_of_an_Array.java │ │ ├── Last_Index.java │ │ └── All_Indices_of_Array.java ├── 9. Recursion on the way up │ ├── Print_Subsequence.cpp │ ├── Print_Permutations.cpp │ ├── Print_StairPaths.cpp │ ├── Print_MazePaths.cpp │ ├── Print_Kpc.cpp │ ├── Print_MazePathwithJumps.cpp │ └── Print_Encodings.cpp ├── 11. Time and Space Complexity │ ├── Sort-01.cpp │ ├── Partition_An_Array.cpp │ ├── Quick_Sort.cpp │ ├── Bubble_Sort.cpp │ ├── Insertion_Sort.cpp │ └── Selection_Sort.cpp ├── 5. 2-D Arrays │ ├── cpp │ │ ├── The_State_of_Wakanda-2.cpp │ │ ├── Rotate_by_90Degree.cpp │ │ └── SpiralDisplay.cpp │ └── java │ │ └── The_State_of_Wakanda1.java ├── 13. Stack and Queues │ ├── 12. Smallest-Number-Following-Pattern.cpp │ ├── 4. Stock-span.cpp │ ├── 3. Next_Greater_to_Right.cpp │ ├── 1. Duplicate-Brackets.cpp │ ├── 6. Sliding-Window-Maximum.cpp │ ├── 13. Merge-Overlapping-Intervals.cpp │ └── 7. Celebrity-Problem.cpp ├── 10. Recursion-with-backtracking │ ├── Target_sum_subsets.cpp │ └── Flood_fill.cpp ├── String,StringBuilder-and-ArrayList │ ├── String_with_Difference_of_Every_Two_Consecutive_Characters.java │ ├── Toggle_Case.java │ └── Print_All_Palindromic_SubStrings.java ├── 8. Recursion-with-ArrayList │ ├── GetSubsequence.cpp │ └── Get_StairPath.cpp └── 18. Graphs │ ├── Perfect-Friends.cpp │ ├── Order-of-Compilation.cpp │ └── Number-of-Islands.cpp ├── Level-2 ├── 21. Graphs │ ├── Floyd Warshall.cpp │ ├── Alien Dictionary.cpp │ ├── Optimize Water Distribution.cpp │ ├── Euler Path and Circuit.cpp │ ├── Minimum Number Of Swaps Required To Sort An Array.cpp │ ├── Number of Islands.cpp │ ├── Number of Enclaves.cpp │ └── Pepcoding Course Schedule.cpp ├── 19. LinkedList-2 │ ├── Quicksort In LinkedList.cpp │ ├── Remove All Duplicates from Sorted LinkedList.cpp │ ├── Segregate 01 Node of LinkedList Over Swapping Data.cpp │ ├── Segregate 01 Node of LinkedList Over Swapping Nodes.cpp │ ├── Middle of LinkedList.cpp │ └── Reverse a Linkedlist.cpp ├── 20. Tree-2 │ ├── Vertical Order Sum Of A Binarytree.cpp │ └── Validate Bst.cpp ├── 19. Stacks │ └── test.txt ├── 18. Recursion-and-Backtracking │ ├── Josephus Problem.cpp │ ├── Combinations-I.cpp │ ├── Lexicographical Numbers.cpp │ ├── Permutation-Words-1.cpp │ ├── Coin Change - Combinations - 1.cpp │ ├── Coin Change - Combinations - 2.cpp │ ├── Queens-Combination-2D-As-2D-BoxChooses.cpp │ ├── Words-K-Selection-1.cpp │ ├── Permutations-II.cpp │ ├── Combinations-II.cpp │ ├── Largest Number Possible After At Most K Swaps.cpp │ ├── Coin Change - Permutations - 1.cpp │ ├── Coin Change - Permutations - 2.cpp │ ├── Words - K Length Words - 4.cpp │ ├── All Palindromic Partitions.cpp │ ├── Words - K Selection - 4.cpp │ ├── Permutation-Words-2.cpp │ ├── Words - K Selection - 3.cpp │ ├── Friends Pairing - 2.cpp │ ├── Queens-Permutation-2D-As-2D-QueenChooses.cpp │ ├── Words-K-Length-Words-2.cpp │ ├── Word Break - I.cpp │ ├── Abbreviation Using Backtracking.cpp │ ├── Words - K Length Words - 3.cpp │ ├── Permutations-I.cpp │ ├── Queens-Permutation-2D-As-2D-BoxChooses.cpp │ ├── Words-K-Selection-2.cpp │ ├── Queen-Combinations-2D-As-1D-QueenChooses.cpp │ ├── Queens-Combination-2D-As-2D-QueenChooses.cpp │ ├── NQueens-Branch-and-Bound.cpp │ └── Words-K-Length-Words-1.cpp ├── 22. HashMap and Heap │ ├── X Of A Kind In A Deck.cpp │ ├── Brick Wall.cpp │ ├── Rabbits In The Forest.cpp │ ├── Check Arithmetic Sequence.cpp │ ├── Largest Subarray With Zero Sum.cpp │ ├── Count Of All Subarrays With Zero Sum.cpp │ ├── Task Completion.cpp │ ├── Valid Anagram.cpp │ ├── K Anagrams.cpp │ ├── Longest Subarray With Equal Number Of Zeroes And Ones.cpp │ ├── Find Anagram Mappings.cpp │ ├── Count Of Subarrays With Equal Number Of Zeroes And Ones.cpp │ ├── Pairs With Equal Sum.cpp │ ├── Longest Subarray With Sum Divisible By K.cpp │ ├── Recurring Sequence In A Fraction.cpp │ ├── Count Of Subarrays With Sum Divisible By K.cpp │ ├── Double Pair Array.cpp │ ├── Isomorphic Strings.cpp │ ├── Find Itinerary From Tickets.cpp │ ├── Count Of Subarrays With Equal Number Of 0s 1s And 2s.cpp │ ├── Longest Subarray With Equal Number Of 0s 1s And 2s.cpp │ ├── Maximum Consecutive Ones - 1.cpp │ ├── Largest Subarray With Contiguous Elements.cpp │ ├── Maximum Consecutive Ones - 2.cpp │ ├── Count Distinct Elements In Every Window Of Size K.cpp │ ├── Count Of Substrings Having All Unique Characters.cpp │ ├── Longest Substring Without Repeating Characters.cpp │ ├── Word Pattern.cpp │ ├── Group Shifted String.cpp │ ├── Equivalent Subarrays.cpp │ ├── Count Of Substrings With Exactly K Unique Characters.cpp │ ├── Count Of Substrings Having At Most K Unique Characters.cpp │ └── Longest Substring With Exactly K Unique Characters.cpp └── 23. Dynamic Programming │ ├── Longest Increasing Subsequence.cpp │ ├── Longest Palindromic Substring.cpp │ ├── Count Palindromic Substrings.cpp │ ├── Maximum Sum Increasing Subsequence.cpp │ ├── Maximum Length Of Pair Chain.cpp │ └── Longest Bitonic Subsequence.cpp ├── .gitignore └── README.md /Level-1/14. LinkedList/Remove-At.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/14. LinkedList/Remove-Last.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/21. Graphs/Floyd Warshall.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/14. LinkedList/Remove-First.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/21. Graphs/Alien Dictionary.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/19. LinkedList-2/Quicksort In LinkedList.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/21. Graphs/Optimize Water Distribution.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Paint-fence.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/20. Tree-2/Vertical Order Sum Of A Binarytree.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Count-Encodings.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Tiling-2_1-Tiles.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Tiling-M_1-Tiles.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Zero-One-Knapsack.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Count-Binary-Strings.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Unbounded-Knapsack.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Any_Base_Multiplication.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Count_A+b+c_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/19. LinkedList-2/Remove All Duplicates from Sorted LinkedList.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/19. LinkedList-2/Segregate 01 Node of LinkedList Over Swapping Data.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/19. LinkedList-2/Segregate 01 Node of LinkedList Over Swapping Nodes.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Level-2/19. Stacks/test.txt: -------------------------------------------------------------------------------- 1 | // will remove the file once solution files are added 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | .cph 3 | largest-area-histogram.cpp 4 | sliding-window-max.cpp 5 | .exe -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-3.cpp: -------------------------------------------------------------------------------- 1 | /*--- 2 | 3 | * 4 | * * 5 | * * * 6 | * * * * 7 | * * * * * 8 | --*/ 9 | //code 10 | -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Reverse_an_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int a[n]; 8 | for(int i=0;i> a[i]; 10 | } 11 | for(int i=n-1;i>=0;i--){ 12 | cout << a[i] << " "; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/TheCuriousCaseOfBenjaminBulbs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | for (int fact = 1; fact * fact <= n; fact++) { 8 | cout << fact * fact << endl;; 9 | } 10 | return 0; 11 | 12 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/ReverseANumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(int argc, char **argv){ 5 | int n; 6 | cin >> n; 7 | while(n>0){ 8 | cout << n%10 << endl; 9 | n=n/10; 10 | } 11 | //write your code here 12 | return 0; 13 | 14 | } -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Digit_Frequency.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,count=0,d; 6 | cin >> n >> d; 7 | while(n>0){ 8 | if(n%10==d){ 9 | count++; 10 | } 11 | n/=10; 12 | } 13 | cout << count << endl; 14 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n){ 5 | if(n <= 1){ 6 | return 1; 7 | } 8 | return n * factorial(n - 1); 9 | } 10 | 11 | int main(){ 12 | int n; 13 | cin >> n; 14 | cout << factorial(n) <<"\n"; 15 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/CountDigitsInANumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(int argc, char **argv){ 5 | int n,count=0; 6 | cin >> n; 7 | while(n>0){ 8 | count++; 9 | n=n/10; 10 | } 11 | cout << count << endl; 12 | //write your code here 13 | 14 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/PrintZ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc, char** agrc){ 4 | //write your code here 5 | cout <<"*****" << endl; 6 | cout <<" *" << endl; 7 | cout <<" *" << endl; 8 | cout <<" *" << endl; 9 | cout <<"*****" << endl; 10 | return 0; 11 | 12 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Print_Decreasing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printDecreasing(int n){ 5 | if (n < 1) { 6 | return; 7 | } 8 | cout << n << endl; 9 | printDecreasing(n - 1); 10 | } 11 | 12 | int main(){ 13 | int n; 14 | cin >> n; 15 | printDecreasing(n); 16 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Buy_and_Sell_Stocks_Infinite-Transactions.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int infiniteTrans(int arr[],int n){ 5 | 6 | } 7 | 8 | int main(){ 9 | int n; 10 | cin >> n; 11 | int arr[n]; 12 | for(int i=0;i> arr[i]; 14 | } 15 | cout << infiniteTrans(arr,n); 16 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Print_Decreasing_Increasing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void pdi(int n){ 5 | if (n < 1) { 6 | return; 7 | } 8 | cout << n << endl; 9 | pdi(n - 1); 10 | cout << n << endl; 11 | 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cin >> n; 17 | pdi(n); 18 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Print_ZigZag.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void pzz(int n){ 5 | if(n==0){ 6 | return; 7 | } 8 | cout << n << " "; 9 | pzz(n-1); 10 | cout << n << " "; 11 | pzz(n-1); 12 | cout << n << " "; 13 | } 14 | int main() 15 | { 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Print_Increasing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void printIncreasing(int n){ 4 | if (n < 1) { 5 | return; 6 | } 7 | printIncreasing(n - 1); 8 | cout << n << endl; 9 | } 10 | int main() 11 | { 12 | int n; 13 | cin >> n; 14 | printIncreasing(n); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PepCoding-Practice 2 | This repository contains the [PepCoding](https://www.pepcoding.com/resources/online-java-foundation) C++ solutions. 3 | 4 | All the codes are working fine. If you do have any suggestions or improvements kindly create an issue and pull request after that. 5 | 6 | If you stuck somewhere and didn't understand the codes , feel free to contact me 7 | -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Buy_and_Sell_Stocks_Infinite-Transactions(Transaction Fee).cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int infiTranswithFee(int arr[],int n){ 5 | 6 | } 7 | 8 | int main(){ 9 | int n; 10 | cin >> n; 11 | int arr[n]; 12 | for(int i=0;i> arr[i]; 14 | } 15 | cout << infiTranswithFee(arr,n); 16 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Josephus Problem.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int jc(int n, int k) 5 | { 6 | if (n == 1) 7 | { 8 | return 0; 9 | } 10 | int ans = jc(n - 1, k); 11 | return (ans + k) % n; 12 | } 13 | 14 | int main() 15 | { 16 | int n, k; 17 | cin >> n >> k; 18 | cout << jc(n, k) << endl; 19 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/X Of A Kind In A Deck.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasGroupsSizeX(vector& deck) { 4 | unordered_map map; 5 | for(auto c:deck) map[c]++; 6 | int mn=0; 7 | for(auto m:map){ 8 | mn=__gcd(mn,m.second); 9 | } 10 | 11 | return mn>=2; 12 | 13 | } 14 | }; -------------------------------------------------------------------------------- /Level-1/19. HashMap and Heaps/Highest Frequency Character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin >> s; 7 | map mp; 8 | for(char ch:s){ 9 | mp[ch]++; 10 | } 11 | int mx=INT_MIN; 12 | char ch; 13 | for(auto m:mp){ 14 | if(m.second>mx){ 15 | mx=m.second; 16 | ch=m.first; 17 | } 18 | } 19 | cout << ch << endl; 20 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/PrintZ.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // Write your code here 7 | System.out.println("*****"); 8 | System.out.println(" *"); 9 | System.out.println(" *"); 10 | System.out.println(" *"); 11 | System.out.println("*****"); 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/ReverseANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | while (n > 0) { 10 | System.out.println(n % 10); 11 | n /= 10; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Span_of_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int a[n]; 8 | for(int i=0;i> a[i]; 10 | } 11 | int mn=INT_MAX; 12 | int mx=INT_MIN; 13 | for(int i=0;i 2 | using namespace std; 3 | 4 | // convertion from decimal to base b 5 | void getBase(int n,int b){ 6 | string s=""; 7 | while(n>0){ 8 | s=to_string(n%b)+s; 9 | n=n/b; 10 | } 11 | cout << s << endl; 12 | } 13 | 14 | int main(){ 15 | int n,b; 16 | cin >> n >> b; 17 | getBase(n,b); 18 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/TheCuriousCaseOfBenjaminBulbs.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | for (int fact = 1; fact * fact <= n; fact++) { 10 | System.out.println(fact * fact); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Power_Linear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int power(int x,int n){ 4 | if (x == 0) { 5 | return 1; 6 | } 7 | int xM1power = power(x - 1, n); 8 | int npower = n * xM1power; 9 | return npower; 10 | } 11 | 12 | int main(){ 13 | int n,x; 14 | cin >> n; 15 | cin >> x; 16 | cout << power(x,n); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Find_element_in_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,ele,position=-1; 6 | cin >> n; 7 | int a[n]; 8 | for(int i=0;i> a[i]; 10 | } 11 | cin >> ele; 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | int oneTrans(int arr[],int n){ 5 | int lpsf=arr[0],mpsf=0;// lpsf->least price so far mpsf->max profit so far 6 | return mpsf; 7 | } 8 | 9 | int main(){ 10 | int n; 11 | cin >> n; 12 | int arr[n]; 13 | for(int i=0;i> arr[i]; 15 | } 16 | 17 | cout << oneTrans(arr,n); 18 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Power_Logarithmic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int x,int n){ 5 | if (n == 0) { 6 | return 1; 7 | } 8 | int ans=power(x, n / 2); 9 | ans=ans*ans; 10 | if(n%2==1){ 11 | ans=ans*x; 12 | } 13 | return ans; 14 | } 15 | 16 | int main(){ 17 | int x,n; 18 | cin >> x >> n; 19 | cout << power(x,n) << "\n"; 20 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/PythagoreanTriplet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(int argc, char **argv){ 5 | int a,b,c; 6 | cin >> a >> b >> c; 7 | if ((a * a == b * b + c * c) || (b * b == a * a + c * c) || (c * c == a * a + b * b)) { 8 | cout << "true"; 9 | } 10 | else{ 11 | cout << "false"; 12 | } 13 | return 0; 14 | //write your code here 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/GcdAndLcm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int gcd(int a,int b){ 4 | if(b==0){ 5 | return a; 6 | } 7 | return gcd(b,a%b); 8 | } 9 | int main(int argc, char **argv){ 10 | int num1, num2; 11 | cin >> num1 >> num2; 12 | 13 | int y= gcd(num1,num2); 14 | int x=(num1*num2)/y; 15 | //write your code here 16 | cout << y << " "<< x < 2 | using namespace std; 3 | 4 | int main(int argc, char **argv) 5 | { 6 | int n; 7 | cin >> n; 8 | int arr[n]; 9 | arr[0]=0; 10 | arr[1]=1; 11 | for(int i=2;i 12 | using namespace std; 13 | 14 | int main(){ 15 | int n; 16 | cin >> n; 17 | for (int row = n; row >= 1; row--) { 18 | for (int i = 1; i <= row; i++) { 19 | cout << "*\t"; 20 | } 21 | cout << "\n"; 22 | } 23 | 24 | return 0; 25 | 26 | } -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Any_Base_to_Decimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // convertion from any base b to decimal(base 10) 5 | void getDec(int n,int b){ 6 | int sum=0,i=0; 7 | while(n>0){ 8 | sum+=pow(b,i)*(n%10); 9 | n=n/10; 10 | i++; 11 | } 12 | cout << sum << endl; 13 | } 14 | 15 | int main(){ 16 | int n,b; 17 | cin >> n >> b; 18 | getDec(n,b); 19 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/cpp/Display_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void display(int arr[], int idx, int n){ 5 | if(idx==n){ 6 | return; 7 | } 8 | cout << arr[idx] << endl; 9 | display(arr,idx+1,n); 10 | 11 | } 12 | 13 | int main(){ 14 | int n; 15 | cin >> n; 16 | int arr[n]; 17 | for (int i = 0; i < n; i++) 18 | cin >> arr[i]; 19 | display(arr, 0, n); 20 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Friends-Pairing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int friendPairing(int n) 5 | { 6 | int dp[n + 1]; 7 | dp[0] = 1; 8 | dp[1] = 1; 9 | for (int i = 2; i <= n; i++) 10 | { 11 | dp[i] = dp[i - 1] + (i - 1) * (dp[i - 2]); 12 | } 13 | return dp[n]; 14 | } 15 | 16 | int main() 17 | { 18 | int n; 19 | cin >> n; 20 | cout << friendPairing(n); 21 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/CountDigitsInANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | int count = 0; 10 | while (n > 0) { 11 | count++; 12 | n = n / 10; 13 | } 14 | System.out.println(count); 15 | } 16 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/PrimeFactorisationOfANumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc, char **argv){ 4 | int n; 5 | cin >> n; 6 | for (int i = 2; i * i <= n; i++) { 7 | while (n % i == 0) { 8 | cout << i << " "; 9 | n = n / i; 10 | } 11 | } 12 | if (n != 1) { 13 | cout < 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char **argv){ 6 | int n; 7 | cin >> n; 8 | int x=0,count=0; 9 | int temp=n; 10 | while(n>0){ 11 | n=n/10; 12 | count++; 13 | } 14 | for(int i=1;i<=count;i++){ 15 | x=x+i*pow(10,(temp%10)-1); 16 | temp=temp/10; 17 | } 18 | cout << x << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Digit_frequency.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner scn = new Scanner(System.in); 7 | int n = scn.nextInt(); 8 | int d = scn.nextInt(); 9 | int f = getDigitFrequency(n, d); 10 | System.out.println(f); 11 | } 12 | 13 | public static int getDigitFrequency(int n, int d) { 14 | // write code here 15 | } 16 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/cpp/Tower_of_Hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void toh(int n,int t1id, int t2id, int t3id){ 5 | if (n == 0) { 6 | return; 7 | } 8 | toh(n - 1, t1id, t3id, t2id); 9 | // toh(1,t1id,t2id,t3id); 10 | cout << n << "[" << t1id << " -> " << t2id << "]" << endl; 11 | toh(n - 1, t3id, t2id, t1id); 12 | } 13 | 14 | int main(){ 15 | int n,t1,t2,t3; 16 | cin >> n >> t1 >> t2 >> t3; 17 | toh(n,t1,t2,t3); 18 | } 19 | -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 3 4 | 4 5 6 5 | 7 8 9 10 6 | 11 12 13 14 15 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | int main(int argc, char **argv){ 12 | int n; 13 | cin >> n; 14 | int num=1; 15 | for(int r=1;r<=n;r++){ 16 | for(int c=1;c<=r;c++){ 17 | cout << num <<"\t"; 18 | num++; 19 | } 20 | cout << "\n"; 21 | } 22 | 23 | //write your code here 24 | 25 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/cpp/Display_Array_in_Reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void display(int arr[], int idx, int n){ 5 | // write your code here 6 | if(idx==n){ 7 | return; 8 | } 9 | display(arr,idx+1,n); 10 | cout << arr[idx] << endl; 11 | 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cin >> n; 17 | int arr[n]; 18 | for (int i = 0; i < n; i++) 19 | cin >> arr[i]; 20 | display(arr, 0, n); 21 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-14.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 * 1 = 3 3 | 3 * 2 = 6 4 | 3 * 3 = 9 5 | 3 * 4 = 12 6 | 3 * 5 = 15 7 | 3 * 6 = 18 8 | 3 * 7 = 21 9 | 3 * 8 = 24 10 | 3 * 9 = 27 11 | 3 * 10 = 30 12 | */ 13 | #include 14 | using namespace std; 15 | int main(int agrc, char** argv){ 16 | int n; 17 | cin >> n; 18 | for(int i=1;i<=10;i++){ 19 | cout << n <<" * " << i << " = " << n*i; 20 | cout << "\n"; 21 | } 22 | 23 | //write your code here 24 | 25 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-1.cpp: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * 4 | * * 5 | * * * 6 | * * * * 7 | * * * * * 8 | 9 | ---*/ 10 | //Code 11 | #include 12 | using namespace std; 13 | 14 | int main(int argc, char **argv){ 15 | int n; 16 | cin >> n; 17 | for (int row = 1; row <= n; row++) { 18 | for (int str = 1; str <= row; str++) { 19 | cout <<"*\t"; 20 | } 21 | cout << endl; 22 | } 23 | return 0; 24 | //write your code here 25 | 26 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Combinations-I.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void combinations(int cb,int tb,int ssf,int ts,string asf) { 5 | if(cb>tb){ 6 | if(ssf==ts){ 7 | cout << asf << endl; 8 | } 9 | return; 10 | } 11 | combinations(cb+1,tb,ssf+1,ts,asf+"i"); 12 | combinations(cb+1,tb,ssf,ts,asf+"-"); 13 | 14 | } 15 | 16 | 17 | int main(){ 18 | int nboxes , ritems; 19 | cin >> nboxes >> ritems; 20 | combinations(1,nboxes,0, ritems,""); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * * * * * 3 | * * * * 4 | * * * 5 | * * 6 | * 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | int n; 13 | cin >> n; 14 | for (int row = 1; row <= n; row++) { 15 | for (int i = 1; i <= row - 1; i++) { 16 | cout <<"\t"; 17 | } 18 | for (int j = 1; j <= n - row + 1; j++) { 19 | cout <<"*\t"; 20 | } 21 | cout << "\n"; 22 | } 23 | return 0; 24 | 25 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Lexicographical Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void family_print(int num, int n) 5 | { 6 | if (num > n) 7 | { 8 | return; 9 | } 10 | 11 | cout << num << endl; 12 | for (int i = 0; i <= 9; i++) 13 | { 14 | family_print(num * 10 + i, n); 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cin >> n; 22 | for (int i = 1; i <= 9; i++) 23 | { 24 | family_print(i, n); 25 | } 26 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/PrintAllPrimeTillN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc, char **argv){ 4 | int low, high; 5 | cin >> low >> high; 6 | 7 | //write your code here 8 | for(int i=low;i<=high;i++){ 9 | int flag=0; 10 | for(int j=2;j*j<=i;j++){ 11 | if(i%j==0){ 12 | flag=1; 13 | break; 14 | } 15 | } 16 | if(flag==0){ 17 | cout << i << endl; 18 | } 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 0 3 | 1 1 4 | 2 3 5 5 | 8 13 21 34 6 | 55 89 144 233 377 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | int main(int argc, char **argv){ 12 | int n; 13 | cin >> n; 14 | int fi=0,fy=1; 15 | for(int r=1;r<=n;r++){ 16 | for(int c=1;c<=r;c++){ 17 | cout << fi << "\t"; 18 | int x=fi+fy; 19 | fi=fy; 20 | fy=x; 21 | } 22 | cout << "\n"; 23 | } 24 | //write your code here 25 | 26 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * 4 | * 5 | * 6 | * 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | int main(int argc, char **argv){ 12 | int n; 13 | cin >> n; 14 | for(int r=1;r<=n;r++){ 15 | for(int c=1;c<=n;c++){ 16 | if(r==c){ 17 | cout <<"*\t"; 18 | } 19 | else{ 20 | cout <<"\t"; 21 | } 22 | } 23 | cout <<"\n"; 24 | } 25 | 26 | //write your code here 27 | 28 | } -------------------------------------------------------------------------------- /Level-1/9. Recursion on the way up/Print_Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printSS(string ques, string ans){ 5 | // write your code here 6 | if(ques.length()==0){ 7 | cout << ans << endl; 8 | return; 9 | } 10 | int len=ques.length(); 11 | string emp=""; 12 | emp+=ques[0]; 13 | string sub=ques.substr(1); 14 | printSS(sub,ans+emp); 15 | printSS(sub,ans); 16 | } 17 | 18 | int main(){ 19 | string str; 20 | cin >> str; 21 | printSS(str, ""); 22 | } -------------------------------------------------------------------------------- /Level-1/19. HashMap and Heaps/Get Common Elements - 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m; 6 | cin >> n; 7 | int arr1[n]; 8 | unordered_map mp; 9 | //unordered_map mp2; 10 | for(int i=0;i> arr1[i]; 12 | mp[arr1[i]]++; 13 | } 14 | cin >> m; 15 | int arr2[m]; 16 | for(int i=0;i> arr2[i]; 18 | if(mp.find(arr2[i])!=mp.end() && mp[arr2[i]]!=0){ 19 | cout << arr2[i] << endl; 20 | mp[arr2[i]]--; 21 | } 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Fibonacci-dp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n,vector &v){ 5 | // write your code here 6 | if(n==0 || n==1){ 7 | return n; 8 | } 9 | if(v[n]!=-1){ 10 | return v[n]; 11 | } 12 | int fibn=fib(n-1,v)+fib(n-2,v); 13 | v[n]=fibn; 14 | return fibn; 15 | } 16 | 17 | int main(){ 18 | int n; 19 | cin>>n; 20 | vector arr(n+1,-1); 21 | int ans=fib(n,arr); 22 | cout << ans << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Level-1/19. HashMap and Heaps/K Largest Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int arr[n]; 8 | for(int i=0;i> arr[i]; 10 | } 11 | int k; 12 | cin >> k; 13 | priority_queue,greater> p; 14 | int i=0; 15 | for(i=0;ip.top()){ 20 | p.pop(); 21 | p.push(arr[i]); 22 | } 23 | } 24 | while(p.size()>0){ 25 | cout << p.top() << endl; 26 | p.pop(); 27 | } 28 | } -------------------------------------------------------------------------------- /Level-1/19. HashMap and Heaps/Sort K Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int arr[n]; 8 | for(int i=0;i> arr[i]; 10 | } 11 | int k; 12 | cin >> k; 13 | priority_queue,greater> p; 14 | for(int i=0;i<=k;i++){ 15 | p.push(arr[i]); 16 | } 17 | for(int i=k+1;i0){ 23 | cout << p.top()<< endl; 24 | p.pop(); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * * 3 | * * 4 | * 5 | * * 6 | * * 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | int main(int argc, char **argv){ 12 | int n; 13 | cin >> n; 14 | for(int r=1;r<=n;r++){ 15 | for(int c=1;c<=n;c++){ 16 | if(r+c==n+1 || r==c){ 17 | cout <<"*\t"; 18 | } 19 | else{ 20 | cout << "\t"; 21 | } 22 | } 23 | cout << "\n"; 24 | } 25 | 26 | //write your code here 27 | 28 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-1.java: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * 4 | * * 5 | * * * 6 | * * * * 7 | * * * * * 8 | 9 | ---*/ 10 | //Code 11 | import java.util.*; 12 | 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | Scanner scn = new Scanner(System.in); 17 | int n = scn.nextInt(); 18 | for (int row = 1; row <= n; row++) { 19 | 20 | for (int str = 1; str <= row; str++) { 21 | System.out.print("*\t"); 22 | } 23 | System.out.println(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/cpp/Max_of_an_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int max(int arr[], int idx, int n){ 5 | // write your code here 6 | if(idx==n){ 7 | return -1; 8 | } 9 | int mx=max(arr,idx+1,n); 10 | if(arr[idx]>mx){ 11 | mx=arr[idx]; 12 | } 13 | return mx; 14 | 15 | } 16 | 17 | int main(){ 18 | int n; 19 | cin >> n; 20 | int arr[n]; 21 | for (int i = 0; i < n; i++) 22 | cin >> arr[i]; 23 | int p = max(arr, 0, n); 24 | cout << p << endl; 25 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Brick Wall.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastBricks(vector>& wall) { 4 | unordered_map map; 5 | for(auto m:wall){ 6 | int ps=0; 7 | for(int i=0;i mx){ 15 | mx=m.second; 16 | } 17 | } 18 | return wall.size()-mx; 19 | } 20 | }; -------------------------------------------------------------------------------- /Level-1/9. Recursion on the way up/Print_Permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printPermutations(string str, string asf){ 5 | // write your code here 6 | if(str.length()==0){ 7 | cout << asf << endl; 8 | } 9 | for(int i=0;i>str; 21 | printPermutations(str,""); 22 | 23 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/PrimeFactorisationOfANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | for (int i = 2; i * i <= n; i++) { 10 | while (n % i == 0) { 11 | System.out.print(i + " "); 12 | n = n / i; 13 | } 14 | } 15 | if (n != 1) { 16 | System.out.println(n); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Level-1/19. HashMap and Heaps/Get Common Elements - 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m; 6 | cin >> n; 7 | int arr1[n]; 8 | unordered_map mp; 9 | unordered_map mp2; 10 | for(int i=0;i> arr1[i]; 12 | mp[arr1[i]]++; 13 | } 14 | cin >> m; 15 | int arr2[m]; 16 | for(int i=0;i> arr2[i]; 18 | // mp[arr2[i]]++; 19 | if(mp.find(arr2[i])!=mp.end() && mp2[arr2[i]]==0){ 20 | cout << arr2[i] << endl; 21 | mp2[arr2[i]]++; 22 | } 23 | } 24 | 25 | 26 | 27 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | * 5 | * 6 | * 7 | * 8 | * 9 | 10 | 11 | */ 12 | #include 13 | using namespace std; 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int n; 18 | cin >> n; 19 | for(int r=n;r>=1;r--){ 20 | for(int c=1;c<=n;c++){ 21 | if(c==r){ 22 | cout <<"*\t"; 23 | } 24 | else{ 25 | cout << "\t"; 26 | } 27 | } 28 | cout << "\n"; 29 | } 30 | 31 | //write your code here 32 | 33 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Factorial.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int ans = factorial(n); 11 | System.out.println(ans); 12 | } 13 | 14 | public static int factorial(int n) { 15 | if (n <= 1) { 16 | return 1; 17 | } 18 | 19 | return n * factorial(n - 1); 20 | 21 | 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Print_Deacreasing_Increasing.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | pdi(n); 11 | } 12 | 13 | public static void pdi(int n) { 14 | if (n < 1) { 15 | return; 16 | } 17 | System.out.println(n); 18 | pdi(n - 1); 19 | System.out.println(n); 20 | 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Print_Decreasing.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | printDecreasing(n); 11 | } 12 | 13 | public static void printDecreasing(int n) { 14 | if (n < 1) { 15 | return; 16 | } 17 | System.out.println(n); 18 | printDecreasing(n - 1); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-2.java: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * * * * * 4 | * * * * 5 | * * * 6 | * * 7 | * 8 | -- 9 | */ 10 | import java.util.*; 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) { 15 | Scanner scn = new Scanner(System.in); 16 | int n = scn.nextInt(); 17 | for (int row = n; row >= 1; row--) { 18 | for (int i = 1; i <= row; i++) { 19 | System.out.print("*\t"); 20 | } 21 | System.out.println(); 22 | } 23 | // write ur code here 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Bar_Chart.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,max=INT_MIN; 6 | cin >> n; 7 | int arr[n]; 8 | for(int i=0;i>arr[i]; 10 | if(arr[i]>max){ 11 | max=arr[i]; 12 | } 13 | } 14 | for(int r=0;r=max-arr[c]){ 17 | cout <<"* "; 18 | } 19 | else{ 20 | cout<<" "; 21 | } 22 | } 23 | cout<<"\n"; 24 | } 25 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/PythagoreanTriplet.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int a = scn.nextInt(); 9 | int b = scn.nextInt(); 10 | int c = scn.nextInt(); 11 | if ((a * a == b * b + c * c) || (b * b == a * a + c * c) || (c * c == a * a + b * b)) { 12 | System.out.println("true"); 13 | } else { 14 | System.out.println("false"); 15 | } 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Print_Increasing.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | printIncreasing(n); 11 | } 12 | 13 | public static void printIncreasing(int n) { 14 | if (n < 1) { 15 | return; 16 | } 17 | printIncreasing(n - 1); 18 | System.out.println(n); 19 | 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/PrintFibonacciTillN.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | int f1 = 0; 10 | int f2 = 1; 11 | System.out.println(f1); 12 | System.out.println(f2); 13 | for (int i = 2; i < n; i++) { 14 | int f3 = f1 + f2; 15 | System.out.println(f3); 16 | f1 = f2; 17 | f2 = f3; 18 | 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/DigitsOfANumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | 5 | int pow(int n,int b){ 6 | int sum=1; 7 | for(int i=0;i> n; 16 | ll y=n; 17 | while (y != 0) { 18 | x = y % 10; 19 | sum = sum * 10 + x; 20 | y /= 10; 21 | count++; 22 | } 23 | while(count>0){ 24 | cout << sum%10 << endl; 25 | sum=sum/10; 26 | count--; 27 | } 28 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-11.java: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 3 4 | 4 5 6 5 | 7 8 9 10 6 | 11 12 13 14 15 7 | */ 8 | import java.util.*; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Scanner scn = new Scanner(System.in); 14 | int n=scn.nextInt(); 15 | int num=1; 16 | for(int r=1;r<=n;r++){ 17 | for(int c=1;c<=r;c++){ 18 | System.out.print(num+"\t"); 19 | num++; 20 | } 21 | System.out.print("\n"); 22 | } 23 | // write ur code here 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Permutation-Words-1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void generateWords(int cs,int ts,map &mp,string asf){ 5 | if(cs>ts){ 6 | cout << asf << endl; 7 | } 8 | 9 | for(auto x:mp){ 10 | if(x.second>0){ 11 | mp[x.first]--; 12 | generateWords(cs+1,ts,mp,asf+x.first); 13 | mp[x.first]++; 14 | } 15 | } 16 | 17 | } 18 | 19 | int main(){ 20 | string s; 21 | cin >> s; 22 | map mp; 23 | for(int i=0;i 2 | using namespace std; 3 | 4 | int maxSum(int *arr, int n) 5 | { 6 | int in = 0, ex = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | int ni = ex + arr[i]; 10 | int ne = max(in, ex); 11 | 12 | in = ni; 13 | ex = ne; 14 | } 15 | return max(in, ex); 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cin >> n; 22 | int arr[n]; 23 | for (int i = 0; i < n; i++) 24 | { 25 | cin >> arr[i]; 26 | } 27 | cout << maxSum(arr, n); 28 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/IsANumberPrime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin >> t; 6 | while(t--){ 7 | int n,flag=0; 8 | cin >> n; 9 | for(int i=2;i<=sqrt(n);i++){ 10 | if(n%i==0){ 11 | flag=1; 12 | break; 13 | } 14 | } 15 | if(flag==1){ 16 | cout << "not prime" << endl; 17 | } 18 | else{ 19 | cout << "prime" << endl; 20 | } 21 | } 22 | return 0; 23 | 24 | //write your code here 25 | 26 | } -------------------------------------------------------------------------------- /Level-1/9. Recursion on the way up/Print_StairPaths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printStairPaths(int n, string psf){ 5 | // write your code here 6 | if(n==0){ 7 | cout << psf << endl; 8 | return; 9 | } 10 | if(n<0){ 11 | return; 12 | } 13 | string x=to_string(1); 14 | printStairPaths(n-1,psf+x); 15 | string y=to_string(2); 16 | printStairPaths(n-2,psf+y); 17 | string z=to_string(3); 18 | printStairPaths(n-3,psf+z); 19 | } 20 | 21 | int main(){ 22 | 23 | int n; 24 | cin >> n; 25 | printStairPaths(n, ""); 26 | } -------------------------------------------------------------------------------- /Level-1/9. Recursion on the way up/Print_MazePaths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printMazePaths(int sr, int sc, int dr, int dc, string psf){ 5 | // write your code here 6 | if(sr==dr && sc==dc){ 7 | cout << psf << endl; 8 | return; 9 | } 10 | if(sr>dr || sc>dc){ 11 | return; 12 | } 13 | string a="h"; 14 | printMazePaths(sr,sc+1,dr,dc,psf+a); 15 | string b="v"; 16 | printMazePaths(sr+1,sc,dr,dc,psf+b); 17 | } 18 | 19 | int main(){ 20 | int n; 21 | int m; 22 | cin >> n >> m; 23 | printMazePaths(0, 0, n - 1, m - 1, ""); 24 | } -------------------------------------------------------------------------------- /Level-2/21. Graphs/Euler Path and Circuit.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/euler-circuit-and-path/1 2 | 3 | 4 | int isEularCircuit(int V, vectoradj[]){ 5 | // Code here 6 | vector degree(V); 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int arr[n]; 8 | for(int i=0;i> arr[i]; 10 | } 11 | 12 | int i=0,j=0; 13 | for(int k=0;k 2 | using namespace std; 3 | 4 | string codes[] = {".;", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tu", "vwx", "yz"}; 5 | 6 | void printKPC(string ques, string asf){ 7 | // write your code here 8 | if(ques.length()==0){ 9 | cout << asf << endl; 10 | } 11 | string z=codes[ques[0]-'0']; 12 | for(int i=0;i> str; 23 | printKPC(str, ""); 24 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Print_ZigZag.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn=new Scanner(System.in); 9 | int n=scn.nextInt(); 10 | pzz(n); 11 | } 12 | 13 | public static void pzz(int n) { 14 | if(n==0){ 15 | return; 16 | } 17 | System.out.print(n+" "); 18 | pzz(n-1); 19 | System.out.print(n+" "); 20 | pzz(n-1); 21 | System.out.print(n+" "); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/cpp/First_index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int firstIndex(int arr[], int idx, int x, int n){ 5 | // write your code here 6 | if(idx==n){ 7 | return -1; 8 | } 9 | if(arr[idx]==x){ 10 | return idx; 11 | } 12 | else{ 13 | return firstIndex(arr,idx+1,x,n); 14 | } 15 | 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cin >> n; 22 | int d; 23 | int arr[n]; 24 | for (int i = 0; i < n; i++) 25 | cin >> arr[i]; 26 | cin >> d; 27 | int p = firstIndex(arr, 0, d, n); 28 | cout << p << endl; 29 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Coin Change - Combinations - 1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void coinchangeCombination(int idx,int *arr,int n,int sum,int ssf,string asf){ 5 | if(ssf == sum){ 6 | cout << asf << "." << endl; 7 | return; 8 | } 9 | 10 | for(int i=idx+1;i> n; 21 | int arr[n]; 22 | for(int i=0;i> arr[i]; 24 | } 25 | int sum; 26 | cin>> sum; 27 | coinchangeCombination(-1,arr,n,sum,0,""); 28 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/InverseOfANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | int x = 0, count = 0; 10 | int temp = n; 11 | while (n > 0) { 12 | n = n / 10; 13 | count++; 14 | } 15 | for (int i = 1; i <= count; i++) { 16 | x = x + (int)(i * (Math.pow(10, (temp % 10) - 1))); 17 | temp = temp / 10; 18 | } 19 | System.out.println(x); 20 | } 21 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Broken_Economy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | int *arr = new int[n]; 9 | for (int i = 0; i < n; i++) 10 | { 11 | cin >> arr[i]; 12 | } 13 | int data; 14 | cin >> data; 15 | 16 | for (int i = 0; i < n; i++) 17 | { 18 | if (data == arr[i]) 19 | { 20 | cout << arr[i] << endl; 21 | } 22 | else if (arr[i] < data && arr[i + 1] > data && i < n - 1) 23 | { 24 | cout << arr[i + 1] << endl; 25 | cout << arr[i] << endl; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Level-1/5. 2-D Arrays/cpp/The_State_of_Wakanda-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | vector> arr(n,vector (n)); 8 | for(int i=0;i> arr[i][j]; 11 | } 12 | } 13 | 14 | //state of wakanda2 15 | int count=0; 16 | while(count 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | unordered_map map; 6 | for(int i:v){ 7 | map[i]++; 8 | } 9 | int ans = 0; 10 | for(auto m:map){ 11 | int val = m.second; 12 | int k = m.first+1; 13 | 14 | ans += k*(int)ceil((val*1.0)/k); 15 | } 16 | 17 | return ans; 18 | } 19 | 20 | 21 | int main(){ 22 | int n; 23 | cin >> n; 24 | vector v(n); 25 | for(int i=0;i> v[i]; 27 | } 28 | 29 | cout << solution(v) << endl; 30 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Arrange-Buildings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | 5 | //tabulation 6 | ll countBinary0(ll n){ 7 | ll dp[2][n+1]; 8 | dp[0][0]=0; 9 | dp[1][0]=0; 10 | for(int j=1;j> n; 31 | long long ans =countBinary0(n); 32 | cout << ans*ans; 33 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Check Arithmetic Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | unordered_map map; 6 | for(int i:v){ 7 | map[i]++; 8 | } 9 | int ans = 0; 10 | for(auto m:map){ 11 | int val = m.second; 12 | int k = m.first+1; 13 | 14 | ans += k*(int)ceil((val*1.0)/k); 15 | } 16 | 17 | return ans; 18 | } 19 | 20 | 21 | int main(){ 22 | int n; 23 | cin >> n; 24 | vector v(n); 25 | for(int i=0;i> v[i]; 27 | } 28 | 29 | cout << solution(v) << endl; 30 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/GradingSystem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(int argc, char**argv){ 4 | int marks; 5 | cin>>marks; 6 | if (marks > 90) { 7 | cout<<"excellent" << endl; 8 | } 9 | else if (marks > 80 && marks <= 90) { 10 | cout << "good" < 70 && marks <= 80) { 13 | cout<<"fair"< 60 && marks <= 70) { 16 | cout<<"meets expectations"< 2 | using namespace std; 3 | 4 | void fun(string s){ 5 | int cnt=1; 6 | stack st; 7 | for(int i=0;i0){ 17 | cout << st.top(); 18 | st.pop(); 19 | } 20 | 21 | } 22 | } 23 | st.push(cnt); 24 | while(st.size()>0){ 25 | cout << st.top(); 26 | st.pop(); 27 | } 28 | 29 | } 30 | 31 | int main(){ 32 | string s; 33 | cin >> s; 34 | fun(s); 35 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-14.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 * 1 = 3 3 | 3 * 2 = 6 4 | 3 * 3 = 9 5 | 3 * 4 = 12 6 | 3 * 5 = 15 7 | 3 * 6 = 18 8 | 3 * 7 = 21 9 | 3 * 8 = 24 10 | 3 * 9 = 27 11 | 3 * 10 = 30 12 | */ 13 | import java.util.*; 14 | 15 | public class Main { 16 | 17 | public static void main(String[] args) { 18 | Scanner scn = new Scanner(System.in); 19 | int n = scn.nextInt(); 20 | for (int i = 1; i <= 10; i++) { 21 | // cout << n <<"*" << i << "=" << n*i; 22 | // cout << "\n"; 23 | System.out.println(n + " * " + i + " = " + n * i); 24 | } 25 | 26 | // write ur code here 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Largest Subarray With Zero Sum.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | unordered_map mp; 6 | int ps=0;int ans=0; 7 | mp[0]=-1; 8 | for(int i=0;i> n; 23 | vector v(n); 24 | for(int i=0;i> v[i]; 26 | } 27 | cout << solution(v) << endl; 28 | 29 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-12.java: -------------------------------------------------------------------------------- 1 | /* 2 | 0 3 | 1 1 4 | 2 3 5 5 | 8 13 21 34 6 | 55 89 144 233 377 7 | */ 8 | import java.util.*; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Scanner scn = new Scanner(System.in); 14 | int n=scn.nextInt(); 15 | int fi=0,fy=1; 16 | for(int r=1;r<=n;r++){ 17 | for(int c=1;c<=r;c++){ 18 | System.out.print(fi+"\t"); 19 | int x=fi+fy; 20 | fi=fy; 21 | fy=x; 22 | } 23 | System.out.print("\n"); 24 | } 25 | // write ur code here 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Power_Linear.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int x = scn.nextInt(); 11 | int ans = power(x, n); 12 | System.out.println(ans); 13 | } 14 | 15 | public static int power(int x, int n) { 16 | if (x == 0) { 17 | return 1; 18 | } 19 | int xM1power = power(x - 1, n); 20 | int npower = n * xM1power; 21 | return npower; 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/cpp/Last_Index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lastIndex(int arr[], int idx, int x, int n){ 5 | // write your code here 6 | if(idx==n){ 7 | return -1; 8 | } 9 | int newIdx=lastIndex(arr,idx+1,x,n); 10 | if(newIdx==-1){ 11 | if(arr[idx]==x){ 12 | newIdx=idx; 13 | } 14 | } 15 | return newIdx; 16 | 17 | } 18 | 19 | int main(){ 20 | int n; 21 | cin >> n; 22 | int d; 23 | int arr[n]; 24 | for (int i = 0; i < n; i++) 25 | cin >> arr[i]; 26 | cin >> d; 27 | int p = lastIndex(arr, 0, d, n); 28 | cout << p << endl; 29 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Coin Change - Combinations - 2.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void coinchangeCombination(int idx,int *arr,int n,int sum,int ssf,string asf){ 5 | if(ssf>sum){ 6 | return; 7 | } 8 | if(ssf == sum){ 9 | cout << asf << "." << endl; 10 | return; 11 | } 12 | 13 | for(int i=idx;i> n; 24 | int arr[n]; 25 | for(int i=0;i> arr[i]; 27 | } 28 | int sum; 29 | cin>> sum; 30 | coinchangeCombination(0,arr,n,sum,0,""); 31 | } -------------------------------------------------------------------------------- /Level-1/11. Time and Space Complexity/Partition_An_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,pivt; 6 | cin >> n; 7 | int arr[n]; 8 | for(int i=0;i> arr[i]; 10 | } 11 | cin >> pivt; 12 | 13 | //partion 14 | int idx=0; 15 | for(int i=0;i=arr[i]){ 17 | swap(arr[idx],arr[i]); 18 | cout << "Swapping " << arr[idx] << " and " << arr[i] << endl; 19 | idx++; 20 | } 21 | } 22 | 23 | //display array 24 | for(int i=0;i> n; 27 | queencombinationbox(0,0,0,n,""); 28 | return 0; 29 | 30 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * * * * * 3 | * * * * 4 | * * * 5 | * * 6 | * 7 | */ 8 | import java.util.*; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Scanner scn = new Scanner(System.in); 14 | int n = scn.nextInt(); 15 | for (int row = 1; row <= n; row++) { 16 | for (int i = 1; i <= row - 1; i++) { 17 | System.out.print("\t"); 18 | } 19 | for (int j = 1; j <= n - row + 1; j++) { 20 | System.out.print("*\t"); 21 | } 22 | System.out.println(); 23 | } 24 | // write ur code here 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /Level-1/13. Stack and Queues/4. Stock-span.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void stockSpan(int *arr,int n){ 5 | stack st; 6 | // st.push(arr[0]); 7 | vector a(n); 8 | for(int i=0;i0 && arr[st.top()]> n; 29 | int arr[n]; 30 | for (int i = 0; i < n; i++) 31 | { 32 | cin >> arr[i]; 33 | } 34 | stockSpan(arr,n); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/PrintAllPrimeTillN.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | // write your code here 6 | Scanner scn = new Scanner(System.in); 7 | int low = scn.nextInt(); 8 | int high = scn.nextInt(); 9 | for (int i = low; i <= high; i++) { 10 | int flag = 0; 11 | for (int j = 2; j * j <= i; j++) { 12 | if (i % j == 0) { 13 | flag = 1; 14 | break; 15 | } 16 | } 17 | if (flag == 0) { 18 | System.out.println(i); 19 | } 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Subarray_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void subarray(int *arr, int n) 5 | { 6 | int str = 0; 7 | while (str < n) 8 | { 9 | int end = str; 10 | while (end < n) 11 | { 12 | for (int i = str; i <= end; i++) 13 | { 14 | cout << arr[i] << "\t"; 15 | } 16 | cout << endl; 17 | end++; 18 | } 19 | str++; 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | int n; 26 | cin >> n; 27 | int *arr = new int[n]; 28 | for (int i = 0; i < n; i++) 29 | { 30 | cin >> arr[i]; 31 | } 32 | 33 | subarray(arr, n); 34 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words-K-Selection-1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | void combination(int idx,string ustr,int ssf,int ts,string asf){ 6 | if(idx == ustr.length()){ 7 | if(ssf == ts){ 8 | cout << asf << endl; 9 | } 10 | return; 11 | } 12 | 13 | combination(idx+1,ustr,ssf+1,ts,asf+ustr[idx]); 14 | combination(idx+1,ustr,ssf,ts,asf); 15 | } 16 | 17 | 18 | int main(){ 19 | string s; 20 | int n; 21 | set un; 22 | cin >> s >> n; 23 | for(char ch:s){ 24 | un.insert(ch); 25 | } 26 | string ustr=""; 27 | for(auto x:un){ 28 | ustr+=x; 29 | } 30 | // cout << ustr << endl; 31 | combination(0,ustr,0,n,""); 32 | return 0; 33 | 34 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-8.java: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * 4 | * 5 | * 6 | * 7 | * 8 | 9 | 10 | --*/ 11 | import java.util.*; 12 | 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | Scanner scn = new Scanner(System.in); 17 | int n=scn.nextInt(); 18 | for(int r=n;r>=1;r--){ 19 | for(int c=1;c<=n;c++){ 20 | if(c==r){ 21 | System.out.print("*\t"); 22 | } 23 | else{ 24 | System.out.print("\t"); 25 | } 26 | } 27 | System.out.print("\n"); 28 | } 29 | // write ur code here 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of All Subarrays With Zero Sum.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | unordered_map mp; 6 | int ps=0;int ans=0; 7 | mp[0]=1; 8 | for(int i=0;i> n; 24 | vector v(n); 25 | for(int i=0;i> v[i]; 27 | } 28 | // int k;cin>>k; 29 | cout << solution(v) << endl; 30 | 31 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-3.java: -------------------------------------------------------------------------------- 1 | /*--- 2 | 3 | * 4 | * * 5 | * * * 6 | * * * * 7 | * * * * * 8 | --*/ 9 | //code 10 | import java.util.*; 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) { 15 | Scanner scn = new Scanner(System.in); 16 | int n = scn.nextInt(); 17 | for (int row = 1; row <= n; row++) { 18 | for (int i = 1; i <= n - row; i++) { 19 | System.out.print("\t"); 20 | } 21 | for (int j = 1; j <= row; j++) { 22 | System.out.print("*\t"); 23 | } 24 | System.out.println(); 25 | } 26 | // write ur code here 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-9.java: -------------------------------------------------------------------------------- 1 | /* 2 | * * 3 | * * 4 | * 5 | * * 6 | * * 7 | */ 8 | import java.util.*; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Scanner scn = new Scanner(System.in); 14 | int n = scn.nextInt(); 15 | for (int r = 1; r <= n; r++) { 16 | for (int c = 1; c <= n; c++) { 17 | if (r + c == n + 1 || r == c) { 18 | System.out.print("*\t"); 19 | } else { 20 | System.out.print("\t"); 21 | } 22 | } 23 | System.out.println(); 24 | } 25 | // write ur code here 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Any_Base_Addition.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | int getSum(int b, int n1, int n2) { 9 | // write your code here 10 | int ans=0,carry=0,p=1; 11 | while(n1>0 || n2>0 || carry!=0){ 12 | int ld=n1%10; 13 | int rd=n2%10; 14 | n1=n1/10; 15 | n2=n2/10; 16 | 17 | int sum=ld+rd+carry; 18 | int val=sum%b; 19 | carry=sum/b; 20 | ans=ans+val*p; 21 | p=p*10; 22 | } 23 | 24 | return ans; 25 | 26 | 27 | } 28 | 29 | 30 | int main() { 31 | int b, n1, n2; 32 | cin >> b >> n1 >> n2; 33 | cout << getSum(b, n1, n2) << endl; 34 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/DigitsOfANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // write your code here 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | int y = n; 10 | int sum = 1, count = 0; 11 | while (n > 0) { 12 | n = n / 10; 13 | count++; 14 | } 15 | for (int i = 1; i < count; i++) { 16 | sum = sum * 10; 17 | } 18 | while (count > 0) { 19 | System.out.println(y / sum); 20 | y = y % sum; 21 | sum = sum / 10; 22 | count--; 23 | } 24 | 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Coin-Change-Combination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int CCC(int *arr,int n,int amt,int *dp){ 5 | dp[0]=1; 6 | for(int i=0;i=0){ 10 | dp[j]+=dp[left]; 11 | } 12 | } 13 | } 14 | return dp[amt]; 15 | } 16 | int main(){ 17 | int n,amt; 18 | cin >> n; 19 | int arr[n]; 20 | for(int i=0;i> arr[i]; 22 | } 23 | cin >> amt; 24 | int dp[amt+1]; 25 | for(int i=0;i<=amt;i++){ 26 | dp[i]=0; 27 | } 28 | cout << CCC(arr,n,amt,dp) << endl; 29 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Inverse-of-an-Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *inverse(int *arr, int n) 5 | { 6 | int *inv = new int[n]; 7 | for (int i = 0; i < n; i++) 8 | { 9 | int pos = arr[i]; 10 | inv[pos] = i; 11 | } 12 | return inv; 13 | } 14 | 15 | void display(int *arr, int n) 16 | { 17 | for (int i = 0; i < n; i++) 18 | { 19 | cout << arr[i] << endl; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() 25 | { 26 | int n; 27 | cin >> n; 28 | int *arr = new int[n]; 29 | for (int i = 0; i < n; i++) 30 | { 31 | cin >> arr[i]; 32 | } 33 | 34 | int *inv = inverse(arr, n); 35 | display(inv, n); 36 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Coin-Change-Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int CCP(int *arr,int n,int amt,int *dp){ 5 | dp[0]=1; 6 | for(int i=1;i=0){ 10 | dp[i]+=dp[t]; 11 | 12 | } 13 | } 14 | } 15 | return dp[amt]; 16 | } 17 | 18 | int main(){ 19 | int n,amt; 20 | cin >> n; 21 | int arr[n]; 22 | for(int i=0;i> arr[i]; 24 | } 25 | cin >> amt; 26 | int dp[amt+1]; 27 | for(int i=0;i &nba,int ssf,int ts,string asf) { 5 | if(cb>tb){ 6 | if(ssf==ts){ 7 | cout << asf << endl; 8 | } 9 | return; 10 | } 11 | for(int i=0;i> nboxes >> ritems; 26 | vector nba(ritems,false); 27 | permutationsII(1,nboxes,nba,0, ritems,""); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Level-1/19. HashMap and Heaps/Longest Consecutive Sequence Of Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m; 6 | cin >> n; 7 | int arr1[n]; 8 | vector gl; 9 | map mp; 10 | for(int i=0;i> arr1[i]; 12 | mp[arr1[i]]++; 13 | } 14 | int cnt=0,temp=(*mp.begin()).first; 15 | vector v; 16 | for(auto x:mp){ 17 | if(x.first==temp){ 18 | cnt++; 19 | temp++; 20 | v.push_back(x.first); 21 | 22 | } 23 | else{ 24 | 25 | cnt=1; 26 | temp=x.first+1; 27 | v.clear(); 28 | v.push_back(x.first); 29 | } 30 | if(cnt>gl.size()){ 31 | gl=v; 32 | } 33 | } 34 | 35 | for(auto y:gl){ 36 | cout << y << endl; 37 | } 38 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Combinations-II.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void combinationsII(int cb,int tb,vector &nba,int ts) { 5 | if(cb>tb){ 6 | for(int i=0;i> nboxes >> ritems; 31 | vector nba(nboxes,false); 32 | combinationsII(1,ritems,nba,-1); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Largest Number Possible After At Most K Swaps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string mx; 5 | 6 | void findMax(string s,int k){ 7 | mx = max(mx,s); 8 | if(k==0){ 9 | return; 10 | } 11 | 12 | 13 | for(int i=0;i s[i]){ 16 | swap(s[i],s[j]); 17 | findMax(s,k-1); 18 | swap(s[i],s[j]); 19 | } 20 | } 21 | } 22 | } 23 | 24 | 25 | int main(){ 26 | string s; 27 | cin >> s; 28 | int k; 29 | cin >> k; 30 | mx=s; 31 | findMax(s,k); 32 | cout << mx << endl; 33 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Buy_and_Sell_Stocks_K-Transactions.cpp: -------------------------------------------------------------------------------- 1 | // K Transactions 2 | 3 | #include 4 | using namespace std; 5 | 6 | int kTrans(int *arr, int n,int k){ 7 | if(k>n){return 0;} 8 | int dp[k+1][n]; 9 | for(int i=0;i<=k;i++){ 10 | int mx=INT_MIN; 11 | for(int j=0;j> n; 26 | int arr[n]; 27 | for (int i = 0; i < n; i++) 28 | { 29 | cin >> arr[i]; 30 | } 31 | cin >> k; 32 | cout << kTrans(arr,n,k); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/java/Display_Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | 10 | int n = scn.nextInt(); 11 | 12 | int arr[] = new int[n]; 13 | for (int i = 0; i < n; i++) { 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | displayArr(arr, 0); 18 | } 19 | 20 | public static void displayArr(int[] arr, int idx) { 21 | if(idx==arr.length){ 22 | return; 23 | } 24 | System.out.println(arr[idx]); 25 | displayArr(arr, idx+1); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Coin Change - Permutations - 1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void coinchangePermutation(int idx,int *arr,int n,int sum,int ssf,string asf){ 5 | if(ssf > sum) { 6 | return; 7 | } 8 | if(ssf == sum){ 9 | cout << asf << "."<< endl; 10 | return; 11 | } 12 | 13 | for(int i=0;i> n; 27 | int arr[n]; 28 | for(int i=0;i> arr[i]; 30 | } 31 | int sum; 32 | cin>> sum; 33 | coinchangePermutation(0,arr,n,sum,0,""); 34 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Rotate_an_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void reverseArray(int arr[], int start,int end){ 4 | while (start < end){ 5 | swap(arr[start], arr[end]); 6 | start++; 7 | end--; 8 | } 9 | } 10 | 11 | void rotatearray(int a[],int d,int n){ 12 | reverseArray(a, 0, n-1); 13 | reverseArray(a, 0, d-1); 14 | reverseArray(a, d, n-1); 15 | } 16 | 17 | int main(){ 18 | int n,k; 19 | cin >> n; 20 | int a[n]; 21 | for(int i=0;i> a[i]; 23 | } 24 | cin >> k; 25 | if(k<0){ 26 | k=abs(k)%n; 27 | k=n-abs(k); 28 | } 29 | k=k%n; 30 | rotatearray(a,k,n); 31 | for(int i=0;i sum) { 6 | return; 7 | } 8 | if(ssf == sum){ 9 | cout << asf << "."<< endl; 10 | return; 11 | } 12 | 13 | for(int i=0;i> n; 27 | int arr[n]; 28 | for(int i=0;i> arr[i]; 30 | } 31 | int sum; 32 | cin>> sum; 33 | coinchangePermutation(0,arr,n,sum,0,""); 34 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Task Completion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m; 6 | cin >> n >> m; 7 | vector v(m); 8 | for(int i=0;i> v[i]; 10 | } 11 | 12 | vector s(n+1,0); 13 | for(int i=0;i ans; 18 | for(int i=1;i<=n;i++){ 19 | if(s[i]==0){ 20 | ans.push_back(i); 21 | } 22 | } 23 | 24 | 25 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool isValidAnagram(string s,string t){ 5 | unordered_map map; 6 | for(char ch:s){ 7 | map[ch]++; 8 | } 9 | 10 | for(char ch:t){ 11 | if(map.find(ch)==map.end()){ 12 | return false; 13 | } 14 | map[ch]--; 15 | } 16 | 17 | 18 | for(auto m:map){ 19 | if(m.second!=0){ 20 | return false; 21 | } 22 | } 23 | return true; 24 | } 25 | 26 | int main(){ 27 | string s,t; 28 | cin >> s >> t; 29 | if(isValidAnagram(s,t)){ 30 | cout << "true" << endl; 31 | } 32 | else{ 33 | cout << "false" << endl; 34 | } 35 | } -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Any_Base_Substraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getDifference(int b, int n1, int n2){ 5 | // write your code here 6 | int ans=0,p=1,borrow=0; 7 | while(n2>0){ 8 | int ld=n1%10; 9 | int rd=n2%10; 10 | n1=n1/10; 11 | n2=n2/10; 12 | 13 | int val=(rd+borrow)-ld; 14 | if(val<0){ 15 | val=val+b; 16 | borrow=-1; 17 | } 18 | else{ 19 | borrow=0; 20 | } 21 | 22 | ans=ans+val*p; 23 | p=p*10; 24 | } 25 | return ans; 26 | 27 | } 28 | 29 | 30 | int main(){ 31 | int b, n1, n2; 32 | cin>>b>>n1>>n2; 33 | cout << getDifference(b,n1,n2); 34 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words - K Length Words - 4.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | void wordklength4(string ustr,map &mp,int idx,int ts,string asf){ 6 | if(idx==ts){ 7 | cout << asf << endl; 8 | return; 9 | } 10 | 11 | for(int i=0;i0){ 14 | mp[ch]--; 15 | wordklength4(ustr,mp,idx+1,ts,asf+ch); 16 | mp[ch]++; 17 | } 18 | } 19 | 20 | } 21 | 22 | int main(){ 23 | int n; 24 | string s; 25 | cin >> s >> n; 26 | string ustr=""; 27 | map mp; 28 | for(char ch:s){ 29 | if(mp[ch]>0){ 30 | mp[ch]++; 31 | } 32 | else{ 33 | ustr+=ch; 34 | mp[ch]++; 35 | } 36 | } 37 | 38 | wordklength4(ustr,mp,0,n,""); 39 | 40 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/K Anagrams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool KAnagrams(string &s,string &t,int k){ 5 | if(s.size()!=t.size()) return false; 6 | 7 | vector v1(26),v2(26); 8 | for(auto ch:s){ 9 | v1[ch-'a']++; 10 | } 11 | for(auto ch:t){ 12 | v2[ch-'a']++; 13 | } 14 | int cnt=0; 15 | for(int i=0;i<26;i++){ 16 | cnt+=(v1[i]-v2[i]>0?v1[i]-v2[i]:0); 17 | } 18 | // cout << cnt<< endl; 19 | return cnt<=k; 20 | } 21 | 22 | 23 | int main(){ 24 | string s,t; 25 | cin >> s>>t; 26 | int k; 27 | cin >> k; 28 | if(KAnagrams(s,t,k)){ 29 | cout << "true" << endl; 30 | } 31 | else { 32 | cout << "false" << endl; 33 | } 34 | } -------------------------------------------------------------------------------- /Level-2/23. Dynamic Programming/Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // O(n^2) 5 | int LIS(vector &V){ 6 | vector dp(V.size()); 7 | int omax = 0; 8 | 9 | for(int i=0;i> n; 29 | vector v(n); 30 | for(int i=0;i> v[i]; 32 | } 33 | 34 | cout << LIS(v) << endl; 35 | } -------------------------------------------------------------------------------- /Level-1/10. Recursion-with-backtracking/Target_sum_subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printtargetSumSubsets(int arr[],int n,int sum,int tar,string asf,int idx){ 5 | if(sum>tar){ 6 | return; 7 | } 8 | if(idx==n){ 9 | if(sum==tar){ 10 | cout << asf << "." << endl; 11 | } 12 | return; 13 | } 14 | string x=to_string(arr[idx]); 15 | printtargetSumSubsets(arr,n,sum+arr[idx],tar,asf+x+", ",idx+1); 16 | printtargetSumSubsets(arr,n,sum,tar,asf,idx+1); 17 | } 18 | int main(){ 19 | int n,tar; 20 | cin >> n; 21 | int arr[n]; 22 | for(int i=0;i> arr[i]; 24 | } 25 | cin >> tar; 26 | printtargetSumSubsets(arr,n,0,tar,"",0); 27 | } -------------------------------------------------------------------------------- /Level-1/13. Stack and Queues/3. Next_Greater_to_Right.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | int arr[n]; 8 | for(int i=0;i> arr[i]; 10 | } 11 | int a[n]; 12 | stack st; 13 | st.push(arr[n-1]); 14 | a[n-1]=-1; 15 | for(int i=n-2;i>=0;i--){ 16 | while(st.size()>0 && st.top()< arr[i]){ 17 | st.pop(); 18 | } 19 | if(st.size()==0){ 20 | a[i]=-1; 21 | // st.push(arr[i]); 22 | } 23 | else{ 24 | a[i]=st.top(); 25 | 26 | } 27 | st.push(arr[i]); 28 | } 29 | 30 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool isPalindrome(string s){ 5 | int l=0,r=s.length()-1; 6 | while(l> s; 37 | solution(s,""); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/java/First_Index_Last_Index.java: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void firstAndLastIndex(int arr[],int d,int n){ 5 | int flg=0; 6 | for(int i=0;i=0;j--){ 14 | if(arr[j]==d){ 15 | cout << j << endl; 16 | break; 17 | } 18 | } 19 | if(flg==0){ 20 | cout << -1 << endl << -1 << endl; 21 | } 22 | } 23 | 24 | int main(){ 25 | int n,d; 26 | cin >> n; 27 | int arr[n]; 28 | for(int i=0;i> arr[i]; 30 | } 31 | cin >> d; 32 | firstAndLastIndex(arr,d,n); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Level-1/String,StringBuilder-and-ArrayList/String_with_Difference_of_Every_Two_Consecutive_Characters.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static String solution(String str) { 7 | // write your code here 8 | String res=""; 9 | res+=str.charAt(0); 10 | for(int i=1;i 9 | using namespace std; 10 | 11 | int main(int argc, char **argv){ 12 | int n; 13 | cin >> n; 14 | int str=1; 15 | for(int row=1;row<=n;row++){ 16 | int nsp=(n-str)/2; 17 | for(int i=1;i<=nsp;i++){ 18 | cout << "\t"; 19 | } 20 | for(int j=1;j<=str;j++){ 21 | cout << "*\t"; 22 | } 23 | for(int i=1;i<=nsp;i++){ 24 | cout << "\t"; 25 | } 26 | if(row<((n/2)+1)){ 27 | str=str+2; 28 | } 29 | if(row>=((n/2)+1)){ 30 | str=str-2; 31 | } 32 | cout <<"\n"; 33 | } 34 | //write your code here 35 | return 0; 36 | 37 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/cpp/All_Indices_of_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector allIdx(int arr[],int n,int x,int idx){ 5 | if(idx==n){ 6 | vector v; 7 | return v; 8 | } 9 | vector c=allIdx(arr,n,x,idx+1); 10 | if(arr[idx]==x){ 11 | c.push_back(idx); 12 | } 13 | return c; 14 | } 15 | 16 | int main(){ 17 | int n,x; 18 | cin >> n; 19 | int arr[n]; 20 | for(int i=0;i> arr[i]; 22 | } 23 | cin >> x; 24 | vector vec=allIdx(arr,n,x,0); 25 | if(vec.size()==0){ 26 | cout << endl; 27 | } 28 | else{ 29 | for(int i=vec.size()-1;i>=0;i--){ 30 | cout << vec[i] << endl; 31 | } 32 | } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words - K Selection - 4.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void wordkselection4(int cs,int idx,string ustr,map &mp,int ts,string asf){ 5 | if(cs==ts){ 6 | // if(asf.length()==ts){ 7 | cout << asf << endl; 8 | // } 9 | return; 10 | } 11 | 12 | for(int i=idx;i0){ 15 | mp[ustr[i]]--; 16 | wordkselection4(cs+1,i,ustr,mp,ts,asf+ustr[i]); 17 | mp[ustr[i]]++; 18 | } 19 | } 20 | } 21 | 22 | 23 | int main(){ 24 | int n; 25 | string s; 26 | cin >> s >> n; 27 | map mp; 28 | string ustr=""; 29 | for(char ch:s){ 30 | if(mp[ch]==0){ 31 | ustr+=ch; 32 | } 33 | mp[ch]++; 34 | } 35 | 36 | wordkselection4(0,0,ustr,mp,n,""); 37 | 38 | 39 | } -------------------------------------------------------------------------------- /Level-1/3. Functions and Number System/Any_Base_to_Any_Base.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //to convert from given number in base b to decimal base 5 | int getDec(int n,int b){ 6 | int sum = 0, i = 0; 7 | while(n > 0){ 8 | sum += pow(b,i) * (n%10); 9 | n = n/10; 10 | i++; 11 | } 12 | return sum; 13 | } 14 | // to convert given number to number in base b 15 | int getBase(int n,int b){ 16 | int sum = 0,i = 0; 17 | while(n > 0){ 18 | sum += (n%b) * pow(10,i); 19 | n = n/b; 20 | i++; 21 | } 22 | return sum; 23 | } 24 | 25 | int main(){ 26 | int n,b1,b2; 27 | cin >> n >> b1 >> b2; 28 | int val=getDec(n,b1);//covert to decimal val 29 | cout << getBase(val,b2) << endl;// convert decimal to base b 30 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Permutation-Words-2.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void generateWords(int cc,string s,vector &spots,map &mp){ 5 | if(cc==s.length()){ 6 | for(auto ch:spots){ 7 | cout << ch; 8 | } 9 | cout << endl; 10 | return; 11 | 12 | } 13 | 14 | char ch=s[cc]; 15 | int loch=mp[ch];// last occurence of characte 16 | for(int i=loch+1;i> s; 29 | map mp; 30 | for(auto ch:s){ 31 | mp[ch]=-1; 32 | } 33 | vector a(s.length()); 34 | generateWords(0,s,a,mp); 35 | return 0; 36 | 37 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/cpp/RotateANumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main(){ 6 | int n,k; 7 | cin >> n >> k; 8 | int count = 0, sum = 0; 9 | int tem = n; 10 | while (tem > 0) { 11 | tem = tem / 10; 12 | count++; 13 | } 14 | // System.out.println(count); 15 | k = k % count; 16 | if (k < 0) { 17 | // k=Math.abs(k)%count; 18 | k = k + count; 19 | } 20 | int temp = k; 21 | while (k > 0) { 22 | int rem = n % 10; 23 | sum = sum * 10 + rem; 24 | n /= 10; 25 | k--; 26 | } 27 | while (temp > 0) { 28 | cout<< sum % 10; 29 | sum /= 10; 30 | temp--; 31 | } 32 | cout << n << "\n"; 33 | 34 | //write your code here 35 | 36 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words - K Selection - 3.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void wordkselection(int cs,string ustr,map &mp,int ts,string asf){ 5 | if(ustr.length()==cs){ 6 | if(asf.length()==ts){ 7 | cout << asf << endl; 8 | } 9 | 10 | return; 11 | } 12 | 13 | char ch=ustr[cs]; 14 | int freq=mp[ch]; 15 | 16 | for(int i=freq;i>=0;i--){ 17 | string ans=""; 18 | for(int j=0;j> s >> n; 30 | map mp; 31 | string ustr=""; 32 | for(char ch:s){ 33 | if(mp[ch]==0){ 34 | ustr+=ch; 35 | } 36 | mp[ch]++; 37 | } 38 | 39 | wordkselection(0,ustr,mp,n,""); 40 | 41 | 42 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-6.cpp: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * * * * * * 4 | * * * * 5 | * * 6 | * * * * 7 | * * * * * * 8 | 9 | --*/ 10 | #include 11 | using namespace std; 12 | 13 | int main(int argc, char **argv){ 14 | int n; 15 | cin >> n; 16 | int nsp=1; 17 | for(int r=1;r<=n;r++){ 18 | int str=(n-nsp)/2; 19 | for(int i=1;i<=str+1;i++){ 20 | cout << "*\t"; 21 | } 22 | for(int i=1;i<=nsp;i++){ 23 | cout <<"\t"; 24 | } 25 | for(int i=1;i<=str+1;i++){ 26 | cout << "*\t"; 27 | } 28 | if(r<((n/2)+1)){ 29 | nsp+=2; 30 | } 31 | if(r>=((n/2)+1)){ 32 | nsp-=2; 33 | } 34 | cout <<"\n"; 35 | } 36 | 37 | //write your code here 38 | 39 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Longest Subarray With Equal Number Of Zeroes And Ones.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | for(int i=0;i mp; 14 | int ps=0;int ans=0; 15 | mp[0]=-1; 16 | for(int i=0;i> n; 31 | vector v(n); 32 | for(int i=0;i> v[i]; 34 | } 35 | cout << solution(v) << endl; 36 | 37 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-20.cpp: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * * 4 | * * 5 | * * * 6 | * * * * 7 | * * 8 | 9 | --*/ 10 | //code 11 | #include 12 | using namespace std; 13 | int main(int argc, char**argv){ 14 | int n; 15 | cin >> n; 16 | for(int r=1;r<=n;r++){ 17 | for(int c=1;c<=n;c++){ 18 | if(c==1 || c==n){ 19 | cout <<"*\t"; 20 | } 21 | else if(r==c && r>(n/2)){ 22 | cout << "*\t"; 23 | } 24 | else if((r+c==n+1) && (r>=(n/2)+1)){ 25 | cout <<"*\t"; 26 | } 27 | else{ 28 | cout <<"\t"; 29 | } 30 | 31 | } 32 | 33 | 34 | cout <<"\n"; 35 | } 36 | 37 | //write your code here 38 | 39 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Find Anagram Mappings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector findMapping(int a1[],int a2[],int n){ 5 | unordered_map> map; 6 | for(int i=0;i ans(n); 10 | for(int i=0;i v=map[a1[i]]; 12 | ans[i]=v[0]; 13 | map[a1[i]].erase(map[a1[i]].begin()); 14 | } 15 | 16 | return ans; 17 | } 18 | 19 | int main(){ 20 | int n; 21 | cin >> n; 22 | int a1[n],a2[n]; 23 | for(int i=0;i> a1[i]; 25 | } 26 | for(int i=0;i> a2[i]; 28 | } 29 | 30 | vector ans = findMapping(a1,a2,n); 31 | for(auto a:ans){ 32 | cout << a << " "; 33 | } 34 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of Subarrays With Equal Number Of Zeroes And Ones.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | for(int i=0;i mp; 14 | int ps=0;int ans=0; 15 | mp[0]=1; 16 | for(int i=0;i> n; 32 | vector v(n); 33 | for(int i=0;i> v[i]; 35 | } 36 | cout << solution(v) << endl; 37 | 38 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Pairs With Equal Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool solve(vector &v){ 5 | map map; 6 | int n=v.size(); 7 | for(int i=0;i> n; 30 | vector v(n); 31 | for(int i=0;i> v[i]; 33 | } 34 | 35 | if(solve(v)){ 36 | cout << "true" << endl; 37 | } 38 | else{ 39 | cout << "false" << endl; 40 | } 41 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Longest Subarray With Sum Divisible By K.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v,int k){ 5 | unordered_map map; 6 | 7 | int ps=0;int ans=0; 8 | map[0]=-1; 9 | for(int i=0;i> n; 33 | vector arr(n); 34 | for(int i=0;i> arr[i]; 36 | } 37 | int k;cin >> k; 38 | cout << solution(arr,k) << endl; 39 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/GradingSystem.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class GradingSystem { 4 | 5 | public static void main(String[] args) { 6 | // input - don't change this code 7 | Scanner scn = new Scanner(System.in); 8 | int marks = scn.nextInt(); 9 | // input - don't change this code 10 | if (marks > 90) { 11 | System.out.println("excellent"); 12 | } else if (marks > 80 && marks <= 90) { 13 | System.out.println("good"); 14 | } else if (marks > 70 && marks <= 80) { 15 | System.out.println("fair"); 16 | } else if (marks > 60 && marks <= 70) { 17 | System.out.println("meets expectations"); 18 | } else { 19 | System.out.println("below par"); 20 | } 21 | // code here 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/java/First_Index.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args){ 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int arr[]=new int[n]; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | string fracToDec(int num, int deno){ 6 | if(!num)return "0"; 7 | string ans; 8 | if(num > 0 ^ deno > 0){ 9 | ans+="-"; 10 | } 11 | long n=labs(num),d=labs(deno),r=n%d; 12 | ans+=to_string(n/d); 13 | if(!r)return ans; 14 | ans+="."; 15 | unordered_mapmp; 16 | while(r){ 17 | if(mp.find(r)!= mp.end()){ 18 | ans.insert(mp[r],"("); 19 | ans+=")"; 20 | break; 21 | } 22 | mp[r]=ans.size(); 23 | r*=10; 24 | ans+=to_string(r/d); 25 | r=r%d; 26 | 27 | } 28 | return ans; 29 | } 30 | 31 | 32 | int main(){ 33 | int n,d; 34 | cin >> n >> d; 35 | cout << fracToDec(n,d) << endl; 36 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of Subarrays With Sum Divisible By K.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v,int k){ 5 | unordered_map map; 6 | 7 | int ps=0;int ans=0; 8 | map[0]=1; 9 | for(int i=0;i> n; 34 | vector arr(n); 35 | for(int i=0;i> arr[i]; 37 | } 38 | int k;cin >> k; 39 | cout << solution(arr,k) << endl; 40 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/MaximumSubarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // This is a functional problem. You have to complete this function. 8 | // It takes as input an integer array. It should return the required sum. 9 | int maxSum(vector < int > & arr) { 10 | // write your code here. 11 | int sum=INT_MIN,curr_sum=0; 12 | int n=arr.size(); 13 | for(int i=0;i> n; 28 | vector < int > A(n); 29 | for (int i = 0; i < n; i++) 30 | cin >> A[i]; 31 | cout << maxSum(A) << endl; 32 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/java/Max_of_an_Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | 10 | int n = scn.nextInt(); 11 | 12 | int arr[] = new int[n]; 13 | for (int i = 0; i < n; i++) { 14 | arr[i] = scn.nextInt(); 15 | } 16 | // int max=Integer.MIN_VALUE; 17 | System.out.println(maxOfArray(arr,0)); 18 | } 19 | 20 | public static int maxOfArray(int[] arr, int idx) { 21 | if(idx==arr.length){ 22 | return 0; 23 | } 24 | int newmx=maxOfArray(arr,idx+1); 25 | if(newmx &used,string asf){ 7 | if(idx==used.size()){ 8 | cout << cnt << "."; 9 | cout << asf << endl; 10 | return; 11 | } 12 | 13 | if(used[idx]==true){ 14 | solution(idx+1,n,used,asf); 15 | } 16 | else{ 17 | used[idx]=true; 18 | solution(idx+1,n,used,asf+"("+to_string(idx)+") "); 19 | 20 | for(int i=idx+1;i<=n;i++){ 21 | if(used[i]==false){ 22 | cnt++; 23 | used[i]=true; 24 | solution(idx+1,n,used,asf+"("+to_string(idx)+","+to_string(i)+") "); 25 | used[i]=false; 26 | } 27 | } 28 | used[idx]=false; 29 | } 30 | 31 | 32 | } 33 | 34 | 35 | int main(){ 36 | int n; 37 | cin >> n; 38 | vector used(n+1,false); 39 | 40 | solution(1,n,used,""); 41 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Climb-Stairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // tabulation 6 | int cs(int n){ 7 | int dp[n+1]; 8 | dp[0]=1; 9 | for(int i=1;i<=n;i++){ 10 | if(i==1){ 11 | dp[i]=1; 12 | } 13 | else if(i==2){ 14 | dp[i]=2; 15 | } 16 | else{ 17 | dp[i]=dp[i-1]+dp[i-2]+dp[i-3]; 18 | } 19 | } 20 | 21 | return dp[n]; 22 | } 23 | 24 | 25 | // memoization 26 | int cs(int n,vector &v){ 27 | // write your code here 28 | if(n==0){return 1;} 29 | if(n<0){return 0;} 30 | if(v[n]!=-1){ return v[n];} 31 | int csp=cs(n-1,v)+cs(n-2,v)+cs(n-3,v); 32 | return v[n]=csp; 33 | } 34 | 35 | 36 | 37 | int main(){ 38 | int n; 39 | cin>>n; 40 | vector v(n+1,-1); 41 | cout< 9 | using namespace std; 10 | int permutation(int n,int r){ 11 | if(r==0 || n==0){ 12 | return 1; 13 | } 14 | if(n==r){ 15 | return 1; 16 | } 17 | int nfact=1,rfact=1,nrfact=1; 18 | for(int i=1;i<=n;i++){ 19 | nfact*=i; 20 | } 21 | for(int i=1;i<=n-r;i++){ 22 | nrfact*=i; 23 | } 24 | for(int i=1;i<=r;i++){ 25 | rfact*=i; 26 | } 27 | return nfact/(rfact*nrfact); 28 | } 29 | 30 | int main(int argc, char **argv){ 31 | int n; 32 | cin >> n; 33 | for(int r=1;r<=n;r++){ 34 | for(int c=1;c<=r;c++){ 35 | cout << permutation(r-1,c-1) <<"\t"; 36 | } 37 | cout <<"\n"; 38 | } 39 | 40 | //write your code here 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Queens-Permutation-2D-As-2D-QueenChooses.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void queenpermutationnqueen(vector> &chess,int qpsf,int tq){ 5 | if(qpsf>tq){ 6 | for(int i=0;i> n; 35 | vector> chess(n,vector(n,0)); 36 | queenpermutationqueen(chess,1,n); 37 | return 0; 38 | 39 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/java/Last_Index.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int arr[] = new int[n]; 11 | for (int i = 0; i < n; i++) { 12 | arr[i] = scn.nextInt(); 13 | } 14 | int x = scn.nextInt(); 15 | System.out.println(lastIndex(arr, 0, x)); 16 | } 17 | 18 | public static int lastIndex(int[] arr, int idx, int x) { 19 | if (idx == arr.length) { 20 | return -1; 21 | } 22 | int res = lastIndex(arr, idx + 1, x); 23 | if (res== -1 && arr[idx] == x) { 24 | return idx; 25 | } 26 | else { 27 | return res; 28 | } 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words-K-Length-Words-2.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | void wordklengthword2(vector &box,string ustr,int ssf,int ts,string asf){ 6 | // if(idx == ){ 7 | if(ssf == ts){ 8 | cout << asf << endl; 9 | return; 10 | 11 | } 12 | 13 | for(int i=0;i v; 27 | cin >> s >> n; 28 | for(char ch:s){ 29 | auto it=find(v.begin(),v.end(),ch); 30 | if(it==v.end()){ 31 | v.push_back(ch); 32 | } 33 | } 34 | string ustr=""; 35 | for(auto x:v){ 36 | ustr+=x; 37 | } 38 | vector box(ustr.length(),false); 39 | wordklengthword2(box,ustr,0,n,""); 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /Level-1/6. Introduction-to-Recursion/java/Tower_of_Hanoi.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int t1id = scn.nextInt(); 11 | int t2id = scn.nextInt(); 12 | int t3id = scn.nextInt(); 13 | toh(n, t1id, t2id, t3id); 14 | } 15 | 16 | public static void toh(int n, int t1id, int t2id, int t3id) { 17 | if (n == 0) { 18 | // System.out.println(n + "[" + t1id + " -> " + t2id + "]"); 19 | return; 20 | } 21 | toh(n - 1, t1id, t3id, t2id); 22 | // toh(1,t1id,t2id,t3id); 23 | System.out.println(n + "[" + t1id + " -> " + t2id + "]"); 24 | toh(n - 1, t3id, t2id, t1id); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Word Break - I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void wordBreak(string str, string ans, unordered_set &set) 5 | { 6 | if (str.length() == 0) 7 | { 8 | cout << ans << endl; 9 | return; 10 | } 11 | 12 | for (int i = 0; i < str.length(); i++) 13 | { 14 | string left = str.substr(0, i + 1); 15 | if (set.count(left) == 1) 16 | { 17 | string right = str.substr(i + 1); 18 | wordBreak(right, ans + left + " ", set); 19 | } 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | int n; 26 | cin >> n; 27 | unordered_set set; 28 | for (int i = 0; i < n; i++) 29 | { 30 | string s; 31 | cin >> s; 32 | set.insert(s); 33 | } 34 | string word; 35 | cin >> word; 36 | wordBreak(word, "", set); 37 | } -------------------------------------------------------------------------------- /Level-2/23. Dynamic Programming/Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int countSubstrings(string s) { 4 | int n=s.length(); 5 | int si=-1,sj=-1; 6 | vector> dp(n,vector(n,false)); 7 | for(int d=0;d> s; 31 | cout << countSubstrings(s) << endl; 32 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Double Pair Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool canReorderDoubled(vector& arr) { 5 | sort(arr.begin(),arr.end()); 6 | unordered_map map; 7 | for(int a:arr) map[a]++; 8 | 9 | for(int a:arr){ 10 | if(map[a]==0) 11 | continue; 12 | if(a < 0 && abs(a)%2 == 1) 13 | return false; 14 | int next_val = a < 0 ? a/2 : a*2; 15 | if(map[next_val]==0) return false; 16 | map[a]--,map[next_val]--; 17 | 18 | } 19 | return true; 20 | } 21 | 22 | 23 | int main(){ 24 | int n; 25 | cin >> n; 26 | vector arr(n); 27 | for(int i=0;i> arr[i]; 29 | } 30 | 31 | if(canReorderDoubled(arr)){ 32 | cout << "true" << endl; 33 | } 34 | else{ 35 | cout << "false" << endl; 36 | } 37 | } -------------------------------------------------------------------------------- /Level-1/String,StringBuilder-and-ArrayList/Toggle_Case.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static String toggleCase(String str) { 7 | //write your code here 8 | // StringBuilder s=new StringBuilder(str); 9 | String ne=""; 10 | for(int i=0;i='a' && ch<='z'){ 13 | char n=(char)((ch-'a')+'A'); 14 | ne+=n; 15 | } 16 | else if(ch>='A' && ch<='Z'){ 17 | char n=(char)((ch-'A')+'a'); 18 | ne+=n; 19 | } 20 | } 21 | 22 | return ne; 23 | } 24 | public static void main(String[] args) { 25 | Scanner scn = new Scanner(System.in); 26 | String str = scn.next(); 27 | System.out.println(toggleCase(str)); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Isomorphic Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isIsomorphic(string &s,string &t){ 5 | unordered_map map1; 6 | unordered_map map2; 7 | 8 | for(int i=0;i> s >> t; 30 | if(isIsomorphic(s,t)){ 31 | cout << "true" << endl; 32 | } 33 | else{ 34 | cout << "false" << endl; 35 | } 36 | } -------------------------------------------------------------------------------- /Level-1/8. Recursion-with-ArrayList/GetSubsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector gss(string s){ 6 | // write your code here 7 | vector x; 8 | if(s.length()==0){ 9 | x.push_back(""); 10 | return x; 11 | 12 | } 13 | string small=s.substr(0,s.length()-1); 14 | vector getsub=gss(small); 15 | for(int i=0;i> s; 25 | vector ans = gss(s); 26 | int cnt = 0; 27 | 28 | cout << '['; 29 | for (string str : ans){ 30 | if (cnt != ans.size() - 1) 31 | cout << str << ", "; 32 | else 33 | cout << str; 34 | cnt++; 35 | } 36 | cout << ']'; 37 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Buy_and_Sell_Stocks_Infinite-Transactions(Cooldown).cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int infiTranswithCooldown(int arr[],int n){ 5 | int obsp=-arr[0],ossp=0,ocsp=0; 6 | for(int i=1;iobsp) { 10 | nbsp=ocsp-arr[i]; 11 | } 12 | else{ 13 | nbsp=obsp; 14 | } 15 | 16 | 17 | // sell 18 | if(obsp+arr[i]>ossp) { 19 | nssp=obsp+arr[i]; 20 | } 21 | else{ 22 | nssp=ossp; 23 | } 24 | 25 | 26 | // cooldown 27 | if(ossp>ocsp) { 28 | ncsp=ossp; 29 | } 30 | else{ 31 | ncsp=ocsp; 32 | } 33 | 34 | obsp=nbsp; 35 | ossp=nssp; 36 | ocsp=ncsp; 37 | 38 | } 39 | 40 | return ossp; 41 | } 42 | 43 | 44 | int main(){ 45 | int n; 46 | cin >> n; 47 | int arr[n]; 48 | for(int i=0;i> arr[i]; 50 | } 51 | cout << infiTranswithCooldown(arr,n); 52 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Find Itinerary From Tickets.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | int n;cin>> n; 7 | unordered_map map; 8 | for(int i=0;i>s1>>s2; 10 | map[s1]=s2; 11 | } 12 | 13 | unordered_map path; 14 | 15 | for(auto m:map){ 16 | string s1 = m.first,s2=m.second; 17 | 18 | if(path.find(s1)==path.end()){ 19 | path[s1]=true; 20 | } 21 | path[s2]=false; 22 | } 23 | string src=""; 24 | for(auto m:path){ 25 | if(m.second==true){ 26 | src=m.first; 27 | break; 28 | } 29 | } 30 | string pa=src; 31 | while(map.find(src)!=map.end()){ 32 | string ndes = map[src]; 33 | pa += " -> "+ ndes; 34 | src = ndes; 35 | } 36 | 37 | cout << pa << "." << endl; 38 | 39 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/First-and-Last-Index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | int *arr = new int[n]; 9 | for (int i = 0; i < n; i++) 10 | { 11 | cin >> arr[i]; 12 | } 13 | int d; 14 | cin >> d; 15 | 16 | int count = 0, str = -1, end = -1; 17 | for (int i = 0; i < n; i++) 18 | { 19 | if (arr[i] == d) 20 | { 21 | str = i; 22 | for (int j = i + 1; j < n; j++) 23 | { 24 | if (arr[j] == d) 25 | { 26 | count++; 27 | } 28 | else 29 | { 30 | break; 31 | } 32 | } 33 | end = str + count; 34 | break; 35 | } 36 | } 37 | cout << str << endl 38 | << end << endl; 39 | // write your code here 40 | } -------------------------------------------------------------------------------- /Level-2/23. Dynamic Programming/Count Palindromic Substrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int countSubstrings(string s) { 4 | int n=s.length(); 5 | int cnt=0; 6 | vector> dp(n,vector(n,false)); 7 | for(int d=0;d> s; 29 | cout << countSubstrings(s) << endl; 30 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Abbreviation Using Backtracking.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void getabbreviations(string s, int idx, string asf, int ssf) 5 | { 6 | if (idx == s.length()) 7 | { 8 | if (ssf == 0) 9 | { 10 | cout << asf << endl; 11 | } 12 | else 13 | { 14 | cout << asf << ssf << endl; 15 | } 16 | return; 17 | } 18 | if (ssf != 0) 19 | { 20 | getabbreviations(s, idx + 1, asf + to_string(ssf) + s[idx], 0); 21 | } 22 | else 23 | { 24 | getabbreviations(s, idx + 1, asf + s[idx], 0); 25 | } 26 | 27 | // if(ssf!=0){ 28 | // asf=asf.substr(0,asf.length()-1); 29 | // } 30 | // ssf=ssf+1; 31 | getabbreviations(s, idx + 1, asf, ssf + 1); 32 | } 33 | 34 | int main() 35 | { 36 | string s; 37 | cin >> s; 38 | getabbreviations(s, 0, "", 0); 39 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words - K Length Words - 3.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | void wordklength3(int idx,int ssf,string s,map &mp,int ts,vector &get){ 5 | if(idx == s.length()){ 6 | if(ssf == ts){ 7 | for(auto x:get){ 8 | cout << x; 9 | } 10 | cout << endl; 11 | } 12 | return; 13 | } 14 | 15 | 16 | char ch=s[idx]; 17 | int locs=mp[ch]; 18 | 19 | for(int i=locs+1;i> s >> n; 38 | map mp; 39 | for(char ch:s){ 40 | mp[ch]=-1; 41 | } 42 | vector get(n,'\0'); 43 | wordklength3(0,0,s,mp,n,get); 44 | 45 | 46 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Partition-into-Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | ll partitionintoSubsets(ll n, ll k) 6 | { 7 | ll dp[n + 1][k + 1]; 8 | for (ll i = 0; i <= n; i++) 9 | { 10 | for (ll j = 0; j <= k; j++) 11 | { 12 | if (i == 0 || j == 0) 13 | { 14 | dp[i][j] = 0; 15 | } 16 | else if (i == j || j == 1) 17 | { 18 | dp[i][j] = 1; 19 | } 20 | else if (i < j) 21 | { 22 | dp[i][j] = 0; 23 | } 24 | else 25 | { 26 | dp[i][j] = dp[i - 1][j] * j + dp[i - 1][j - 1]; 27 | } 28 | } 29 | } 30 | return dp[n][k]; 31 | } 32 | 33 | int main() 34 | { 35 | ll n, k; 36 | cin >> n >> k; 37 | cout << partitionintoSubsets(n, k); 38 | } -------------------------------------------------------------------------------- /Level-1/13. Stack and Queues/1. Duplicate-Brackets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool duplicateBrackets(string s){ 5 | stack st; 6 | bool check=true; 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | void printMazePaths(int sr, int sc, int dr, int dc, string psf) { 6 | // write your code here 7 | if(sr==dr && sc==dc){ 8 | cout << psf << endl; 9 | } 10 | if(sr>dr || sc>dc){ 11 | return; 12 | } 13 | string a="h"; 14 | for(int i=1;sc+i<=dc;i++){ 15 | string x=to_string(i); 16 | printMazePaths(sr,sc+i,dr,dc,psf+a+x); 17 | } 18 | string b="v"; 19 | for(int j=1;sr+j<=dr;j++){ 20 | string y=to_string(j); 21 | printMazePaths(sr+j,sc,dr,dc,psf+b+y); 22 | } 23 | string c="d"; 24 | for(int k=1;sr+k<=dr && sc+k<=dc;k++){ 25 | string z=to_string(k); 26 | printMazePaths(sr+k,sc+k,dr,dc,psf+c+z); 27 | } 28 | } 29 | 30 | int main() { 31 | int n ; 32 | int m ;cin>>n>>m; 33 | printMazePaths(0, 0, n - 1, m - 1, ""); 34 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 1 3 | 1 2 2 1 4 | 1 2 3 3 2 1 5 | 1 2 3 4 4 3 2 1 6 | 1 2 3 4 5 5 4 3 2 1 7 | 1 2 3 4 5 6 6 5 4 3 2 1 8 | 1 2 3 4 5 6 7 6 5 4 3 2 1 9 | */ 10 | #include 11 | using namespace std; 12 | int main(int argc, char**argv){ 13 | int n; 14 | cin >> n; 15 | int c=2*n-1; 16 | for(int r=1;r<=n;r++){ 17 | int str=r; 18 | int nsp=c-2*str; 19 | int num=1; 20 | for(int i=1;i<=str;i++){ 21 | cout << num << "\t"; 22 | num++; 23 | } 24 | for(int i=1;i<=nsp;i++){ 25 | cout << "\t"; 26 | } 27 | if(r==n){ 28 | num--; 29 | str--; 30 | } 31 | for(int i=1;i<=str;i++){ 32 | num--; 33 | cout << num <<"\t"; 34 | 35 | } 36 | cout <<"\n"; 37 | } 38 | 39 | //write your code here 40 | 41 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/java/Subarray_problem.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int arr[] = new int[n]; 11 | for (int i = 0; i < n; i++) { 12 | arr[i] = scn.nextInt(); 13 | } 14 | subarray(arr); 15 | } 16 | public static void subarray(int arr[]) { 17 | int str = 0; 18 | while (str < arr.length) { 19 | int end = str; 20 | while (end < arr.length) { 21 | for (int i = str; i <= end; i++) { 22 | System.out.print(arr[i] + "\t"); 23 | } 24 | System.out.println(); 25 | end++; 26 | } 27 | // System.out.println(); 28 | str++; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Level-1/5. 2-D Arrays/java/The_State_of_Wakanda1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner scn = new Scanner(System.in); 9 | int n = scn.nextInt(); 10 | int m = scn.nextInt(); 11 | int arr[][] = new int[n][m]; 12 | for (int i = 0; i < n; i++) { 13 | for (int j = 0; j < m; j++) { 14 | arr[i][j] = scn.nextInt(); 15 | } 16 | } 17 | for (int j = 0; j < m; j++) { 18 | if ((j + 1) % 2 == 0) { 19 | for (int i = n - 1; i >= 0; i--) { 20 | System.out.println(arr[i][j]); 21 | } 22 | } else { 23 | for (int i = 0; i < n; i++) { 24 | System.out.println(arr[i][j]); 25 | } 26 | } 27 | } 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/IsANumberPrime.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class IsANumberPrime { 4 | 5 | public static void main(String[] args) { 6 | Scanner scn = new Scanner(System.in); 7 | int t = scn.nextInt(); 8 | 9 | // write ur code here 10 | for (int j = 0; j < t; j++) { 11 | int n = scn.nextInt(); 12 | int flag = 0; 13 | if (n == 2) { 14 | System.out.println("prime"); 15 | } else { 16 | for (int i = 2; i * i <= n; i++) { 17 | if (n % i == 0) { 18 | flag = 1; 19 | break; 20 | } 21 | } 22 | if (flag == 1) { 23 | System.out.println("not prime"); 24 | } else { 25 | System.out.println("prime"); 26 | } 27 | } 28 | } 29 | 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Level-1/13. Stack and Queues/6. Sliding-Window-Maximum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | vector nextGreateronright(ll *arr,ll n){ 5 | vector v(n); 6 | stack st; 7 | st.push(n-1); 8 | v[n-1]=n; 9 | for(ll i=n-2;i>=0;i--){ 10 | while(st.size()>0 && arr[st.top()] ngor=nextGreateronright(arr,n); 28 | // vector vec(n-k+1); 29 | for(ll i=0;i<=n-k;i++){ 30 | int idx=i; 31 | while(ngor[idx]> n; 42 | ll arr[n]; 43 | for(ll i=0;i> arr[i]; 45 | } 46 | cin >> k; 47 | swm(arr,n,k); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-5.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * * 4 | * * * * * 5 | * * * 6 | * 7 | */ 8 | import java.util.*; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Scanner scn = new Scanner(System.in); 14 | int n=scn.nextInt(); 15 | int str=1; 16 | for(int row=1;row<=n;row++){ 17 | int nsp=(n-str)/2; 18 | for(int i=1;i<=nsp;i++){ 19 | System.out.print("\t"); 20 | } 21 | for(int j=1;j<=str;j++){ 22 | System.out.print("*\t"); 23 | } 24 | for(int i=1;i<=nsp;i++){ 25 | System.out.print("\t"); 26 | } 27 | if(row<((n/2)+1)){ 28 | str=str+2; 29 | } 30 | if(row>=((n/2)+1)){ 31 | str=str-2; 32 | } 33 | System.out.println(); 34 | } 35 | // write ur code here 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Climb-stairs-with-variable-jumps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // memoization 5 | int csvjmem(int src,int n,int *jumps,int *dp){ 6 | if(src==n){ 7 | return 1; 8 | } 9 | if(dp[src]!=-1){ 10 | return dp[src]; 11 | } 12 | int ans=0; 13 | for(int i=1;i<=jumps[src];i++){ 14 | ans+=csvjmem(src+i,n,jumps,dp); 15 | } 16 | dp[src]=ans; 17 | return ans; 18 | } 19 | 20 | //tabular method 21 | int cstab(int *arr,int src, int n,int *a){ 22 | a[n]=1; 23 | for(int i=n-1;i>=0;i--){ 24 | int ans=0; 25 | for(int j=1;j<=arr[i] && i+j<=n;j++){ 26 | ans+=a[i+j]; 27 | } 28 | a[i]=ans; 29 | } 30 | return a[0]; 31 | } 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | int arr[n]; 37 | for(int i = 0 ; i < n ;i++){ 38 | cin>>arr[i]; 39 | } 40 | int a[n+1]; 41 | cout<< cstab(arr,0,n,a) << endl; 42 | } -------------------------------------------------------------------------------- /Level-1/5. 2-D Arrays/cpp/Rotate_by_90Degree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | vector> arr(n,vector (n)); 8 | for(int i=0;i> arr[i][j]; 11 | } 12 | } 13 | 14 | //rotation by 90 degree 15 | 16 | //swapping of elements 17 | for(int i=0;i &nba, int ci, int ti) 6 | { 7 | // base case when current item is greater than total items 8 | if (ci > ti) 9 | { 10 | // print the array and return 11 | for (auto x : nba) 12 | { 13 | cout << x << ""; 14 | } 15 | cout << endl; 16 | return; 17 | } 18 | for (int i = 0; i < nba.size(); i++) 19 | { 20 | // if the box is empty then you can place the item only 21 | if (nba[i] == 0) 22 | { 23 | // place the item in that box 24 | nba[i] = ci; 25 | 26 | // call for other vacant boxes 27 | permutations(nba, ci + 1, ti); 28 | // unmark the item from that box so that you can place other items 29 | nba[i] = 0; 30 | } 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | int nboxes, ritems; 37 | cin >> nboxes >> ritems; 38 | vector nba(nboxes, 0); 39 | 40 | permutations(nba, 1, ritems); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Level-1/13. Stack and Queues/13. Merge-Overlapping-Intervals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin >> n; 7 | vector> vec; 8 | stack> s; 9 | for(int i=0;i> a>>b; 12 | vec.push_back(make_pair(a,b)); 13 | } 14 | sort(vec.begin(),vec.end()); 15 | for(auto x:vec){ 16 | if(s.empty()){ 17 | s.push(x); 18 | } 19 | else if(s.top().second >= x.first){ 20 | s.top().second=max(s.top().second,x.second); 21 | }else{ 22 | s.push(x); 23 | } 24 | 25 | } 26 | stack> st; 27 | while(!s.empty()){ 28 | st.push({s.top().first,s.top().second}); 29 | s.pop(); 30 | } 31 | while(!st.empty()){ 32 | cout << st.top().first << " " << st.top().second << endl; 33 | st.pop(); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /Level-1/9. Recursion on the way up/Print_Encodings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | string codes[]={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; 6 | void printEncoding(string str, string asf){ 7 | // write your code here 8 | if(str.length()==0){ 9 | cout << asf << endl; 10 | return; 11 | } 12 | if(str[0]=='0'){ 13 | return; 14 | } 15 | int idx=int(str[0]-'0'); 16 | string emp=""; 17 | string opt=codes[str[0]-'0']; 18 | emp+=opt; 19 | printEncoding(str.substr(1),asf+emp); 20 | if(str.length()>=2){ 21 | string val=str.substr(0,2); 22 | int v=stoi(val); 23 | if(v<=26){ 24 | string get=codes[v]; 25 | printEncoding(str.substr(2),asf+get); 26 | } 27 | } 28 | 29 | 30 | } 31 | 32 | int main(){ 33 | string str; 34 | cin>>str; 35 | printEncoding(str,""); 36 | 37 | } -------------------------------------------------------------------------------- /Level-2/23. Dynamic Programming/Maximum Sum Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // This is a functional problem. You have to complete this function. 6 | // It takes as input an integer array. It should return the required sum. 7 | int maxSum(vector &V) { 8 | // write your code here. 9 | vector dp(V.size()); 10 | int omax = INT_MIN; 11 | 12 | for(int i=0;i>n; 30 | vector A(n); 31 | for (int i = 0; i < n; i++) 32 | cin>>A[i]; 33 | cout<=((n/2)+1)){ 33 | nsp-=2; 34 | } 35 | System.out.print("\n"); 36 | } 37 | // write ur code here 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-20.java: -------------------------------------------------------------------------------- 1 | /*-- 2 | 3 | * * 4 | * * 5 | * * * 6 | * * * * 7 | * * 8 | 9 | --*/ 10 | //code 11 | import java.util.*; 12 | 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | Scanner scn = new Scanner(System.in); 17 | int n=scn.nextInt(); 18 | for(int r=1;r<=n;r++){ 19 | for(int c=1;c<=n;c++){ 20 | if(c==1 || c==n){ 21 | System.out.print("*\t"); 22 | } 23 | else if(r==c && r>(n/2)){ 24 | System.out.print("*\t"); 25 | } 26 | else if((r+c==n+1) && (r>=(n/2)+1)){ 27 | System.out.print("*\t"); 28 | } 29 | else{ 30 | System.out.print("\t"); 31 | } 32 | 33 | } 34 | 35 | System.out.println(); 36 | } 37 | // write ur code here 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of Subarrays With Equal Number Of 0s 1s And 2s.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | unordered_map map; 6 | int n=v.size(); 7 | int ans=0,c0=0,c1=0,c2=0; 8 | string st = to_string(c1-c0)+"@"+to_string(c2-c1); 9 | map[st]=1; 10 | for(int i=0;i> n; 36 | vector arr(n); 37 | for(int i=0;i> arr[i]; 39 | } 40 | cout << solution(arr) << endl; 41 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Longest Subarray With Equal Number Of 0s 1s And 2s.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(vector &v){ 5 | unordered_map map; 6 | int n=v.size(); 7 | int ans=0,c0=0,c1=0,c2=0; 8 | string st = to_string(c1-c0)+"@"+to_string(c2-c1); 9 | map[st]=-1; 10 | for(int i=0;i> n; 36 | vector arr(n); 37 | for(int i=0;i> arr[i]; 39 | } 40 | cout << solution(arr) << endl; 41 | } -------------------------------------------------------------------------------- /Level-1/1. Getting Started/java/RotateANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner scn = new Scanner(System.in); 7 | // write your code here. 8 | int n = scn.nextInt(); 9 | int k = scn.nextInt(); 10 | int count = 0, sum = 0; 11 | int tem = n; 12 | while (tem > 0) { 13 | tem = tem / 10; 14 | count++; 15 | } 16 | // System.out.println(count); 17 | k = k % count; 18 | if (k < 0) { 19 | // k=Math.abs(k)%count; 20 | k = k + count; 21 | } 22 | int temp = k; 23 | while (k > 0) { 24 | int rem = n % 10; 25 | sum = sum * 10 + rem; 26 | n /= 10; 27 | k--; 28 | } 29 | while (temp > 0) { 30 | System.out.print(sum % 10); 31 | sum /= 10; 32 | temp--; 33 | } 34 | System.out.println(n); 35 | } 36 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Maximum Consecutive Ones - 1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(int nums[],int n,int k){ 5 | int i=-1,j=-1,c0=0,ans=0; 6 | // unordered_map mp; 7 | 8 | while(i < n-1){ 9 | //acquire 10 | while(i < n-1){ 11 | i++; 12 | if(nums[i]==0){ 13 | c0++; 14 | } 15 | 16 | if(c0 <= 1){ 17 | ans=max(ans,i-j); 18 | } 19 | else{ 20 | break; 21 | } 22 | } 23 | 24 | // release 25 | while(j 1){ 26 | j++; 27 | if(nums[j]==0){ 28 | c0--; 29 | } 30 | } 31 | } 32 | 33 | return ans; 34 | } 35 | 36 | int main(){ 37 | int n; 38 | cin >> n; 39 | int arr[n]; 40 | for(int i=0;i> arr[i]; 42 | } 43 | 44 | cout << solution(arr,n,1) << endl; 45 | } -------------------------------------------------------------------------------- /Level-2/23. Dynamic Programming/Maximum Length Of Pair Chain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findLongestChain(vector> &v){ 5 | //write your code here 6 | int n = v.size(); 7 | 8 | // if(n==1) return 1; 9 | sort(v.begin(),v.end(),[&](const auto&a ,const auto &b){ 10 | return a[1] < b[1]; 11 | }); 12 | 13 | 14 | 15 | 16 | // sort(v.begin(), v.end(), cmp); 17 | int cnt = 0; 18 | for (int i = 0, j = 0; j < v.size(); j++) { 19 | if (j == 0 || v[i][1] < v[j][0]) { 20 | cnt++; 21 | i = j; 22 | } 23 | } 24 | return cnt; 25 | } 26 | 27 | int main(int argc, char** argv){ 28 | int n; 29 | cin>>n; 30 | 31 | vector > pairs(n, vector (2)); 32 | for (int i = 0; i < pairs.size(); i++) { 33 | cin>>pairs[i][0]; 34 | cin>>pairs[i][1]; 35 | } 36 | cout< 14 | using namespace std; 15 | int main(int agrc, char**argv){ 16 | int n; 17 | cin >> n; 18 | int ns1=n/2; 19 | int ns2=n/2; 20 | int str=0; 21 | for(int r=1;r<=n;r++){ 22 | if(r==(n/2)+1){ 23 | ns1=0; 24 | str=n; 25 | } 26 | else{ 27 | ns1=n/2; 28 | str=n-ns1-ns2; 29 | } 30 | for(int i=1;i<=ns1;i++){ 31 | cout <<"\t"; 32 | } 33 | for(int i=1;i<=str;i++){ 34 | cout <<"*\t"; 35 | } 36 | for(int i=1;i<=ns2;i++){ 37 | cout <<"\t"; 38 | } 39 | 40 | if(r<(n/2)+1){ 41 | ns2--; 42 | } 43 | if(r>=(n/2)+1){ 44 | ns2++; 45 | } 46 | cout <<"\n"; 47 | } 48 | 49 | //write your code here 50 | 51 | } -------------------------------------------------------------------------------- /Level-2/19. LinkedList-2/Middle of LinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ListNode 5 | { 6 | public: 7 | int val = 0; 8 | ListNode *next = nullptr; 9 | 10 | ListNode(int val) 11 | { 12 | this->val = val; 13 | } 14 | }; 15 | 16 | ListNode *midNode(ListNode *head) 17 | { 18 | ListNode* slow=head , *fast=head; 19 | while(fast && fast->next && fast->next->next){ 20 | slow=slow->next; 21 | fast=fast->next->next; 22 | } 23 | return slow; 24 | } 25 | 26 | int main() 27 | { 28 | int n; 29 | cin >> n; 30 | ListNode *dummy = new ListNode(-1); 31 | ListNode *prev = dummy; 32 | while (n-- > 0) 33 | { 34 | int val; 35 | cin >> val; 36 | prev->next = new ListNode(val); 37 | prev = prev->next; 38 | } 39 | 40 | ListNode *head = midNode(dummy->next); 41 | while (head != nullptr) 42 | { 43 | cout << head->val << " "; 44 | head = head->next; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Queens-Permutation-2D-As-2D-BoxChooses.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | // qpsf->queen placed so far 4 | // tq->total queen 5 | // asf->ans so far 6 | void queenpermutation(vector &queens,int qpsf,int N,string asf,int r,int c){ 7 | if(r==N){ 8 | if(qpsf==N){ 9 | cout << asf+"\n" << endl; 10 | } 11 | return; 12 | } 13 | 14 | for(int q=0;q> n; 38 | vector queens(n,false); 39 | queenpermutation(queens,0,n,"",0,0); 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Largest Subarray With Contiguous Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solution(int arr[],int n){ 5 | int ans=0; 6 | for(int i=0;i st; 11 | 12 | for(int j=i;j> n; 38 | int arr[n]; 39 | for(int i=0;i> arr[i]; 41 | } 42 | 43 | cout << solution(arr,n) << endl; 44 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Maximum Consecutive Ones - 2.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int solution(int nums[],int n,int k){ 5 | int i=-1,j=-1,c0=0,ans=0; 6 | // unordered_map mp; 7 | 8 | while(i < n-1){ 9 | //acquire 10 | while(i < n-1){ 11 | i++; 12 | if(nums[i]==0){ 13 | c0++; 14 | } 15 | 16 | if(c0 <= k){ 17 | ans=max(ans,i-j); 18 | } 19 | else{ 20 | break; 21 | } 22 | } 23 | 24 | // release 25 | while(j k){ 26 | j++; 27 | if(nums[j]==0){ 28 | c0--; 29 | } 30 | } 31 | } 32 | 33 | return ans; 34 | } 35 | 36 | int main(){ 37 | int n; 38 | cin >> n; 39 | int arr[n]; 40 | for(int i=0;i> arr[i]; 42 | } 43 | int k;cin>>k; 44 | cout << solution(arr,n,k) << endl; 45 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-13.java: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 1 1 4 | 1 2 1 5 | 1 3 3 1 6 | 1 4 6 4 1 7 | */ 8 | import java.util.*; 9 | public class Main { 10 | public static void main(String[] args) { 11 | Scanner scn = new Scanner(System.in); 12 | int n=scn.nextInt(); 13 | for(int r=1;r<=n;r++){ 14 | for(int c=1;c<=r;c++){ 15 | System.out.print(permutation(r-1,c-1)+"\t"); 16 | } 17 | System.out.print("\n"); 18 | } 19 | //write your code here 20 | 21 | } 22 | public static int permutation(int n,int r){ 23 | if(r==0 || n==0){ 24 | return 1; 25 | } 26 | if(n==r){ 27 | return 1; 28 | } 29 | int nfact=1,rfact=1,nrfact=1; 30 | for(int i=1;i<=n;i++){ 31 | nfact*=i; 32 | } 33 | for(int i=1;i<=n-r;i++){ 34 | nrfact*=i; 35 | } 36 | for(int i=1;i<=r;i++){ 37 | rfact*=i; 38 | } 39 | return nfact/(rfact*nrfact); 40 | } 41 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/java/Inverse_of_an_Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void display(int[] a) { 6 | StringBuilder sb = new StringBuilder(); 7 | 8 | for (int val: a) { 9 | sb.append(val + " 10 | "); 11 | } 12 | System.out.println(sb); 13 | } 14 | 15 | public static int[] inverse(int[] a) { 16 | // write your code here 17 | int n=a.length; 18 | int b[]=new int[n]; 19 | for(int i=0;i &box,int idx,string ustr,int ssf,int ts,string asf){ 6 | // if(idx == ){ 7 | if(ssf == ts){ 8 | cout << asf << endl; 9 | return; 10 | 11 | } 12 | 13 | for(int i=idx+1;i v; // vector is used instead of unordered and set because in some cases the order in which string is given has to be maintained which is lost in both set and unordered set 27 | cin >> s >> n; 28 | for(char ch:s){ 29 | auto it=find(v.begin(),v.end(),ch); 30 | if(it==v.end()){ 31 | v.push_back(ch); 32 | } 33 | } 34 | string ustr=""; 35 | for(auto x:v){ 36 | ustr+=x; 37 | } 38 | vector box(ustr.length(),false); 39 | combination(box,-1,ustr,0,n,""); 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /Level-1/11. Time and Space Complexity/Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void quicksort(int *arr,int s,int e){ 5 | int pvtidx=e; 6 | int pivot=arr[pvtidx]; 7 | if(s<=e){ 8 | cout << "pivot -> " << pivot << endl; 9 | int i=s,j=s; 10 | for(;j<=e;){ 11 | if(arr[j]>pivot){ 12 | j++; 13 | } 14 | else{ 15 | swap(arr[i],arr[j]); 16 | cout << "Swapping " << arr[i] <<" and " << arr[j] << endl; 17 | i++,j++; 18 | } 19 | } 20 | pvtidx=i-1; 21 | cout << "pivot index -> " << pvtidx<< endl; 22 | quicksort(arr,s,pvtidx-1); 23 | quicksort(arr,pvtidx+1,e); 24 | } 25 | } 26 | 27 | 28 | int main(){ 29 | int n,pivt; 30 | cin >> n; 31 | int arr[n]; 32 | for(int i=0;i> arr[i]; 34 | } 35 | 36 | quicksort(arr,0,n-1); 37 | 38 | for(int i=0;i solution(vector &v,int k){ 5 | unordered_map mp; 6 | vector ans; 7 | for(int i=0;i> n; 38 | vector v(n); 39 | for(int i=0;i> v[i]; 41 | } 42 | int k;cin>>k; 43 | vector ans = solution(v,k); 44 | for(auto a:ans){ 45 | cout << a << " "; 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-19.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * * * * 3 | * * 4 | * * * * * 5 | * * 6 | * * * * 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | int main(int argc, char** argv){ 12 | int n; 13 | cin >> n; 14 | // int str=2; 15 | // int nsp2=n/2; 16 | // int nsp1=(n/2)-1; 17 | // int str2=1; 18 | for(int r=1;r<=n;r++){ 19 | for(int j=1;j<=n;j++){ 20 | if(r==1 && (j==n || j<=(n/2)+1)){ 21 | cout << "*\t"; 22 | } 23 | else if(r<=(n/2) && (j==n || j==(n/2)+1)){ 24 | cout <<"*\t"; 25 | } 26 | else if(r==(n/2)+1){ 27 | cout << "*\t"; 28 | } 29 | else if(r>(n/2)+1 && (j==1 || j==(n/2)+1)){ 30 | cout << "*\t"; 31 | } 32 | else if(r==n && (j==1 || j>=(n/2)+1)){ 33 | cout <<"*\t"; 34 | } 35 | else{ 36 | cout <<"\t"; 37 | } 38 | } 39 | cout <<"\n"; 40 | 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Queen-Combinations-2D-As-1D-QueenChooses.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | // qpsf->queen placed so far 4 | // tq->total queen 5 | // asf->ans so far 6 | void queencombinationqueenchoose(vector> &queens,int qpsf,int tq,int lr){ 7 | if(qpsf==tq){ 8 | // if(){ 9 | for(int i=0;i> n; 41 | vector> queens(n,vector(n,false)); 42 | queencombinationqueenchoose(queens,0,n,0); 43 | return 0; 44 | 45 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Queens-Combination-2D-As-2D-QueenChooses.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | // qpsf->queen placed so far 4 | // tq->total queen 5 | // asf->ans so far 6 | void queencombinationqueenchoose(vector> &queens,int qpsf,int tq,int lr,int lc){ 7 | if(qpsf==tq){ 8 | // if(){ 9 | for(int i=0;i> n; 41 | vector> queens(n,vector(n,false)); 42 | queencombinationqueenchoose(queens,0,n,0,-1); 43 | return 0; 44 | 45 | } -------------------------------------------------------------------------------- /Level-1/10. Recursion-with-backtracking/Flood_fill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void floodfill(vector> maze,int sr,int sc,vector> visited,string asf) { 7 | if(sr==maze.size()-1 && sc==maze[0].size()-1){ 8 | cout << asf << endl; 9 | return; 10 | } 11 | if(sr<0 || sc<0 || sr>=maze.size() || sc>=maze[0].size()|| maze[sr][sc] == 1 || visited[sr][sc]==true ){ 12 | return; 13 | } 14 | visited[sr][sc] = true; 15 | floodfill(maze,sr-1,sc,visited,asf+"t"); 16 | floodfill(maze,sr,sc-1,visited,asf+"l"); 17 | floodfill(maze,sr+1,sc,visited,asf+"d"); 18 | floodfill(maze,sr,sc+1,visited,asf+"r"); 19 | visited[sr][sc] = false; 20 | } 21 | 22 | int main() { 23 | int n, m; 24 | cin >> n >> m; 25 | vector < vector < int >> arr(n, vector < int > (m)); 26 | 27 | for (int i = 0; i < n; i++) 28 | for (int j = 0; j < m; j++) 29 | cin >> arr[i][j]; 30 | vector> vis(n,vector (m)); 31 | floodfill(arr,0,0,vis,""); 32 | } -------------------------------------------------------------------------------- /Level-1/11. Time and Space Complexity/Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(vector &arr, int i, int j) { 5 | cout << "Swapping " << arr[i] << " and " << arr[j] << endl; 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | bool isSmaller(vector &arr, int i, int j) { 12 | cout << "Comparing " << arr[i] << " and " << arr[j] << endl; 13 | if (arr[i] < arr[j]) { 14 | return true; 15 | } else { 16 | return false; 17 | } 18 | } 19 | 20 | void bubbleSort(vector &v){ 21 | int n=v.size(); 22 | int i=n-1; 23 | while(i>=0){ 24 | for(int j=0;j> n; 35 | vector v(n); 36 | for(int i=0;i> v[i]; 38 | } 39 | 40 | bubbleSort(v); 41 | // / 42 | for(auto x:v){ 43 | cout << x << endl; 44 | } 45 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Subsets_of_an_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *decToBinary(int n, int len) 5 | { 6 | int *arr = new int[len]; 7 | int i = len - 1; 8 | while (n > 0) 9 | { 10 | int rem = n % 2; 11 | arr[i] = rem; 12 | n = n / 2; 13 | i--; 14 | } 15 | return arr; 16 | } 17 | 18 | void printSubsets(int *arr, int n) 19 | { 20 | int ts = (int)pow(2, n); 21 | for (int d = 0; d < ts; d++) 22 | { 23 | int *subset = decToBinary(d, n); 24 | for (int i = 0; i < n; i++) 25 | { 26 | if (subset[i] == 0) 27 | { 28 | cout << "-\t"; 29 | } 30 | else if (subset[i] == 1) 31 | { 32 | cout << arr[i] << "\t"; 33 | } 34 | } 35 | cout << endl; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | int n; 42 | cin >> n; 43 | int arr[n]; 44 | for (int i = 0; i < n; i++) 45 | { 46 | cin >> arr[i]; 47 | } 48 | printSubsets(arr, n); 49 | } -------------------------------------------------------------------------------- /Level-2/21. Graphs/Minimum Number Of Swaps Required To Sort An Array.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | int minSwaps(vector &v){ 6 | int n=v.size(); 7 | vector> vec; 8 | for(int i=0;i vis(n); 16 | 17 | for(int i=0;i> n; 42 | vector v(n); 43 | for(int i=0;i> v[i]; 45 | } 46 | cout << minSwaps(v) << endl; 47 | 48 | } -------------------------------------------------------------------------------- /Level-2/19. LinkedList-2/Reverse a Linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ListNode 5 | { 6 | public: 7 | int val = 0; 8 | ListNode *next = nullptr; 9 | 10 | ListNode(int val) 11 | { 12 | this->val = val; 13 | } 14 | }; 15 | 16 | ListNode *reverse(ListNode *head) 17 | { 18 | 19 | ListNode *curr = head, *prev = nullptr, *nex = nullptr; 20 | while (curr) 21 | { 22 | nex = curr->next; 23 | curr->next = prev; 24 | prev = curr; 25 | curr = nex; 26 | } 27 | return prev; 28 | } 29 | 30 | int main() 31 | { 32 | int n; 33 | cin >> n; 34 | ListNode *dummy = new ListNode(-1); 35 | ListNode *prev = dummy; 36 | while (n-- > 0) 37 | { 38 | int val; 39 | cin >> val; 40 | prev->next = new ListNode(val); 41 | prev = prev->next; 42 | } 43 | 44 | ListNode *head = reverse(dummy->next); 45 | while (head != nullptr) 46 | { 47 | cout << head->val << " "; 48 | head = head->next; 49 | } 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Level-1/11. Time and Space Complexity/Insertion_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isGreater(vector &arr, int i, int j) { 5 | cout << "Comparing " << arr[i] << " and " << arr[j] << endl; 6 | if (arr[i] < arr[j]) { 7 | return true; 8 | } else { 9 | return false; 10 | } 11 | } 12 | void swap(vector &arr, int i, int j) { 13 | cout << "Swapping " << arr[i] << " and " << arr[j] << endl; 14 | int temp = arr[i]; 15 | arr[i] = arr[j]; 16 | arr[j] = temp; 17 | 18 | } 19 | 20 | void insertionSort(vector &v){ 21 | int n=v.size(); 22 | for(int j=1;j=1;i--){ 24 | if(isGreater(v,i,i-1)){ 25 | swap(v,i-1,i); 26 | } 27 | else{ 28 | break; 29 | } 30 | } 31 | } 32 | } 33 | int main(){ 34 | int n; 35 | cin >> n; 36 | vector v(n); 37 | for(int i=0;i> v[i]; 39 | } 40 | 41 | insertionSort(v); 42 | for(auto x:v){ 43 | cout << x << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 3 2 4 | 3 4 5 4 3 5 | 2 3 2 6 | 1 7 | 8 | */ 9 | #include 10 | using namespace std; 11 | int main(int argc, char**argv){ 12 | int n; 13 | cin >> n; 14 | int str=1; 15 | 16 | for(int row=1;row<=n;row++){ 17 | int num=row; 18 | if(row>(n/2)+1){ 19 | num=n-row+1; 20 | } 21 | int nsp=(n-str)/2; 22 | for(int i=1;i<=nsp;i++){ 23 | cout << "\t"; 24 | } 25 | for(int j=1;j<=str;j++){ 26 | if(j>(str/2)+1){ 27 | num--; 28 | } 29 | cout <=((n/2)+1)){ 42 | str=str-2; 43 | num--; 44 | } 45 | cout <<"\n"; 46 | } 47 | 48 | //write your code here 49 | 50 | } -------------------------------------------------------------------------------- /Level-1/12. Dynamic-Prgramming-and-Greedy/Target-Sum-Subsets-dp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //tabulation 5 | bool targetSumSubset(int *arr,int n,int tar,int sum){ 6 | int dp[n+1][tar+1]; 7 | for(int i=0;i<=n;i++){ 8 | for(int j=0;j<=tar;j++){ 9 | if(j==0){ 10 | dp[i][j]=true; 11 | } 12 | else if(i==0){ 13 | dp[i][j]=false; 14 | } 15 | else{ 16 | bool a=dp[i-1][j]; 17 | bool b=false; 18 | if(j>=arr[i-1]){ 19 | b=dp[i-1][j-arr[i-1]]; 20 | } 21 | dp[i][j]=a || b; 22 | } 23 | } 24 | } 25 | return dp[n][tar]; 26 | 27 | } 28 | 29 | int main(){ 30 | int n,tar; 31 | cin >> n; 32 | int arr[n]; 33 | for(int i=0;i> arr[i]; 35 | } 36 | cin >> tar; 37 | 38 | if(targetSumSubset(arr,n,tar,0)){ 39 | cout << "true" << endl; 40 | } 41 | else{ 42 | cout << "false" << endl; 43 | } 44 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/NQueens-Branch-and-Bound.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | vector colB; 5 | vector regDB; 6 | vector revDB; 7 | 8 | void nqueensbnb(int r,vector> chess,string asf){ 9 | if(r==chess.size()){ 10 | cout << asf << "." << endl; 11 | return; 12 | } 13 | 14 | for(int c=0;c> n; 36 | vector> chess(n,vector(n,0)); 37 | colB.clear();regDB.clear();revDB.clear(); 38 | colB.resize(n,false); 39 | regDB.resize((2*n)-1,false); 40 | revDB.resize((2*n)-1,false); 41 | nqueensbnb(0,chess,""); 42 | return 0; 43 | 44 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of Substrings Having All Unique Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int lengthOfLongestSubstring (string &s) { 7 | //Write your code here 8 | int i=-1,j=-1; 9 | int n=s.length(); 10 | 11 | int ans = 0; 12 | map mp; 13 | while(i1){ 20 | break; 21 | } 22 | else{ 23 | ans += i-j; 24 | } 25 | } 26 | 27 | while(j < i){ 28 | j++; 29 | if(mp[s[j]]==2){ 30 | mp[s[j]]--; 31 | ans += i-j; 32 | break; 33 | } 34 | else{ 35 | mp[s[j]]--; 36 | } 37 | } 38 | } 39 | return ans; 40 | } 41 | 42 | int main(int argc,char** argv){ 43 | string s; 44 | cin>>s; 45 | cout< 2 | using namespace std; 3 | 4 | bool isSmaller(vector &arr, int i, int j) { 5 | cout << "Comparing " << arr[i] << " and " << arr[j] << endl; 6 | if (arr[i] < arr[j]) { 7 | return true; 8 | } else { 9 | return false; 10 | } 11 | } 12 | void swap(vector &arr, int i, int j) { 13 | cout << "Swapping " << arr[i] << " and " << arr[j] << endl; 14 | int temp = arr[i]; 15 | arr[i] = arr[j]; 16 | arr[j] = temp; 17 | 18 | } 19 | 20 | void selectionSort(vector &v){ 21 | for(int i=0;i> n; 36 | vector v(n); 37 | for(int i=0;i> v[i]; 39 | } 40 | 41 | selectionSort(v); 42 | for(auto x:v){ 43 | cout << x << endl; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int lengthOfLongestSubstring (string &s) { 7 | //Write your code here 8 | int i=-1,j=-1; 9 | int n=s.length(); 10 | 11 | int ans =-1; 12 | map mp; 13 | while(i1){ 20 | break; 21 | } 22 | else{ 23 | ans = max(ans,i-j); 24 | } 25 | } 26 | 27 | while(j < i){ 28 | j++; 29 | if(mp[s[j]]==2){ 30 | mp[s[j]]--; 31 | ans = max(ans,i-j); 32 | break; 33 | } 34 | else{ 35 | mp[s[j]]--; 36 | } 37 | } 38 | } 39 | return ans; 40 | } 41 | 42 | int main(int argc,char** argv){ 43 | string s; 44 | cin>>s; 45 | cout< 2 | using namespace std; 3 | 4 | bool wordPattern(string &pattern, string &s) { 5 | stringstream ns(pattern); 6 | stringstream ss(s); 7 | vector v; 8 | string word; 9 | while(ss >> word){ 10 | v.push_back(word); 11 | } 12 | ns >> pattern; 13 | if(v.size() != pattern.size()) return false; 14 | map map; 15 | set st; 16 | for(int i=0;i 0){ 21 | return false; 22 | } 23 | else{ 24 | map[pattern[i]]=v[i]; 25 | st.insert(v[i]); 26 | } 27 | 28 | } 29 | return true; 30 | } 31 | int main(){ 32 | string s; 33 | getline(cin,s); 34 | string t; 35 | getline(cin,t); 36 | // cout << s << endl; 37 | if(wordPattern(s,t)){ 38 | cout << "true" << endl; 39 | } 40 | else{ 41 | cout << "false" << endl; 42 | } 43 | } -------------------------------------------------------------------------------- /Level-1/13. Stack and Queues/7. Celebrity-Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void celebPrblm(vector &v,int n,stack &st){ 5 | while(st.size()>1){ 6 | int v1=st.top(); 7 | st.pop(); 8 | int v2=st.top(); 9 | st.pop(); 10 | 11 | if(v[v1][v2]=='1'){ 12 | st.push(v2); 13 | } 14 | else{ 15 | st.push(v1); 16 | } 17 | } 18 | 19 | for(int i=0;i> n; 40 | vector s; 41 | stack st; 42 | for(int i=0;i> str; 45 | s.push_back(str); 46 | st.push(i); 47 | } 48 | celebPrblm(s,n,st); 49 | } -------------------------------------------------------------------------------- /Level-2/21. Graphs/Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void dfs(vector> &grid,int r,int c,vector> &vis){ 6 | if(r<0 || c<0 || r==grid.size() || c==grid[0].size() || grid[r][c]=='1' || vis[r][c]==true){ 7 | return; 8 | } 9 | vis[r][c]=true; 10 | dfs(grid,r-1,c,vis); 11 | dfs(grid,r,c+1,vis); 12 | dfs(grid,r+1,c,vis); 13 | dfs(grid,r,c-1,vis); 14 | } 15 | 16 | int countIslands(vector> grid){ 17 | int cnt=0; 18 | int m=grid.size(),n=grid[0].size(); 19 | vector> vis(m,vector(n,false)); 20 | for(int i=0;i> m >> n; 34 | vector> grid(m,vector (n)); 35 | for(int i=0;i> ch; 39 | grid[i][j]=(int)(ch); 40 | } 41 | } 42 | 43 | cout << countIslands(grid); 44 | return 0; 45 | 46 | } -------------------------------------------------------------------------------- /Level-2/21. Graphs/Number of Enclaves.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void dfs(vector> &grid,int r,int c){ 5 | if(r<0 || c<0 || r==grid.size() || c==grid[0].size() || grid[r][c]==0){ 6 | return; 7 | } 8 | grid[r][c]=0; 9 | dfs(grid,r-1,c); 10 | dfs(grid,r,c+1); 11 | dfs(grid,r+1,c); 12 | dfs(grid,r,c-1); 13 | } 14 | 15 | int countEnclaves(vector> grid){ 16 | int m=grid.size(),n=grid[0].size(); 17 | for(int i=0;i> m >> n; 42 | vector> grid(m,vector (n)); 43 | for(int i=0;i> grid[i][j]; 46 | } 47 | } 48 | 49 | cout << countEnclaves(grid); 50 | return 0; 51 | 52 | } -------------------------------------------------------------------------------- /Level-2/21. Graphs/Pepcoding Course Schedule.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void courseSchedule(vector> &edges,int v){ 5 | vector inD(v,0); 6 | 7 | vector> g(v); 8 | for(auto edge:edges){ 9 | g[edge[1]].push_back(edge[0]); 10 | 11 | inD[edge[0]]++; 12 | } 13 | 14 | queue q; 15 | for(int i=0;i ans; 21 | 22 | // cout << q.size() << endl; 23 | while(!q.empty()) { 24 | int node = q.front(); 25 | q.pop(); 26 | ans.push_back(node); 27 | for(int nbr : g[node]){ 28 | inD[nbr]--; 29 | 30 | if(inD[nbr]==0) 31 | q.push(nbr); 32 | } 33 | } 34 | // reverse(ans.begin(),ans.end()); 35 | if(ans.size()!=v){ 36 | cout << -1 << endl; 37 | return; 38 | } 39 | for(auto a:ans){ 40 | cout << a << " "; 41 | } 42 | cout << endl; 43 | 44 | } 45 | 46 | 47 | 48 | 49 | int main(){ 50 | int v,e; 51 | cin>>v>>e; 52 | vector> edges(e,vector (2)); 53 | for(int i=0;i> edges[i][0] >> edges[i][1]; 56 | } 57 | 58 | courseSchedule(edges,v); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Level-1/18. Graphs/Perfect-Friends.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void getall(vector> graph,int src,vector &v,vector &x){ 5 | x[src]=true; 6 | v.push_back(src); 7 | for(int y:graph[src]){ 8 | if(!x[y]) { 9 | getall(graph,y,v,x); 10 | } 11 | } 12 | } 13 | int perfectfriends(vector> &graph){ 14 | vector vis(graph.size(),false); 15 | vector vec; 16 | for(int i=0;i v; 18 | getall(graph,i,v,vis); 19 | vec.push_back(v.size()); 20 | 21 | } 22 | int count=0; 23 | for(int i=0;i> v >> e; 34 | vector> graph(v,vector()); 35 | for(int i=0;i> v1 >> v2; 38 | 39 | graph[v1].push_back(v2); 40 | graph[v2].push_back(v1); 41 | } 42 | cout << perfectfriends(graph) << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Level-2/20. Tree-2/Validate Bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class TreeNode 6 | { 7 | public: 8 | int val = 0; 9 | TreeNode *left = nullptr; 10 | TreeNode *right = nullptr; 11 | 12 | TreeNode(int val) 13 | { 14 | this->val = val; 15 | } 16 | }; 17 | 18 | bool isValidBST(TreeNode *root) 19 | { 20 | return true; 21 | } 22 | 23 | TreeNode *createTree(vector &arr, vector &IDX) 24 | { 25 | if (IDX[0] > arr.size() || arr[IDX[0]] == -1) 26 | { 27 | IDX[0]++; 28 | return nullptr; 29 | } 30 | 31 | TreeNode *node = new TreeNode(arr[IDX[0]++]); 32 | node->left = createTree(arr, IDX); 33 | node->right = createTree(arr, IDX); 34 | 35 | return node; 36 | } 37 | 38 | void solve() 39 | { 40 | int n; 41 | cin >> n; 42 | vector arr(n, 0); 43 | for (int i = 0; i < n; i++) 44 | { 45 | cin >> arr[i]; 46 | } 47 | 48 | vector IDX(1, 0); 49 | TreeNode *root = createTree(arr, IDX); 50 | cout << (boolalpha) << isValidBST(root); 51 | } 52 | 53 | int main() 54 | { 55 | solve(); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * * * * * * * 3 | * * 4 | * * 5 | * 6 | * * * 7 | * * * * * 8 | * * * * * * * 9 | 10 | */ 11 | #include 12 | using namespace std; 13 | int main(int agrc, char**argv){ 14 | int n; 15 | cin >> n; 16 | //upper half 17 | for(int r=1;r<=n/2;r++){ 18 | for(int i=1;i<=n;i++){ 19 | if(r==1 || r==n){ 20 | cout <<"*\t"; 21 | } 22 | else if(r==i || r+i==n+1){ 23 | cout <<"*\t"; 24 | } 25 | else{ 26 | cout <<"\t"; 27 | } 28 | } 29 | cout <<"\n"; 30 | } 31 | //lower half pyramid 32 | int spc=n/2; 33 | int str=1; 34 | for(int r=(n/2)+1;r<=n;r++){ 35 | 36 | for(int i=1;i<=spc;i++){ 37 | cout <<"\t"; 38 | } 39 | for(int i=1;i<=str;i++){ 40 | cout <<"*\t"; 41 | } 42 | for(int i=1;i<=spc;i++){ 43 | cout <<"\t"; 44 | } 45 | spc--; 46 | str+=2; 47 | cout <<"\n"; 48 | } 49 | 50 | //write your code here 51 | 52 | } -------------------------------------------------------------------------------- /Level-2/18. Recursion-and-Backtracking/Words-K-Length-Words-1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | // need to print all the permutations 6 | void wordklengthword1(int idx,vector &box,string ustr,int ssf,int ts,vector &ans){ 7 | if(idx==ustr.length()){ 8 | if(ssf==ts){ 9 | for(auto x:ans){ 10 | cout << x << " "; 11 | } 12 | cout << endl; 13 | 14 | } 15 | return; 16 | } 17 | 18 | // include call 19 | for(int i=0;i v; 37 | cin >> s >> n; 38 | for(char ch:s){ 39 | auto it=find(v.begin(),v.end(),ch); 40 | if(it==v.end()){ 41 | v.push_back(ch); 42 | } 43 | } 44 | string ustr=""; 45 | for(auto x:v){ 46 | ustr+=x; 47 | } 48 | vector box(n,false); 49 | vector vec(n); 50 | wordklengthword1(0,box,ustr,0,n,vec); 51 | return 0; 52 | 53 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-16.java: -------------------------------------------------------------------------------- 1 | /* 2 | 1 1 3 | 1 2 2 1 4 | 1 2 3 3 2 1 5 | 1 2 3 4 4 3 2 1 6 | 1 2 3 4 5 5 4 3 2 1 7 | 1 2 3 4 5 6 6 5 4 3 2 1 8 | 1 2 3 4 5 6 7 6 5 4 3 2 1 9 | */ 10 | import java.util.*; 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) { 15 | Scanner scn = new Scanner(System.in); 16 | int n=scn.nextInt(); 17 | int c=2*n-1; 18 | for(int r=1;r<=n;r++){ 19 | int str=r; 20 | int nsp=c-2*str; 21 | int num=1; 22 | for(int i=1;i<=str;i++){ 23 | System.out.print(num+"\t"); 24 | num++; 25 | } 26 | for(int i=1;i<=nsp;i++){ 27 | System.out.print("\t"); 28 | } 29 | if(r==n){ 30 | num--; 31 | str--; 32 | } 33 | for(int i=1;i<=str;i++){ 34 | num--; 35 | System.out.print(num+"\t"); 36 | 37 | } 38 | System.out.print("\n"); 39 | } 40 | 41 | // write ur code here 42 | 43 | } 44 | } -------------------------------------------------------------------------------- /Level-1/4. Arrays/cpp/Sum_of_Two_Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void sumOfTwoArrays(int arr1[],int arr2[],int n, int m){ 5 | int len=max(n,m)+1; 6 | int arr[len]; 7 | int i=n-1; 8 | int j=m-1; 9 | int k=len-1; 10 | int carry=0; 11 | while(i>=0 || j>=0 || k>=0){ 12 | int sum=0; 13 | if(i>=0){ 14 | sum+=arr1[i]; 15 | } 16 | if(j>=0){ 17 | sum+=arr2[j]; 18 | } 19 | sum+=carry; 20 | int rem=sum%10; 21 | carry=sum/10; 22 | arr[k]=rem; 23 | i--; 24 | j--; 25 | k--; 26 | } 27 | if(arr[0]!=0){ 28 | cout <> n1; 39 | int arr1[n1]; 40 | for(int i=0;i> arr1[i]; 42 | } 43 | cin >> n2; 44 | int arr2[n2]; 45 | for(int i=0;i> arr2[i]; 47 | } 48 | sumOfTwoArrays(arr1,arr2,n1,n2); 49 | return 0; 50 | 51 | } -------------------------------------------------------------------------------- /Level-1/18. Graphs/Order-of-Compilation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Edge{ 5 | int src; 6 | int nbr; 7 | int wt; 8 | // Edge(){ 9 | // 10 | // } 11 | Edge(int src,int nbr,int wt){ 12 | this->src=src; 13 | this->nbr=nbr; 14 | this->wt=wt; 15 | } 16 | }; 17 | stack st; 18 | void orderofcompilation(vector> &graph,int src,vector &vis){ 19 | if(vis[src]){ 20 | return; 21 | } 22 | vis[src]=true; 23 | for(int nr:graph[src]){ 24 | if(!vis[nr]){ 25 | orderofcompilation(graph,nr,vis); 26 | } 27 | } 28 | st.push(src); 29 | } 30 | 31 | 32 | 33 | int main(){ 34 | int v,e; 35 | cin >> v >> e; 36 | vector> graph(v,vector()); 37 | for(int i=0;i> v1 >> v2; 40 | graph[v1].push_back(v2); 41 | } 42 | vector vis(v,false); 43 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | vector> groupShiftedStrings(vector &v){ 6 | map> map; 7 | 8 | for(auto s:v){ 9 | string temp=""; 10 | 11 | for(int i=1;i> ans; 21 | for(auto a:map){ 22 | ans.push_back(a.second); 23 | } 24 | 25 | return ans; 26 | 27 | } 28 | 29 | void display(vector> &list) { 30 | for (auto i:list) { 31 | for (auto j:i) { 32 | cout << j << " "; 33 | } 34 | cout << endl; 35 | } 36 | } 37 | 38 | int main() { 39 | int N; 40 | cin >> N; 41 | vector arr(N); 42 | for (int i = 0; i < N; i++) { 43 | cin >> arr[i]; 44 | } 45 | vector> ans = groupShiftedStrings(arr); 46 | for (auto i:ans) { 47 | sort(i.begin(),i.end()); 48 | } 49 | display(ans); 50 | } -------------------------------------------------------------------------------- /Level-1/18. Graphs/Number-of-Islands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void getislands(vector> &v,vector> &vis,int r,int c){ 5 | if(r<0 || c<0 || r>=v.size() || c>=v[0].size()|| v[r][c]==1 || vis[r][c]==true){ 6 | return; 7 | } 8 | vis[r][c]=true; 9 | getcompo(v,vis,r-1,c); 10 | getcompo(v,vis,r,c+1); 11 | getcompo(v,vis,r+1,c); 12 | getcompo(v,vis,r,c-1); 13 | } 14 | 15 | 16 | int noofislands(vector> v){ 17 | int n=v.size(),m=v[0].size(),count=0; 18 | vector> vis(n,vector(m,false)); 19 | for(int i = 0; i < n; ++i){ 20 | for (int j = 0; j < m; ++j){ 21 | if(!vis[i][j] && v[i][j]==0){ 22 | getislands(v,vis,i,j); 23 | count++; 24 | } 25 | } 26 | } 27 | 28 | return count; 29 | } 30 | 31 | int main(){ 32 | int n,m; 33 | cin >> n >> m; 34 | vector> v(n,vector(m)); 35 | for(int i = 0; i < n; ++i){ 36 | for (int j = 0; j < m; ++j){ 37 | cin >> v[i][j]; 38 | } 39 | } 40 | cout << noofislands(v) << endl; 41 | return 0; 42 | 43 | } -------------------------------------------------------------------------------- /Level-1/5. 2-D Arrays/cpp/SpiralDisplay.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | void spiralDisplay(vector> arr,int n,int m){ 7 | int rmin=0,rmax=n-1,cmin=0,cmax=m-1,count=0; 8 | while(count < (n*m)){ 9 | for(int i=rmin;i<=rmax && count < (n*m);i++){ 10 | cout << arr[i][cmin] << endl; 11 | count++; 12 | } 13 | cmin++; 14 | for(int i=cmin;i<=cmax && count < (n*m);i++){ 15 | cout << arr[rmax][i]<< endl; 16 | count++; 17 | } 18 | rmax--; 19 | for(int i=rmax;i>=rmin && count < (n*m);i--){ 20 | cout << arr[i][cmax]<< endl; 21 | count++; 22 | } 23 | cmax--; 24 | for(int i=cmax;i>=cmin && count < (n*m);i--){ 25 | cout << arr[rmin][i]<< endl; 26 | count++; 27 | } 28 | rmin++; 29 | } 30 | } 31 | 32 | 33 | 34 | int main(){ 35 | int n,m; 36 | cin >> n >> m; 37 | vector> arr(n,vector (m)); 38 | for(int i=0;i> arr[i][j]; 41 | } 42 | } 43 | spiralDisplay(arr,n,m); 44 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Equivalent Subarrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(vector&v){ 5 | set st; 6 | for(auto a:v){ 7 | st.insert(a); 8 | } 9 | int k = st.size(); 10 | 11 | int i=-1,j=-1; 12 | map map; 13 | int ans =0; 14 | int n = v.size(); 15 | 16 | while(i> n; 50 | vector v(n); 51 | for(int i=0;i> v[i]; 53 | } 54 | 55 | cout << solve(v) << endl; 56 | } -------------------------------------------------------------------------------- /Level-2/23. Dynamic Programming/Longest Bitonic Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int longBit(vector &V) { 6 | vector dp1(V.size()); 7 | vector dp2(V.size()); 8 | int omax = 0; 9 | 10 | // lis 11 | for(int i=0;i=0;i--){ 23 | int mx = 0; 24 | for(int j=i+1;j>n; 46 | vector A(n); 47 | for (int i = 0; i < n; i++) 48 | cin >> A[i]; 49 | cout<=(n/2)+1){ 42 | ns2++; 43 | } 44 | System.out.print("\n"); 45 | } 46 | // write ur code here 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of Substrings With Exactly K Unique Characters.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | 5 | int check(string &s,int k){ 6 | int i=-1; 7 | int j=-1; 8 | int ans=0; 9 | int n=s.length(); 10 | if(n==0){ 11 | return 0; 12 | } 13 | map mp; 14 | while(i < n-1){ 15 | // acquire; 16 | while(i < n-1){ 17 | i++; 18 | mp[s[i]]++; 19 | if(mp.size()<=k){ 20 | ans+=(i-j); 21 | } 22 | else { 23 | break; 24 | } 25 | } 26 | while(jk){ 27 | j++; 28 | if(mp[s[j]]==1){ 29 | mp.erase(s[j]); 30 | } 31 | else { 32 | mp[s[j]]--; 33 | } 34 | 35 | if(mp.size()==k){ 36 | ans+=(i-j); 37 | } 38 | } 39 | } 40 | 41 | 42 | return ans; 43 | } 44 | 45 | int longestKSubstr(string s, int k) { 46 | return check(s,k)-check(s,k-1); 47 | } 48 | 49 | int main(){ 50 | string s;cin>>s; 51 | int k;cin>>k; 52 | cout << longestKSubstr(s,k) << endl; 53 | } -------------------------------------------------------------------------------- /Level-1/7. Recursion-with-Arrays/java/All_Indices_of_Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args){ 7 | Scanner scn = new Scanner(System.in); 8 | int n = scn.nextInt(); 9 | int arr[] = new int[n]; 10 | for (int i = 0; i < arr.length; i++) { 11 | arr[i] = scn.nextInt(); 12 | } 13 | int x = scn.nextInt(); 14 | 15 | int res[] = allIndices(arr, x, 0, 0); 16 | 17 | if (res.length != 0) { 18 | for (int val: res) { 19 | System.out.println(val); 20 | } 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int[] allIndices(int[] arr, int x, int idx, int fsf) { 26 | // write ur code here 27 | int res[]=new int[fsf]; 28 | if(idx==arr.length){ 29 | // int res[]=new int[fsf]; 30 | return res; 31 | } 32 | if(arr[idx]==x){ 33 | res=allIndices(arr,x,idx+1,fsf+1); 34 | res[fsf]=idx; 35 | } 36 | else{ 37 | res=allIndices(arr,x,idx+1,fsf); 38 | } 39 | return res; 40 | 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Count Of Substrings Having At Most K Unique Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int lengthOfLongestSubstringKDistinct(string &s, int k) { 6 | // write your code here 7 | int i=-1; 8 | int j=-1; 9 | int ans=0; 10 | int n=s.length(); 11 | if(n==0){ 12 | return 0; 13 | } 14 | map mp; 15 | while(i < n-1){ 16 | // acquire; 17 | while(i < n-1){ 18 | i++; 19 | mp[s[i]]++; 20 | if(mp.size()<=k){ 21 | ans+=(i-j); 22 | } 23 | else { 24 | break; 25 | } 26 | } 27 | while(jk){ 28 | j++; 29 | if(mp[s[j]]==1){ 30 | mp.erase(s[j]); 31 | } 32 | else { 33 | mp[s[j]]--; 34 | } 35 | 36 | if(mp.size()==k){ 37 | ans+=(i-j); 38 | } 39 | } 40 | } 41 | 42 | 43 | return ans; 44 | } 45 | 46 | 47 | 48 | int main(){ 49 | string s; 50 | cin >> s; 51 | int k;cin>>k; 52 | cout << lengthOfLongestSubstringKDistinct(s,k) << endl; 53 | } -------------------------------------------------------------------------------- /Level-2/22. HashMap and Heap/Longest Substring With Exactly K Unique Characters.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int longestKSubstr(string s, int k) { 5 | // your code here 6 | int i=-1; 7 | int j=-1; 8 | int ans=-1; 9 | int n=s.length(); 10 | map mp; 11 | while(i < n-1){ 12 | // cout << n << endl; 13 | while(i < n-1){ 14 | i++; 15 | mp[s[i]]++; 16 | 17 | // cout << "ans" << endl; 18 | 19 | if(mp.size()==k){ 20 | ans=max(ans,i-j); 21 | } 22 | else if(mp.size()>k){ 23 | break; 24 | } 25 | } 26 | while(j>s; 49 | int k;cin>>k; 50 | cout << longestKSubstr(s,k) << endl; 51 | } -------------------------------------------------------------------------------- /Level-1/2. Patterns/cpp/Pattern-10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * 5 | * * 6 | * 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | int n; 13 | cin >> n; 14 | int str=2; 15 | int nsp=n/2; 16 | int ns=-1; 17 | int ext=0; 18 | for(int r=1;r<=n;r++){ 19 | 20 | if((r==1) || (r==n)){ 21 | str=1; 22 | ext=1; 23 | } 24 | else{ 25 | str=2; 26 | ext=0; 27 | } 28 | 29 | 30 | for(int i=1;i<=nsp;i++){ 31 | cout <<"\t"; 32 | } 33 | for(int i=1;i<=str-1+ext;i++){ 34 | cout <<"*\t"; 35 | } 36 | for(int i=1;i<=ns;i++){ 37 | cout <<"\t"; 38 | } 39 | for(int i=1;i<=str-1;i++){ 40 | cout <<"*\t"; 41 | } 42 | // for(int i=1;i<=nsp;i++){ 43 | // cout <<"\t"; 44 | // } 45 | 46 | if(r<((n/2)+1)){ 47 | nsp--; 48 | ns+=2; 49 | } 50 | if(r>=((n/2)+1)){ 51 | nsp++; 52 | ns-=2; 53 | } 54 | cout <<"\n"; 55 | 56 | } 57 | 58 | //write your code here 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Level-1/2. Patterns/java/Pattern-19.java: -------------------------------------------------------------------------------- 1 | /*-- 2 | * * * * 3 | * * 4 | * * * * * 5 | * * 6 | * * * * 7 | 8 | --*/ 9 | import java.util.*; 10 | 11 | public class Main { 12 | 13 | public static void main(String[] args) { 14 | Scanner scn = new Scanner(System.in); 15 | int n=scn.nextInt(); 16 | for(int r=1;r<=n;r++){ 17 | for(int j=1;j<=n;j++){ 18 | if(r==1 && (j==n || j<=(n/2)+1)){ 19 | System.out.print("*\t"); 20 | } 21 | else if(r<=(n/2) && (j==n || j==(n/2)+1)){ 22 | System.out.print("*\t"); 23 | } 24 | else if(r==(n/2)+1){ 25 | System.out.print("*\t"); 26 | } 27 | else if(r>(n/2)+1 && (j==1 || j==(n/2)+1)){ 28 | System.out.print("*\t"); 29 | } 30 | else if(r==n && (j==1 || j>=(n/2)+1)){ 31 | System.out.print("*\t"); 32 | } 33 | else{ 34 | System.out.print("\t"); 35 | } 36 | } 37 | System.out.print("\n"); 38 | } 39 | // write ur code here 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /Level-1/String,StringBuilder-and-ArrayList/Print_All_Palindromic_SubStrings.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void solution(String str) { 7 | //write your code here 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | vector getPath(int n){ 6 | if(n<=0){ 7 | vector v; 8 | v.push_back(""); 9 | return v; 10 | } 11 | vector ouput; 12 | if(n-1>=0){ 13 | vector nm1 = getPath(n-1); 14 | for(int i=0;i=0){ 20 | vector nm2 = getPath(n-2); 21 | for(int i=0;i=0){ 26 | vector nm3 = getPath(n-3); 27 | for(int i=0;i> n; 38 | vector res=getPath(n); 39 | int cnt=0; 40 | cout << "["; 41 | for(string y:res){ 42 | if(cnt!=res.size()-1){ 43 | cout << y << ", "; 44 | cnt++; 45 | } 46 | else{ 47 | cout << y; 48 | } 49 | } 50 | cout <<"]"; 51 | 52 | } --------------------------------------------------------------------------------