├── Assignment1 ├── Test.txt ├── a.out ├── Power.cpp ├── Collecting the balls.cpp └── Sortingskills.cpp ├── Leetcode ├── 718.cpp ├── Test.txt ├── codenation1.cpp └── ARRAY │ ├── Best Time to Buy and Sell Stock.cpp │ ├── Contains Duplicate.cpp │ ├── Two Sum.cpp │ └── 3Sum.cpp ├── Number Theory ├── Test.txt ├── a.out └── prime.cpp ├── String ├── input.txt ├── KMP ├── a.out ├── output.txt ├── 0000000000000183s.zip ├── Zalgo.cpp └── KMP.cpp ├── Dynamic programming 2 ├── Test.txt ├── maxprofit.cpp ├── a.out ├── Knapsack.cpp ├── Shortestsubsequence.cpp ├── LCS.cpp ├── Subsetsum.cpp ├── Editdistance.cpp ├── Miserman.cpp ├── Smallestsuperseq.cpp ├── Charlie.cpp └── Maxprofit.cpp ├── DPandbitmasking ├── Test.txt ├── a.out ├── Mehta.cpp ├── Job.cpp ├── Diliema.cpp ├── Countingstrings.cpp ├── Stringmaker.cpp └── Candy.cpp ├── adhoc problems ├── Test.txt ├── a.out ├── Circularlist.cpp └── Lightupbulbs.cpp ├── Language-Tools ├── test.txt ├── a.out ├── Tripletsum ├── Extractunique ├── Pairsumtozero ├── Warm-reception ├── Different-names ├── tell-thepositions ├── Find-the-Unique-Element ├── Love-for-characters.cpp ├── Find-the-Unique-Element.cpp ├── Different-names.cpp ├── Repeat-and-Missing-Number -Array.cpp ├── Duplicateinarray.cpp ├── 3-zero-sum.cpp ├── Pairsumtozero.cpp ├── tell-thepositions.cpp ├── Warm-reception.cpp └── Tripletsum.cpp ├── NeuralNetwork ├── bin │ └── .gitignore ├── .gitignore ├── obj │ └── main.obj ├── README.md ├── inc │ ├── main.hpp │ └── NeuralNetwork.hpp ├── Makefile └── src │ └── main.cpp ├── Greedy ├── Test.txt ├── a.out ├── Perimeter ├── minabsdiff.cpp ├── Nikunjanddonuts.cpp ├── Activityselection.cpp ├── Perimeter.cpp ├── Weightedjob.cpp ├── Problemdiscussion.cpp └── FractionalKnapsack.cpp ├── Segment Trees ├── Test.txt ├── a.out └── Minimuminsub.cpp ├── Advanced Graphs ├── Test.txt ├── a.out ├── Newyear.cpp └── Monk.cpp ├── Graphs1 ├── a.out ├── Test.txt ├── DFS.cpp ├── Prims MST.cpp ├── 3cycle.cpp ├── Allconnectedcomponents.cpp ├── getpathbfs.cpp ├── largestpiece.cpp ├── Kruskal MST.cpp ├── Board.cpp ├── bfs.cpp ├── strongly_connected_components.cpp ├── isconnected.cpp ├── haspath.cpp └── Islandsc.cpp ├── fileupload ├── static │ └── css │ │ └── app.css ├── templates │ ├── home.html │ ├── upload.html │ └── base.html ├── asgi.py ├── wsgi.py └── urls.py ├── Backtracking ├── a.out ├── Test.txt ├── Rateinamaze.cpp └── Nqueens.cpp ├── Cpp-STL-cheat-sheet.pdf ├── DP1(Very important) ├── a.out ├── Allzeroes ├── Adjacentbit ├── Vanya and gcd ├── angrychildren ├── Test.txt ├── staircase.cpp ├── Loot.cpp ├── mincount.cpp ├── Royandcoin.cpp ├── Maximumsum.cpp ├── Boredom.cpp ├── minchoc.cpp ├── coinchange.cpp ├── Vanya and gcd.cpp ├── angrychildren.cpp ├── Allzeroes.cpp ├── longestbitonicsubarray.cpp └── Hasanandtrip.cpp ├── Interview Prep Books ├── Algorithms_3rd.pdf └── epilight_cpp_new.pdf ├── Searching and sorting applications ├── a.out ├── Murderer ├── Momos market ├── Aggressive cows ├── Inversion Count ├── Distrributecandies ├── Test.txt ├── Inversion Count.cpp ├── Distrributecandies.cpp ├── Murderer.cpp ├── Tajmahalentry.cpp ├── Momos market.cpp └── Aggressive cows.cpp ├── Time-complexity-analysis ├── K-Concatenation ├── Mindbedingproduct.cpp └── K-Concatenation.cpp ├── C++ tricks for competitive programming (for C++ 11) - GeeksforGeeks.pdf ├── SelfCoded ├── 1528. Shuffle String.cpp ├── 1221. Split a String in Balanced Strings.cpp ├── 1486. XOR Operation in an Array.cpp ├── 1313. Decompress Run-Length Encoded List.cpp ├── 771 Jewels and Stones.cpp ├── 1281. Subtract the Product and Sum of Digits of an Integer.cpp ├── 1389. Create Target Array in the Given Order.cpp ├── 1342 Number of Steps to Reduce a Number to Zero.cpp ├── 1512 Number of Good Pairs.cpp ├── 1470 Shuffle the Array.cpp ├── 1295. Find Numbers with Even Number of Digits.cpp ├── 1431 Kids With the Greatest Number of Candies.cpp ├── 1108 Defanging an IP Address.cpp ├── 1365. How Many Numbers Are Smaller Than the Current Number.cpp ├── 1588. Sum of All Odd Length Subarrays.cpp ├── 1534. Count Good Triplets.cpp ├── LinkedList │ ├── print_forward_and_reverse_ll.cpp │ └── 1290. Convert Binary Number in a Linked List to Integer.cpp └── Backtracking │ └── 46. Permutations.cpp ├── Binary Search ├── BS on real values template ├── 1408C- Discrete Acceleration └── 1199C-MP3 ├── Prerequisites ├── Eveenodd.cpp ├── pre3.cpp ├── chakri.cpp └── marbles.cpp ├── Trees ├── Postorder.cpp ├── Inorder Traversal.cpp └── Preorder.cpp ├── Modulo Arithmetic └── Balancesbst.cpp ├── Binary_Search_Program ├── Bit Manipulation └── Unset.cpp └── README.md /Assignment1/Test.txt: -------------------------------------------------------------------------------- 1 | 68 -------------------------------------------------------------------------------- /Leetcode/718.cpp: -------------------------------------------------------------------------------- 1 | NA 2 | -------------------------------------------------------------------------------- /Number Theory/Test.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /String/input.txt: -------------------------------------------------------------------------------- 1 | aaaa -------------------------------------------------------------------------------- /Leetcode/Test.txt: -------------------------------------------------------------------------------- 1 | NA 2 | -------------------------------------------------------------------------------- /Leetcode/codenation1.cpp: -------------------------------------------------------------------------------- 1 | NA 2 | -------------------------------------------------------------------------------- /Dynamic programming 2/Test.txt: -------------------------------------------------------------------------------- 1 | a 2 | ab -------------------------------------------------------------------------------- /Dynamic programming 2/maxprofit.cpp: -------------------------------------------------------------------------------- 1 | jds -------------------------------------------------------------------------------- /DPandbitmasking/Test.txt: -------------------------------------------------------------------------------- 1 | abc 2 | abc 3 | abc -------------------------------------------------------------------------------- /adhoc problems/Test.txt: -------------------------------------------------------------------------------- 1 | 5 1 10 2 | 01000 -------------------------------------------------------------------------------- /Language-Tools/test.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 0 7 2 5 4 7 1 3 6 -------------------------------------------------------------------------------- /NeuralNetwork/bin/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /Greedy/Test.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 3 10 20 3 | 1 2 50 4 | 6 19 100 5 | 2 100 200 6 | -------------------------------------------------------------------------------- /Segment Trees/Test.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 1 8 4 6 8 3 | 4 8 6 3 7 4 | 4 5 | 1 4 6 | 2 4 7 | 3 4 8 | 1 5 -------------------------------------------------------------------------------- /Advanced Graphs/Test.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 5 6 100 3 | 1 2 5 4 | 1 3 5 5 | 1 4 5 6 | 1 5 5 7 | 3 4 8 8 | 4 5 8 -------------------------------------------------------------------------------- /NeuralNetwork/.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | * 3 | !/**/ 4 | !*.* 5 | *.in 6 | *.out 7 | *.o 8 | !Makefile -------------------------------------------------------------------------------- /String/KMP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/String/KMP -------------------------------------------------------------------------------- /Graphs1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Graphs1/a.out -------------------------------------------------------------------------------- /Greedy/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Greedy/a.out -------------------------------------------------------------------------------- /String/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/String/a.out -------------------------------------------------------------------------------- /Greedy/Perimeter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Greedy/Perimeter -------------------------------------------------------------------------------- /fileupload/static/css/app.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #5c848e; 3 | } 4 | h4 5 | { 6 | 7 | } -------------------------------------------------------------------------------- /Assignment1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Assignment1/a.out -------------------------------------------------------------------------------- /Backtracking/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Backtracking/a.out -------------------------------------------------------------------------------- /Graphs1/Test.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 4 4 4 3 | 1 1 4 | 1 2 5 | 3 1 6 | 3 2 7 | 4 4 4 8 | 1 1 9 | 1 2 10 | 3 1 11 | 4 4 -------------------------------------------------------------------------------- /Number Theory/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Number Theory/a.out -------------------------------------------------------------------------------- /Segment Trees/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Segment Trees/a.out -------------------------------------------------------------------------------- /Advanced Graphs/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Advanced Graphs/a.out -------------------------------------------------------------------------------- /DPandbitmasking/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/DPandbitmasking/a.out -------------------------------------------------------------------------------- /Language-Tools/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/a.out -------------------------------------------------------------------------------- /adhoc problems/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/adhoc problems/a.out -------------------------------------------------------------------------------- /Cpp-STL-cheat-sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Cpp-STL-cheat-sheet.pdf -------------------------------------------------------------------------------- /String/output.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | 1 4 | 1 5 | 2 6 | 2 7 | 3 8 | 3 9 | 4 10 | 4 11 | 0 12 | 2 13 | 3 14 | 0 15 | 2 16 | -------------------------------------------------------------------------------- /DP1(Very important)/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/DP1(Very important)/a.out -------------------------------------------------------------------------------- /Language-Tools/Tripletsum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/Tripletsum -------------------------------------------------------------------------------- /NeuralNetwork/obj/main.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/NeuralNetwork/obj/main.obj -------------------------------------------------------------------------------- /DP1(Very important)/Allzeroes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/DP1(Very important)/Allzeroes -------------------------------------------------------------------------------- /Dynamic programming 2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Dynamic programming 2/a.out -------------------------------------------------------------------------------- /Language-Tools/Extractunique: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/Extractunique -------------------------------------------------------------------------------- /Language-Tools/Pairsumtozero: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/Pairsumtozero -------------------------------------------------------------------------------- /Language-Tools/Warm-reception: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/Warm-reception -------------------------------------------------------------------------------- /String/0000000000000183s.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/String/0000000000000183s.zip -------------------------------------------------------------------------------- /DP1(Very important)/Adjacentbit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/DP1(Very important)/Adjacentbit -------------------------------------------------------------------------------- /Language-Tools/Different-names: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/Different-names -------------------------------------------------------------------------------- /fileupload/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block content %} 4 |

Upload System

5 | {% endblock %} -------------------------------------------------------------------------------- /DP1(Very important)/Vanya and gcd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/DP1(Very important)/Vanya and gcd -------------------------------------------------------------------------------- /DP1(Very important)/angrychildren: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/DP1(Very important)/angrychildren -------------------------------------------------------------------------------- /Language-Tools/tell-thepositions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/tell-thepositions -------------------------------------------------------------------------------- /Interview Prep Books/Algorithms_3rd.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Interview Prep Books/Algorithms_3rd.pdf -------------------------------------------------------------------------------- /Language-Tools/Find-the-Unique-Element: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Language-Tools/Find-the-Unique-Element -------------------------------------------------------------------------------- /DP1(Very important)/Test.txt: -------------------------------------------------------------------------------- 1 | 5 4 2 | 1 2 3 5 3 | 3 1 3 2 4 | 4 5 2 3 5 | 5 5 3 2 6 | 4 4 3 4 7 | 6 8 | 1 1 9 | 2 5 10 | 4 5 11 | 3 5 12 | 1 3 13 | 1 5 -------------------------------------------------------------------------------- /Interview Prep Books/epilight_cpp_new.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Interview Prep Books/epilight_cpp_new.pdf -------------------------------------------------------------------------------- /Searching and sorting applications/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Searching and sorting applications/a.out -------------------------------------------------------------------------------- /Time-complexity-analysis/K-Concatenation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Time-complexity-analysis/K-Concatenation -------------------------------------------------------------------------------- /Searching and sorting applications/Murderer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Searching and sorting applications/Murderer -------------------------------------------------------------------------------- /Searching and sorting applications/Momos market: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Searching and sorting applications/Momos market -------------------------------------------------------------------------------- /Searching and sorting applications/Aggressive cows: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Searching and sorting applications/Aggressive cows -------------------------------------------------------------------------------- /Searching and sorting applications/Inversion Count: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Searching and sorting applications/Inversion Count -------------------------------------------------------------------------------- /Searching and sorting applications/Distrributecandies: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/Searching and sorting applications/Distrributecandies -------------------------------------------------------------------------------- /Backtracking/Test.txt: -------------------------------------------------------------------------------- 1 | +-++++++++ 2 | +A++-+++++ 3 | +C------++ 4 | +-++-+++++ 5 | +-++-+++++ 6 | +A++-+++++ 7 | ++++-+++++ 8 | ++++-+++++ 9 | ++++++++++ 10 | --L--L---- 11 | CALIFORNIA;NIGERIA;CANADA;TELAVIV -------------------------------------------------------------------------------- /C++ tricks for competitive programming (for C++ 11) - GeeksforGeeks.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GouravSingh2580/Competitive-Programming/HEAD/C++ tricks for competitive programming (for C++ 11) - GeeksforGeeks.pdf -------------------------------------------------------------------------------- /Searching and sorting applications/Test.txt: -------------------------------------------------------------------------------- 1 | +-++++++++ 2 | +-++++++++ 3 | +-++++++++ 4 | +-----++++ 5 | +-+++-++++ 6 | +-+++-++++ 7 | +++++-++++ 8 | ++------++ 9 | +++++-++++ 10 | +++++-++++ 11 | LONDON;DELHI;ICELAND;ANKARA -------------------------------------------------------------------------------- /NeuralNetwork/README.md: -------------------------------------------------------------------------------- 1 | # Simple Cpp Neural Network 2 | 3 | Simple Cpp Neural Network 4 | 5 | ## Commands: 6 | 7 | Build the project and put the binary in ./bin 8 | 9 | make 10 | 11 | Build and run the project 12 | 13 | make dev 14 | 15 | Only run the project 16 | 17 | make run 18 | -------------------------------------------------------------------------------- /SelfCoded/1528. Shuffle String.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string restoreString(string s, vector& in) { 4 | 5 | string z=s; 6 | 7 | for(int i=0;iUpload 5 |
6 | {% csrf_token %} 7 | 8 | 9 |
10 | {% if url %} 11 |

Uploaded file: {{ url }}

12 | {% endif %} 13 | {% endblock %} -------------------------------------------------------------------------------- /SelfCoded/1486. XOR Operation in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int xorOperation(int n, int start) { 4 | 5 | vector v; 6 | for(int i=0;i decompressRLElist(vector& nums) { 4 | 5 | vector v; 6 | for(int i=1;i 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "NeuralNetwork.hpp" 10 | 11 | #define nl std::cout << std::endl; 12 | 13 | double relu(double x) { 14 | return x > 0 ? x : 0; 15 | } 16 | 17 | double dtanh(double value) { 18 | return -0.5 * ((tanh(value) - 1) * (tanh(value) + 1)); 19 | } 20 | 21 | const char* message = "CPP-PROJECT TEMPLATE"; -------------------------------------------------------------------------------- /SelfCoded/1281. Subtract the Product and Sum of Digits of an Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subtractProductAndSum(int n) { 4 | int z=n; 5 | int x=n; 6 | int r=1; 7 | int m=0; 8 | while(z) 9 | { 10 | r*=z%10; 11 | z=z/10; 12 | } 13 | while(x) 14 | { 15 | m+=x%10; 16 | x=x/10; 17 | } 18 | return (r-m); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /SelfCoded/1389. Create Target Array in the Given Order.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector createTargetArray(vector& nums, vector& index) { 4 | vector v(nums.size(),0); 5 | for(int i=0;i& nums) { 4 | int k=0; 5 | 6 | for(int i=0;i shuffle(vector& nums, int n) { 4 | 5 | vector v; 6 | v.push_back(nums[0]); 7 | int i=0; 8 | int x=1,y=1; 9 | for(int k=0;k& nums) { 4 | int k=0; 5 | int z=0; 6 | for(int i=0;i kidsWithCandies(vector& c, int e) { 4 | vector v; 5 | 6 | for(int i=0;i=*max_element(c.begin(), c.end())) 9 | { 10 | v.push_back(1); 11 | } 12 | else 13 | { 14 | v.push_back(0); 15 | } 16 | } 17 | return v; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /SelfCoded/1108 Defanging an IP Address.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string defangIPaddr(string a) { 4 | 5 | string v; 6 | 7 | for(int i=0;i smallerNumbersThanCurrent(vector& nums) { 4 | int k=0; 5 | vector v; 6 | for(int i=0;i &prices) { 8 | int paisa = 0; 9 | int minPrice = INT_MAX; 10 | for(int i = 0; i < prices.size(); i++){ 11 | minPrice = min(minPrice, prices[i]); 12 | paisa = max(paisa, prices[i] - minPrice); 13 | } 14 | return paisa; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /SelfCoded/1588. Sum of All Odd Length Subarrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumOddLengthSubarrays(vector& arr) { 4 | int sum=0; 5 | int res=0; 6 | vector v; 7 | for(int i=0;i& arr, int a, int b, int c) { 4 | int z=0; 5 | for(int i=0;i 5 | 6 | using namespace std; 7 | 8 | 9 | int main( int argc , char ** argv ) 10 | { 11 | ios_base::sync_with_stdio(false) ; 12 | cin.tie(NULL) ; 13 | 14 | int n; 15 | cin>> n; 16 | int* arr = new int[n]; 17 | int sum1=0, sum2=0; 18 | 19 | for (int i = 0; i < n; ++i) 20 | { 21 | int a; 22 | cin>>a; 23 | arr[i] = a; 24 | if(i%2==0 && a%2==0){ 25 | sum1+=a; 26 | }else if(i%2 != 0 && a%2!=0){ 27 | sum2+=a; 28 | } 29 | } 30 | 31 | cout << sum1<<" "< 4 | 5 | using namespace std; 6 | 7 | 8 | int main( int argc , char ** argv ) 9 | { 10 | ios_base::sync_with_stdio(false) ; 11 | cin.tie(NULL) ; 12 | 13 | int n; 14 | cin>>n; 15 | 16 | int sum = 0; 17 | int** arr = new int*[n]; 18 | for (int i = 0; i < n; ++i) 19 | { 20 | arr[i]=new int[n]; 21 | for (int j = 0; j < n; ++j) 22 | { 23 | cin>>arr[i][j]; 24 | if(i==0 || j==0 || i==j || i==n-1 || j==n-1 || i+j==n-1){ 25 | sum=sum+arr[i][j]; 26 | } 27 | } 28 | } 29 | 30 | cout << sum << '\n'; 31 | 32 | 33 | 34 | return 0 ; 35 | 36 | 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Leetcode/ARRAY/Contains Duplicate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime: 44 ms, faster than 65.08% of C++ online submissions for Contains Duplicate. 3 | Memory Usage: 9 MB, less than 100.00% of C++ online submissions for Contains Duplicate. 4 | */ 5 | 6 | 7 | 8 | 9 | class Solution { 10 | public: 11 | bool containsDuplicate(vector& v) { 12 | sort(v.begin(), v.end()); 13 | 14 | if(v.size()==0) 15 | return false; 16 | 17 | for(int i=0;idata<<"-"; 14 | head = head->next; 15 | } 16 | } 17 | 18 | //This function prints ll in reverse direction 19 | void backwardPrint(struct Node* head) 20 | { 21 | if(head == NULL) return; 22 | backwardPrint(head->next); 23 | cout<data<<"-"; 24 | } 25 | 26 | /* 27 | Sample Input: 28 | 1 29 | 3 30 | 1 31 | 2 32 | 3 33 | 34 | Sample Output: 35 | 1-2-3- 36 | 3-2-1- 37 | */ 38 | -------------------------------------------------------------------------------- /Leetcode/ARRAY/Two Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Runtime: 564 ms, faster than 10.10% of C++ online submissions for Two Sum. 3 | Memory Usage: 7 MB, less than 100.00% of C++ online submissions for Two Sum. 4 | */ 5 | 6 | 7 | 8 | class Solution { 9 | public: 10 | vector twoSum(vector& nums, int target) { 11 | int a=0; 12 | int b=0; 13 | for(int i=0;i 11 | 12 | using namespace std; 13 | 14 | 15 | int main( int argc , char ** argv ) 16 | { 17 | ios_base::sync_with_stdio(false) ; 18 | cin.tie(NULL) ; 19 | 20 | int n; 21 | cin>>n; 22 | int a[n]; 23 | for(int i=0;i>a[i]; 25 | int profit=0; 26 | for(int i=0;i0) && ((a[j]-a[i])>profit)) 31 | { 32 | profit=a[j]-a[i]; 33 | } 34 | } 35 | } 36 | cout< 11 | 12 | using namespace std; 13 | 14 | 15 | int main( int argc , char ** argv ) 16 | { 17 | ios_base::sync_with_stdio(false) ; 18 | cin.tie(NULL) ; 19 | 20 | int n; 21 | cin>>n; 22 | int* arr=new int[n]; 23 | 24 | for (int i = 0; i < n; ++i) 25 | { 26 | cin>>arr[i]; 27 | } 28 | 29 | long double sum=0; 30 | 31 | for (int i = 0; i < n; ++i) 32 | { 33 | sum+=(long double)log10(arr[i]); 34 | } 35 | 36 | for (int i = 0; i < n; ++i) 37 | { 38 | cout << int(pow((long double)10.00, sum-log10(arr[i]))) << " "; 39 | } 40 | 41 | 42 | 43 | return 0 ; 44 | 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Advanced Graphs/Newyear.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int n, t; 14 | cin >> n >> t; 15 | 16 | // int **graph = new int*[n]; 17 | // for(int i = 0; i < n; i++){ 18 | // graph[i] = new int[n]; 19 | // for(int j = 0; j < n; j++) graph[i][j] = 0; 20 | // } 21 | 22 | int position = 0; 23 | 24 | for(int i = 0; i < n-1; i++){ 25 | if(position == t-1){ 26 | cout << "YES" << endl; 27 | return 0; 28 | } 29 | int a; 30 | cin >> a; 31 | // graph[i][i+a] = 1; 32 | if(i == position) position += a; 33 | } 34 | 35 | cout << "NO" << endl; 36 | } 37 | -------------------------------------------------------------------------------- /SelfCoded/Backtracking/46. Permutations.cpp: -------------------------------------------------------------------------------- 1 | // Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. 2 | 3 | // Idk why I love this solution of mine so much ;) 4 | 5 | class Solution { 6 | public: 7 | vector> v; 8 | void permut(vector nums, int l, int r) 9 | { 10 | if(l==r) v.push_back(nums); 11 | else 12 | { 13 | for(int i=l;i<=r;i++) 14 | { 15 | swap(nums[l],nums[i]); 16 | 17 | permut(nums, l+1, r); 18 | 19 | swap(nums[l],nums[i]); 20 | } 21 | } 22 | } 23 | vector> permute(vector& nums) { 24 | permut(nums,0,nums.size()-1); 25 | return v; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Language-Tools/Love-for-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Ayush loves the characters ‘a’, ‘s’, and ‘p’. 11 | He got a string of lowercase letters and he wants to find out how many times characters ‘a’, ‘s’, and ‘p’ 12 | occurs in the string respectively. Help him find it out. 13 | */ 14 | #include 15 | 16 | using namespace std; 17 | 18 | 19 | int main( int argc , char ** argv ) 20 | { 21 | ios_base::sync_with_stdio(false) ; 22 | cin.tie(NULL) ; 23 | 24 | int n; 25 | cin>>n; 26 | string s; 27 | std::cin>>s; 28 | 29 | unordered_map m1; 30 | 31 | for (int i = 0; i < s.size(); ++i) 32 | { 33 | m1[s[i]]++; 34 | } 35 | 36 | cout << m1['a']<<" "<next; 20 | k++; 21 | } 22 | int sum=0; 23 | while(head!=NULL) 24 | { 25 | sum+=pow(2,k-1) * head->val; 26 | head=head->next; 27 | k--; 28 | } 29 | return sum; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Trees/Postorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node* left, *right; 7 | Node(int data) 8 | { 9 | this->data = data; 10 | left = right = NULL; 11 | } 12 | }; 13 | void printPostorder(struct Node* node) 14 | { 15 | if (node == NULL) 16 | return; 17 | 18 | printPostorder(node->left); 19 | printPostorder(node->right); 20 | cout << node->data << " "; 21 | } 22 | 23 | int main() 24 | { 25 | struct Node *root = new Node(1); 26 | root->left = new Node(2); 27 | root->right = new Node(3); 28 | root->right->left = new Node(3); 29 | root->left->left= new Node(4); 30 | root->left->right = new Node(5); 31 | 32 | cout << "\nPostorder traversal of binary tree is \n"; 33 | printPostorder(root); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Modulo Arithmetic/Balancesbst.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | int balancedBTs(int h) { 14 | 15 | if (h==0) 16 | { 17 | return 1; 18 | } 19 | if (h==1) 20 | { 21 | return 1; 22 | } 23 | 24 | long long x = balancedBTs(h-1)%1000000007; 25 | long long y = balancedBTs(h-2)%1000000007; 26 | long long temp = (long long)(((2*x)%1000000007)*(y%1000000007))+(long long)((x%1000000007)*(x%1000000007)); 27 | 28 | long long ans = 0; 29 | ans += ((temp%1000000007))%(1000000007); 30 | 31 | return ans; 32 | 33 | 34 | } 35 | 36 | int main( int argc , char ** argv ) 37 | { 38 | ios_base::sync_with_stdio(false) ; 39 | cin.tie(NULL) ; 40 | 41 | int h; 42 | cin >> h; 43 | int ans = balancedBTs(h); 44 | cout << ans << endl; 45 | 46 | 47 | return 0 ; 48 | 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Trees/Inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node 6 | { 7 | int data; 8 | struct Node* left, *right; 9 | Node(int data) 10 | { 11 | this->data = data; 12 | left = right = NULL; 13 | } 14 | }; 15 | 16 | void printInorder(struct Node* node) 17 | { 18 | if (node == NULL) 19 | return; 20 | 21 | printInorder(node->left); 22 | cout << node->data << " "; 23 | printInorder(node->right); 24 | } 25 | 26 | int main() 27 | { 28 | struct Node *root = new Node(1); 29 | root->left = new Node(2); 30 | root->right->left = new Node(3); 31 | root->left->left = new Node(4); 32 | root->left->right = new Node(5); 33 | 34 | cout << "\nInorder traversal of binary tree is \n"; 35 | printInorder(root); 36 | 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Graphs1/DFS.cpp: -------------------------------------------------------------------------------- 1 | 2 | public: 3 | Graph(int V); 4 | void addEdge(int src, int dest); 5 | void DFS(int vertex); 6 | }; 7 | 8 | // Initialize graph 9 | Graph::Graph(int vertices) { 10 | numVertices = vertices; 11 | adjLists = new list[vertices]; 12 | visited = new bool[vertices]; 13 | } 14 | 15 | // Add edges 16 | void Graph::addEdge(int src, int dest) { 17 | adjLists[src].push_front(dest); 18 | } 19 | 20 | // DFS algorithm 21 | void Graph::DFS(int vertex) { 22 | visited[vertex] = true; 23 | list adjList = adjLists[vertex]; 24 | 25 | cout << vertex << " "; 26 | 27 | list::iterator i; 28 | for (i = adjList.begin(); i != adjList.end(); ++i) 29 | if (!visited[*i]) 30 | DFS(*i); 31 | } 32 | 33 | int main() { 34 | Graph g(4); 35 | g.addEdge(0, 1); 36 | g.addEdge(0, 2); 37 | g.addEdge(1, 2); 38 | g.addEdge(2, 3); 39 | 40 | g.DFS(2); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Trees/Preorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | struct Node 7 | { 8 | int data; 9 | struct Node* left, *right; 10 | Node(int data) 11 | { 12 | this->data = data; 13 | left = right = NULL; 14 | } 15 | }; 16 | 17 | 18 | void printPreorder(struct Node* node) 19 | { 20 | if (node == NULL) 21 | return; 22 | 23 | cout << node->data << " "; 24 | printPreorder(node->left); 25 | printPreorder(node->right); 26 | } 27 | 28 | int main() 29 | { 30 | struct Node *root = new Node(1); 31 | root->left = new Node(2); 32 | root->right = new Node(3); 33 | root->right->left = new Node(3); 34 | root->left->left= new Node(4); 35 | root->left->right = new Node(5); 36 | 37 | cout << "\nPreorder traversal of binary tree is \n"; 38 | printPreorder(root); 39 | 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Assignment1/Power.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Find power of a number 11 | Send Feedback 12 | Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer. 13 | Input format : 14 | Two integers x and n (separated by space) 15 | Output Format : 16 | x^n (i.e. x raise to the power n) 17 | Sample Input 1 : 18 | 3 4 19 | Sample Output 1 : 20 | 81 21 | Sample Input 2 : 22 | 2 5 23 | Sample Output 2 : 24 | 32 25 | */ 26 | 27 | 28 | #include 29 | 30 | using namespace std; 31 | int power(int x, int n){ 32 | if (n==0) 33 | { 34 | return 1; 35 | } 36 | 37 | return x*power(x, n-1); 38 | 39 | 40 | } 41 | 42 | int main( int argc , char ** argv ) 43 | { 44 | ios_base::sync_with_stdio(false) ; 45 | cin.tie(NULL) ; 46 | 47 | int x, n; 48 | cin>>x>>n; 49 | 50 | cout << power(x,n) << '\n'; 51 | 52 | 53 | return 0 ; 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Binary_Search_Program: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int count, i, arr[30], num, first, last, middle; 7 | cout<<"how many elements would you like to enter?:"; 8 | cin>>count; 9 | 10 | for (i=0; i>arr[i]; 14 | } 15 | cout<<"Enter the number that you want to search:"; 16 | cin>>num; 17 | first = 0; 18 | last = count-1; 19 | middle = (first+last)/2; 20 | while (first <= last) 21 | { 22 | if(arr[middle] < num) 23 | { 24 | first = middle + 1; 25 | 26 | } 27 | else if(arr[middle] == num) 28 | { 29 | cout< last) 38 | { 39 | cout< 4 | #include 5 | 6 | #include "Matrix.hpp" 7 | 8 | class NeuralNetwork { 9 | private: 10 | int input_nodes; 11 | int hidden_nodes; 12 | int output_nodes; 13 | Matrix weights_ih; 14 | Matrix weights_ho; 15 | Matrix bias_h; 16 | Matrix bias_o; 17 | 18 | double learningRate = 0.1; 19 | 20 | public: 21 | NeuralNetwork(const NeuralNetwork &); 22 | NeuralNetwork(unsigned, unsigned, unsigned); 23 | NeuralNetwork(); 24 | 25 | unsigned getInputNodes(); 26 | unsigned getHiddenNodes(); 27 | unsigned getOutputNodes(); 28 | 29 | Matrix getWeightsIh(); 30 | Matrix getWeightsHo(); 31 | 32 | Matrix getHiddenBias(); 33 | Matrix getOutputBias(); 34 | 35 | void setLearningRate(double); 36 | 37 | void train(vector, vector); 38 | vector predict(vector); 39 | void mutate(functioncall); 40 | 41 | NeuralNetwork& copy(); 42 | 43 | //Tests 44 | void print(); 45 | }; -------------------------------------------------------------------------------- /Greedy/minabsdiff.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | 12 | Given an integer array A of size N, find and return the minimum absolute difference between 13 | any two elements in the array. 14 | We define the absolute difference between two elements ai, and aj (where i != j ) is |ai - aj|. 15 | Input format : 16 | Line 1 : Integer N, Array Size 17 | Line 2 : Array elements (separated by space) 18 | Output Format : 19 | Minimum difference 20 | */ 21 | 22 | int minAbsoluteDiff(int arr[], int n) { 23 | int minimum = INT_MIN; 24 | 25 | sort(arr, arr+n); 26 | 27 | for (int i = 0; i < n-1; ++i) 28 | { 29 | int temp = arr[i+1]-arr[i]; 30 | minimum = min(temp, minimum); 31 | } 32 | 33 | return minimum; 34 | 35 | } 36 | 37 | 38 | #include 39 | 40 | using namespace std; 41 | 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | 48 | 49 | 50 | 51 | return 0 ; 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /String/Zalgo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | 12 | */ 13 | 14 | #include 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef unordered_map umapii; 19 | typedef unordered_map umapib; 20 | typedef unordered_map umapsi; 21 | typedef unordered_map umapss; 22 | typedef map mapsi; 23 | typedef map, int> mappiii; 24 | typedef map mapii; 25 | typedef pair pii; 26 | typedef pair pll; 27 | typedef unordered_set useti; 28 | 29 | #define uset unordered_set 30 | #define it iterator 31 | #define mp make_pair 32 | #define pb push_back 33 | #define all(x) (x).begin(), (x).end() 34 | #define f first 35 | #define s second 36 | #define MOD 1000000007 37 | 38 | 39 | int main( int argc , char ** argv ) 40 | { 41 | ios_base::sync_with_stdio(false) ; 42 | cin.tie(NULL) ; 43 | 44 | 45 | 46 | 47 | 48 | 49 | return 0 ; 50 | 51 | 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Bit Manipulation/Unset.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | /* 8 | PROBLEM STATEMENT 9 | You are given two integers N and i. You need to make ith bit of binary representation of N to 0 10 | and return the updated N. 11 | Counting of bits start from 0 from right to left. 12 | Input Format : 13 | Two integers N and i (separated by space) 14 | Output Format : 15 | Updated N 16 | Sample Input 1 : 17 | 7 2 18 | Sample Output 1 : 19 | 3 20 | Sample Input 2 : 21 | 12 1 22 | Sample Output 2 : 23 | 12 24 | */ 25 | 26 | #include 27 | 28 | using namespace std; 29 | 30 | int turnOffIthBit(int n, int i){ 31 | // k must be greater than 0 32 | if (i < 0) return n; 33 | 34 | // Do & of n with a number with all set bits except 35 | // the k'th bit 36 | return (n & ~(1 << (i))); 37 | 38 | } 39 | 40 | 41 | int main( int argc , char ** argv ) 42 | { 43 | ios_base::sync_with_stdio(false) ; 44 | cin.tie(NULL) ; 45 | 46 | int n, i; 47 | 48 | cin >> n >> i; 49 | 50 | cout<< turnOffIthBit(n, i) < 11 | using namespace std; 12 | 13 | 14 | void printSolution(int** solution,int n){ 15 | for(int i=0;i=n || x<0 || y>=n || y<0 || maze[x][y] ==0 || solution[x][y] ==1){ 32 | return; 33 | } 34 | solution[x][y] = 1; 35 | mazeHelp(maze,n,solution,x-1,y); 36 | mazeHelp(maze,n,solution,x+1,y); 37 | mazeHelp(maze,n,solution,x,y-1); 38 | mazeHelp(maze,n,solution,x,y+1); 39 | solution[x][y] = 0; 40 | } 41 | void ratInAMaze(int maze[][20], int n){ 42 | 43 | int** solution = new int*[n]; 44 | for(int i=0;i 25 | 26 | using namespace std; 27 | long staircase(int n){ 28 | long* arr = new long[n+1]; 29 | arr[0] = 0; 30 | arr[1] = 1; 31 | arr[2] = 2; 32 | arr[3] = 4; 33 | 34 | for (int i = 4; i < n+1; ++i) 35 | { 36 | arr[i] = arr[i-1] + arr[i-2] + arr[i-3]; 37 | } 38 | 39 | return arr[n]; 40 | 41 | } 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | 48 | int n; 49 | cin >> n ; 50 | cout << staircase(n) << endl; 51 | 52 | 53 | return 0 ; 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /fileupload/urls.py: -------------------------------------------------------------------------------- 1 | """fileupload URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from distribution import views 19 | from django.conf import settings 20 | from django.conf.urls.static import static 21 | urlpatterns = [ 22 | path('admin/', admin.site.urls), 23 | path('', views.Home.as_view(), name='home'), 24 | path('upload/', views.upload, name='upload'), 25 | 26 | 27 | ] 28 | if settings.DEBUG: 29 | urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -------------------------------------------------------------------------------- /Language-Tools/Find-the-Unique-Element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | Given an integer array of size 2N + 1. In this given array, N numbers are present twice and one number 12 | is present only once in the array.You need to find and return that number which is unique in the array. 13 | Note : Given array will always contain odd number of elements. 14 | 15 | */ 16 | 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | int FindUnique(int arr[], int size){ 23 | unordered_map m1; 24 | 25 | for (int i = 0; i < size; ++i) 26 | { 27 | m1[arr[i]]++; 28 | } 29 | 30 | for (int i = 0; i < size; ++i) 31 | { 32 | if (m1[arr[i]]==1) 33 | { 34 | return arr[i]; 35 | } 36 | } 37 | 38 | return 0; 39 | 40 | 41 | } 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | 48 | int size; 49 | 50 | cin>>size; 51 | int *input=new int[1+size]; 52 | 53 | for(int i=0;i>input[i]; 55 | 56 | cout<> threeSum(vector& nums) { 12 | set> ans; 13 | int n= nums.size(); 14 | if(n <3) 15 | return {}; 16 | 17 | sort(nums.begin(), nums.end()); //for using two pointers technique 18 | 19 | for(int i=0; i0 && nums[i] == nums[i-1]) 23 | continue; 24 | int start = i+1, end= n-1; 25 | while(start < end){ 26 | int sum = nums[i] + nums[start]+ nums[end]; 27 | if(sum == 0) 28 | { ans.insert({nums[i],nums[start], nums[end]}); start +=1;} 29 | 30 | else if(sum > 0) 31 | end -=1; 32 | else 33 | start +=1; 34 | 35 | } 36 | } 37 | 38 | vector> yo; 39 | for(auto i= ans.begin(); i != ans.end(); i++) 40 | yo.push_back(*i); 41 | return yo; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /Language-Tools/Different-names.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | In Little Flowers Public School, there are many students with same first names. 10 | You are given a task to find the students with same names. 11 | You will be given a string comprising of all the names of students and you have 12 | to tell the name and count of those students having same. If all the names are unique, print -1 instead. 13 | Note: We don't have to mention names whose frequency is 1. 14 | 15 | */ 16 | 17 | #include 18 | 19 | using namespace std; 20 | 21 | 22 | int main( int argc , char ** argv ) 23 | { 24 | ios_base::sync_with_stdio(false) ; 25 | cin.tie(NULL) ; 26 | 27 | string names; 28 | getline(cin, names); 29 | //cout << names << '\n'; 30 | 31 | stringstream iss(names); 32 | 33 | unordered_map m1; 34 | string temp; 35 | 36 | while(iss>>temp){ 37 | m1[temp]++; 38 | } 39 | 40 | unordered_map :: iterator it=m1.begin(); 41 | int count = 0; 42 | for (it; it != m1.end(); ++it) 43 | { 44 | if(it->second>1){ 45 | cout << it->first<<" "<< it->second << '\n'; 46 | count++; 47 | } 48 | } 49 | if (count == 0) 50 | { 51 | cout << -1 << '\n'; 52 | } 53 | 54 | return 0 ; 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Language-Tools/Repeat-and-Missing-Number -Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | You are given a read only array of n integers from 1 to n. 12 | 13 | Each integer appears exactly once except A which appears twice and B which is missing. 14 | 15 | Return A and B. 16 | 17 | Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 18 | 19 | Note that in your output A should precede B. 20 | 21 | 22 | 23 | 24 | */ 25 | 26 | 27 | 28 | #include 29 | 30 | using namespace std; 31 | vector Solution::repeatedNumber(const vector &A) { 32 | long long int len = A.size(); 33 | long long int Sum_N = (len * (len+1) ) /2, Sum_NSq = (len * (len +1) *(2*len +1) )/6; 34 | long long int missingNumber=0, repeating=0; 35 | 36 | for(int i=0;i ans; 44 | ans.push_back(repeating); 45 | ans.push_back(missingNumber); 46 | return ans; 47 | 48 | 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Graphs1/Prims MST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector taken; 5 | vector > > AdjList; 6 | priority_queue > pq; //Max-Heap in C++ STL 7 | 8 | void process(int vtx) 9 | { 10 | taken[vtx]=1; 11 | for(int j=1; j<(int)AdjList[vtx].size(); j++) 12 | { 13 | pair v=AdjList[vtx][j]; 14 | if(!taken[v.first]) pq.push(make_pair(-v.second,-v.first)); 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | cout<<"Enter the number of vertices: "; 21 | int V; 22 | cin>>V; 23 | vector > temp(1,make_pair(-1,-1)); 24 | for(int i=0;i>E; 28 | cout<<"Enter the edges with their weights: "; 29 | for(int i=0; i>u>>v>>w; 33 | AdjList[u].push_back(make_pair(v,w)); 34 | AdjList[v].push_back(make_pair(u,w)); 35 | } 36 | taken.assign(V,0); 37 | int weight=0; 38 | 39 | process(0); 40 | while(!pq.empty()) 41 | { 42 | pair p=pq.top(); 43 | pq.pop(); 44 | int u=-p.second; 45 | int w=-p.first; 46 | 47 | if(!taken[u]) 48 | weight+=w, process(u); 49 | } 50 | 51 | cout<<"The weight of the MST is: "< 29 | 30 | using namespace std; 31 | 32 | int getMaxMoney(int arr[], int n){ 33 | std::vector dp(n, 0); 34 | 35 | if (n==1) 36 | { 37 | return arr[0]; 38 | } 39 | if (n==2) 40 | { 41 | return max(arr[0], arr[1]); 42 | } 43 | 44 | dp[0] = arr[0]; 45 | dp[1] = max(arr[1], arr[0]); 46 | 47 | for (int i = 2; i < n; ++i) 48 | { 49 | dp[i] =max(dp[i-1], arr[i] + dp[i-2]); 50 | } 51 | 52 | return *max_element(dp.begin(), dp.end()); 53 | 54 | } 55 | 56 | int main( int argc , char ** argv ) 57 | { 58 | ios_base::sync_with_stdio(false) ; 59 | cin.tie(NULL) ; 60 | 61 | int n; 62 | cin >> n; 63 | int arr[10000]; 64 | for(int i=0; i> arr[i]; 66 | } 67 | cout << getMaxMoney(arr, n); 68 | 69 | 70 | 71 | return 0 ; 72 | 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Language-Tools/Duplicateinarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given an array of integers of size n which contains numbers from 0 to n - 2. Each number is present at least once. That is, if n = 5, numbers from 0 to 3 is present in the given array at least once and one number is present twice. You need to find and return that duplicate number present in the array. 11 | Assume, duplicate number is always present in the array. 12 | Input format : 13 | Line 1 : Size of input array 14 | Line 2 : Array elements (separated by space) 15 | Output Format : 16 | Duplicate element 17 | 18 | */ 19 | 20 | 21 | #include 22 | 23 | using namespace std; 24 | int MissingNumber(int arr[], int size){ 25 | unordered_map m1; 26 | for (int i = 0; i < size; ++i) 27 | { 28 | m1[arr[i]]++; 29 | } 30 | 31 | for (int i = 0; i < size; ++i) 32 | { 33 | if (m1[arr[i]] == 2) 34 | { 35 | return arr[i]; 36 | } 37 | } 38 | 39 | return -1; 40 | } 41 | 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | int size; 48 | cin >> size; 49 | int *input = new int[1 + size]; 50 | 51 | for(int i = 0; i < size; i++) 52 | cin >> input[i]; 53 | 54 | cout << MissingNumber(input, size); 55 | 56 | delete [] input; 57 | 58 | return 0; 59 | 60 | 61 | 62 | return 0 ; 63 | 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /Language-Tools/3-zero-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | PROBLEM STATEMENT 9 | Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? 10 | Find all unique triplets in the array which gives the sum of zero. 11 | 12 | Note: 13 | 14 | Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) 15 | The solution set must not contain duplicate triplets. For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: 16 | (-1, 0, 1) 17 | (-1, -1, 2) 18 | 19 | */ 20 | 21 | #include 22 | 23 | using namespace std; 24 | 25 | vector > threeSum(vector &A) { 26 | sort(A.begin(), A.end()); 27 | int n = A.size(); 28 | vector > result; 29 | int c = 0; 30 | for (auto i = 0; i0 && A[i]==A[i-1]) 34 | continue; 35 | 36 | while(l temp = {A[i], A[l], A[r]}; 42 | int s = result.size(); 43 | if(s>0 && result[s-1][0]==temp[0] && result[s-1][1]==temp[1] && result[s-1][2]==temp[2]); 44 | else 45 | result.push_back(temp); 46 | ++l; --r; 47 | } 48 | else if(sum>0) 49 | --r; 50 | else 51 | ++l; 52 | } 53 | } 54 | return result; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Number Theory/prime.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | 12 | */ 13 | 14 | #include 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef unordered_map umapii; 19 | typedef unordered_map umapib; 20 | typedef unordered_map umapsi; 21 | typedef unordered_map umapss; 22 | typedef map mapsi; 23 | typedef map, int> mappiii; 24 | typedef map mapii; 25 | typedef pair pii; 26 | typedef pair pll; 27 | typedef unordered_set useti; 28 | 29 | #define uset unordered_set 30 | #define it iterator 31 | #define mp make_pair 32 | #define pb push_back 33 | #define all(x) (x).begin(), (x).end() 34 | #define f first 35 | #define s second 36 | #define MOD 1000000007 37 | 38 | 39 | int main( int argc , char ** argv ) 40 | { 41 | ios_base::sync_with_stdio(false) ; 42 | cin.tie(NULL) ; 43 | 44 | int n; 45 | cin>>n; 46 | int count = 0; 47 | 48 | if (n>=2) 49 | { 50 | count++; 51 | } 52 | if (n>=3) 53 | { 54 | count++; 55 | } 56 | 57 | //cout << n << '\n'; 58 | for (int i = 2; i <= n; ++i) 59 | { 60 | int flag = 0; 61 | for (int j = 2; j*j <= i; ++j) 62 | { 63 | if (i%j==0) 64 | { 65 | flag = 0; 66 | break; 67 | }else{ 68 | flag = 1; 69 | } 70 | 71 | } 72 | if (flag == 1) 73 | { 74 | count++; 75 | } 76 | } 77 | 78 | cout <u,vectorv) 21 | { 22 | //Creating matrix 23 | int** matrix = new int*[n+1]; 24 | 25 | for(int i=0;i<=n;i++){ 26 | matrix[i] = new int[n+1]; 27 | 28 | for(int j=0;j<=n;j++){ 29 | matrix[i][j]=0; 30 | } 31 | 32 | } 33 | 34 | for(int i=0;i 13 | 14 | using namespace std; 15 | 16 | int board[11][11]; 17 | 18 | bool isPossible(int n,int row,int col){ 19 | 20 | // Same Column 21 | for(int i=row-1;i>=0;i--){ 22 | if(board[i][col] == 1){ 23 | return false; 24 | } 25 | } 26 | //Upper Left Diagonal 27 | for(int i=row-1,j=col-1;i>=0 && j>=0 ; i--,j--){ 28 | if(board[i][j] ==1){ 29 | return false; 30 | } 31 | } 32 | 33 | // Upper Right Diagonal 34 | 35 | for(int i=row-1,j=col+1;i>=0 && j 33 | 34 | using namespace std; 35 | 36 | int knapSack(int W, int wt[], int val[], int n) 37 | { 38 | int i, w; 39 | int K[n+1][W+1]; 40 | 41 | // Build table K[][] in bottom up manner 42 | for (i = 0; i <= n; i++) 43 | { 44 | for (w = 0; w <= W; w++) 45 | { 46 | if (i==0 || w==0) 47 | K[i][w] = 0; 48 | else if (wt[i-1] <= w) 49 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 50 | else 51 | K[i][w] = K[i-1][w]; 52 | } 53 | } 54 | 55 | return K[n][W]; 56 | } 57 | 58 | int knapsack(int* weights, int* values, int n, int maxWeight){ 59 | 60 | return knapSack(maxWeight, weights, values, n); 61 | 62 | 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /adhoc problems/Circularlist.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | You are given a circular list of students as follows: 11 | 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 12 | This list is circular, means that 11 will follow 0 again. You will be given the student number ‘i’ and 13 | some position ‘p’. You will have to tell that if the list will start from (i+1)th student, 14 | then which student will be at pth position. 15 | Input Format: 16 | First line will have an integer ‘t’, denoting the number of test cases. 17 | Next line will have two space separated integers denoting the value of ‘i’ and ‘p’ respectively. 18 | Output Format: 19 | Print ‘t’ lines containing single integer denoting the student number. 20 | Constraints: 21 | 1 <= t <= 10^5 22 | 0 <= i <= 11 23 | 1 <= p <= 12 24 | Sample Input: 25 | 2 26 | 2 3 27 | 5 8 28 | Sample Output: 29 | 5 30 | 1 31 | Explanation: 32 | First, list will start at 3. 3 -> 4 -> 5. Hence, 5 will be at third position. 33 | Second, list will start at 6. 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 0 -> 1. Hence, 1 will be at 8th 34 | position. 35 | */ 36 | 37 | 38 | #include 39 | 40 | using namespace std; 41 | 42 | int go(int i, int p){ 43 | int ans = (i+p)%12 44 | return ans; 45 | } 46 | 47 | 48 | int main( int argc , char ** argv ) 49 | { 50 | ios_base::sync_with_stdio(false) ; 51 | cin.tie(NULL) ; 52 | 53 | int t; 54 | cin>>t; 55 | 56 | while(t--){ 57 | int i, p; 58 | cin>>i>>p; 59 | 60 | cout << go(i, p) << '\n'; 61 | } 62 | 63 | 64 | return 0 ; 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /Prerequisites/marbles.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | int main( int argc , char ** argv ) 16 | { 17 | ios_base::sync_with_stdio(false) ; 18 | cin.tie(NULL) ; 19 | 20 | int n,sum; 21 | 22 | cin>> n>>sum; 23 | 24 | int* arr = new int[n]; 25 | for (int i = 0; i < n; ++i) 26 | { 27 | cin>>arr[i]; 28 | } 29 | 30 | int start=0; 31 | int end=0; 32 | int tempsum=arr[end]; 33 | 34 | while(1){ 35 | //tempsum=tempsum+arr[end]; 36 | cout << "temp"<sum) 61 | { if (startend){ 66 | end++; 67 | tempsum = arr[end]; 68 | } 69 | continue; 70 | }else{ 71 | cout << "false" << '\n'; 72 | return 0; 73 | } 74 | 75 | }else if (start == n-1) 76 | { 77 | cout << "false" << '\n'; 78 | return 0; 79 | } 80 | 81 | 82 | 83 | 84 | } 85 | 86 | 87 | 88 | 89 | 90 | 91 | return 0 ; 92 | 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Language-Tools/Pairsumtozero.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given a random integer array A of size N. Find and print the pair of elements in the array which sum to 0. 11 | Array A can contain duplicate elements. 12 | While printing a pair, print the smaller element first. 13 | That is, if a valid pair is (6, -6) print "-6 6". There is no constraint that out of 5 pairs which have to be printed in 1st line. 14 | You can print pairs in any order, just be careful about the order of elements in a pair. 15 | 16 | */ 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | void PairSum(int *input, int n) { 23 | unordered_map m1; 24 | for (int i = 0; i < n; ++i) 25 | { 26 | if(m1[0-input[i]]==0){ 27 | m1[input[i]]++; 28 | 29 | }else{ 30 | m1[input[i]]++; 31 | } 32 | } 33 | 34 | unordered_map::iterator it=m1.begin(); 35 | while(it!=m1.end()){ 36 | int total = 0; 37 | int left = it->second; 38 | int right = m1[-it->first]; 39 | total = left*right; 40 | while(total>0){ 41 | cout << min(it->first, -it->first)<<" "<< max(it->first, -it->first) << '\n'; 42 | total--; 43 | } 44 | m1[it->first]=0; 45 | m1[-it->first]=0; 46 | it++; 47 | 48 | } 49 | 50 | } 51 | 52 | int main( int argc , char ** argv ) 53 | { 54 | ios_base::sync_with_stdio(false) ; 55 | cin.tie(NULL) ; 56 | 57 | int n; 58 | int arr[100000]; 59 | cin>>n; 60 | for(int i=0; i>arr[i]; 63 | } 64 | PairSum(arr, n); 65 | return 0; 66 | 67 | 68 | 69 | return 0 ; 70 | 71 | 72 | 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive Programming 🌟 2 | 3 | ## Topics in the Repo 4 | 5 | - ## `Advanced Graphs` 6 | ![Graphs](https://media.geeksforgeeks.org/wp-content/cdn-uploads/undirectedgraph.png) 7 | 8 | - Airport2.cpp 9 | - Airportwrong.cpp 10 | - Bottom.cpp 11 | - Connectedhorses.cpp 12 | - Dominos.cpp 13 | - Kingdom.cpp 14 | - Matrix.cpp 15 | - Monk.cpp 16 | - Newyear.cpp 17 | - Permutationsswap.cpp 18 | - Test.txt 19 | - a.out 20 | 21 | - ## `Assignment1` 22 | ![Assignment](https://static1.squarespace.com/static/543d9b11e4b0847bb28295dc/t/55371738e4b0cda0deaebec3/1587394842454/) 23 | 24 | - Collecting the balls.cpp 25 | - Power.cpp 26 | - Sortingskills.cpp 27 | - Test.txt 28 | - a.out 29 | 30 | - ## `Backtracking` 31 | ![backtracking](https://miro.medium.com/max/527/1*Pfj_YTlS5xc9xoIb1cDzUA.jpeg) 32 | 33 | - Crossword.cpp 34 | - Fillthematrix.cpp 35 | - Nqueens.cpp 36 | - Rateinamaze.cpp 37 | - Test.txt 38 | - a.out 39 | - check.cpp 40 | 41 | - ## `Bit Manipulation` 42 | ![bitman](https://he-s3.s3.amazonaws.com/media/uploads/3fb910b.jpg) 43 | 44 | - Unset.cpp 45 | 46 | - ## `DP1(Very important)` 47 | ![DP](https://he-s3.s3.amazonaws.com/media/uploads/6b68f98.png) 48 | 49 | - Adjacentbit 50 | - Adjacentbit.cpp 51 | - Allzeroes 52 | - Allzeroes.cpp 53 | - AlphaCode-Question.cpp 54 | - Boredom.cpp 55 | - Hasanandtrip.cpp 56 | - Loot.cpp 57 | - Maximumsum.cpp 58 | - Mergegrid.cpp 59 | - Royandcoin.cpp 60 | - Test.txt 61 | - Vanya and gcd 62 | - Vanya and gcd.cpp 63 | - a.out 64 | - angrychildren 65 | - angrychildren.cpp 66 | - coinchange.cpp 67 | - jonsnow.cpp 68 | - longestbitonicsubarray.cpp 69 | - minchoc.cpp 70 | - mincount.cpp 71 | - spreadsheet.cpp 72 | - staircase.cpp 73 | -------------------------------------------------------------------------------- /DPandbitmasking/Mehta.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | #define rep(i,a,b) for(int i = a; i < b; i++) 13 | #define S(x) scanf("%d",&x) 14 | #define P(x) printf("%d\n",x) 15 | typedef long long int LL; 16 | typedef pair pii; 17 | const int MAXN = 2001; 18 | LL P[11] = {1,2,3,5,7,11,13,17,19,23,29}; 19 | LL dp[2][MAXN][MAXN]; 20 | int profit[MAXN]; 21 | int weight[MAXN]; 22 | vector v; 23 | int main() { 24 | int N,W; 25 | scanf("%d%d",&N,&W); 26 | assert(N >= 1 && N <= 2000); 27 | assert(W >= 1 && W <= 2000); 28 | rep(i,1,N+1) { 29 | scanf("%d%d",&profit[i], &weight[i]); 30 | assert(profit[i] >= 1 && profit[i] <= 1000000000); 31 | assert(weight[i] >= 1 && weight[i] <= 2000); 32 | v.push_back(make_pair(profit[i], weight[i])); 33 | } 34 | sort(v.begin(), v.end()); 35 | rep(i,1,N+1) { 36 | profit[i] = v[i-1].first; 37 | weight[i] = v[i-1].second; 38 | } 39 | rep(p,0,11) rep(i,1,N+1) rep(j,1,W+1) { 40 | if(!p) { 41 | dp[0][i][j] = max(dp[0][i-1][j], dp[0][i][j-1]); 42 | if(j >= weight[i]) 43 | dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][j-weight[i]]+profit[i]); 44 | } else { 45 | int cur = p&1; 46 | int prev = 1-cur; 47 | dp[cur][i][j] = dp[prev][i][j]; 48 | dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i-1][j]); 49 | dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i][j-1]); 50 | if(j >= weight[i]) { 51 | dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i-1][j-weight[i]]+profit[i]); 52 | dp[cur][i][j] = max(dp[cur][i][j], dp[prev][i-1][j-weight[i]]+P[p]*profit[i]); 53 | } 54 | } 55 | } 56 | cout << dp[0][N][W] << endl; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Graphs1/Allconnectedcomponents.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | 13 | #include 14 | using namespace std; 15 | void connected(bool** edges, int n, int sv, int *visited) 16 | { 17 | int init=sv; 18 | 19 | while(init q; 26 | q.push(init); 27 | init++; 28 | vector v1; 29 | while(!q.empty()) 30 | { 31 | int x = q.front(); 32 | v1.push_back(x); 33 | 34 | q.pop(); 35 | 36 | for(int i=0;i> V >> E; 59 | 60 | bool** edges = new bool*[V]; 61 | for(int i=0; i>f; 71 | cin>>s; 72 | 73 | edges[f][s]=1; 74 | edges[s][f]=1; 75 | 76 | 77 | } 78 | 79 | int *visited = new int[V]; 80 | for(int i=0;i 38 | 39 | using namespace std; 40 | 41 | int minCount(int n){ 42 | //non Recursive solution 43 | 44 | std::vector dp(n+1, 0); 45 | dp[0] = 0; 46 | dp[1] = 1; 47 | for (int i = 1; i <= n; ++i) 48 | { 49 | int count = INT_MAX; 50 | for (int j = 1; j <= i; ++j) 51 | { 52 | if (i-j*j<0) 53 | { 54 | break; 55 | } 56 | 57 | count = min(count, dp[i-j*j]); 58 | } 59 | dp[i] = 1+count; 60 | } 61 | 62 | return dp[n]; 63 | 64 | } 65 | 66 | int main( int argc , char ** argv ) 67 | { 68 | ios_base::sync_with_stdio(false) ; 69 | cin.tie(NULL) ; 70 | 71 | int num; 72 | cin >> num; 73 | cout << minCount(num)< 27 | 28 | using namespace std; 29 | 30 | int solve(string s1,string s2) 31 | { 32 | int m =s1.size(); 33 | int n =s2.size(); 34 | 35 | vector> dp(m+1, vector(n+1, 0)); 36 | 37 | for (int i = 0; i < n+1; ++i) 38 | { 39 | dp[m][i] = n-i; 40 | } 41 | 42 | for (int i = 0; i < m+1; ++i) 43 | { 44 | dp[i][n] = m-i; 45 | } 46 | 47 | for (int i = m-1; i >= 0; --i) 48 | { 49 | for (int j = n-1; j >= 0; --j) 50 | { 51 | int k = j; 52 | 53 | while(k>S>>V; 86 | cout< 30 | 31 | using namespace std; 32 | 33 | long long go(vector donuts){ 34 | int n = donuts.size(); 35 | 36 | sort(donuts.begin(), donuts.end(), greater()); 37 | 38 | //int count = 0; 39 | long long ans = 0; 40 | for (int i = 0; i < n ; ++i) 41 | { 42 | ans+=pow(2, i)*donuts.at(i); 43 | } 44 | 45 | return ans; 46 | } 47 | 48 | 49 | int main( int argc , char ** argv ) 50 | { 51 | ios_base::sync_with_stdio(false) ; 52 | cin.tie(NULL) ; 53 | 54 | int n; 55 | cin>>n; 56 | 57 | std::vector donuts; 58 | while(n--){ 59 | int a; 60 | cin>>a; 61 | donuts.push_back(a); 62 | } 63 | 64 | cout << go(donuts) << '\n'; 65 | 66 | 67 | return 0 ; 68 | 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /Time-complexity-analysis/K-Concatenation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | typedef long long ll; 15 | 16 | ll kadane(int arr[], int n){ 17 | ll curr_sum = 0; 18 | ll max_so_far = INT_MIN; 19 | for (int i = 0; i < n; ++i) 20 | { 21 | curr_sum = curr_sum +arr[i]; 22 | if (max_so_far=0; --i) 51 | { 52 | curr_sufffix_sum += arr[i]; 53 | max_suffix_sum = max(max_suffix_sum, curr_sufffix_sum); 54 | } 55 | 56 | ll ans; 57 | if (totalSum<0) 58 | { 59 | ans=max(max_suffix_sum+max_suffix_sum, kadanes_sum); 60 | }else{ 61 | ans = max(max_suffix_sum+max_prefix_sum+totalSum*(k-2), kadanes_sum); 62 | } 63 | return ans; 64 | 65 | 66 | 67 | } 68 | int main( int argc , char ** argv ) 69 | { 70 | ios_base::sync_with_stdio(false) ; 71 | cin.tie(NULL) ; 72 | 73 | int t; 74 | cin>>t; 75 | while(t--){ 76 | int n,k; 77 | cin>>n>>k; 78 | int arr[n]; 79 | for (int i = 0; i < n; ++i) 80 | { 81 | std::cin>>arr[i]; 82 | } 83 | cout << maxSubarraySum(arr, n, k) << '\n'; 84 | 85 | } 86 | 87 | 88 | return 0 ; 89 | 90 | 91 | 92 | } 93 | -------------------------------------------------------------------------------- /Graphs1/getpathbfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector* getPath(int** edges, int n, int sv, int ev) 7 | { queue bfsQ; 8 | bool * visited = new bool[n]; 9 | for (int i = 0; i < n; i++) { 10 | visited[i] = false; 11 | } 12 | 13 | bfsQ.push(sv); 14 | 15 | visited[sv] = true; 16 | bool done = false; 17 | unordered_map parent; 18 | while (!bfsQ.empty() && !done) { 19 | int front = bfsQ.front(); 20 | bfsQ.pop(); 21 | for (int i = 0; i < n; i++) { 22 | if (edges[front][i] && !visited[i]) { 23 | parent[i] = front; bfsQ.push(i); 24 | visited[i] = true; if (i == ev) { 25 | done = true; 26 | break; 27 | } 28 | } 29 | } 30 | } 31 | delete [] visited; 32 | if (!done) { 33 | return NULL; 34 | } else { 35 | vector* output = new vector(); 36 | int current = ev; 37 | output->push_back(ev); 38 | while (current != sv) { 39 | current = parent[current]; 40 | output->push_back(current); 41 | } return output; 42 | } 43 | } 44 | 45 | 46 | 47 | int main() { 48 | int n; 49 | int e; 50 | cin >> n >> e; 51 | int** edges = 52 | new int*[n]; 53 | for (int i = 0; i < n; i++) { 54 | edges[i] = new int[n]; 55 | for (int j = 0; j < n; j++) { 56 | edges[i][j] = 0; 57 | } 58 | } for (int i = 0; i < e; i++) { 59 | int f, s; 60 | cin >> f >> s; 61 | edges[f][s] = 1; edges[s][f] = 1; } 62 | int sv, ev; 63 | cin >> sv >> ev; 64 | vector* output = getPath(edges, n, sv, ev); 65 | if (output != NULL) { 66 | for (int i = 0; i < output->size(); i++) { 67 | cout << output->at(i) << " "; 68 | } delete output; 69 | } for (int i = 0; i < n; i++) { 70 | delete [] edges[i]; 71 | } delete [] edges; 72 | } -------------------------------------------------------------------------------- /Greedy/Activityselection.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | You are given n activities with their start and finish times. Select the maximum number of 12 | activities that can be performed by a single person, assuming that a person can only work on a 13 | single activity at a time. 14 | Input 15 | The first line of input contains one integer denoting N. 16 | Next N lines contains two space separated integers denoting the start time and finish time for 17 | the ith activity. 18 | 19 | Output 20 | Output one integer, the maximum number of activities that can be performed 21 | Constraints 22 | 1 ≤ N ≤ 10^6 23 | 1 ≤ ai, di ≤ 10^9 24 | */ 25 | 26 | #include 27 | 28 | using namespace std; 29 | 30 | bool mySort(pair slot1, pair slot2){ 31 | return(slot1.first> time){ 35 | int count = 1; 36 | int n = time.size(); 37 | sort(time.begin(), time.end(), mySort); 38 | 39 | pair current = time.at(0); 40 | 41 | for (int i = 1; i < n; ++i) 42 | { 43 | if (current.second<=time.at(i).first) 44 | { 45 | count++; 46 | current = time.at(i); 47 | 48 | }else if (current.second>time.at(i).first) 49 | { 50 | if (time.at(i).second<=current.second) 51 | { 52 | current = time.at(i); 53 | } 54 | } 55 | } 56 | 57 | return count; 58 | 59 | } 60 | 61 | 62 | int main( int argc , char ** argv ) 63 | { 64 | ios_base::sync_with_stdio(false) ; 65 | cin.tie(NULL) ; 66 | 67 | int n; 68 | cin>>n; 69 | std::vector> time; 70 | 71 | while(n--){ 72 | int start, end; 73 | cin>>start>>end; 74 | pair a = make_pair(start, end); 75 | time.push_back(a); 76 | } 77 | 78 | cout << go(time) << '\n'; 79 | 80 | 81 | return 0 ; 82 | 83 | 84 | 85 | } 86 | -------------------------------------------------------------------------------- /DPandbitmasking/Job.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | */ 12 | 13 | #include 14 | using namespace std; 15 | 16 | typedef long long ll; 17 | typedef unordered_map umapii; 18 | typedef unordered_map umapib; 19 | typedef unordered_map umapsi; 20 | typedef unordered_map umapss; 21 | typedef map mapsi; 22 | typedef map, int> mappiii; 23 | typedef map mapii; 24 | typedef pair pii; 25 | typedef pair pll; 26 | typedef unordered_set useti; 27 | 28 | #define uset unordered_set 29 | #define it iterator 30 | #define mp make_pair 31 | #define pb push_back 32 | #define all(x) (x).begin(), (x).end() 33 | #define f first 34 | #define s second 35 | #define MOD 1000000007 36 | 37 | 38 | int mincost(int cost[4][4], int n, int p, int mask, int* dp){ 39 | 40 | if (p>=n) 41 | { 42 | return 0; 43 | } 44 | 45 | if (dp[mask]!=INT_MAX) 46 | { 47 | return dp[mask]; 48 | } 49 | 50 | int minimum = INT_MAX; 51 | for (int i = 0; i < n; ++i) 52 | { 53 | if (!(mask&(1< 2 | 3 | using namespace std; 4 | #include 5 | #include 6 | 7 | 8 | static int rowNbr[] = {-1, 1, 0, 0}; 9 | static int colNbr[] = {0, 0, 1, -1}; 10 | 11 | 12 | //To check if the index is an eligible index for our cake 13 | bool iseligible(char cake[][55], int x, int y, int** visited, int n) 14 | { 15 | // if(x == 0 && y == 4) 16 | // cout << "Chirag" << endl; 17 | //cout << x << " " << y << endl; 18 | if(x<0 || x>=n || y<0 || y>=n|| visited[x][y] == 1 || cake[x][y] == '0' ){ 19 | return 0; 20 | } 21 | else 22 | return 1; 23 | } 24 | 25 | //Returns the maximum peice of cake that can be taken starting from x,y 26 | int solver(int n, char cake[][55], int** visited, int x, int y){ 27 | int sum=0; 28 | 29 | for(int k=0;k<4;k++){ 30 | if(iseligible(cake,x+rowNbr[k], y+colNbr[k], visited, n)){ 31 | 32 | visited[x+rowNbr[k]][y+colNbr[k]]=1; 33 | sum=sum+solver(n, cake, visited, x+rowNbr[k], y+colNbr[k]); 34 | } 35 | } 36 | return 1+sum; 37 | } 38 | 39 | 40 | //passes index of each one to solver as starting point and returns max of all 41 | int solve(int n,char cake[][55]) 42 | { 43 | int** visited = new int*[n]; 44 | for(int i=0;i 46 | 47 | using namespace std; 48 | 49 | 50 | int main( int argc , char ** argv ) 51 | { 52 | ios_base::sync_with_stdio(false) ; 53 | cin.tie(NULL) ; 54 | 55 | int n, m; 56 | cin>>n>>m; 57 | vector s(n+1, 0); 58 | vector e(n+1, 0); 59 | 60 | vector buffer(n+1, 0); 61 | 62 | 63 | for (int i = 0; i < m; ++i) 64 | { 65 | int a, b; 66 | cin>>a>>b; 67 | 68 | s[a]++; 69 | e[b]++; 70 | } 71 | 72 | for (int i = 1; i < n+1; ++i) 73 | { 74 | buffer[i] = s[i] - e[i-1] + buffer[i-1]; 75 | } 76 | 77 | //Will store ki i coins kitne boxes me hai 78 | vector coins(n+1,0); 79 | for (int i = 1; i < n+1; ++i) 80 | { 81 | coins[buffer[i]]++; 82 | } 83 | 84 | for (int i = n-1; i >= 0; --i) 85 | { 86 | coins[i] += coins[i+1]; 87 | } 88 | 89 | int q; 90 | cin>>q; 91 | 92 | for (int i = 0; i < q; ++i) 93 | { 94 | int temp; 95 | cin>>temp; 96 | cout << coins[temp] << '\n'; 97 | } 98 | 99 | 100 | 101 | return 0 ; 102 | 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /Language-Tools/tell-thepositions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | In a class there are ‘n’ number of students. They have three different subjects: Data Structures, Algorithm Design & Analysis and Operating Systems. 12 | Marks for each subject of all the students are provided to you. You have to tell the position of each student in the class. 13 | Print the names of each student according to their position in class. Tie is broken on the basis of their roll numbers. Between two students having same marks, 14 | the one with less roll number will have higher rank. The input is provided in order of roll number. 15 | 16 | */ 17 | 18 | 19 | #include 20 | 21 | using namespace std; 22 | 23 | class student{ 24 | public: 25 | string name; 26 | int marks; 27 | int roll; 28 | }; 29 | 30 | bool mycompare(student a, student b){ 31 | 32 | if (a.marks!=b.marks) 33 | { 34 | return (a.marks>b.marks); 35 | }else{ 36 | return(a.roll>n; 49 | 50 | //std::vector name; //ncross1 51 | std::vector stud; //ncross3 52 | 53 | for (int i = 0; i < n; ++i) 54 | { 55 | string temp_name; 56 | int sub1; 57 | int sub2; 58 | int sub3; 59 | cin>>temp_name>>sub1>>sub2>>sub3; 60 | 61 | int sum = sub1+sub2+sub3; 62 | 63 | student temp_sub; 64 | 65 | temp_sub.name = temp_name; 66 | temp_sub.marks = sum; 67 | temp_sub. roll = i+1; 68 | 69 | stud.push_back(temp_sub); 70 | } 71 | 72 | 73 | 74 | sort(stud.begin(), stud.end(), mycompare); 75 | 76 | for (int i = 0; i < n; ++i) 77 | { 78 | cout << i+1<<" "< 27 | 28 | using namespace std; 29 | 30 | int kadane(vector v){ 31 | int n = v.size(); 32 | int maxtillnow = 0; 33 | int maxendinghere = 0; 34 | 35 | 36 | for (int i = 0; i < n; ++i) 37 | { 38 | maxendinghere = max(v[i], v[i]+maxendinghere); 39 | 40 | maxtillnow = max(maxtillnow, maxendinghere); 41 | 42 | } 43 | 44 | return maxtillnow; 45 | 46 | } 47 | 48 | int go(vector> arr, int m, int n){ 49 | 50 | int ans = INT_MIN; 51 | 52 | for (int i = 0; i < n; ++i) 53 | { 54 | std::vector sum(m, 0); 55 | 56 | for (int j = i; j < n; ++j) 57 | { 58 | for (int k = 0; k < m; ++k) 59 | { 60 | sum[k] += arr[k][j]; 61 | } 62 | 63 | int temp = kadane(sum); 64 | ans = max(ans, temp); 65 | } 66 | } 67 | 68 | return ans; 69 | 70 | } 71 | 72 | 73 | int main( int argc , char ** argv ) 74 | { 75 | ios_base::sync_with_stdio(false) ; 76 | cin.tie(NULL) ; 77 | 78 | int m, n; 79 | cin>>m>>n; 80 | 81 | vector> matrix(m, vector(n, 0)); 82 | for (int i = 0; i < m; ++i) 83 | { 84 | for (int j = 0; j < n; ++j) 85 | { 86 | cin>>matrix[i][j]; 87 | } 88 | } 89 | 90 | cout << go(matrix, m, n) < 17 | 18 | using namespace std; 19 | 20 | class detail 21 | { 22 | public: 23 | int time; 24 | bool status; 25 | 26 | }; 27 | 28 | 29 | bool mysort(detail a, detail b){ 30 | return(a.time < b.time); 31 | } 32 | 33 | 34 | 35 | int main( int argc , char ** argv ) 36 | { 37 | ios_base::sync_with_stdio(false) ; 38 | cin.tie(NULL) ; 39 | 40 | int n; 41 | cin>>n; 42 | 43 | //Arrival 44 | std::vector people; 45 | for (int i = 0; i < n; ++i) 46 | { 47 | int temp_time; 48 | cin>>temp_time; 49 | detail temp; 50 | temp.time = temp_time; 51 | temp.status = 1; 52 | people.push_back(temp); 53 | } 54 | 55 | //Departure 56 | for (int i = 0; i < n; ++i) 57 | { 58 | int temp_time; 59 | cin>>temp_time; 60 | detail temp; 61 | temp.time = temp_time; 62 | temp.status = 0; 63 | people.push_back(temp); 64 | } 65 | 66 | //Sorting 67 | 68 | sort(people.begin(), people.end(), mysort); 69 | 70 | 71 | int ans = 0; 72 | int count = 0; 73 | 74 | vector :: iterator it = people.begin(); 75 | 76 | for (int i = 0; i < people.size(); ++i) 77 | { 78 | if (people.at(i).status == 1) 79 | { 80 | count++; 81 | }else{ 82 | count--; 83 | } 84 | ans = max(ans, count); 85 | } 86 | 87 | cout << ans << '\n'; 88 | 89 | 90 | return 0 ; 91 | 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Searching and sorting applications/Inversion Count.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | /* 9 | PROBLEM STATEMENT 10 | Let A[0 ... n-1] be an array of n distinct positive integers. 11 | If i < j and A[i] > A[j] then the pair (i, j) is called an inversion of A 12 | (where i and j are indexes of A). Given an integer array A, your task is to 13 | find the number of inversions in A. 14 | Input format : 15 | Line 1 : n, array size 16 | Line 2 : Array elements (separated by space). 17 | Output format : 18 | Count of inversions 19 | Constraints : 20 | 1 <= n <= 10^5 21 | 1 <= A[i] <= 10^9 22 | 23 | */ 24 | 25 | #include 26 | 27 | using namespace std; 28 | 29 | long long merge(int A[],int left,int mid,int right){ 30 | 31 | int i=left,j=mid,k=0; 32 | 33 | int temp[right-left+1]; 34 | long long count = 0; 35 | while(i left){ 59 | int mid = (left + right)/2; 60 | 61 | long long countLeft = merge_sort(A,left,mid); 62 | long long countRight = merge_sort(A,mid+1,right); 63 | long long myCount = merge(A,left,mid+1,right); 64 | 65 | return myCount + countLeft + countRight; 66 | } 67 | return count; 68 | 69 | } 70 | long long solve(int A[], int n) 71 | { 72 | long long ans = merge_sort(A,0,n-1); 73 | return ans; 74 | } 75 | 76 | 77 | #define MAXN 100005 78 | 79 | int main() 80 | { 81 | int n,A[MAXN]; 82 | cin>>n; 83 | for(int i = 0; i < n ; i++){ 84 | cin>>A[i]; 85 | } 86 | cout<&v) 31 | { 32 | if(!(mask&(mask-1))&&mask) 33 | // everything was recongnised successfully when mask contains only one bit set 34 | return 0; 35 | 36 | if(pos == -1||mask == 0) 37 | return 100000; 38 | 39 | if(dp[pos][mask]) 40 | return dp[pos][mask]; 41 | 42 | int newmask1 = 0,newmask2 = 0,touches = 0; 43 | //if we touch the v[any][pos],mask will split. 44 | for(int i = 0;i>i)&1) 47 | { 48 | touches++; 49 | if(v[i][pos] == '0') 50 | { 51 | newmask1|= (1< v) 63 | { 64 | return find_touches(v[0].size()-1,(1< 2 | using namespace std; 3 | 4 | class UnionFind //To check for a cycle in the graph. 5 | { 6 | private: vector p,rank; 7 | public: 8 | UnionFind(int N) 9 | { 10 | rank.assign(N,0); 11 | p.assign(N,0); 12 | 13 | for(int i=0;irank[y]) {p[y]=x; rank[x]++;} 29 | else 30 | { 31 | p[x]=y; 32 | if(rank[x]==rank[y]) rank[y]++; 33 | } 34 | } 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | vector > > EdgeList; 41 | cout<<"Enter the number of vertices: \n"; 42 | int V; 43 | cin>>V; 44 | cout<<"Enter the number of edges: \n"; 45 | int E; 46 | cin>>E; 47 | cout<<"Enter the edges with their weights: (Vertex 1 Vertex 2 Weight)\n"; 48 | for(int i=0;i>u>>v>>w; 52 | EdgeList.push_back(make_pair(w,make_pair(u,v))); 53 | } 54 | 55 | sort(EdgeList.begin(),EdgeList.end()); 56 | 57 | int weight=0; 58 | 59 | UnionFind UF(V); 60 | 61 | for(int i=0;i > front=EdgeList[i]; 64 | if(!UF.isSameSet(front.second.first, front.second.second)) 65 | { 66 | weight+=front.first; 67 | UF.unionSet(front.second.first, front.second.second); 68 | } 69 | } 70 | cout<<"Final Weight of the MST is: "< 25 | 26 | using namespace std; 27 | 28 | int lcs(string s1, string s2){ 29 | 30 | int m = s1.size(); 31 | int n = s2.size(); 32 | 33 | vector> dp(m, vector (n,0)); 34 | 35 | if (s1[0] == s2[0]) 36 | { 37 | dp[0][0] = 1; 38 | } 39 | 40 | for (int i = 1; i < n; ++i) 41 | { 42 | if(s1[0] == s2[i]) 43 | dp[0][i] = 1; 44 | else 45 | dp[0][i] = dp[0][i-1]; 46 | } 47 | 48 | for (int i = 1; i < m; ++i) 49 | { 50 | if(s1[i] == s2[0]) 51 | dp[i][0] = 1; 52 | else 53 | dp[i][0] = dp[i-1][0]; 54 | } 55 | 56 | for (int i = 1; i < m; ++i) 57 | { 58 | for (int j = 1; j < n; ++j) 59 | { 60 | if(s1[i] == s2[j]) 61 | dp[i][j] = dp[i-1][j-1]+1; 62 | else 63 | dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 64 | } 65 | } 66 | 67 | // for (int i = 0; i < m; ++i) 68 | // { 69 | // for (int j = 0; j < n; ++j) 70 | // { 71 | // cout << dp[i][j]<<" " ; 72 | // } 73 | // cout <<'\n'; 74 | // } 75 | 76 | 77 | return dp[m-1][n-1]; 78 | 79 | 80 | } 81 | 82 | int main( int argc , char ** argv ) 83 | { 84 | ios_base::sync_with_stdio(false) ; 85 | cin.tie(NULL) ; 86 | 87 | string s1, s2; 88 | cin>>s1>>s2; 89 | 90 | cout << lcs(s1, s2) << '\n'; 91 | 92 | return 0 ; 93 | 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /DP1(Very important)/Boredom.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Gary is bored and wants to play an interesting but tough game . So he figured out a new board game called "destroy the neighbours" . In this game there are N integers on a board. In one move, he can pick any integer x from the board and then all the integers with value x+1 or x-1 gets destroyed .This move will give him x points. 12 | He plays the game until the board becomes empty . But as he want show this game to his friend Steven, he wants to learn techniques to maximise the points to show off . Can you help Gary in finding out the maximum points he receive grab from the game ? 13 | Input Format : 14 | Line 1 : Integer N 15 | Line 2 : A list of N integers 16 | Output Format : 17 | Maximum points Gary can recieve from the Game setup 18 | Constraints : 19 | 1<=N<=10^5 20 | 1<=A[i]<=1000 21 | Sample Input : 22 | 2 23 | 1 2 24 | Sample Output : 25 | 2 26 | */ 27 | #include 28 | 29 | using namespace std; 30 | 31 | int n; 32 | vectorA; 33 | int freq[1002],dp[1002]; 34 | int solve(int n,vectorA){ 35 | 36 | for(int i=0;i>n; 59 | for(int i=0;i>x; 63 | A.push_back(x); 64 | } 65 | cout< toPos(double index) { 4 | return {index == 1 ? 1.0 : 0.0, index == 2 ? 1.0 : 0.0, index == 3 ? 1.0 : 0.0}; 5 | } 6 | 7 | double fromPos(std::vector v) { 8 | if (v[0] == 1.0) { 9 | return 1; 10 | } else if (v[1] == 1.0) { 11 | return 2; 12 | } else { 13 | return 3; 14 | } 15 | } 16 | 17 | double fromOut(std::vector v) { 18 | return std::distance(v.begin(), std::max_element(v.begin(), v.end())) + 1; 19 | } 20 | 21 | int main(int argc, char** argv) { 22 | NeuralNetwork nn = NeuralNetwork(4, 5, 3); 23 | 24 | std::ifstream file("bin/iris_data_files/iris.data"); 25 | std::string line; 26 | 27 | std::vector> train; 28 | std::vector> label; 29 | 30 | double in1, in2, in3, in4, out; 31 | 32 | while (std::getline(file, line)) { 33 | std::istringstream iss(line); 34 | iss >> in1 >> in2 >> in3 >> in4 >> out; 35 | train.push_back({in1, in2, in3, in4}); 36 | label.push_back(toPos(out)); 37 | } 38 | 39 | double totalAcc; 40 | 41 | for (int k = 0; k < 10; k++) { 42 | for (int i = 0; i < 100000; i++) { 43 | int index = i % (train.size()); 44 | nn.train(train[index], label[index]); 45 | } 46 | 47 | double rate = 0; 48 | double acc = 0; 49 | 50 | for (int i = 0; i < train.size(); i++) { 51 | std::vector output = nn.predict(train[i]); 52 | double o = fromPos(label[i]); 53 | double a = fromOut(output); 54 | //std::cout << "output: " << a << " label: " << o << std::endl; 55 | if (a == o) rate++; 56 | } 57 | 58 | acc = rate / train.size(); 59 | 60 | std::cout << "rate: " << rate << "/" << train.size() << " acc: " << acc << std::endl; 61 | 62 | totalAcc += acc; 63 | } 64 | 65 | std::cout << "Average acc: " << totalAcc / 10 << std::endl; 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /Searching and sorting applications/Distrributecandies.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Shaky has N (1<=N<=50000) candy boxes each of them contains a non-zero number of candies 12 | (between 1 and 1000000000). 13 | Shaky want to distibute these candies among his K (1<=K<=1000000000) IIIT-Delhi students. 14 | He want to distibute them in a way such that: 15 | 1. All students get equal number of candies. 16 | 2. All the candies which a student get must be from a single box only. 17 | As he want to make all of them happy so he want to give as many candies as possible. 18 | Help Shaky in finding out what is the maximum number of candies which a student can get. 19 | 20 | */ 21 | 22 | 23 | #include 24 | 25 | using namespace std; 26 | int is_possible(vector v, int k, int mid){ 27 | int n = v.size(); 28 | int givento = 0; 29 | 30 | int i = 0; 31 | while(i= k); 37 | } 38 | 39 | int go(vector v, int k){ 40 | 41 | int n = v.size(); 42 | sort(v.begin(), v.end()); 43 | 44 | int max_candies = v.at(n-1); 45 | int min_candies = 0; 46 | int mid = (min_candies+max_candies)/2; 47 | 48 | int i = 0; 49 | int ans = 0; 50 | 51 | while(max_candies>=min_candies){ 52 | if (is_possible(v, k, mid)) 53 | { 54 | ans = max(ans, mid); 55 | min_candies = mid+1; 56 | mid = (min_candies+max_candies)/2; 57 | 58 | }else{ 59 | max_candies = mid-1; 60 | mid = (min_candies+max_candies)/2; 61 | } 62 | } 63 | 64 | return ans; 65 | 66 | } 67 | 68 | 69 | 70 | 71 | int main( int argc , char ** argv ) 72 | { 73 | ios_base::sync_with_stdio(false) ; 74 | cin.tie(NULL) ; 75 | 76 | int t; 77 | cin>>t; 78 | 79 | while(t--){ 80 | int n, k; 81 | cin>>n>>k; 82 | int m=n; 83 | std::vector v; 84 | while(m--){ 85 | int a; 86 | cin>>a; 87 | v.push_back(a); 88 | } 89 | cout << go(v,k) << '\n'; 90 | } 91 | 92 | return 0 ; 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Dynamic programming 2/Subsetsum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | 9 | /* 10 | Given a set of n integers, find if a subset of sum k can be formed from the given set. Print Yes or No. 11 | 12 | Input Format 13 | First line contains a single integer n (1<=n<=1000) 14 | Second line contains n space separated integers (1<=a[i]<=1000) 15 | Last line contains a single positive integer k (1<=k<=1000) 16 | Output Format 17 | Output Yes if there exists a subset whose sum is k, else output No. 18 | Sample Input 19 | 3 20 | 1 2 3 21 | 4 22 | Sample Output 23 | YES 24 | */ 25 | 26 | #include 27 | 28 | using namespace std; 29 | 30 | void go(vector arr, int n, int i , int sum){ 31 | vector> dp(n+1, vector(sum+1, 0)); 32 | 33 | for (int i = 0; i < n+1; ++i) 34 | { 35 | dp[i][0] = 1; 36 | } 37 | 38 | for (int i = 1; i < sum+1; ++i) 39 | { 40 | dp[n][i] = 0; 41 | } 42 | 43 | for (int j = 1; j < sum+1; ++j) 44 | { 45 | for (int i = n-1; i >= 0; --i) 46 | { 47 | bool c1 = 0; 48 | if (j-arr[i]>=0) 49 | { 50 | c1 = dp[i+1][j-arr[i]]; 51 | } 52 | 53 | if (c1 == 1) 54 | dp[i][j] = 1; 55 | else 56 | { 57 | dp[i][j] = dp[i+1][j]; 58 | 59 | } 60 | } 61 | } 62 | 63 | // for (int i = 0; i < n+1; ++i) 64 | // { 65 | // for (int j = 0; j < sum+1; ++j) 66 | // { 67 | // cout<>n; 91 | 92 | vector arr(n, 0); 93 | for (int i = 0; i < n; ++i) 94 | { 95 | cin>>arr[i]; 96 | } 97 | 98 | int sum; 99 | cin>>sum; 100 | 101 | go(arr, n, 0, sum); 102 | 103 | 104 | 105 | return 0 ; 106 | 107 | 108 | 109 | } 110 | -------------------------------------------------------------------------------- /Binary Search/1408C- Discrete Acceleration: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define int long long 4 | #define MAX (int)(1e7+5) 5 | #define MAX2D (int)(1e3+5) 6 | #define itn int 7 | #define inf 5e18 8 | #define MOD (int)(1e9 + 7) 9 | #define pb push_back 10 | #define mk make_pair 11 | #define all(x) x.begin(), x.end() 12 | #define rall(x) x.rbegin(),x.rend() 13 | #define pii pair 14 | #define vii vector 15 | #define vi vector 16 | #define vll vector 17 | #define test \ 18 | int t; \ 19 | cin >> t; \ 20 | while (t--) 21 | #define fast \ 22 | ios_base ::sync_with_stdio(0); \ 23 | cin.tie(NULL); \ 24 | cout.tie(NULL) 25 | using namespace std; 26 | 27 | int n,L; 28 | const int N = 1e5+1; 29 | vector a(N); 30 | double eps = 1e-6; 31 | 32 | bool good(double m) { 33 | double t1 = m, t2 = m; 34 | double speed1 = 1, speed2 = 1; 35 | double pos1 = 0, pos2 = L; 36 | for(int i=1;i<=n+1;i++) { 37 | if(speed1*t1*1.0f >= 1.0f*((a[i]-a[i-1]))) pos1 += 1.0f*(a[i]-a[i-1]), t1 -= (1.0f*(a[i]-a[i-1]))/speed1, speed1++; 38 | else pos1 += 1.0f*speed1*t1, t1 = 0; 39 | t1 = max(t1, (double)0); 40 | } 41 | for(int i=n;i>=0;i--) { 42 | if(speed2*t2*1.0f >= 1.0f*(a[i+1]-a[i])) pos2 -= 1.0f*(a[i+1]-a[i]), t2 -= (1.0f*(a[i+1]-a[i]))/speed2, speed2++; 43 | else pos2 -= 1.0f*speed2*t2, t2 = 0; 44 | t2 = max(t2, (double)0); 45 | } 46 | if((pos2-pos1) <= 0.00001) return true; 47 | return false; 48 | } 49 | 50 | double f() { 51 | double l = 0, r = L; 52 | double ans = l; 53 | for(int i=0;i<200 && l+eps <= r;i++) { 54 | double mid = (l+r)*0.5f; 55 | if(good(mid)) ans = mid, r = mid; 56 | else l = mid; 57 | } 58 | return ans; 59 | } 60 | signed main() 61 | { 62 | fast; 63 | test { 64 | cin>>n>>L; 65 | a.resize(n+2); 66 | for(int i=1;i<=n;i++) cin>>a[i]; 67 | a[0] = 0, a[n+1] = L; 68 | cout< 34 | 35 | using namespace std; 36 | 37 | int editDistance(string s1, string s2){ 38 | int m = s1.size(); 39 | int n = s2.size(); 40 | 41 | vector>dp(m+1, vector(n+1, 0)); 42 | 43 | for (int i = 0; i < m+1; ++i) 44 | { 45 | dp[i][n] = m-i; 46 | } 47 | 48 | 49 | for (int i = 0; i < n+1; ++i) 50 | { 51 | dp[m][i] = n-i; 52 | } 53 | 54 | //dp[m][n] = 0; 55 | 56 | for (int i = m-1; i >= 0; --i) 57 | { 58 | for (int j = n-1; j >= 0; --j) 59 | { 60 | if(s1[i]==s2[j]){ 61 | dp[i][j] = dp[i+1][j+1]; 62 | }else{ 63 | 64 | dp[i][j] = min(1+dp[i+1][j+1], min(1+dp[i+1][j], 1+dp[i][j+1])); 65 | } 66 | } 67 | } 68 | 69 | // for (int i = 0; i < m+1; ++i) 70 | // { 71 | // for (int j = 0; j < n+1; ++j) 72 | // { 73 | // cout<> s1; 94 | cin >> s2; 95 | 96 | cout << editDistance(s1,s2) << endl; 97 | 98 | return 0 ; 99 | 100 | 101 | 102 | } 103 | -------------------------------------------------------------------------------- /Graphs1/Board.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | /* 8 | Gary has a board of size NxM. Each cell in the board is a coloured dot. There exist only 26 colours denoted by uppercase Latin characters (i.e. A,B,...,Z). Now Gary is getting bore and wants to play a game. The key of this game is to find a cycle that contain dots of same colour. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition: 9 | 1. These k dots are different: if i ≠ j then di is different from dj. 10 | 2. k is at least 4. 11 | 3. All dots belong to the same colour. 12 | 4. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge. 13 | Since Gary is colour blind, he wants your help. Your task is to determine if there exists a cycle on the board. 14 | Assume input to be 0-indexed based. 15 | Input Format : 16 | Line 1 : Two integers N and M, the number of rows and columns of the board 17 | Next N lines : a string consisting of M characters, expressing colors of dots in each line. Each character is an uppercase Latin letter. 18 | Output Format : 19 | Return 1 if there is a cycle else return 0 20 | Constraints : 21 | 2 ≤ N, M ≤ 50 22 | Sample Input : 23 | 3 4 24 | AAAA 25 | ABCA 26 | AAAA 27 | Sample Output : 28 | 1 29 | */ 30 | 31 | 32 | #include 33 | #define MAXN 51 34 | 35 | using namespace std; 36 | 37 | int helper(char board[][MAXN], int n, int m, int* visited){ 38 | for (int i = 0; i < n; ++i) 39 | { 40 | for (int j = 0; j < m; ++j) 41 | { 42 | if () 43 | { 44 | 45 | } 46 | } 47 | } 48 | } 49 | 50 | int solve(char board[][MAXN],int n, int m) 51 | { 52 | int* visited = new int[n](); 53 | 54 | return helper(board, n, m, visited); 55 | } 56 | 57 | int main( int argc , char ** argv ) 58 | { 59 | ios_base::sync_with_stdio(false) ; 60 | cin.tie(NULL) ; 61 | 62 | int N,M,i; 63 | char board[MAXN][MAXN]; 64 | cin>>N>>M; 65 | for(i = 0;i < N; i++){ 66 | cin>>board[i]; 67 | } 68 | cout<adj[]) 45 | { 46 | queueq; 47 | dp[1]=0; 48 | q.push(1); 49 | while(!q.empty()) 50 | { 51 | int idx=q.front(); 52 | q.pop(); 53 | for(int i=0;i>t; 69 | while(t--) 70 | { 71 | cin>>n>>m; 72 | memset(dp,-1,sizeof(dp)); 73 | vectoradj[n+1]; 74 | while(m--) 75 | { 76 | int x,y; 77 | cin>>x>>y; 78 | adj[x].push_back(y); 79 | adj[y].push_back(x); 80 | } 81 | cout< 25 | 26 | using namespace std; 27 | long long merge(vector &v, long long start, long long mid, long long end){ 28 | 29 | long long sum = 0; 30 | long long i = start; 31 | long long j = mid+1; 32 | 33 | 34 | while(j<=end && i<=mid){ 35 | 36 | if (v.at(j)>v.at(i)) 37 | { 38 | 39 | sum += (end-j+1)*v.at(i); 40 | i++; 41 | }else{ 42 | j++; 43 | } 44 | 45 | } 46 | return sum; 47 | 48 | } 49 | 50 | long long go(vector &v, long long start, long long end){ 51 | 52 | if (start>=end) 53 | { 54 | return 0; 55 | } 56 | long long mid = (start+end)/2; 57 | 58 | long long left = go(v, start, mid); 59 | long long right = go(v, mid+1, end); 60 | 61 | sort(v.begin()+start, v.begin()+mid+1); 62 | sort(v.begin()+mid+1, v.begin()+end+1); 63 | 64 | return left+right+merge(v, start, mid, end); 65 | 66 | } 67 | 68 | 69 | int main( int argc , char ** argv ) 70 | { 71 | ios_base::sync_with_stdio(false) ; 72 | cin.tie(NULL) ; 73 | 74 | long long t; 75 | cin>>t; 76 | 77 | while(t--){ 78 | long long n; 79 | cin>>n; 80 | long long m = n; 81 | 82 | std::vector v; 83 | while(n--){ 84 | long long a; 85 | cin>>a; 86 | v.push_back(a); 87 | } 88 | 89 | cout << go(v, 0, m-1) << '\n'; 90 | } 91 | 92 | return 0 ; 93 | } 94 | -------------------------------------------------------------------------------- /DP1(Very important)/minchoc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Noor is a teacher. She wants to give some chocolates to the students in her class. All the students sit in a line and each of them has a score according to performance. Noor wants to give at least 1 chocolate to each student. She distributes chocolates to them such that If two students sit next to each other then the one with the higher score must get more chocolates. Noor wants to save money, so she wants to minimise the total number of chocolates. 11 | Note that when two students have equal score they are allowed to have different number of chocolates. 12 | Input Format: 13 | First Line: Integer N, the number of students in Noor’s class. 14 | Second Line: Each of the student's score separated by spaces. 15 | Output Format: 16 | Output a single line containing the minimum number of chocolates Noor must give. 17 | Input Constraints 18 | 1 <= N <= 100000 19 | 1 <= score <= 100000 20 | Sample Input: 21 | 4 22 | 1 4 4 6 23 | sample Output: 24 | 6 25 | Sample Input: 26 | 3 27 | 8 7 5 28 | sample Output: 29 | 6 30 | */ 31 | 32 | #include 33 | 34 | using namespace std; 35 | 36 | int getMin(int *arr, int n){ 37 | //left case 38 | 39 | std::vector dp(n, 0); 40 | dp[0] = 1; 41 | for (int i = 1; i < n; ++i) 42 | { 43 | if(arr[i]>arr[i-1]) 44 | dp[i] = dp[i-1]+1; 45 | else 46 | dp[i] = 1; // made changes here 47 | } 48 | 49 | 50 | dp[n-1] = max(dp[n-1], 1); 51 | 52 | for (int i = n-2; i >= 0; --i) 53 | { 54 | if(arr[i+1]> n; 77 | int *arr = new int[n]; 78 | for(int i = 0; i < n; i++){ 79 | cin >> arr[i]; 80 | } 81 | 82 | cout << getMin(arr, n); 83 | delete []arr; 84 | 85 | 86 | 87 | return 0 ; 88 | 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Searching and sorting applications/Tajmahalentry.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | /* 11 | PROBLEM STATEMENT 12 | Taj Mahal is one of the seven wonders of the world. Aahad loves to travel places 13 | and wants to visit Taj Mahal. He visited Agra to view Taj Mahal. There is a ticketing 14 | system at Taj Mahal. There are total ‘n’ windows which provide the tickets to get entry 15 | into Taj Mahal. There are ‘Ai’ people already present at each window to get the tickets. 16 | Each window gives ticket to one person in one minute. Initially, Aahad stands in front of 17 | the first window. After each minute, if he didn’t get the ticket, he moves on to the next 18 | window to get the ticket. If he is at window 1, he will move to 2. If at 2nd, he will move to 3rd. 19 | If he is at last window, he will move to 1st again and so on. 20 | Find the window number at which he will get the ticket. 21 | 22 | 23 | */ 24 | 25 | #include 26 | 27 | using namespace std; 28 | 29 | int go(vector window, int min_index){ 30 | int n = window.size(); 31 | int pos = window.at(min_index)%n; 32 | int min_value = window.at(min_index); 33 | //cout << min_index << '\n'; 34 | //cout << pos << '\n'; 35 | 36 | if (pos==min_index) 37 | { 38 | return min_index; 39 | } 40 | 41 | int cycles = min_value/n; 42 | 43 | for (int i = pos; i < n+pos; ++i) 44 | { 45 | window.at(i%n) = window.at(i%n) - abs(i-pos)-cycles*n-min_value; 46 | if (window.at(i%n)<=0) 47 | { 48 | return i%n; 49 | } 50 | } 51 | 52 | return 0; 53 | 54 | } 55 | 56 | 57 | int main( int argc , char ** argv ) 58 | { 59 | ios_base::sync_with_stdio(false) ; 60 | cin.tie(NULL) ; 61 | 62 | int n; 63 | cin>>n; 64 | //int m = n; 65 | int min_index= 0; 66 | int min_value = INT_MAX; 67 | int h = 0; 68 | 69 | std::vector window; 70 | while(n--){ 71 | int a; 72 | cin>>a; 73 | window.push_back(a); 74 | if (window.at(min_index)>window.at(h)) 75 | { 76 | min_index = h; 77 | min_value = window.at(min_index); 78 | } 79 | h++; 80 | } 81 | 82 | cout << go(window, min_index)+1 << '\n'; 83 | 84 | 85 | 86 | 87 | return 0 ; 88 | 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Greedy/Perimeter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | /* 11 | PROBLEM STATEMENT 12 | Aahad gives an array of integers and asks Harshit to find which three elements form a triangle (non-degenerate). The task seems easy to Harshit. 13 | So, Aahad adds some conditions to this task - 14 | 1. Find the triangle with maximum perimeter 15 | 2. If there are two or more combinations with same value of maximum perimeter, then find the one with the longest side. 16 | 3.If there are more than one combinations which satisfy all the above conditions the find with maximum longest minimum side. 17 | Input Format 18 | The First line contains no of elements of array: N 19 | Each T lines contains N space-separated integers: A [i] 20 | Output Format 21 | The output contains three space-separated elements that denote the length of the sides of triangle. If no such triangle is possible, then print -1. 22 | Constraints 23 | 1 =< N <= 10^5 24 | 1 <= A[i] <= 10^9 25 | Time Limit: 1 second 26 | Sample Input1: 27 | 5 28 | 1 1 1 3 3 29 | Sample Output1: 30 | 1 3 3 31 | Sample Input2: 32 | 3 33 | 2 2 4 34 | Sample Output3: 35 | -1 36 | Explaination 37 | In the First Sample case, the elements that form a triangle with maximum perimeter is 1,3,3. 38 | In the Second Sample case, the elements that can form a triangle are degenerate, so, we printed -1. 39 | 40 | */ 41 | 42 | #include 43 | 44 | using namespace std; 45 | void go(int* arr, int n){ 46 | sort(arr, arr+n, greater()); 47 | 48 | for (int i = 0; i < n-2; ++i) 49 | { 50 | int s1, s2, s3; 51 | 52 | s1 = arr[i]; 53 | s2 = arr[i+1]; 54 | s3 = arr[i+2]; 55 | 56 | if (s2+s3>s1 && s1+s2>s3 && s3+s1>s2) 57 | { 58 | cout << s3<<" "<>n; 79 | 80 | int* arr = new int[n]; 81 | 82 | for (int i = 0; i < n; ++i) 83 | { 84 | cin>>arr[i] 85 | } 86 | go(arr, n); 87 | 88 | delete [] arr; 89 | 90 | return 0 ; 91 | 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Searching and sorting applications/Momos market.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | /* 11 | PROBLEM STATEMENT 12 | Shreya loves to eat momos. Her mother gives her money to buy vegetables but she 13 | manages to save some money out of it daily. After buying vegetables, she goes to 14 | "Momos Market", where there are ‘n’ number of shops of momos. Each of the shop of 15 | momos has a rate per momo. She visits the market and starts buying momos (one from 16 | each shop) starting from the first shop. She will visit the market for ‘q’ days. 17 | You have to tell that how many momos she can buy at each day if she starts buying 18 | from the first shop daily. She cannot use the remaining money of one day on some other 19 | day. But she will save them for other expenses in future, so, 20 | you also need to tell the sum of money left with her at the end of each day. 21 | */ 22 | 23 | #include 24 | 25 | using namespace std; 26 | pair go(vector sum, int q){ 27 | //cout << "q "<sum.size()-1) 32 | { 33 | pos--; 34 | } 35 | if (sum.at(pos) > q) 36 | { 37 | pos--; 38 | } 39 | //cout << "Position "<< pos << '\n'; 40 | 41 | if (pos<0) 42 | { 43 | int momos = 0; 44 | int money_left = q; 45 | return make_pair(momos, money_left); 46 | } 47 | 48 | 49 | int momos = pos+1; 50 | int money_left = q-sum.at(pos); 51 | return make_pair(momos, money_left); 52 | 53 | } 54 | 55 | 56 | int main( int argc , char ** argv ) 57 | { 58 | ios_base::sync_with_stdio(false) ; 59 | cin.tie(NULL) ; 60 | 61 | int n; 62 | cin>>n; 63 | 64 | std::vector v; 65 | std::vector sum; 66 | int temp_sum = 0; 67 | 68 | while(n--){ 69 | int a; 70 | cin>>a; 71 | v.push_back(a); 72 | temp_sum+=a; 73 | sum.push_back(temp_sum); 74 | } 75 | 76 | int q; 77 | cin>>q; 78 | while(q--){ 79 | int total_money; 80 | cin>>total_money; 81 | 82 | pair ans = make_pair(0,0); 83 | ans = go(sum, total_money); 84 | 85 | cout << ans.first<<" "<< ans.second << '\n'; 86 | 87 | } 88 | 89 | return 0 ; 90 | 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /DPandbitmasking/Countingstrings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given a string 's' consisting of upper case alphabets, i.e. from 'A' to 'Z'. Your task is to find how many strings 't' with length equal to that of 's', also consisting of upper case alphabets are there satisfying the following conditions: 11 | -> String 't' is lexicographical larger than string 's'. 12 | -> When you write both 's' and 't' in the reverse order, 't' is still lexicographical larger than 's'. 13 | Find out number of such strings 't'. As the answer could be very large, take modulo 10^9 + 7. 14 | */ 15 | 16 | #include 17 | using namespace std; 18 | 19 | typedef long long ll; 20 | typedef unordered_map umapii; 21 | typedef unordered_map umapib; 22 | typedef unordered_map umapsi; 23 | typedef unordered_map umapss; 24 | typedef map mapsi; 25 | typedef map, int> mappiii; 26 | typedef map mapii; 27 | typedef pair pii; 28 | typedef pair pll; 29 | typedef unordered_set useti; 30 | 31 | #define uset unordered_set 32 | #define it iterator 33 | #define mp make_pair 34 | #define pb push_back 35 | #define all(x) (x).begin(), (x).end() 36 | #define f first 37 | #define s second 38 | #define MOD 1000000007 39 | 40 | #include 41 | using namespace std; 42 | long long ans[100001],mod=1e9+7; 43 | int go(string s){ 44 | string str=""; 45 | int n=s.length(); 46 | for(int i=n-1;i>=1;i--) 47 | str.push_back(s.at(i)); 48 | for(int i=0;i>s; 77 | int ans = countStrings(s); 78 | cout< 33 | 34 | using namespace std; 35 | 36 | 37 | void print(int** edges, int n, int* visited, int sv){ 38 | //cout << sv << '\n'; 39 | visited[sv] = 1; 40 | 41 | queue temp; 42 | temp.push(sv); 43 | while(!temp.empty()){ 44 | int top = temp.front(); 45 | temp.pop(); 46 | cout << top << " "; 47 | //visited[temp] = 1; 48 | 49 | for (int i = 0; i < n; ++i) 50 | { 51 | if (i==top) 52 | { 53 | continue; 54 | } 55 | if (edges[top][i] == 1 && visited[i] == 0) 56 | { 57 | temp.push(i); 58 | visited[i] = 1; 59 | } 60 | } 61 | } 62 | 63 | return; 64 | } 65 | 66 | 67 | int main( int argc , char ** argv ) 68 | { 69 | ios_base::sync_with_stdio(false) ; 70 | cin.tie(NULL) ; 71 | 72 | int n, e; 73 | cin>>n>>e; 74 | 75 | int** edges = new int*[n]; 76 | for (int i = 0; i < n; ++i) 77 | { 78 | edges[i] = new int[n]; 79 | for (int j = 0; j < n; ++j) 80 | { 81 | edges[i][j] = 0; 82 | } 83 | } 84 | 85 | int* visited = new int[n]; 86 | 87 | for (int i = 0; i < n; ++i) 88 | { 89 | visited[i] = 0; 90 | } 91 | 92 | for (int i = 0; i < e; ++i) 93 | { 94 | int a, b; 95 | cin>>a>>b; 96 | 97 | edges[a][b] = 1; 98 | edges[b][a] = 1; 99 | } 100 | 101 | 102 | 103 | print(edges, n, visited, 0); 104 | 105 | 106 | 107 | 108 | return 0 ; 109 | 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /Graphs1/strongly_connected_components.cpp: -------------------------------------------------------------------------------- 1 | // Strongly connected components of graph 2 | #include 3 | using namespace std; 4 | void addEdge(vector> adj[], int u, int v, int w){ 5 | adj[u].push_back({v, w}); 6 | } 7 | void dfsrec(vector> adj[], int V, int u, vector &visited){ 8 | visited[u] = true; 9 | for(auto x : adj[u]){ 10 | if(!visited[x.first]) 11 | dfsrec(adj, V, x.first, visited); 12 | } 13 | cout << u << " "; 14 | } 15 | void dfs(vector> adj[], int V){ 16 | vector visited(V, false); 17 | if(V <= 0) 18 | return; 19 | cout << endl; 20 | for(int i = 0; i < V; i++){ 21 | if(!visited[i]){ 22 | dfsrec(adj, V, i, visited); 23 | } 24 | } 25 | cout << endl; 26 | } 27 | void indegree(vector> adj[], int V, vector &indeg){ 28 | for(int i = 0; i < V; i++){ 29 | for(auto x : adj[i]){ 30 | indeg[x.first]++; 31 | } 32 | } 33 | } 34 | void dfsrec(vector> adj[], int u, stack &s, vector &visited){ 35 | for(auto x : adj[u]){ 36 | if(!visited[x.first]) 37 | { 38 | visited[x.first] = true; 39 | dfsrec(adj, x.first, s, visited); 40 | } 41 | } 42 | s.push(u); 43 | } 44 | void rev(vector> adj[], vector> adjrev[], int V){ 45 | for(int i = 0; i < V; i++){ 46 | for(auto x : adj[i]){ 47 | adjrev[x.first].push_back({i, 0}); 48 | } 49 | } 50 | } 51 | int scc(vector> adj[], int V){ 52 | stack s; 53 | vector visited(V, false); 54 | for(int i = 0; i < V; i++){ 55 | if(!visited[i]){ 56 | visited[i] = true; 57 | dfsrec(adj, i, s, visited); 58 | } 59 | } 60 | //show(s); 61 | int c = 0; 62 | vector> adjrev[V]; 63 | rev(adj, adjrev, V); 64 | vector visitedd(V, false); 65 | cout << "Strongly connected components are\n"; 66 | while(!s.empty()){ 67 | int u = s.top(); 68 | s.pop(); 69 | if(!visitedd[u]){ 70 | dfsrec(adjrev, V, u, visitedd); 71 | cout << endl; 72 | c++; 73 | } 74 | } 75 | return c; 76 | } 77 | int main(){ 78 | int V = 6; 79 | vector> adj[V]; 80 | addEdge(adj, 0, 1, 10); 81 | addEdge(adj, 4, 2, 10); 82 | addEdge(adj, 4, 5, 30); 83 | addEdge(adj, 3, 4, 90); 84 | addEdge(adj, 2, 1, 10); 85 | addEdge(adj, 3, 5, 30); 86 | addEdge(adj, 1, 4, 90); 87 | addEdge(adj, 4, 0, 100); 88 | scc(adj, V); 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Language-Tools/Tripletsum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given a random integer array and a number x. Find and print the triplets of elements in the array which sum to x. 11 | While printing a triplet, print the smallest element first. 12 | That is, if a valid triplet is (6, 5, 10) print "5 6 10". 13 | There is no constraint that out of 5 triplets which have to be printed on 1st line. 14 | You can print triplets in any order, just be careful about the order of elements in a triplet. 15 | */ 16 | 17 | 18 | #include 19 | 20 | using namespace std; 21 | void FindTriplet(int arr[], int size, int x) { 22 | sort(arr, arr+size); 23 | //int end = size-1; 24 | for (int i = 0; i < size; i++) 25 | { 26 | int val = x-arr[i]; 27 | int j = i+1; 28 | int k = size-1; 29 | while(jval) 33 | { 34 | k--; 35 | 36 | }else if (arr[j]+arr[k]= j; ptr--) 55 | { 56 | if (arr[ptr] == arr[k]) 57 | { 58 | rightcount++; 59 | }else 60 | break; 61 | } 62 | 63 | int total = leftcount*rightcount; 64 | // cout<>size; 92 | 93 | int *arr=new int[1+size]; 94 | 95 | for(int i=0;i>arr[i]; 97 | cin>>x; 98 | 99 | FindTriplet(arr,size,x); 100 | 101 | return 0; 102 | 103 | 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Binary Search/1199C-MP3: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define int long long 4 | #define MAX (int)(1e7+5) 5 | #define MAX2D (int)(1e3+5) 6 | #define itn int 7 | #define inf 5e18 8 | #define MOD (int)(1e9 + 7) 9 | #define pb push_back 10 | #define mk make_pair 11 | #define all(x) x.begin(), x.end() 12 | #define rall(x) x.rbegin(),x.rend() 13 | #define pii pair 14 | #define vii vector 15 | #define vi vector 16 | #define test \ 17 | int t; \ 18 | cin >> t; \ 19 | while (t--) 20 | #define fast \ 21 | ios_base ::sync_with_stdio(0); \ 22 | cin.tie(NULL); \ 23 | cout.tie(NULL) 24 | using namespace std; 25 | 26 | int n, I, maxK; 27 | const int N = 4e5+1; 28 | struct custom_hash { 29 | static uint64_t splitmix64(uint64_t x) { 30 | // http://xorshift.di.unimi.it/splitmix64.c 31 | x += 0x9e3779b97f4a7c15; 32 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 33 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 34 | return x ^ (x >> 31); 35 | } 36 | 37 | size_t operator()(uint64_t x) const { 38 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 39 | return splitmix64(x + FIXED_RANDOM); 40 | } 41 | }; 42 | //Now we can simply define our unordered_map or our gp_hash_table as follows: 43 | //Use either, both are significantly fast 44 | 45 | 46 | bool ok(int m, vi a) { 47 | unordered_map mp; 48 | for(int i=0;i>n>>I; 77 | vi a(n); 78 | I *= 8; 79 | for(int i=0;i>a[i]; 80 | sort(all(a)); 81 | maxK = pow(2,(I/n)); 82 | int ans = g(a); 83 | cout< 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef unordered_map umapii; 19 | typedef unordered_map umapib; 20 | typedef unordered_map umapsi; 21 | typedef unordered_map umapss; 22 | typedef map mapsi; 23 | typedef map, int> mappiii; 24 | typedef map mapii; 25 | typedef pair pii; 26 | typedef pair pll; 27 | typedef unordered_set useti; 28 | 29 | #define uset unordered_set 30 | #define it iterator 31 | #define mp make_pair 32 | #define pb push_back 33 | #define all(x) (x).begin(), (x).end() 34 | #define f first 35 | #define s second 36 | #define MOD 1000000007 37 | 38 | // S and T - input strings 39 | // You need to check if string T is present in S or not 40 | 41 | int findString(char S[], char T[]) { 42 | int length = strlen(T); 43 | int n = strlen(S); 44 | 45 | 46 | vector pitable(length, 0); 47 | int i = 0; 48 | int j = i+1; 49 | 50 | while(j 35 | 36 | using namespace std; 37 | int countWaysToMakeChange(int S[], int n, int m){ 38 | int i, j, x, y; 39 | 40 | // We need n+1 rows as the table 41 | // is constructed in bottom up 42 | // manner using the base case 0 43 | // value case (n = 0) 44 | int table[n + 1][m]; 45 | 46 | // Fill the enteries for 0 47 | // value case (n = 0) 48 | for (i = 0; i < m; i++) 49 | table[0][i] = 1; 50 | 51 | // Fill rest of the table entries 52 | // in bottom up manner 53 | for (i = 1; i < n + 1; i++) 54 | { 55 | for (j = 0; j < m; j++) 56 | { 57 | // Count of solutions including S[j] 58 | x = (i-S[j] >= 0) ? table[i - S[j]][j] : 0; 59 | 60 | // Count of solutions excluding S[j] 61 | y = (j >= 1) ? table[i][j - 1] : 0; 62 | 63 | // total count 64 | table[i][j] = x + y; 65 | } 66 | } 67 | return table[n][m - 1]; 68 | 69 | } 70 | 71 | 72 | int main( int argc , char ** argv ) 73 | { 74 | ios_base::sync_with_stdio(false) ; 75 | cin.tie(NULL) ; 76 | int numDenominations; 77 | cin >> numDenominations; 78 | int* denominations = new int[numDenominations]; 79 | for(int i = 0; i < numDenominations; i++){ 80 | cin >> denominations[i]; 81 | } 82 | int value; 83 | cin >> value; 84 | cout << countWaysToMakeChange(denominations, numDenominations, value)< 40 | 41 | using namespace std; 42 | 43 | void marker(int** edges, int n, int* visited, int sv){ 44 | if (n==1) 45 | { 46 | visited[sv] = 1; 47 | return; 48 | } 49 | 50 | visited[sv] = 1; 51 | 52 | for (int i = 0; i < n; ++i) 53 | { 54 | if (edges[sv][i]==1 && visited[i]==0) 55 | { 56 | marker(edges, n, visited, i); 57 | } 58 | } 59 | return; 60 | 61 | } 62 | 63 | int main( int argc , char ** argv ) 64 | { 65 | ios_base::sync_with_stdio(false) ; 66 | cin.tie(NULL) ; 67 | 68 | int n, e; 69 | cin>>n>>e; 70 | 71 | int** edges = new int*[n]; 72 | 73 | for (int i = 0; i < n; ++i) 74 | { 75 | edges[i] = new int[n]; 76 | for (int j = 0; j < n; ++j) 77 | { 78 | edges[i][j] = 0; 79 | } 80 | 81 | } 82 | 83 | int* visited = new int[n]; 84 | for (int i = 0; i < n; ++i) 85 | { 86 | visited[i] = 0; 87 | } 88 | 89 | for (int i = 0; i < e; ++i) 90 | { 91 | int a, b; 92 | cin>>a>>b; 93 | 94 | edges[a][b] = 1; 95 | edges[b][a] = 1; 96 | } 97 | 98 | 99 | marker(edges, n, visited, 0); 100 | 101 | for (int i = 0; i < n; ++i) 102 | { 103 | if (visited[i] == 0) 104 | { 105 | cout << "false" << '\n'; 106 | return 0; 107 | } 108 | 109 | } 110 | cout << "true" << '\n'; 111 | 112 | 113 | return 0 ; 114 | 115 | 116 | 117 | } 118 | -------------------------------------------------------------------------------- /Dynamic programming 2/Miserman.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | /* 9 | Jack is a wise and miser man. Always tries to save his money. 10 | One day, he wants to go from city A to city B. Between A and B, there are N number of cities(including B and excluding A) and in each city there are M buses numbered from 1 to M. And the fare of each bus is different. Means for all N*M busses, fare (K) may be different or same. Now Jack has to go from city A to city B following these conditions: 11 | 1. At every city, he has to change the bus. 12 | 2. And he can switch to only those buses which have number either equal or 1 less or 1 greater to the previous. 13 | You are to help Jack to go from A to B by spending the minimum amount of money. 14 | N, M, K <= 100. 15 | Input 16 | Line 1: N M 17 | 18 | Line 2: NxM Grid 19 | 20 | Each row lists the fares the M busses to go form the current city to the next city. 21 | Output 22 | Single Line containing the minimum amount of fare that Jack has to give. 23 | Sample Input 24 | 5 5 25 | 1 3 1 2 6 26 | 10 2 5 4 15 27 | 10 9 6 7 1 28 | 2 7 1 5 3 29 | 8 2 6 1 9 30 | Sample Output 31 | 10 32 | */ 33 | 34 | #include 35 | using namespace std; 36 | 37 | int findFare(int **input, int n, int m, int row, int col, int **dp){ 38 | if(row == n) return 0; 39 | 40 | if(dp[row][col] != -1) return dp[row][col]; 41 | 42 | int a = INT_MAX, b = INT_MAX, c = INT_MAX; 43 | if(col-1 >= 0) a = input[row][col]+findFare(input, n, m, row+1, col-1, dp); 44 | b = input[row][col]+findFare(input, n, m, row+1, col, dp); 45 | if(col+1 < m) c = input[row][col]+findFare(input, n, m, row+1, col+1, dp); 46 | 47 | dp[row][col] = min(a, min(b, c)); 48 | return min(a, min(b, c)); 49 | } 50 | 51 | int main(){ 52 | int n, m; 53 | cin >> n >> m; 54 | 55 | int **input = new int *[n]; 56 | for(int i = 0; i < n; i++){ 57 | input[i] = new int[m]; 58 | for(int j = 0; j < m; j++) cin >> input[i][j]; 59 | } 60 | 61 | int **dp = new int *[n]; 62 | for(int i = 0; i < n; i++) dp[i] = new int[m]; 63 | 64 | int minFare = INT_MAX; 65 | for(int i = 0; i < m; i++){ 66 | for(int j = 0; j < n; j++){ 67 | for(int k = 0; k < m; k++) dp[j][k] = -1; 68 | } 69 | 70 | minFare = min(minFare, findFare(input, n, m, 0, i, dp)); 71 | } 72 | 73 | cout << minFare << endl; 74 | } 75 | -------------------------------------------------------------------------------- /fileupload/templates/base.html: -------------------------------------------------------------------------------- 1 | {% load static %} 2 | 3 | 4 | 5 | 6 | 7 | 8 | {% block title %}My Site{% endblock %} 9 | 10 | 11 | 29 |
30 |
31 |
32 |
33 |
34 | {% block content %} 35 | {% endblock %} 36 |
37 |
38 |
39 |
40 |
41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /DP1(Very important)/Vanya and gcd.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Vanya has been studying all day long about sequences and other Complex Mathematical Terms. She thinks she has now become really good at it. So, her friend Vasya decides to test her knowledge and keeps the following challenge it front of her: 11 | Vanya has been given an integer array A of size N. Now, she needs to find the number of increasing sub-sequences of this array with length ≥1 and GCD=1. A sub-sequence of an array is obtained by deleting some (or none) elements and maintaining the relative order of the rest of the elements. As the answer may be large, print it Modulo 109+7 12 | She finds this task really easy, and thinks that you can do it too. Can you? 13 | Input Format: 14 | The first line contains a single integer N denoting size of array A. The next line contains N space separated integers denoting the elements of array A 15 | Output Format: 16 | Print the required answer Modulo 10^9+7 on a single line. 17 | Constraints: 18 | 1≤N≤500 19 | 20 | 1≤A[i]≤100 21 | Sample Input 22 | 3 23 | 1 2 3 24 | Sample Output 25 | 5 26 | */ 27 | 28 | #include 29 | 30 | using namespace std; 31 | int go(vector v){ 32 | int n = v.size(); 33 | vector> dp(101, vector(n, 0)); 34 | 35 | for (int i = 0; i < n; ++i) 36 | { 37 | dp[v[i]][i] = 1; 38 | } 39 | 40 | for (int i = 0; i < n; ++i) 41 | { 42 | for (int j = 0; j < i; ++j) 43 | { 44 | if (v[i]>v[j]) 45 | { 46 | for (int k = 1; k < 101; ++k) 47 | { 48 | int gcn = __gcd(k, v[i]); 49 | dp[gcn][i] = (dp[gcn][i]+dp[k][j])%1000000007; 50 | } 51 | } 52 | } 53 | } 54 | 55 | int ans = 0; 56 | 57 | // for (int i = 1; i < 101; ++i) 58 | // { 59 | // for (int j = 0; j < n; ++j) 60 | // { 61 | // cout<>n; 86 | vector v(n, 0); 87 | 88 | for (int i = 0; i < n; ++i) 89 | { 90 | cin>>v[i]; 91 | } 92 | 93 | cout << go(v) << '\n'; 94 | 95 | 96 | 97 | return 0 ; 98 | 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /Graphs1/haspath.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | 10 | /* 11 | Given an undirected graph G(V, E) and two vertices v1 and v2(as integers), check if there exists any path between them or not. Print true or false. 12 | V is the number of vertices present in graph G and vertices are numbered from 0 to V-1. 13 | E is the number of edges present in graph G. 14 | Input Format : 15 | Line 1: Two Integers V and E (separated by space) 16 | Next E lines : Two integers a and b, denoting that there exists an edge between vertex a and vertex b (separated by space) 17 | Line (E+2) : Two integers v1 and v2 (separated by space) 18 | Output Format : 19 | true or false 20 | Constraints : 21 | 2 <= V <= 1000 22 | 1 <= E <= 1000 23 | 0 <= v1, v2 <= V-1 24 | Sample Input 1 : 25 | 4 4 26 | 0 1 27 | 0 3 28 | 1 2 29 | 2 3 30 | 1 3 31 | Sample Output 1 : 32 | true 33 | Sample Input 2 : 34 | 6 3 35 | 5 3 36 | 0 1 37 | 3 4 38 | 0 3 39 | Sample Output 2 : 40 | false 41 | */ 42 | 43 | #include 44 | 45 | using namespace std; 46 | bool hasPath(int** edges, int n, int a, int b, int* visited){ 47 | //Base Case 48 | if (a==b) 49 | { 50 | return 1; 51 | } 52 | int result = 0; 53 | 54 | for (int i = 0; i < n; ++i) 55 | { 56 | if (edges[a][i] == 1 && visited[i] == 0) 57 | { 58 | visited[i] = 1; 59 | result = hasPath(edges, n, i, b, visited); 60 | if (result==1) 61 | { 62 | return 1; 63 | } 64 | } 65 | } 66 | 67 | // for (int i = 0; i < n; ++i) 68 | // { 69 | // cout << visited[i] << ' '; 70 | // } 71 | 72 | return result; 73 | 74 | } 75 | 76 | int main( int argc , char ** argv ) 77 | { 78 | ios_base::sync_with_stdio(false) ; 79 | cin.tie(NULL) ; 80 | 81 | int n, e; 82 | cin>>n>>e; 83 | 84 | int** edges = new int*[n]; 85 | 86 | for (int i = 0; i < n; ++i) 87 | { 88 | edges[i] = new int[n]; 89 | for (int j = 0; j < n; ++j) 90 | { 91 | edges[i][j] = 0; 92 | } 93 | 94 | } 95 | 96 | int* visited = new int[n]; 97 | for (int i = 0; i < n; ++i) 98 | { 99 | visited[i] = 0; 100 | } 101 | 102 | for (int i = 0; i < e; ++i) 103 | { 104 | int a, b; 105 | cin>>a>>b; 106 | 107 | edges[a][b] = 1; 108 | edges[b][a] = 1; 109 | } 110 | 111 | int c, d; 112 | cin>>c>>d; 113 | 114 | if(hasPath(edges, n, c, d, visited)){ 115 | cout << "true" << '\n'; 116 | }else{ 117 | cout << "false" << '\n'; 118 | } 119 | 120 | return 0 ; 121 | 122 | 123 | 124 | } 125 | -------------------------------------------------------------------------------- /Dynamic programming 2/Smallestsuperseq.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | Given two strings S and T, find and return the length of their smallest super-sequence. 11 | A shortest super sequence of two strings is defined as the shortest possible string containing both strings as subsequences. 12 | Note that if the two strings do not have any common characters, then return the sum of lengths of the two strings. 13 | Input Format: 14 | Line 1 : A string 15 | Line 2: Another string 16 | Output Format: 17 | Length of the smallest super-sequence of given two strings. 18 | Sample Input: 19 | ab 20 | ac 21 | Sample Output: 22 | 3 23 | Sample Output Explanation: 24 | Their smallest super-sequence can be "abc" which has length=3. 25 | */ 26 | #include 27 | 28 | using namespace std; 29 | 30 | int go(string s1, string s2, int i, int j, int** mem){ 31 | int m = s1.size(); 32 | int n = s2.size(); 33 | 34 | //cout << m<<" "< m-1) 37 | { 38 | return n-j; 39 | } 40 | 41 | if (j > n-1) 42 | { 43 | return m-i; 44 | } 45 | 46 | if (mem[i][j]!=-1) 47 | { 48 | return mem[i][j]; 49 | } 50 | 51 | if (s1[i] == s2[j]) 52 | { 53 | mem[i][j] = 1 + go(s1, s2, i+1, j+1, mem); 54 | return mem[i][j]; 55 | }else{ 56 | mem[i][j] = min(1+go(s1, s2, i+1, j, mem), 1+go(s1, s2, i, j+1, mem)); 57 | return mem[i][j]; 58 | } 59 | 60 | } 61 | 62 | int smallestSuperSequence(char str1[], int len1, char str2[], int len2) { 63 | string s1 = ""; 64 | string s2 = ""; 65 | 66 | for (int i = 0; i < len1; ++i) 67 | { 68 | s1 += str1[i]; 69 | } 70 | 71 | for (int i = 0; i < len2; ++i) 72 | { 73 | s2 += str2[i]; 74 | } 75 | 76 | int** mem = new int*[len1+1]; 77 | 78 | for (int i = 0; i < len1+1; ++i) 79 | { 80 | mem[i] = new int[len2+1]; 81 | for (int j = 0; j < len2+1; ++j) 82 | { 83 | mem[i][j] = -1; 84 | } 85 | } 86 | 87 | 88 | return go(s1, s2, 0, 0, mem); 89 | 90 | 91 | 92 | } 93 | 94 | int main( int argc , char ** argv ) 95 | { 96 | ios_base::sync_with_stdio(false) ; 97 | cin.tie(NULL) ; 98 | 99 | char str1[50], str2[50]; 100 | cin>>str1; 101 | cin>>str2; 102 | int len1 = strlen(str1); 103 | int len2 = strlen(str2); 104 | int min_len = smallestSuperSequence(str1, len1, str2, len2); 105 | cout< 46 | 47 | using namespace std; 48 | 49 | long go(vector candies, long n, long k){ 50 | //size is n+1 51 | 52 | sort(candies.begin(), candies.end()); 53 | 54 | long sum = 0; 55 | long buffer = 0; 56 | for (long i = 1; i <=k; ++i) 57 | { 58 | sum += i*candies[i]; 59 | sum -= (k-i+1)*candies[i]; 60 | 61 | buffer += candies[i]; 62 | } 63 | 64 | 65 | long ans = sum; 66 | for (long i = 2; i <= n-k+1; ++i) 67 | { 68 | sum = sum - 2*(buffer - candies[i-1])+(k-1)*candies[i-1]+(k-1)*candies[i+k-1]; 69 | 70 | buffer = buffer - candies[i-1]+candies[i+k-1]; 71 | 72 | 73 | ans = min(ans, sum); 74 | } 75 | 76 | return ans; 77 | } 78 | 79 | int main( int argc , char ** argv ) 80 | { 81 | ios_base::sync_with_stdio(false) ; 82 | cin.tie(NULL) ; 83 | 84 | long n, k; 85 | cin>>n>>k; 86 | 87 | vector candies(n+1, 0); 88 | for (int i = 1; i <= n; ++i) 89 | { 90 | cin>>candies[i]; 91 | } 92 | 93 | cout << go(candies, n, k) << '\n'; 94 | 95 | return 0 ; 96 | 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /DP1(Very important)/Allzeroes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | Given a n*m matrix which contains only 0s and 1s, find out the size of maximum square sub-matrix with all 0s. You need to return the size of square with all 0s. 10 | Input format : 11 | Line 1 : n and m (space separated positive integers) 12 | Next n lines : m elements of each row (separated by space). 13 | Output Format: 14 | Line 1 : Size of maximum square sub-matrix 15 | Sample Input : 16 | 3 3 17 | 1 1 0 18 | 1 1 1 19 | 1 1 1 20 | Sample Output : 21 | 1 22 | */ 23 | 24 | #include 25 | 26 | using namespace std; 27 | 28 | int lef(int** arr, int m, int n){ 29 | int ans = 0; 30 | for (int i = n-1; i >= 0; --i) 31 | { 32 | if (arr[m][i] != 0) 33 | { 34 | return ans; 35 | } 36 | 37 | if (arr[m][i] == 0) 38 | { 39 | ans++; 40 | } 41 | } 42 | 43 | return ans; 44 | 45 | } 46 | 47 | int up(int** arr, int m, int n){ 48 | int ans = 0; 49 | for (int i = m-1; i >= 0; --i) 50 | { 51 | if (arr[i][n] != 0) 52 | { 53 | return ans; 54 | } 55 | 56 | if (arr[i][n] == 0) 57 | { 58 | ans++; 59 | } 60 | } 61 | 62 | return ans; 63 | 64 | } 65 | 66 | 67 | int findMaxSquareWithAllZeros(int** arr, int m, int n){ 68 | 69 | vector> dp(m, (vector(n, 0))); 70 | 71 | for (int i = 0; i < n; ++i) 72 | { 73 | if (arr[0][i] == 0) 74 | { 75 | dp[0][i] = 1; 76 | }else{ 77 | dp[0][i] = 0; 78 | } 79 | } 80 | 81 | for (int j = 0; j < m; ++j) 82 | { 83 | dp[j][0] = (arr[j][0] == 0); 84 | } 85 | 86 | for (int i = 1; i < m; ++i) 87 | { 88 | for (int j = 1; j < n; ++j) 89 | { 90 | if (arr[i][j] == 0) 91 | { 92 | int dig = dp[i-1][j-1]; 93 | int upper = up(arr, i, j); 94 | int left = lef(arr, i, j); 95 | int z = min(dig, min(upper, left)); 96 | dp[i][j] = z+1; 97 | 98 | }else{ 99 | dp[i][j] = 0; 100 | } 101 | 102 | } 103 | } 104 | 105 | int max = 0; 106 | for (int i = 0; i < m; ++i) 107 | { 108 | for (int j = 0; j < n; ++j) 109 | { 110 | max = std::max(max, dp[i][j]); 111 | } 112 | } 113 | 114 | return max; 115 | 116 | 117 | } 118 | int main( int argc , char ** argv ) 119 | { 120 | ios_base::sync_with_stdio(false) ; 121 | cin.tie(NULL) ; 122 | 123 | 124 | 125 | 126 | return 0 ; 127 | 128 | 129 | 130 | } 131 | -------------------------------------------------------------------------------- /Graphs1/Islandsc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | /* 9 | An island is a small piece of land surrounded by water . A group of islands is said to be connected if we can reach from any given island to any other island in the same group . Given N islands (numbered from 1 to N) and two lists of size M (u and v) denoting island u[i] is connected to island v[i] and vice versa . Can you count the number of connected groups of islands. 10 | Constraints : 11 | 1<=N<=100 12 | 1<=M<=(N*(N-1))/2 13 | 1<=u[i],v[i]<=N 14 | Input Format : 15 | Line 1 : Two integers N and M 16 | Line 2 : List u of size of M 17 | Line 3 : List v of size of M 18 | Output Return Format : 19 | The count the number of connected groups of islands 20 | Sample Input : 21 | 2 1 22 | 1 23 | 2 24 | Sample Output : 25 | 1 26 | */ 27 | 28 | #include 29 | 30 | using namespace std; 31 | 32 | void DFS(int** arr, int* visited, int n, int sv){ 33 | 34 | visited[sv] = 1; 35 | 36 | for (int i = 0; i < n; ++i) 37 | { 38 | if (i==sv) 39 | { 40 | continue; 41 | } 42 | if (arr[sv][i] == 1 && visited[i] == 0) 43 | { 44 | DFS(arr, visited, n, i); 45 | } 46 | } 47 | 48 | return; 49 | } 50 | 51 | int solve(int n,int m,vectoru,vectorv) 52 | { 53 | int** arr = new int*[n]; 54 | for (int i = 0; i < n; ++i) 55 | { 56 | arr[i] = new int[n]; 57 | for (int j = 0; j < n; ++j) 58 | { 59 | arr[i][j] = 0; 60 | } 61 | } 62 | 63 | 64 | 65 | for (int i = 0; i < m; ++i) 66 | { 67 | arr[u[i]-1][v[i]-1] = 1; 68 | arr[v[i]-1][u[i]-1] = 1; 69 | } 70 | 71 | // for (int i = 0; i < n; ++i) 72 | // { 73 | // for (int j = 0; j < n; ++j) 74 | // { 75 | // cout<u,v; 107 | cin>>n>>m; 108 | for(int i=0;i>x; 112 | u.push_back(x); 113 | } 114 | for(int i=0;i>x; 118 | v.push_back(x); 119 | } 120 | cout< 31 | using namespace std; 32 | 33 | typedef long long ll; 34 | #define MOD 1000000007 35 | ll dp[100][100][100]; 36 | 37 | int memsolver(string a,string b,string c){ 38 | ll x = a.size(); 39 | ll y = b.size(); 40 | ll z = c.size(); 41 | 42 | ll ans = 0; 43 | 44 | if (z == 0) 45 | { 46 | return 1; 47 | } 48 | 49 | if (x<=0 && y<=0) 50 | { 51 | return 0; 52 | } 53 | 54 | if (dp[x][y][z]!=-1) 55 | { 56 | return dp[x][y][z]; 57 | } 58 | 59 | for (ll i = 0; i < a.size(); ++i) 60 | { 61 | if (a[i]==c[0]) 62 | { 63 | ans+=memsolver(a.substr(i+1), b, c.substr(1)); 64 | } 65 | } 66 | 67 | 68 | 69 | for (ll i = 0; i < b.size(); ++i) 70 | { 71 | if (b[i] == c[0]) 72 | { 73 | ans+=memsolver(a, b.substr(i+1), c.substr(1)); 74 | } 75 | } 76 | 77 | dp[x][y][z] = ans%MOD; 78 | return ans%MOD; 79 | } 80 | 81 | int solve(string a,string b,string c) 82 | { 83 | memset(dp, -1, sizeof(dp)); 84 | return memsolver(a, b, c); 85 | } 86 | 87 | int main( int argc , char ** argv ) 88 | { 89 | ios_base::sync_with_stdio(false) ; 90 | cin.tie(NULL) ; 91 | 92 | string a,b,c; 93 | cin>>a>>b>>c; 94 | 95 | cout< 41 | 42 | using namespace std; 43 | 44 | int go(int c, vector barn){ 45 | 46 | sort(barn.begin(), barn.end()); 47 | 48 | int n = barn.size(); 49 | int start = 0; 50 | int end = barn[n-1]-barn[0]; 51 | int max_dist = end+start; 52 | int mid = max_dist/2; 53 | int ans = 0; 54 | 55 | 56 | 57 | while(start<=end){ 58 | int last_cow_at = 0; 59 | int cow = c-1; 60 | mid = max_dist/2; 61 | 62 | for (int i = 0; i < n; ++i) 63 | { 64 | 65 | if (barn[i]-barn[last_cow_at]>=mid) 66 | { 67 | 68 | cow--; 69 | last_cow_at = i; 70 | if (cow == 0) 71 | { 72 | ans = max(ans, mid); 73 | break; 74 | } 75 | 76 | } 77 | 78 | } 79 | if (cow==0) 80 | { 81 | start = mid+1; 82 | end = end; 83 | }else{ 84 | start = start; 85 | end = mid-1; 86 | } 87 | max_dist = end+start; 88 | 89 | } 90 | return ans; 91 | 92 | 93 | } 94 | 95 | 96 | int main( int argc , char ** argv ) 97 | { 98 | ios_base::sync_with_stdio(false) ; 99 | cin.tie(NULL) ; 100 | 101 | int t; 102 | cin >> t; 103 | 104 | while(t>0){ 105 | int n, c; 106 | cin>>n>>c; 107 | vector barn; 108 | 109 | while(n>0){ 110 | int b; 111 | cin>>b; 112 | barn.push_back(b); 113 | n--; 114 | } 115 | cout << go(c, barn) << '\n'; 116 | 117 | t--; 118 | } 119 | 120 | 121 | 122 | return 0 ; 123 | 124 | 125 | 126 | } 127 | -------------------------------------------------------------------------------- /Greedy/Weightedjob.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | You are given N jobs where every job is represented as: 12 | 1.Start Time 13 | 2.Finish Time 14 | 3.Profit Associated 15 | Find the maximum profit subset of jobs such that no two jobs in the subset overlap. 16 | Input 17 | The first line of input contains one integer denoting N. 18 | Next N lines contains three space separated integers denoting the start time, finish time and the 19 | profit associated with the ith job. 20 | Output 21 | Output one integer, the maximum profit that can be achieved. 22 | Constraints 23 | 1 ≤ N ≤ 10^6 24 | 1 ≤ ai, di, p ≤ 10^6 25 | Sample Input 26 | 4 27 | 3 10 20 28 | 1 2 50 29 | 6 19 100 30 | 2 100 200 31 | Sample Output 32 | 250 33 | 34 | 35 | */ 36 | 37 | 38 | #include 39 | using namespace std; 40 | typedef long long ll; 41 | 42 | struct job{ 43 | ll start, finish, profit; 44 | 45 | job(ll s, ll f, ll p){ 46 | start = s; 47 | finish = f; 48 | profit = p; 49 | } 50 | }; 51 | 52 | bool compare(job a, job b){ 53 | return a.finish < b.finish; 54 | } 55 | 56 | ll search(vector *input, ll limit, ll si, ll ei){ 57 | if(si > ei) return -1; 58 | 59 | if(si == ei){ 60 | if((input->at(si)).finish <= limit) return si; 61 | else return -1; 62 | } 63 | 64 | ll mid = (si+ei)/2; 65 | if((input->at(mid)).finish <= limit){ 66 | ll answer = search(input, limit, mid+1, ei); 67 | if(answer == -1) return mid; 68 | else return answer; 69 | } 70 | else return search(input, limit, si, mid-1); 71 | } 72 | 73 | int main(){ 74 | ll n; 75 | cin >> n; 76 | 77 | vector input; 78 | for(ll i = 0; i < n; i++){ 79 | ll s, f, p; 80 | cin >> s >> f >> p; 81 | input.push_back(job(s, f, p)); 82 | } 83 | 84 | sort(input.begin(), input.end(), compare); 85 | 86 | ll *dp = new ll[n]; 87 | dp[0] = input[0].profit; 88 | for(ll i = 1; i < n; i++){ 89 | ll include = input[i].profit; 90 | 91 | ll id = -1; 92 | id = search(&input, input[i].start, 0, i-1); 93 | // for(ll j = i-1; j >= 0; j--){ 94 | // if(input[j].finish <= input[i].start){ 95 | // id = j; 96 | // break; 97 | // } 98 | // } 99 | 100 | if(id != -1){ 101 | include += dp[id]; 102 | } 103 | 104 | dp[i] = max(dp[i-1], include); 105 | } 106 | 107 | cout << dp[n-1] << endl; 108 | } 109 | -------------------------------------------------------------------------------- /DP1(Very important)/longestbitonicsubarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | /* 8 | You are given an array of positive integers as input. Write a code to return the length of the largest such sub-array in which the values are arranged first in strictly ascending order and then in strictly descending order. 9 | Such a sub-array is known as bitonic sub-array. A purely increasing or purely decreasing subsequence will also be considered as a bitonic sequence with the other part empty. 10 | Note that the elements in bitonic sub-array need not be consecutive in the given array but the order should remain same. 11 | Input Format: 12 | Line 1 : A positive Integer N, i.e., the size of array 13 | Line 2 : N space-separated integers as elements of the array 14 | Output Format: 15 | Length of Largest Bitonic Sub-Array 16 | Input Constraints: 17 | 1<= N <= 10^5 18 | Sample Input 1: 19 | 6 20 | 15 20 20 6 4 2 21 | Sample Output 1: 22 | 5 23 | Sample Output 1 Explanation: 24 | Here, longest Bitonic sub-array is {15, 20, 6, 4, 2} which has length = 5. 25 | Sample Input 2: 26 | 2 27 | 1 5 28 | Sample Output 2: 29 | 2 30 | Sample Input 3: 31 | 2 32 | 5 1 33 | Sample Output 3: 34 | 2 35 | */ 36 | 37 | #include 38 | 39 | using namespace std; 40 | 41 | int longestBitonicSubarray(int *arr, int n) { 42 | int i, j; 43 | 44 | /* Allocate memory for LIS[] and initialize LIS values as 1 for 45 | all indexes */ 46 | int *lis = new int[n]; 47 | for (i = 0; i < n; i++) 48 | lis[i] = 1; 49 | 50 | /* Compute LIS values from left to right */ 51 | for (i = 1; i < n; i++) 52 | for (j = 0; j < i; j++) 53 | if (arr[i] > arr[j] && lis[i] < lis[j] + 1) 54 | lis[i] = lis[j] + 1; 55 | 56 | /* Allocate memory for lds and initialize LDS values for 57 | all indexes */ 58 | int *lds = new int [n]; 59 | for (i = 0; i < n; i++) 60 | lds[i] = 1; 61 | 62 | /* Compute LDS values from right to left */ 63 | for (i = n-2; i >= 0; i--) 64 | for (j = n-1; j > i; j--) 65 | if (arr[i] > arr[j] && lds[i] < lds[j] + 1) 66 | lds[i] = lds[j] + 1; 67 | 68 | 69 | /* Return the maximum value of lis[i] + lds[i] - 1*/ 70 | int max = lis[0] + lds[0] - 1; 71 | for (i = 1; i < n; i++) 72 | if (lis[i] + lds[i] - 1 > max) 73 | max = lis[i] + lds[i] - 1; 74 | return max; 75 | 76 | } 77 | int main( int argc , char ** argv ) 78 | { 79 | ios_base::sync_with_stdio(false) ; 80 | cin.tie(NULL) ; 81 | int n, input[100000]; 82 | cin>>n; 83 | for(int i=0; i>input[i]; 85 | } 86 | cout< 43 | 44 | using namespace std; 45 | 46 | long long helper(long long start, long long end, long long N, long long &ans){ 47 | // cout << "start: "< end) 51 | { 52 | return ans; 53 | } 54 | 55 | long long n = N; 56 | 57 | long long mid = (start+end)/2; 58 | //cout << "mid; "<=k && n>0){ 65 | sharma += k; 66 | n = n-k; 67 | singh += (n)/10; 68 | n = n-(n)/10; 69 | } 70 | 71 | sharma += n; 72 | // cout << "sharma: " <>n; 99 | 100 | cout << helper(1, n, n, n) << '\n'; 101 | 102 | 103 | return 0 ; 104 | 105 | 106 | 107 | } 108 | -------------------------------------------------------------------------------- /Greedy/Problemdiscussion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Harshit gave Aahad an array of size N and asked to minimize the difference between the maximum 12 | value and minimum value by modifying the array under the condition that each array element either 13 | increase or decrease by k(only once). 14 | It seems difficult for Aahad so he asked for your help 15 | Input Format 16 | The First line contains two space-separated integers: N,K 17 | Next lines contain N space-separated integers denoting elements of the array 18 | Output Format 19 | The output contains a single integer denoting the minimum difference between maximum value and 20 | the minimum value in the array 21 | Constraints 22 | 1 =< N <= 10^5 23 | 1 <= Ai,K <= 10^9 24 | Sample Input1: 25 | 3 6 26 | 1 15 10 27 | Sample Output1: 28 | 5 29 | Explaination 30 | We change from 1 to 6, 15 to 9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can't 31 | get a lower difference. 32 | 33 | */ 34 | 35 | 36 | #include 37 | 38 | using namespace std; 39 | 40 | int getMinDiff(int arr[], int n, int k) 41 | { 42 | if (n == 1) 43 | return 0; 44 | 45 | // Sort all elements 46 | sort(arr, arr+n); 47 | 48 | // Initialize result 49 | int ans = arr[n-1] - arr[0]; 50 | 51 | // Handle corner elements 52 | int small = arr[0] + k; 53 | int big = arr[n-1] - k; 54 | if (small > big) 55 | swap(small, big); 56 | 57 | // Traverse middle elements 58 | for (int i = 1; i < n-1; i ++) 59 | { 60 | int subtract = arr[i] - k; 61 | int add = arr[i] + k; 62 | 63 | // If both subtraction and addition 64 | // do not change diff 65 | if (subtract >= small || add <= big) 66 | continue; 67 | 68 | // Either subtraction causes a smaller 69 | // number or addition causes a greater 70 | // number. Update small or big using 71 | // greedy approach (If big - subtract 72 | // causes smaller diff, update small 73 | // Else update big) 74 | if (big - subtract <= add - small) 75 | small = subtract; 76 | else 77 | big = add; 78 | } 79 | 80 | return min(ans, big - small); 81 | } 82 | 83 | int main( int argc , char ** argv ) 84 | { 85 | ios_base::sync_with_stdio(false) ; 86 | cin.tie(NULL) ; 87 | 88 | int n,k; 89 | cin>>n>>k; 90 | 91 | int* arr = new int[n]; 92 | 93 | for (int i = 0; i < n; ++i) 94 | { 95 | cin>>arr[i]; 96 | } 97 | 98 | cout << getMinDiff(arr, n, k) << '\n'; 99 | 100 | 101 | return 0 ; 102 | 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /DPandbitmasking/Candy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Gary is a teacher at XYZ school. To reward his N students he bought a packet of N candies all with different flavours. But the problem is some students like certain flavour while some doesn't. Now Gary wants to know the number of ways he can distribute these N candies to his N students such that every student gets exactly one candy he likes. 11 | Input Format : 12 | Line 1 : An integer N (1<= N <= 16) denoting number of students and candies. 13 | Next N lines : N integers describing the preferences of one student. 1 at i'th (0 <= i < N) position denotes that this student likes i'th candy , 0 means he doesn't. 14 | Assume input to be 0-indexed based. 15 | Output Format : 16 | Return the number of ways Gary can distribute these N candies to his N students such that every student gets exactly one candy he likes.`` 17 | Sample Input: 18 | 3 19 | 1 1 1 20 | 1 1 1 21 | 1 1 1 22 | Sample Output: 23 | 6 24 | */ 25 | 26 | #include 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef unordered_map umapii; 31 | typedef unordered_map umapib; 32 | typedef unordered_map umapsi; 33 | typedef unordered_map umapss; 34 | typedef map mapsi; 35 | typedef map, int> mappiii; 36 | typedef map mapii; 37 | typedef pair pii; 38 | typedef pair pll; 39 | typedef unordered_set useti; 40 | 41 | #define uset unordered_set 42 | #define it iterator 43 | #define mp make_pair 44 | #define pb push_back 45 | #define all(x) (x).begin(), (x).end() 46 | #define f first 47 | #define s second 48 | #define MOD 1000000007 49 | #define MAXN 16 50 | 51 | int go(int like[][MAXN], int n, int student, int mask, int* dp){ 52 | 53 | int ans = 0; 54 | 55 | if (student>=n) 56 | { 57 | return 1; 58 | } 59 | 60 | if (dp[mask]!=-1) 61 | { 62 | return dp[mask]; 63 | } 64 | 65 | for (int i = 0; i < n; ++i) 66 | { 67 | if (!(mask&(1<>n; 94 | for(int i = 0; i < n ; i++){ 95 | for(int j = 0; j < n; j++){ 96 | cin>>like[i][j]; 97 | } 98 | } 99 | cout< 24 | using namespace std; 25 | 26 | int minSalary(int *assistant, int *captain, int n, int x, int **memo){ 27 | if(n == 0) return 0; 28 | 29 | if(memo[n][x] != -1) return memo[n][x]; 30 | 31 | if(x == 0){ 32 | int answer = assistant[0]+minSalary(assistant+1, captain+1, n-1, x+1, memo); 33 | memo[n][x] = answer; 34 | return answer; 35 | } 36 | else if(x == n){ 37 | int answer = captain[0]+minSalary(assistant+1, captain+1, n-1, x-1, memo); 38 | memo[n][x] = answer; 39 | return answer; 40 | } 41 | else{ 42 | int a = assistant[0]+minSalary(assistant+1, captain+1, n-1, x+1, memo); 43 | int b = captain[0]+minSalary(assistant+1, captain+1, n-1, x-1, memo); 44 | int answer = min(a, b); 45 | memo[n][x] = answer; 46 | return answer; 47 | } 48 | } 49 | 50 | int main(){ 51 | int n; 52 | cin >> n; 53 | 54 | int *captain = new int[n]; 55 | int *assistant = new int[n]; 56 | for(int i = 0; i < n; i++){ 57 | cin >> captain[i] >> assistant[i]; 58 | } 59 | 60 | int **memo = new int *[n+1]; 61 | for(int i = 0; i <= n; i++){ 62 | memo[i] = new int[(n/2)+1]; 63 | for(int j = 0; j <= n/2; j++) memo[i][j] = -1; 64 | } 65 | 66 | cout << minSalary(assistant, captain, n, 0, memo) << endl; 67 | } 68 | -------------------------------------------------------------------------------- /DP1(Very important)/Hasanandtrip.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Hasan has finally finished his final exams and he decided to go in a trip among cities in Syria. 11 | There are N cities in Syria and they are numbered from 1 to N, each city has coordinates on plane, i-th city is in (Xi, Yi). 12 | Hasan is in first city and he wants to visit some cities by his car in the trip but the final destination should be N-th city and the sequence of cities he will visit should be increasing in index (i.e. if he is in city i he can move to city j if and only if i < j ). 13 | Visiting i-th city will increase Hasan's happiness by Fi units (including first and last cities), also Hasan doesn't like traveling too much, so his happiness will decrease by total distance traveled by him. 14 | Help Hasan by choosing a sequence of cities to visit which maximizes his happiness. 15 | Input format: 16 | First line contain integer N. 17 | Next N lines contains three integers each, i-th line contains coordinates of i-th city Xi, Yi and Fi. 18 | Output format: 19 | Output one number rounded to 6 digits after floating point, the maximum possible happiness Hasan can get. Note: If answer is 2 print 2.000000 20 | Constraints: 21 | 1 <= N <= 3,000 22 | 0 <= Xi, Yi, Fi <= 100,000 23 | Sample Input 24 | 3 25 | 0 0 1 26 | 3 1 1 27 | 6 0 9 28 | Sample Output 29 | 4.675445 30 | 31 | */ 32 | #include 33 | 34 | using namespace std; 35 | 36 | class city{ 37 | public: 38 | int x; 39 | int y; 40 | int happiness; 41 | }; 42 | double distance(city a, city b){ 43 | double ans = 0; 44 | long long X = (long long)pow(a.x-b.x, 2); 45 | long long Y = (long long)pow(a.y-b.y, 2); 46 | 47 | ans = (double)sqrt(X+Y); 48 | return ans; 49 | } 50 | double go(vector v){ 51 | int n = v.size(); 52 | vector dp(n, 0); 53 | dp[0] = v[0].happiness; 54 | 55 | for (int i = 1; i < n; ++i) 56 | { 57 | double temp = -1e9; 58 | for (int j = 0; j >n; 79 | std::vector v; 80 | for (int i = 0; i < n; ++i) 81 | { 82 | int a; 83 | int b; 84 | int c; 85 | cin>>a>>b>>c; 86 | city temp; 87 | temp.x = a; 88 | temp.y = b; 89 | temp.happiness = c; 90 | v.push_back(temp); 91 | } 92 | 93 | //cout << distance(v[0], v[1]) << '\n'; 94 | 95 | cout < 25 | 26 | using namespace std; 27 | 28 | bool mySort(vector a, vector b){ 29 | 30 | if (a[0] == b[0]) { 31 | if (a[2] == b[2]) { 32 | return (a[1] < b[1]); 33 | } 34 | return (a[2] > b[2]); 35 | } 36 | return (a[0] < b[0]); 37 | } 38 | 39 | long long go(vector> worker, long long area){ 40 | long long n = worker.size(); 41 | sort(worker.begin(),worker.end(),mySort); 42 | 43 | long long cost = worker.at(0)[1]; 44 | long long area_done = 0; 45 | long long current_worker = 0; 46 | long long last = 0; 47 | 48 | for (int i = 1; i < n && area_done=area) 63 | { 64 | 65 | return cost; 66 | 67 | } 68 | 69 | if (worker.at(current_worker)[2]>n>>d; 93 | vector> worker; 94 | 95 | while(n--){ 96 | long long t,x,y; 97 | cin>>t>>x>>y; 98 | 99 | vector temp; 100 | temp.push_back(t); 101 | temp.push_back(x); 102 | temp.push_back(y); 103 | 104 | worker.push_back(temp); 105 | } 106 | 107 | cout << go(worker, d) << '\n'; 108 | 109 | 110 | 111 | return 0 ; 112 | 113 | 114 | 115 | } 116 | -------------------------------------------------------------------------------- /adhoc problems/Lightupbulbs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | A bulb can be ‘ON’ or ‘OFF’. Mr. Navdeep got ‘n’ number of bulbs and their status, whether they are ‘ON’ or ‘OFF’. Their status is represented in a string of size ‘n’ consisting of 0’s and 1’s, where ‘0’ represents the bulb is in ‘OFF’ condition and ‘1’ represent the bulb is ‘ON’. Mr. Navdeep has been given the task to light up all the bulbs. 12 | He can perform two operations. 13 | First, chose any segment of bulbs and reverse them means chose any substring and reverse it. E.g. “0 110 001” -> “0 011 001”. Substring (1, 3) is reversed here. This operation will cost him Rs. ‘X’. 14 | Second, chose any segment of bulbs and reverse their present condition. i.e. if the bulb is ‘ON’, make it ‘OFF’ and if it is ‘OFF’, make it ‘ON’. E.g. “0 011 001” -> “0 100 001”. Substring (1, 3) is complemented. This operation will cost him Rs. ‘Y’. 15 | You need to help Mr. Navdeep that how much minimum amount it will require to make all the bulbs lightened. (or make all the characters as ‘1’ in the representation string) 16 | Input Format: 17 | The first line will contain three space separated long longegers: ‘n’, ‘X’, ‘Y’ denoting the number of bulbs, cost of first operation and cost of second operation respectively. 18 | The second line contains a representation string of length ‘n’ consisting of 0’s and 1’s representing whether the bulb is ‘OFF’ or ‘ON’. 19 | Output Format: 20 | Prlong long a single long longeger denoting the minimum cost required to light up all the bulbs. 21 | Constralong longs: 22 | 1 <= n <= 3,00,000 23 | 0 <= X, Y <= 10^9 24 | Time Limit: 1 second 25 | Sample Input: 26 | 5 1 10 27 | 01000 28 | Sample Output: 29 | 11 30 | Explanation: 31 | First, Reverse substring (0, 1): “01000” -> “10000”, COST = 1 32 | Second, Invert substring (1, 4): “10000” -> “11111”, COST = 10 33 | Total cost = 1+10 => 11 34 | */ 35 | 36 | #include 37 | 38 | using namespace std; 39 | long long go(long long* arr, long long n, long long x ,long long y){ 40 | long long breaks = 0; 41 | if (arr[0]==0) 42 | { 43 | breaks++; 44 | } 45 | for (long long i = 0; i < n-1; ++i) 46 | { 47 | // cout <>n>>x>>y; 73 | 74 | string str; 75 | cin>>str; 76 | 77 | 78 | long long* arr = new long long[n]; 79 | 80 | for (long long i = 0; i < n; ++i) 81 | { 82 | arr[i] = str[i]-'0'; 83 | } 84 | 85 | cout << go(arr, n, x, y) << '\n'; 86 | 87 | delete [] arr; 88 | 89 | return 0 ; 90 | 91 | 92 | 93 | } 94 | 95 | -------------------------------------------------------------------------------- /Assignment1/Sortingskills.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | There is a company named James Peterson & Co. The company has ‘n’ employees. 11 | The employees have skills from 0 to n-1. All the employees have distinct skills. 12 | The manager of James Peterson & Co. wants to sort the employees on the basis of their 13 | skills in ascending order. He is only allowed to swap two employees which are adjacent to each 14 | other. He is given the skills of employees in an array of size ‘n’. He can swap the skills as 15 | long as the absolute difference between their skills is 1. You need to help the manager out and 16 | tell whether it is possible to sort the skills of employees or not. 17 | Input Format: 18 | First Line will have an integer ‘t’ denoting the no. of test cases. 19 | First line of each test case contains an integer ‘n’ denoting the no. of employees in the company. 20 | Second line of each test case contains ‘n’ distinct integers in the range [0, n-1]. 21 | Output Format: 22 | For each test case, print “Yes” if it is possible to sort the skills otherwise “No”. 23 | Constraints: 24 | 1 <= t <= 10 25 | 1 <= n <= 10^5 26 | Sample Input: 27 | 2 28 | 4 29 | 1 0 3 2 30 | 3 31 | 2 1 0 32 | Sample Output: 33 | Yes 34 | No 35 | Explanation: 36 | In first T.C., [1, 0, 3, 2] -> [0, 1, 3, 2] -> [0, 1, 2, 3] 37 | In second T.C., [2, 1, 0] -> [1, 2, 0] OR [2, 1, 0] -> [2, 0, 1] So, it is impossible to sort. 38 | 39 | */ 40 | 41 | 42 | #include 43 | 44 | using namespace std; 45 | 46 | bool skillSort(vector &skills, int start, int end){ 47 | // cout << start<<" " <= end) 49 | { 50 | return 1; 51 | } 52 | 53 | int n = skills.size(); 54 | int mid = (start+end)/2; 55 | 56 | int left = skillSort(skills, start, mid); 57 | int right = skillSort(skills, mid+1, end); 58 | 59 | if (left!=1 || right != 1) 60 | { 61 | return 0; 62 | } 63 | 64 | if (skills[mid+1]>skills[mid]) 65 | { 66 | return 1; 67 | } 68 | 69 | // cout << "mid "<1) 73 | { 74 | return 0; 75 | } 76 | 77 | if (skills[mid+1]>t; 94 | 95 | while(t--){ 96 | int n; 97 | cin>>n; 98 | //cout << "n "< skills; 101 | for (int i = 0; i < n; ++i) 102 | { 103 | int temp; 104 | cin>>temp; 105 | skills.push_back(temp); 106 | } 107 | 108 | if (skillSort(skills, 0, n-1)==1) 109 | { 110 | cout << "Yes" << '\n'; 111 | }else{ 112 | cout << "No" << '\n'; 113 | } 114 | 115 | } 116 | 117 | 118 | 119 | return 0 ; 120 | 121 | 122 | 123 | } 124 | -------------------------------------------------------------------------------- /Segment Trees/Minimuminsub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | Range Minimum Query 11 | Given an array A of size N, there are two types of queries on this array. 12 | 1) q l r: In this query you need to print the minimum in the sub-array A[l:r]. 13 | 2) u x y: In this query you need to update A[x]=y. 14 | Input: 15 | First line of the test case contains two integers, N and Q, size of array A and number of queries. 16 | Second line contains N space separated integers, elements of A. 17 | Next Q lines contain one of the two queries. 18 | Output: 19 | For each type 1 query, print the minimum element in the sub-array A[l:r]. 20 | Contraints: 21 | 1≤N,Q,y≤10^5 22 | 1≤l,r,x≤N 23 | Sample Input : 24 | 5 5 25 | 1 5 2 4 3 26 | q 1 5 27 | q 1 3 28 | q 3 5 29 | u 3 6 30 | q 1 5 31 | Sample Output : 32 | 1 33 | 1 34 | 2 35 | 1 36 | */ 37 | 38 | #include 39 | 40 | using namespace std; 41 | 42 | int query(int* tree, int start, int end, int treeNode, int left, int right){ 43 | 44 | //Completely out 45 | if (left>end || right=left && end<=right) 52 | { 53 | return tree[treeNode]; 54 | } 55 | 56 | //Partially inside 57 | int mid = (start+end)/2; 58 | 59 | int l = query(tree, start, mid, 2*treeNode+1, left, right); 60 | int r= query(tree, mid+1, end, 2*treeNode+2, left, right); 61 | 62 | return min(l, r); 63 | } 64 | 65 | 66 | void update(int* arr, int* tree, int start, int end, int treeNode, int idx, int value){ 67 | int mid = (start+end)/2; 68 | 69 | if (start == end) 70 | { 71 | arr[idx] = value; 72 | tree[treeNode] = value; 73 | return; 74 | } 75 | 76 | if (idx<=mid) 77 | { 78 | update(arr, tree, start, mid, 2*treeNode+1, idx, value); 79 | }else{ 80 | 81 | update(arr, tree, mid+1, end, 2*treeNode+2, idx, value); 82 | } 83 | 84 | tree[treeNode] = min(tree[2*treeNode+1], tree[2*treeNode+2]); 85 | } 86 | 87 | 88 | void create(int* arr, int* tree, int start, int end, int treeNode){ 89 | if (start == end) 90 | { 91 | tree[treeNode] = arr[start]; 92 | return; 93 | } 94 | 95 | int mid = (start+end)/2; 96 | 97 | create(arr, tree, start, mid, 2*treeNode+1); 98 | create(arr, tree, mid+1, end, 2*treeNode+2); 99 | 100 | tree[treeNode] = min(tree[2*treeNode+1], tree[2*treeNode+2]); 101 | return; 102 | } 103 | 104 | int main( int argc , char ** argv ) 105 | { 106 | ios_base::sync_with_stdio(false) ; 107 | cin.tie(NULL) ; 108 | 109 | int n, q; 110 | cin>>n>>q; 111 | 112 | int* arr = new int[n]; 113 | for (int i = 0; i < n; ++i) 114 | { 115 | cin>>arr[i]; 116 | } 117 | 118 | int* tree = new int[4*n]; 119 | 120 | create(arr, tree, 0, n-1, 0); 121 | 122 | 123 | 124 | while(q--){ 125 | char a; 126 | int b, c; 127 | cin>>a>>b>>c; 128 | 129 | if (a=='q') 130 | { 131 | cout << query(tree, 0, n-1, 0, b-1, c-1) << '\n'; 132 | }else{ 133 | update(arr, tree, 0, n-1, 0, b-1, c); 134 | } 135 | 136 | } 137 | 138 | 139 | return 0 ; 140 | 141 | 142 | 143 | } 144 | -------------------------------------------------------------------------------- /Dynamic programming 2/Maxprofit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Gourav Singh 4 | https://www.linkedin.com/in/gouravsingh2580/ 5 | 6 | 7 | */ 8 | 9 | /* 10 | Mike is a stock trader and makes a profit by buying and selling stocks. He buys a stock at a lower price and sells it at a higher price to book a profit. He has come to know the stock prices of a particular stock for n upcoming days in future and wants to calculate the maximum profit by doing the right transactions (single transaction = buying + selling). Can you help him maximize his profit? 11 | Note: A transaction starts after the previous transaction has ended. Two transactions can't overlap or run in parallel. 12 | The stock prices are given in the form of an array A for n days. 13 | Given the stock prices and a positive integer k, find and print the maximum profit Mike can make in at most k transactions. 14 | Input Format 15 | The first line of input contains an integer q denoting the number of queries. 16 | 17 | The first line of each test case contains a positive integer k, denoting the number of transactions. 18 | 19 | The second line of each test case contains a positive integer n, denoting the length of the array A. 20 | 21 | The third line of each test case contains n space-separated positive integers, denoting the prices of each day in the array A. 22 | Constraints 23 | 1<=q<=100 24 | 25 | 0 52 | using namespace std; 53 | typedef pair pii; 54 | #define f first 55 | #define s second 56 | 57 | int maxProfit(int *input, int id, int n, int k, bool buy, pii **memo){ 58 | if(k == 0 || id == n) return 0; 59 | 60 | if(buy){ 61 | if(memo[id][k].f != -1) return memo[id][k].f; 62 | } 63 | else{ 64 | if(memo[id][k].s != -1) return memo[id][k].s; 65 | } 66 | 67 | if(buy){ 68 | int a = maxProfit(input, id+1, n, k, false, memo)-input[id]; 69 | int b = maxProfit(input, id+1, n, k, true, memo); 70 | memo[id][k].f = max(a, b); 71 | return max(a, b); 72 | } 73 | else{ 74 | int a = input[id]+maxProfit(input, id+1, n, k-1, true, memo); 75 | int b = maxProfit(input, id+1, n, k, false, memo); 76 | memo[id][k].s = max(a, b); 77 | return max(a, b); 78 | } 79 | } 80 | 81 | int main(){ 82 | int q; 83 | cin >> q; 84 | while(q--){ 85 | int k, n; 86 | cin >> k >> n; 87 | 88 | int *input = new int[n]; 89 | for(int i = 0; i < n; i++) cin >> input[i]; 90 | 91 | pii **memo = new pii *[n+1]; 92 | for(int i = 0; i <= n; i++){ 93 | memo[i] = new pii[k+1]; 94 | for(int j = 0; j <= k; j++){ 95 | memo[i][j].f = -1; 96 | memo[i][j].s = -1; 97 | } 98 | } 99 | 100 | cout << maxProfit(input, 0, n, k, true, memo) << endl; 101 | } 102 | } 103 | --------------------------------------------------------------------------------