├── Arrays ├── QuickSort.cpp ├── Max-till-i.cpp ├── Two Sum.js ├── Sum-Subseq.cpp ├── CountingSort.java ├── Remove Duplicates from Sorted Array.cpp ├── SmallRepeat.cpp ├── InsertionSort.java ├── Array Zigzag.cpp ├── QueryCheck.cpp ├── SelectionSort.java ├── Longest-Arth-subarr.cpp ├── SelectionSort.cpp ├── InsertionSort.cpp ├── BubbleSort.cpp ├── BucketSort.java ├── K th Smallest element.cpp ├── SumSubarr.cpp ├── sliding-window-maximums.cpp ├── MergeSort.java ├── Mergesort.cpp ├── topKfrequent.cpp ├── Nut and Bolt Matching Problem.cpp └── heap_sort.c ├── CP-Programming ├── Script.cpp ├── output.txt ├── input.txt ├── Script ├── SortGame.cpp ├── Tricky-permutation.md ├── Strongest-fighter.cpp ├── Strongest-fighter.md ├── Two Cakes.cpp ├── Tricky-permutation.cpp ├── Upper-bound-lower-bound.md ├── SortGame.md ├── Upper-bound-lower-bound.cpp ├── Badge.cpp ├── Pair-Of-Roses.md ├── integerToRoman.cpp └── Pair-Of-Roses.cpp ├── Bitmaps ├── Count1s ├── Setbit ├── CheckPW2 ├── Checkbit ├── Clearbit ├── Updatebit ├── CheckUnique ├── Checkunique2 ├── Printsubsets ├── Checkbit.cpp ├── Setbit.cpp ├── Clearbit.cpp ├── Updatebit.cpp ├── CheckPW2.cpp ├── Count1s.cpp ├── CheckUnique.cpp ├── Printsubsets.cpp ├── MismatchBits.cpp └── Checkunique2.cpp ├── File-Ops ├── FileOps ├── sheet1.xls ├── Attendance.csv ├── FileOps.c └── sheet.csv ├── Stacks ├── ApplicationOfStacks │ ├── In-Post │ ├── Reverse │ ├── Reverse.cpp │ ├── In-Post.cpp │ └── BracketCreate.cpp ├── Stack1.cpp ├── Stack-Lists.cpp ├── stock_span.cpp ├── Stack-Arrays.cpp └── Evaluate_Postfix.cpp ├── .vscode └── settings.json ├── Recursion ├── RevString.cpp ├── GenSubseq.cpp ├── CheckSort.cpp ├── Genasciistr.cpp ├── TowerOfHanoi.cpp ├── ReplacePI.cpp ├── PrintOrder.cpp └── CombinationStringDigits.cpp ├── Dynamic programming ├── FibonaccibyDP.cpp ├── Super Egg Drop.cpp ├── Knapsack1.cpp ├── Longest Palindromic Subsequence.cpp ├── Independent_Set.cpp ├── Shortestpath-floydwarshall.cpp ├── Grouping.cpp ├── Longest Palindromic Substring.cpp ├── Grid1.cpp ├── Longest_Path.cpp ├── Candies.cpp ├── Coins.cpp ├── Stones.cpp ├── Knapsack.cpp ├── LCS.cpp └── Word Wrap using DP.cpp ├── Math ├── Trailing Zeros in Factorial.cpp ├── DigitSum.cpp ├── SieveOfErastothenes.cpp ├── Gcd_Calc.cpp └── MatrixExponentiation.cpp ├── Strings ├── Revstring.cpp ├── CVowRec.cpp ├── string_palindrome.cpp ├── binsearchstr.cpp └── Pursuit.cpp ├── Trees ├── InorderTraversal.cpp ├── Sum of all nodes in binary tree.cpp ├── LevelOrderTraversal.cpp └── BuildTree.cpp ├── BinarySearch ├── Rotated Sorted Search.cpp ├── Two Cakes.cpp ├── CountOccurancesUsingBinarySearch.cpp ├── Algorihtm.cpp └── Recrsive_Algo.cpp ├── Templates └── Diesel ├── Lists ├── Remove Duplicate From Sorted Linked List.c ├── length of Linked List.cpp ├── Queue_using_LL.cpp ├── GetSumlastNodes.cpp ├── RevKnodes.cpp ├── ReverseList.cpp ├── Stack_using_LL.cpp └── LinkedList.cpp ├── Tree └── Right_view_of_binary_tree.java ├── Graph ├── Bipartite Graph Test.cpp ├── DFS.java ├── Dijkstra's Algorithm.cpp ├── Kruskal's Algorithm.cpp ├── Prim's Algorithm.cpp ├── BFS.java └── WaterJugProblem.java ├── README.md ├── Greedy └── DIJKSTRAS_ALGO.cpp ├── Back-tracking ├── nqueen.cpp ├── RatInMaze.cpp └── Solve the Sudoku.cpp ├── two_stack_in_a_array.cpp └── TrieTrees └── SearchSuggestionSystem.java /Arrays/QuickSort.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /CP-Programming/Script.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /CP-Programming/output.txt: -------------------------------------------------------------------------------- 1 | NO 2 | YES 3 | NO 4 | YES 5 | -------------------------------------------------------------------------------- /CP-Programming/input.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 3 10 3 | 9 6 4 | 7 30 5 | 8 12 -------------------------------------------------------------------------------- /Bitmaps/Count1s: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Count1s -------------------------------------------------------------------------------- /Bitmaps/Setbit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Setbit -------------------------------------------------------------------------------- /Bitmaps/CheckPW2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/CheckPW2 -------------------------------------------------------------------------------- /Bitmaps/Checkbit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Checkbit -------------------------------------------------------------------------------- /Bitmaps/Clearbit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Clearbit -------------------------------------------------------------------------------- /Bitmaps/Updatebit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Updatebit -------------------------------------------------------------------------------- /File-Ops/FileOps: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/File-Ops/FileOps -------------------------------------------------------------------------------- /Bitmaps/CheckUnique: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/CheckUnique -------------------------------------------------------------------------------- /Bitmaps/Checkunique2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Checkunique2 -------------------------------------------------------------------------------- /Bitmaps/Printsubsets: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Bitmaps/Printsubsets -------------------------------------------------------------------------------- /File-Ops/sheet1.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/File-Ops/sheet1.xls -------------------------------------------------------------------------------- /CP-Programming/Script: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/CP-Programming/Script -------------------------------------------------------------------------------- /File-Ops/Attendance.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/File-Ops/Attendance.csv -------------------------------------------------------------------------------- /Stacks/ApplicationOfStacks/In-Post: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Stacks/ApplicationOfStacks/In-Post -------------------------------------------------------------------------------- /Stacks/ApplicationOfStacks/Reverse: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shaviaditya/DataStructures-Algorithms/HEAD/Stacks/ApplicationOfStacks/Reverse -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ostream": "cpp" 5 | }, 6 | "python.pythonPath": "/bin/python3" 7 | } -------------------------------------------------------------------------------- /Bitmaps/Checkbit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int lli; 4 | #define loop(i, a, b) for (lli i = a; i < b; i++) 5 | void solve(int n,int pos){ 6 | cout<<((n&(1<>n>>pos; 11 | solve(n,pos); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Bitmaps/Setbit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int lli; 4 | #define loop(i, a, b) for (lli i = a; i < b; i++) 5 | int solve(int n, int pos) 6 | { 7 | return (n|(1 << pos)); 8 | } 9 | int main() 10 | { 11 | int n, pos; 12 | cin >> n >> pos; 13 | cout< 2 | using namespace std; 3 | void reverse(string st){ 4 | if(st.length()==0){ 5 | return; 6 | } else { 7 | reverse(st.substr(1)); 8 | cout<>st; 13 | reverse(st); 14 | cout<<"\n"; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Bitmaps/Clearbit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int lli; 4 | #define loop(i, a, b) for (lli i = a; i < b; i++) 5 | int solve(int n, int pos) 6 | { 7 | return (n ^ (1 << pos)); 8 | } 9 | int main() 10 | { 11 | int n, pos; 12 | cin >> n >> pos; 13 | cout << solve(n, pos) << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Arrays/Max-till-i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define loop(i,a,b) for(int i=a;i>n; 6 | int arr[n]; 7 | loop(i,0,n){ 8 | cin>>arr[i]; 9 | } 10 | int k,mx=0; cin>>k; 11 | loop(i,0,k){ 12 | mx = max(mx,arr[i]); 13 | } 14 | cout< 2 | using namespace std; 3 | int main() { 4 | ios::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | cout.tie(NULL); 7 | int A; 8 | cin >> A; 9 | int ans = 0; 10 | if(A >= 0 && A < 5)cout<<"0"< 2 | using namespace std; 3 | typedef long long int lli; 4 | #define loop(i, a, b) for (lli i = a; i < b; i++) 5 | int solve(int n, int pos,int pos2) 6 | { 7 | return (n & (~(1 << pos))|(1<> n >> pos>>pos2; 13 | cout << solve(n, pos,pos2) << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Strings/Revstring.cpp: -------------------------------------------------------------------------------- 1 | // Reverse a string using recursion 2 | #include 3 | using namespace std; 4 | #define newline printf("\n") 5 | void revstring(string st){ 6 | if(st==""){ 7 | return; 8 | } 9 | revstring(st.substr(1)); 10 | cout< 2 | using namespace std; 3 | vector vst; 4 | void subseq(string st,string ans){ 5 | if(st.length()==0){ 6 | cout< 2 | using namespace std; 3 | int checksort(int A[],int n){ 4 | if(A[n]-A[n-1]<0){ 5 | return 0; 6 | } else if(n==1){ 7 | return 1; 8 | } else { 9 | return checksort(A,n-1); 10 | } 11 | } 12 | int main(){ 13 | int A[5] = {1,2,3,4,5}; 14 | int B[5] = {3,4,5,1,2}; 15 | cout<> 2 | #include 3 | using namespace std; 4 | typedef long long int lli; 5 | #define loop(i, a, b) for (lli i = a; i < b; i++) 6 | #define YY cout<<"Yes"<>n; 18 | check(n); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Recursion/Genasciistr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void subseq(string st, string ans) 4 | { 5 | if (st.length() == 0) 6 | { 7 | cout << ans << endl; 8 | return; 9 | } 10 | char ch = st[0]; 11 | string asc = to_string((int)(ch)); 12 | string sf = st.substr(1); 13 | subseq(sf, ans); 14 | subseq(sf,ans+asc); 15 | subseq(sf, ans + ch); 16 | } 17 | int main() 18 | { 19 | string st = "BA"; 20 | subseq(st, ""); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Arrays/Two Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | var twoSum = function(nums, target) { 7 | for (let index = 0; index < nums.length; index++) { 8 | const diff = target - nums[index]; 9 | const diffIndex = nums.indexOf(diff); 10 | // "diffIndex !== index" takes care of same index not being reused 11 | if (diffIndex !== -1 && diffIndex !== index) { 12 | return [index, diffIndex]; 13 | } 14 | } 15 | 16 | }; -------------------------------------------------------------------------------- /Arrays/Sum-Subseq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define loop(i, a, b) for (int i = a; i < b; i++) 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | int arr[n]; 9 | loop(i, 0, n) 10 | { 11 | cin >> arr[i]; 12 | } 13 | int k=0,s=0; 14 | loop(i, 0, n) 15 | { 16 | s = s + arr[i]; 17 | loop(j,i+1,n){ 18 | s = s + arr[j]; 19 | k = k + s; 20 | cout< 3 | using namespace std; 4 | typedef long long int lli; 5 | #define loop(i, a, b) for (lli i = a; i < b; i++) 6 | #define YY cout << "Yes" << endl; 7 | #define NN cout << "No" << endl; 8 | int count1(int n,int cnt){ 9 | if(n==0){ 10 | return cnt; 11 | } else { 12 | cnt=cnt+1; 13 | return count1(n&(n-1),cnt); 14 | } 15 | } 16 | int main() 17 | { 18 | int n; 19 | cin >> n; 20 | cout< 10 | using namespace std; 11 | void ToI(int n,char src,char des,char help){ 12 | if(n==0){ 13 | return; 14 | } else { 15 | //cout << "Moving from" << src << " to " << des << endl; 16 | ToI(n-1, src, help , des); 17 | cout << "Moving from" << src << " to " << des << endl; 18 | ToI(n - 1, help,des,src); 19 | } 20 | } 21 | int main(){ 22 | int n; cin>>n; 23 | ToI(n,'A','C','B'); 24 | } -------------------------------------------------------------------------------- /Strings/CVowRec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //pippxxppiixipi 4 | string stringpi(string st,string sf){ 5 | //base case 6 | if(st.length()==0){ 7 | return sf; 8 | } else { 9 | if(st.substr(0,2)=="pi"){ 10 | sf = sf + "3.14"; 11 | return stringpi(st.substr(2),sf); 12 | } else { 13 | sf = sf + st[0]; 14 | return stringpi(st.substr(1),sf); 15 | } 16 | } 17 | } 18 | int main(){ 19 | string bt = "pippxxppiixipi"; 20 | cout< 2 | using namespace std; 3 | typedef long long int lli; 4 | 5 | int main() 6 | { 7 | ios::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | cout.tie(NULL); 10 | lli t; 11 | cin >> t; 12 | while (t--) 13 | { 14 | lli n; 15 | cin >> n; 16 | string st = to_string(n); 17 | if (st[st.length() - 1] == '9') 18 | { 19 | cout << (n - (n % 10)) / 10 + 1 << endl; 20 | } 21 | else 22 | { 23 | cout << (n - (n % 10)) / 10 << endl; 24 | } 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Bitmaps/CheckUnique.cpp: -------------------------------------------------------------------------------- 1 | //Program : All elements have been repeated exactly twice except one such number find it! 2 | /* 3 | Properties to keep in mind: 4 | 0 ^ Y = Y 5 | Y ^ Y = 0 6 | */ 7 | #include 8 | using namespace std; 9 | typedef long long int lli; 10 | #define loop(i, a, b) for (lli i = a; i < b; i++) 11 | void solve(int A[],int n){ 12 | int c=A[0]; 13 | loop(i,1,n){ 14 | c = c^A[i]; 15 | } 16 | cout<>n; 20 | int A[n]; 21 | loop(i,0,n){ 22 | cin>>A[i]; 23 | } 24 | solve(A,n); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Recursion/ReplacePI.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Q: Wipro screening basic 3 | Replace each instace of 'pi' in pippxxppiixipi with 3.14 USING RECURSION!! 4 | */ 5 | #include 6 | using namespace std; 7 | string replace(string st,string sg){ 8 | if(st.length()==0){ 9 | return sg; 10 | } 11 | if(st[0]=='p' && st[1]=='i'){ 12 | sg = sg + "3.14"; 13 | return replace(st.substr(2),sg); 14 | } else { 15 | sg = sg + st[0]; 16 | return replace(st.substr(1),sg); 17 | } 18 | } 19 | int main(){ 20 | string st; cin>>st; 21 | string sg=""; 22 | cout<=0;i--) { 17 | temp[count[a[i]] - 1] = a[i]; 18 | count[a[i]]--; 19 | } 20 | 21 | for(int i=0;i 3 | using namespace std; 4 | int removeDuplicates(int a[],int n) 5 | { if(n==0) 6 | return 0; 7 | int i=0,j=1; 8 | while(j 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | inline bool myCompare( pair a , pair b) 7 | { 8 | if(a.second==b.second) 9 | return a.firstb.second; 12 | } 13 | 14 | int main() 15 | { 16 | int x,n,i; 17 | cin>>x; 18 | pair a[100000]; 19 | cin>>n; 20 | for(i=0;i>a[i].first; 23 | cin>>a[i].second; 24 | } 25 | sort(a,a+n,myCompare); 26 | for(i=0;i=x;i++) 27 | { 28 | cout< 4 | using namespace std; 5 | typedef long long int lli; 6 | #define loop(i,a,b) for(lli i=a;i>n; 9 | lli A[n],mx=0; 10 | loop(i,0,n){ 11 | cin>>A[i]; 12 | mx=max(mx,A[i]); 13 | } 14 | lli B[mx+1] = {0}; 15 | loop(i, 0, n){ 16 | B[A[i]]++; 17 | } 18 | int val=0; 19 | loop(i,0,n){ 20 | val = A[i]; 21 | A[i] = B[A[i]]; 22 | if(A[i]>1){ 23 | cout< 3 | using namespace std; 4 | void SieveOfEratosthenes(int n) 5 | { 6 | bool prime[n + 1]; 7 | memset(prime,true,sizeof(prime)/sizeof(prime[0])); 8 | for (int p = 2; p * p <= n; p++) 9 | { 10 | if (prime[p] == true) 11 | { 12 | for (int i = p * p; i <= n; i += p) 13 | prime[i] = false; 14 | } 15 | } 16 | for (int p = 2; p <= n; p++) 17 | if (prime[p]){ 18 | cout << p << " "; 19 | }cout<>n; 25 | SieveOfEratosthenes(n); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Arrays/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package algorithm; 2 | 3 | public class InsertionSort> { 4 | private T[] a; 5 | 6 | public InsertionSort(T[] b) { 7 | a = b; 8 | } 9 | 10 | public InsertionSort() { 11 | 12 | } 13 | 14 | public T[] sort() { 15 | return sort(a); 16 | } 17 | 18 | public T[] sort(T[] a) { 19 | for(int i=1;i= 0) && (a[j].compareTo(key) > 0)) { 24 | a[j+1] = a[j]; 25 | j--; 26 | } 27 | a[j+1] = key; 28 | } 29 | return a; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Trees/InorderTraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int key; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int k) 9 | { 10 | key = k; 11 | left = right = NULL; 12 | } 13 | }; 14 | void inorder(Node *root) 15 | { 16 | if (root != NULL) 17 | { 18 | inorder(root->left); 19 | cout << root->key << endl; 20 | inorder(root->right); 21 | } 22 | } 23 | int main() 24 | { 25 | 26 | Node *root = new Node(10); 27 | root->left = new Node(20); 28 | root->right = new Node(30); 29 | root->right->left = new Node(40); 30 | root->right->right = new Node(50); 31 | 32 | inorder(root); 33 | } -------------------------------------------------------------------------------- /Recursion/PrintOrder.cpp: -------------------------------------------------------------------------------- 1 | /* Q: The Question is a classic to understand how recursion works: 2 | For decreasing we print first then call say for n = 3 3 | print(3) call(2) print(2) call(1) print(1) => 3 2 1 4 | recursion now calls the blocks as: 5 | call(1) print(1) call(2) print(2) call(3) print(3) => 1 2 3 6 | Voila! 7 | */ 8 | #include 9 | using namespace std; 10 | void dec(int n){ 11 | if(n==0){ 12 | cout<<"Increasing Order"<>n; 21 | cout<<"Decreasing Order"< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0;i>arr[i]; 8 | int b; 9 | cin>>b; 10 | int start = 0; 11 | int end = n-1; 12 | bool check = false; 13 | for(int i=0;iend)cout<<"-1"; 30 | } -------------------------------------------------------------------------------- /Templates/Diesel: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define faststream ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 4 | typedef long long int lli; 5 | #define ln(x) x.length() 6 | #define YY cout<<"YES"<>x; } 10 | #define displayst(v,x) for(auto x: v){ cout< v(n) 12 | #define mapit map 13 | #define f(i,a,b) for(lli i=a;i 2 | using namespace std; 3 | 4 | int solve(int arr[],int n,int start) 5 | { 6 | int res = 0; 7 | for(int i=start;i>n; 27 | int arr[n]; 28 | for(int i=0;i>arr[i]; 30 | 31 | cout<> 3 | Important: The program code uses bitwise operators to check for 1's in the binary till 2^length-1 and 4 | using it as a reference printing corresponding index values using a nested loop construct... 5 | */ 6 | #include 7 | #include 8 | using namespace std; 9 | typedef long long int lli; 10 | #define loop(i, a, b) for (lli i = a; i < b; i++) 11 | void solve(int A[],int n) { 12 | loop(i,0,1<> n; 24 | int A[n]; 25 | loop(i,0,n){ 26 | cin>>A[i]; 27 | } 28 | solve(A,n); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /CP-Programming/Tricky-permutation.md: -------------------------------------------------------------------------------- 1 | ## Problem Name: Tricky Permutations 2 | ### Problem Difficulty: None 3 | #### Problem Constraints: 4 | Length of the string <= 8 5 | 6 | 7 | #### Problem Description: 8 | Given a string containing duplicates, print all its distinct permutations such that there are no duplicate permutations and all permutations are printed in a lexicographic order. 9 | #### Input Format: 10 | The first and only line of the test case contains the input string. 11 | 12 | * Sample Input: 13 |
14 | ABA
15 | 
16 | 17 | #### Output Format: 18 | Print all the distinct permutations in a lexicographic order such that each permutation is in a new line. Note that there should not be any duplicate permutations. 19 | 20 | * Sample Output: 21 |
 
22 | AAB
23 | ABA
24 | BAA
25 | 
-------------------------------------------------------------------------------- /Strings/string_palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char str[100]; 7 | int i, length; 8 | int flag = 0; 9 | 10 | cout<<"\n Enter String : "; 11 | cin>>str; 12 | 13 | length = strlen(str); 14 | 15 | for(i=0;i < length ;i++) 16 | { 17 | if(str[i] != str[length-i-1]) 18 | { 19 | flag = 1; 20 | break; 21 | } 22 | } 23 | if(flag) 24 | { 25 | cout<<" "< 2 | using namespace std; 3 | typedef long long int lli; 4 | int main(){ 5 | lli t; cin>>t; 6 | while(t--){ 7 | lli n,m,x=0; cin>>n>>m; 8 | lli hashed[n+1]; 9 | hashed[0] = 0; 10 | for(lli i=1;i<=n;i++){ 11 | cin>>x; 12 | if(x%2==1){ 13 | hashed[i] = hashed[i-1]; 14 | } else { 15 | hashed[i] = hashed[i-1] + 1; 16 | } 17 | } 18 | while(m--){ 19 | lli l1,l2; cin>>l1>>l2; 20 | lli e = hashed[l2] - hashed[l1 - 1]; 21 | lli o = (l2-l1+1)-e; 22 | //1 2 3 4 5 6 23 | lli val = ((e*(e-1)*(e-2))/6)+((o*(o-1))/2)*e; 24 | cout<> { 4 | private T[] a; 5 | 6 | public SelectionSort(T[] b) { 7 | a = b; 8 | } 9 | 10 | public SelectionSort() { 11 | 12 | } 13 | 14 | public T[] sort() { 15 | return sort(a); 16 | } 17 | 18 | public T[] sort(T[] a) { 19 | for(int i=0;i 2 | using namespace std; 3 | typedef long long int lli; 4 | #define fastIO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 5 | int main(){ 6 | lli t; cin>>t; 7 | while(t--){ 8 | lli a,b; cin>>a>>b; 9 | if(__gcd(a,b)>1){ 10 | cout<<0<1){ 18 | cout<<1< 1) 21 | { 22 | cout<<1< 2 | #include 3 | using namespace std; 4 | 5 | int arr[10000005]; 6 | int main() { 7 | deque dq; 8 | int n; 9 | cin>>n; 10 | for(int i=0;i>arr[i]; 12 | int k; 13 | cin>>k; 14 | int mx=0; 15 | for(int i=0;iarr[dq.back()]) 20 | dq.pop_back(); 21 | dq.push_back(i); 22 | } 23 | } 24 | cout<arr[dq.back()]) 29 | dq.pop_back(); 30 | dq.push_back(i); 31 | cout< 3 | using namespace std; 4 | #define loop(i, a, b) for (int i = a; i < b; i++) 5 | int main(){ 6 | int t,m=1; cin>>t; 7 | while(m!=t+1){ 8 | int n; 9 | cin >> n; 10 | int arr[n]; 11 | loop(i, 0, n) 12 | { 13 | cin >> arr[i]; 14 | } 15 | int cnt = 1, mx = -100001; 16 | int diff = arr[1]- arr[0]; 17 | loop(i,1,n-1){ 18 | if(arr[i+1]-arr[i]==diff){ 19 | cnt++; 20 | } else { 21 | mx = max(mx,cnt); 22 | diff = arr[i + 1] - arr[i]; 23 | cnt = 1; 24 | } 25 | } 26 | if(cnt>=1){ 27 | mx = max(mx,cnt); 28 | } 29 | cout<<"Case #"< 5 | 1<=k<=N
6 | 1 <= Ai <= 100000 7 | 8 | 9 | #### Problem Description: 10 | There is a group of MMA fighters standing together in a line. 11 | Given the value of their strengths, find the strength of the strongest fighter in continuous sub-groups of size k. 12 | #### Input Format: 13 | First line contains an integer N, the number of fighters 14 | Followed by N integers where i'th integer denotes the strength of i'th fighter. 15 | Next line contains the size of sub-group k 16 | 17 | * Sample Input: 18 |
19 | 5
20 | 1 3 1 4 5
21 | 3
22 | 
23 | 24 | #### Output Format: 25 | Space separated integers in a single line denoting strength of strongest fighters in the groups. 26 | 27 | * Sample Output: 28 |
 
29 | 3 4 5
30 | 
-------------------------------------------------------------------------------- /Trees/Sum of all nodes in binary tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node{ 4 | int key; 5 | Node* left, *right; 6 | }; 7 | Node* newNode(int key){ 8 | Node* node = new Node; 9 | node->key = key; 10 | node->left = node->right = NULL; 11 | return (node); 12 | } 13 | int addBT(Node* root){ 14 | if (root == NULL) 15 | return 0; 16 | return (root->key + addBT(root->left) + addBT(root->right)); 17 | 18 | int main() 19 | { 20 | Node* root = newNode(1); 21 | root->left = newNode(2); 22 | root->right = newNode(3); 23 | root->left->left = newNode(4); 24 | root->left->right = newNode(5); 25 | root->right->left = newNode(6); 26 | root->right->right = newNode(7); 27 | root->right->left->right = newNode(8); 28 | int sum = addBT(root); 29 | cout << "Sum of all the elements is: " << sum << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Arrays/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int *x, int *y) 4 | { 5 | *x = *x + *y; 6 | *y = *x - *y; 7 | *x = *x - *y; 8 | } 9 | 10 | void selectionSort(int arr[], int n) 11 | { 12 | int i, j, minx; 13 | for (i = 0; i < n - 1; i++) 14 | { 15 | minx = i; 16 | for (j = i + 1; j < n; j++){ 17 | if (arr[j] < arr[minx]){ 18 | minx = j; 19 | } 20 | } 21 | swap(&arr[minx], &arr[i]); 22 | } 23 | } 24 | void display(int arr[], int n) 25 | { 26 | int i; 27 | for (i = 0; i < n; i++) 28 | cout << arr[i] << " "; 29 | cout << endl; 30 | } 31 | int main() 32 | { 33 | int n; 34 | cin >> n; 35 | int arr[n]; 36 | for (auto &x : arr) 37 | { 38 | cin >> x; 39 | } 40 | selectionSort(arr, n); 41 | display(arr, n); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /BinarySearch/Two Cakes.cpp: -------------------------------------------------------------------------------- 1 | problem statement link 2 | https://codeforces.com/contest/911/problem/B 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define PB push_back 8 | #define MP make_pair 9 | #define SZ(v) ((int)(v).size()) 10 | #define FOR(i,a,b) for(int i=(a);i<(b);++i) 11 | #define REP(i,n) FOR(i,0,n) 12 | #define FORE(i,a,b) for(int i=(a);i<=(b);++i) 13 | #define REPE(i,n) FORE(i,0,n) 14 | #define FORSZ(i,a,v) FOR(i,a,SZ(v)) 15 | #define REPSZ(i,v) REP(i,SZ(v)) 16 | typedef long long ll; 17 | typedef unsigned long long ull; 18 | ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } 19 | 20 | int n,a,b; 21 | 22 | void run() { 23 | scanf("%d%d%d",&n,&a,&b); 24 | int l=1,h=a+b+1; 25 | while(l+1=n&&m<=a&&m<=b) l=m; else h=m; 29 | } 30 | printf("%d\n",l); 31 | } 32 | 33 | int main() { 34 | run(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /CP-Programming/Two Cakes.cpp: -------------------------------------------------------------------------------- 1 | problem statement link 2 | https://codeforces.com/contest/911/problem/B 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define PB push_back 8 | #define MP make_pair 9 | #define SZ(v) ((int)(v).size()) 10 | #define FOR(i,a,b) for(int i=(a);i<(b);++i) 11 | #define REP(i,n) FOR(i,0,n) 12 | #define FORE(i,a,b) for(int i=(a);i<=(b);++i) 13 | #define REPE(i,n) FORE(i,0,n) 14 | #define FORSZ(i,a,v) FOR(i,a,SZ(v)) 15 | #define REPSZ(i,v) REP(i,SZ(v)) 16 | typedef long long ll; 17 | typedef unsigned long long ull; 18 | ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } 19 | 20 | int n,a,b; 21 | 22 | void run() { 23 | scanf("%d%d%d",&n,&a,&b); 24 | int l=1,h=a+b+1; 25 | while(l+1=n&&m<=a&&m<=b) l=m; else h=m; 29 | } 30 | printf("%d\n",l); 31 | } 32 | 33 | int main() { 34 | run(); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Stacks/ApplicationOfStacks/Reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void insertBottom(stack &stk, int x) 5 | { 6 | if (stk.empty()) 7 | { 8 | stk.push(x); 9 | return; 10 | } 11 | else 12 | { 13 | int val = stk.top(); 14 | stk.pop(); 15 | insertBottom(stk, x); 16 | stk.push(val); 17 | } 18 | } 19 | void reverse(stack &stk) 20 | { 21 | if (stk.empty()) 22 | { 23 | return; 24 | } 25 | int val = stk.top(); 26 | stk.pop(); 27 | reverse(stk); 28 | insertBottom(stk, val); 29 | } 30 | 31 | //Driver Code 32 | int main() 33 | { 34 | stack stk; 35 | stk.push(10); 36 | stk.push(20); 37 | stk.push(30); 38 | reverse(stk); 39 | while (!stk.empty()) 40 | { 41 | cout << stk.top() << " "; 42 | stk.pop(); 43 | } 44 | printf("\n"); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Arrays/InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Insertion Sort Algorithm 3 | Time C: O(n^2) 4 | Space C: O(1) 5 | Method: Comparison Shifting 6 | */ 7 | #include 8 | using namespace std; 9 | void insertionSort(int arr[], int n) 10 | { 11 | int i, key, j; 12 | for (i = 1; i < n; i++) 13 | { 14 | key = arr[i]; 15 | j = i - 1; 16 | while (j >= 0 && arr[j] > key) 17 | { 18 | arr[j + 1] = arr[j]; 19 | j = j - 1; 20 | } 21 | arr[j + 1] = key; 22 | } 23 | } 24 | void display(int arr[], int n) 25 | { 26 | int i; 27 | for (i = 0; i < n; i++) 28 | cout << arr[i] << " "; 29 | cout << endl; 30 | } 31 | 32 | // Driver code 33 | int main() 34 | { 35 | int n; 36 | cin >> n; 37 | int arr[n]; 38 | for (auto &x : arr) 39 | { 40 | cin >> x; 41 | } 42 | insertionSort(arr, n); 43 | display(arr, n); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /CP-Programming/Tricky-permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void permutationNoDuplicates(string ques, string ans) 5 | { 6 | 7 | if (ques.length() == 0) 8 | { 9 | cout << ans << "\n"; 10 | return; 11 | } 12 | 13 | for (int i = 0; i < ques.length(); i++) 14 | { 15 | 16 | char ch = ques[i]; 17 | string ros = ques.substr(0, i) + ques.substr(i + 1); 18 | 19 | bool flag = true; 20 | 21 | for (int j = i + 1; j < ques.length(); j++) 22 | { 23 | if (ques[j] == ch) 24 | { 25 | flag = false; 26 | } 27 | } 28 | 29 | if (flag) 30 | permutationNoDuplicates(ros, ans + ch); 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | string str; 37 | cin >> str; 38 | 39 | sort(str.begin(), str.end()); 40 | 41 | permutationNoDuplicates(str, ""); 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Recursion/CombinationStringDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void printCombinations(char* input, int index, 6 | char* output, int outLength) 7 | { 8 | if (input[index] == '\0') 9 | { 10 | output[outLength] = '\0'; 11 | cout << output << endl; 12 | return; 13 | } 14 | output[outLength] = input[index]; 15 | 16 | output[outLength + 1] = ' '; 17 | 18 | printCombinations(input, index + 1, output, 19 | outLength + 2); 20 | 21 | if(input[index + 1] != '\0') 22 | printCombinations(input, index + 1, output, 23 | outLength + 1); 24 | 25 | } 26 | int main() 27 | { 28 | char input[] = "1214"; 29 | char *output = new char[100]; 30 | 31 | output[0] = '\0'; 32 | 33 | printCombinations(input, 0, output, 0); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Bitmaps/MismatchBits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Brian Kerningham's Algorithm 3 | */ 4 | #include 5 | using namespace std; 6 | #define NN cout << "NO" << endl 7 | #define YY cout << "YES" << endl 8 | #define ln(x) x.length() 9 | typedef long long int lli; 10 | bool checkbit(int a, int b, int k) 11 | { 12 | int XOR = a ^ b, cnt = 0; 13 | while (XOR) 14 | { 15 | XOR = XOR & (XOR - 1); 16 | cnt++; 17 | } 18 | if (cnt <= k) 19 | { 20 | return true; 21 | } 22 | else 23 | { 24 | return false; 25 | } 26 | } 27 | int main() 28 | { 29 | int n, m, k; 30 | cin >> n >> m >> k; 31 | vector v(m); 32 | for (auto &i : v) 33 | { 34 | cin >> i; 35 | } 36 | int key, c = 0; 37 | cin >> key; 38 | for (auto i : v) 39 | { 40 | if (checkbit(i, key, k) == true) 41 | { 42 | c++; 43 | } 44 | } 45 | cout << c << endl; 46 | return 0; 47 | } -------------------------------------------------------------------------------- /CP-Programming/Upper-bound-lower-bound.md: -------------------------------------------------------------------------------- 1 | ## Problem Name: Find Upper and Lower bound 2 | ### Problem Difficulty: None 3 | #### Problem Constraints: 4 | 1 <= N <= 10^5
5 | 1 <= Q <= 100 6 | 7 | 8 | #### Problem Description: 9 | Find position of the last and first occurrence of a given number in a sorted array. If number does not exist then print lower and upper bound as -1. 10 | #### Input Format: 11 | First line contains an integer n denoting the size of the array.
12 | Second line contains n space separated integers denoting array elements.
13 | Third line contains single integer Q denoting the no of queries.
14 | Q lines follow , each containing a single integer x that is to be searched in the array. 15 | 16 | * Sample Input: 17 |
18 | 5
19 | 1 2 3 3 4
20 | 3
21 | 2
22 | 3
23 | 10
24 | 
25 | 26 | #### Output Format: 27 | For each query , print the lowerbound and the upperbound for the number x in a new line each. 28 | * Sample Output: 29 |
 
30 | 1 1
31 | 2 3
32 | -1 -1
33 | 
-------------------------------------------------------------------------------- /Trees/LevelOrderTraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int key; 6 | struct Node *left; 7 | struct Node *right; 8 | Node(int k) 9 | { 10 | key = k; 11 | left = right = NULL; 12 | } 13 | }; 14 | void levelOrder(Node *root){ 15 | if(root == NULL){ 16 | return; 17 | } 18 | queue q; 19 | q.push(root); 20 | while(!q.empty()){ 21 | Node *temp = q.front(); 22 | cout << temp->key << endl; 23 | q.pop(); 24 | if(temp->left != NULL){ 25 | q.push(temp->left); 26 | } 27 | if(temp->right != NULL){ 28 | q.push(temp->right); 29 | } 30 | } 31 | } 32 | int main() { 33 | 34 | Node *root=new Node(10); 35 | root->left=new Node(20); 36 | root->right=new Node(30); 37 | root->left->left=new Node(40); 38 | root->left->right=new Node(50); 39 | root->right->left=new Node(60); 40 | root->right->right=new Node(70); 41 | 42 | levelOrder(root); 43 | } -------------------------------------------------------------------------------- /Arrays/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Bubble Sort Algorithm 3 | Time C: O(n^2) 4 | Space C: O(1) 5 | Method: Exchanging 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | void swap(int *x, int *y) 11 | { 12 | *x = *x + *y; 13 | *y = *x - *y; 14 | *x = *x - *y; 15 | } 16 | 17 | void bubbleSort(int arr[], int n) 18 | { 19 | int i, j; 20 | for (i = 0; i < n - 1; i++) 21 | { 22 | for (j = 0; j < n - i - 1; j++) 23 | { 24 | if (arr[j] > arr[j + 1]) 25 | { 26 | swap(&arr[j], &arr[j + 1]); 27 | } 28 | } 29 | } 30 | } 31 | void display(int arr[], int n) 32 | { 33 | int i; 34 | for (i = 0; i < n; i++) 35 | cout << arr[i] << " "; 36 | cout << endl; 37 | } 38 | 39 | // Driver code 40 | int main() 41 | { 42 | int n; 43 | cin >> n; 44 | int arr[n]; 45 | for (auto &x : arr) 46 | { 47 | cin >> x; 48 | } 49 | bubbleSort(arr, n); 50 | display(arr, n); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Arrays/BucketSort.java: -------------------------------------------------------------------------------- 1 | package algorithm; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | public class MyBucketSort { 8 | public void sort(int[] a, int k) { 9 | int max = a[0]; 10 | for(int i=1;i max) { 12 | max = a[i]; 13 | } 14 | } 15 | max++; 16 | List[] bucket = new List[k]; 17 | for(int i=0;i(); 19 | } 20 | 21 | for(int i=0;i 2 | #define F first 3 | #define S second 4 | #define MAX 10000003 5 | using namespace std; 6 | 7 | typedef long long int ll; 8 | const ll mod = 1000000007; 9 | 10 | ll max(ll a, ll b) { return (a > b)? a : b; } 11 | 12 | ll knapSack(ll W, ll wt[], ll val[], ll n) 13 | { 14 | ll i, w; 15 | ll K[n+1][W+1]; 16 | for (i = 0; i <= n; i++) 17 | { 18 | for (w = 0; w <= W; w++) 19 | { 20 | if (i==0 || w==0) 21 | K[i][w] = 0; 22 | else if (wt[i-1] <= w) 23 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 24 | else 25 | K[i][w] = K[i-1][w]; 26 | } 27 | } 28 | return K[n][W]; 29 | } 30 | 31 | int main() 32 | { 33 | ios_base::sync_with_stdio(false); 34 | cin.tie(NULL); 35 | cout.tie(NULL); 36 | ll n,w; 37 | cin>>n>>w; 38 | ll a[n],b[n]; 39 | for(ll x=0; x>a[x]>>b[x]; 41 | } 42 | cout< 4 | using namespace std; 5 | 6 | int kthSmallest(int *, int, int, int); 7 | 8 | int main() 9 | { 10 | // ios_base::sync_with_stdio(false); 11 | // cin.tie(NULL); 12 | 13 | int test_case; 14 | cin>>test_case; 15 | while(test_case--) 16 | { 17 | int number_of_elements; 18 | cin>>number_of_elements; 19 | int a[number_of_elements]; 20 | 21 | for(int i=0;i>a[i]; 23 | 24 | int k; 25 | cin>>k; 26 | cout<>!!!!!!!!!!!!!!!!!! 2 | //Program : All elements have been repeated exactly twice except two such unique number find it! 3 | /* 4 | Properties to keep in mind: 5 | 0 ^ Y = Y 6 | Y ^ Y = 0 7 | X ^ Y = Z 8 | X = Z ^ Y 9 | */ 10 | #include 11 | using namespace std; 12 | typedef long long int lli; 13 | #define loop(i, a, b) for (lli i = a; i < b; i++) 14 | int get_bit(int n) 15 | { 16 | int cnt = 0; 17 | while ((n & (1 << cnt)) == 0) 18 | { 19 | cnt++; 20 | } 21 | return cnt; 22 | } 23 | void solve(int A[], int n) 24 | { 25 | int c = A[0]; 26 | loop(i, 1, n) 27 | { 28 | c = c ^ A[i]; 29 | } 30 | //cout<> n; 44 | int A[n]; 45 | loop(i, 0, n) 46 | { 47 | cin >> A[i]; 48 | } 49 | solve(A, n); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Dynamic programming/Longest Palindromic Subsequence.cpp: -------------------------------------------------------------------------------- 1 | proble statemnt 2 | https://leetcode.com/problems/longest-palindromic-subsequence/ 3 | 4 | Video link 5 | https://www.youtube.com/watch?v=YHSjvswCXC8 6 | 7 | class Solution { 8 | public: 9 | 10 | int lps(string str){ 11 | int n = str.size(); 12 | int dp[n][n]; // Create a table to store results of subproblems 13 | 14 | // Strings of length 1 are palindrome of length 1 15 | for (int i = 0; i < n; i++) 16 | dp[i][i] = 1; 17 | 18 | // Build the table. Note that the lower diagonal values of table are 19 | // useless and not filled in the process. 20 | for (int cl = 2; cl <= n; cl++){ 21 | for (int i = 0; i < n - cl + 1; i++){ 22 | int j = i + cl - 1; 23 | if (str[i] == str[j] && cl == 2) 24 | dp[i][j] = 2; 25 | else if (str[i] == str[j]) 26 | dp[i][j] = dp[i + 1][j - 1] + 2; 27 | else 28 | dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]); 29 | } 30 | } 31 | return dp[0][n - 1]; 32 | } 33 | 34 | int longestPalindromeSubseq(string s) { 35 | return lps(s); 36 | } 37 | }; -------------------------------------------------------------------------------- /Arrays/SumSubarr.cpp: -------------------------------------------------------------------------------- 1 | //Google 2008 X Google Kickstart 2019A 2 | //Find constinuous subarray that adds to a given number S 3 | /* 4 | Logic: We would apply 2 pointers and increase till (ptr1+ptr2<=S) 5 | */ 6 | #include 7 | using namespace std; 8 | typedef long long int lli; 9 | #define loop(i, a, b) for (lli i = a; i < b; i++) 10 | void solve(lli A[],lli n,lli s){ 11 | lli c=0; 12 | lli ptr1=A[c],i=c+1; 13 | lli ptr2 = A[i]; 14 | ptr2 = ptr2+ptr1; 15 | while(ptr2!=s){ 16 | while(ptr2 < s) 17 | { 18 | i++; 19 | ptr2 = ptr2 + A[i]; 20 | } 21 | if(ptr2==s){ 22 | cout<>n; 39 | lli A[n]; 40 | loop(i,0,n){ 41 | cin>>A[i]; 42 | } 43 | lli s; cin>>s; 44 | solve(A,n,s); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Dynamic programming/Independent_Set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define MAXN 100005 6 | #define MOD 1000000007 7 | 8 | long long n; 9 | vector> adj(MAXN); 10 | vector waysBlack(MAXN); 11 | vector waysWhite(MAXN); 12 | 13 | void dfs(long long node, long long parent) { 14 | waysBlack[node] = 1; 15 | waysWhite[node] = 1; 16 | 17 | for(auto child : adj[node]) { 18 | if (child != parent) { 19 | dfs(child, node); 20 | waysWhite[node] = (waysWhite[node] * (waysWhite[child] + waysBlack[child])) % MOD; 21 | } 22 | } 23 | for(auto child : adj[node]) { 24 | if (child != parent) { 25 | waysBlack[node] = (waysBlack[node] * waysWhite[child]) % MOD; 26 | } 27 | } 28 | } 29 | 30 | int main() { 31 | cin >> n; 32 | for(long long i = 0; i < n - 1; ++i) { 33 | long long x, y; 34 | cin >> x >> y; 35 | --x; --y; 36 | adj[x].push_back(y); 37 | adj[y].push_back(x); 38 | } 39 | 40 | dfs(0, -1); 41 | 42 | cout << (waysBlack[0] + waysWhite[0]) % MOD << endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Lists/Remove Duplicate From Sorted Linked List.c: -------------------------------------------------------------------------------- 1 | //LinkedList/Remove Duplicate From Sorted Linked List.c 2 | #include 3 | #include 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }*first=NULL,*second=NULL,*third=NULL; 9 | void Display(struct Node *p) 10 | { 11 | while(p!=NULL) 12 | { 13 | printf("%d ",p->data); 14 | p=p->next; 15 | } 16 | } 17 | void create(int A[],int n) 18 | { 19 | int i; 20 | struct Node *t,*last; 21 | first=(struct Node *)malloc(sizeof(struct Node)); 22 | first->data=A[0]; 23 | first->next=NULL; 24 | last=first; 25 | 26 | for(i=1;idata=A[i]; 30 | t->next=NULL; 31 | last->next=t; 32 | last=t; 33 | } 34 | } 35 | void RemoveDuplicate(struct Node *p) 36 | { 37 | struct Node *q=p->next; 38 | 39 | while(q!=NULL) 40 | { 41 | if(p->data!=q->data) 42 | { 43 | p=q; 44 | q=q->next; 45 | } 46 | else 47 | { 48 | p->next=q->next; 49 | free(q); 50 | q=p->next; 51 | } 52 | } 53 | 54 | } 55 | int main() 56 | { 57 | 58 | int A[]={10,20,20,40,50,50,50,60}; 59 | create(A,8); 60 | 61 | 62 | RemoveDuplicate(first); 63 | Display(first); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /CP-Programming/SortGame.md: -------------------------------------------------------------------------------- 1 | ## Problem Name: Sort Game 2 | ### Problem Difficulty: 1 3 | #### Problem Constraints: 1 <= N <= 10^5 4 | 1 <= | Length of the name | <= 100 5 | 1 <= x, salary <= 100 6 | 7 | #### Problem Description: 8 | Sanju needs your help! He gets a list of employees with their salary. The maximum salary is 100. 9 |
10 | Sanju is supposed to arrange the list in such a manner that the list is sorted in decreasing order of salary. And if two employees have the same salary, they should be arranged in lexicographical manner such that the list contains only names of those employees having salary greater than or equal to a given number x. 11 | 12 | Help Sanju prepare the same! 13 |
14 | 15 | #### Input Format: 16 | On the first line of the standard input, there is an integer x. The next line contans integer N, denoting the number of employees. N lines follow, which contain a string and an integer, denoting the name of the employee and his salary. 17 | * Sample Input: 18 |
19 | 79
20 | 4
21 | Eve 78
22 | Bob 99
23 | Suzy 86
24 | Alice 86
25 | 
26 | #### Output Format: 27 | You must print the required list. 28 | * Sample Output: 29 |
30 | Bob 99
31 | Alice 86
32 | Suzy 86
33 | 
34 | -------------------------------------------------------------------------------- /Tree/Right_view_of_binary_tree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | public class right_view { 5 | static class Node{ 6 | int data; 7 | Node right; 8 | Node left; 9 | Node(int data) 10 | { 11 | this.data=data; 12 | } 13 | } 14 | static Node create_tree() 15 | { 16 | Scanner in =new Scanner(System.in); 17 | Node root=null; 18 | System.out.println("Enter data:"); 19 | int data=in.nextInt(); 20 | if (data==-1) 21 | return null; 22 | root=new Node(data); 23 | System.out.println("Enter left of "+data); 24 | root.left=create_tree(); 25 | System.out.println("Enter right of "+data); 26 | root.right=create_tree(); 27 | return root; 28 | } 29 | static void right_view(Node root) 30 | { 31 | Queue q=new LinkedList<>(); 32 | q.add(root); 33 | while(!q.isEmpty()) 34 | { 35 | int size=q.size(); 36 | for (int i=0;i 2 | #define V 4 3 | #define INF 99999 4 | 5 | void printSolution(int d[][V]); 6 | void floydWarshall (int graph[][V]) 7 | { 8 | int d[V][V], i, j, k; 9 | for (i = 0; i < V; i++) 10 | for (j = 0; j < V; j++) 11 | d[i][j] = graph[i][j]; 12 | 13 | 14 | for (k = 0; k < V; k++) 15 | { 16 | for (i = 0; i < V; i++) 17 | { 18 | for (j = 0; j < V; j++) 19 | { 20 | if (d[i][k] + d[k][j] < d[i][j]) 21 | d[i][j] = d[i][k] + d[k][j]; 22 | } 23 | } 24 | } 25 | 26 | printSolution(d); 27 | } 28 | 29 | void printSolution(int d[][V]) 30 | { 31 | printf ("The following matrix shows the shortest distances" 32 | " between every pair of vertices \n"); 33 | for (int i = 0; i < V; i++) 34 | { 35 | for (int j = 0; j < V; j++) 36 | { 37 | if (d[i][j] == INF) 38 | printf("%7s", "INF"); 39 | else 40 | printf ("%7d", d[i][j]); 41 | } 42 | printf("\n"); 43 | } 44 | } 45 | 46 | int main() 47 | { 48 | int graph[V][V] = {{0, 3, INF, 5}, 49 | {2, 0, INF, 4}, 50 | {INF, 1, 0, INF}, 51 | {INF, INF, 2, 0} 52 | }; 53 | floydWarshall(graph); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Lists/length of Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int length(Node *head) { 4 | 5 | int count=0; 6 | while(head) 7 | { 8 | count++; 9 | head=head->next; 10 | } 11 | return count; 12 | } 13 | class Node{ 14 | public: 15 | int data; 16 | Node *next; 17 | Node(int data){ 18 | this -> data = data; 19 | this -> next = NULL; 20 | } 21 | }; 22 | Node* takeinput() { 23 | int data; 24 | cin >> data; 25 | Node* head = NULL, *tail = NULL; 26 | while(data != -1){ 27 | Node *newNode = new Node(data); 28 | if(head == NULL) { 29 | head = newNode; 30 | tail = newNode; 31 | } 32 | else{ 33 | tail -> next = newNode; 34 | tail = newNode; 35 | } 36 | cin >> data; 37 | } 38 | return head; 39 | } 40 | 41 | void print(Node *head) { 42 | Node *temp = head; 43 | while(temp != NULL) { 44 | cout << temp -> data << " "; 45 | temp = temp -> next; 46 | } 47 | cout<> pos; 54 | cout << length(head); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Graph/Bipartite Graph Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long int 5 | #define vi vector 6 | #define vvi vector 7 | #define pb push_back 8 | #define fo(i,a,b) for(int i=a ; i>n>>m; 42 | fo(i,0,m) 43 | { 44 | int x,y; cin>>x>>y; 45 | adjl[x].pb(y); 46 | adjl[y].pb(x); 47 | } 48 | int flag=0; 49 | foe(i,1,n) 50 | { 51 | if(!vis[i]) 52 | { 53 | if(dfs(i,0)==false) flag=1; 54 | } 55 | } 56 | if(flag==0) cout<<"Bipartite Graph"< 3 | using namespace std; 4 | #define n 100 5 | class stack { 6 | string* arr; 7 | int head; 8 | public: 9 | stack(){ 10 | arr = new string[n]; 11 | head = -1; 12 | } 13 | void push(string x){ 14 | if(head==(n-1)){ 15 | cout<<"Stack Overflow"< 13 | using namespace std; 14 | 15 | vector maxSlidingWindow(vector& nums, int k) { 16 | vector ans; 17 | deque q; 18 | 19 | for(int i = 0; i < k; i++){ 20 | while(!q.empty() && q.back() < nums[i]){ 21 | q.pop_back(); 22 | } 23 | q.push_back(nums[i]); 24 | } 25 | ans.push_back(q.front()); 26 | 27 | for(int i = k; i < (int)nums.size(); i++){ 28 | if(q.front() == nums[i-k]) q.pop_front(); 29 | while(!q.empty() && q.back() < nums[i]){ 30 | q.pop_back(); 31 | } 32 | q.push_back(nums[i]); 33 | ans.push_back(q.front()); 34 | } 35 | return ans; 36 | } 37 | 38 | int main() { 39 | vector arr = {1,3,-1,-3,5,3,6,7}; //Array of numbers; 40 | int k = 3; //Sliding window size; 41 | vector sol = maxSlidingWindow(arr, k); 42 | cout<<"The sliding window maximums are: "< 2 | 3 | int findLowerBound(int num, int a[],int n){ 4 | int lowerBound = -1; 5 | 6 | int s=0; 7 | int e=n; 8 | 9 | while(s<=e){ 10 | int mid = (s+e)/2; 11 | if(a[mid]==num){ 12 | lowerBound=mid; 13 | e=mid-1; 14 | }else if(a[mid]>num){ 15 | e=mid-1; 16 | }else{ 17 | s=mid+1; 18 | } 19 | } 20 | 21 | return lowerBound; 22 | } 23 | 24 | int findUpperBound(int num, int a[],int n){ 25 | int upperBound =-1; 26 | 27 | int s=0; 28 | int e=n; 29 | 30 | while(s<=e){ 31 | int mid = (s+e)/2; 32 | if(a[mid]==num){ 33 | upperBound=mid; 34 | s=mid+1; 35 | }else if(a[mid]>num){ 36 | e=mid-1; 37 | }else{ 38 | s=mid+1; 39 | } 40 | } 41 | 42 | return upperBound; 43 | } 44 | 45 | 46 | int main() { 47 | int n; 48 | std::cin >> n; 49 | 50 | int a[n]; 51 | for (int i = 0; i < n; i++) { 52 | std::cin >> a[i]; 53 | } 54 | 55 | int t; 56 | std::cin >> t; 57 | 58 | while(t--){ 59 | int num; 60 | std::cin >> num; 61 | 62 | int lowerBound = findLowerBound(num,a,n); 63 | int upperBound = findUpperBound(num,a,n); 64 | 65 | std::cout << lowerBound<<" "< 2 | using namespace std; 3 | #define sync ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 4 | class Node{ 5 | public: 6 | int data; 7 | Node* next; 8 | }; 9 | Node* pop(Node* head){ 10 | if(head==NULL) 11 | return NULL; 12 | Node* temp = head; 13 | head = head->next; 14 | delete temp; 15 | return head; 16 | } 17 | void push(Node* head){ 18 | while(head->next!=NULL) 19 | head=head->next; 20 | int d; cin>>d; 21 | Node* node = new Node(); 22 | node->data=d;node->next=NULL; 23 | head->next=node; 24 | } 25 | void peek(Node* head){ 26 | if(head==NULL){ 27 | cout<<"EMPTY"<data<next!=NULL) 34 | { 35 | cout<data<<"-->"; 36 | head=head->next; 37 | } 38 | cout<data<>c; 44 | while(c!=0){ 45 | if(c==1){ 46 | if(head==NULL){ 47 | int d;cin>>d; head=new Node(); head->data=d;head->next=NULL; 48 | } 49 | else 50 | push(head); 51 | } 52 | if(c==2){ 53 | head=pop(head); 54 | } 55 | if(c==3){ 56 | peek(head); 57 | } 58 | if(c==4){ 59 | print(head); 60 | } 61 | cin>>c; 62 | } 63 | } -------------------------------------------------------------------------------- /Stacks/Stack-Lists.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class StackNode { 4 | public: 5 | int data; 6 | StackNode* next; 7 | }; 8 | StackNode* newNode(int data){ 9 | StackNode* stknode = new StackNode(); 10 | stknode->data = data; 11 | stknode->next = NULL; 12 | return stknode; 13 | } 14 | bool isEmpty(StackNode* root){ 15 | return (!root); 16 | } 17 | void push(StackNode** root, int val){ 18 | StackNode* node = newNode(val); 19 | node->next = *root; 20 | *root = node; 21 | printf("%d pushed into the stack",val); 22 | } 23 | int pop(StackNode** root){ 24 | if(isEmpty(*root)){ 25 | printf("Stack Underflow\n"); 26 | return INT_MIN; 27 | } else { 28 | StackNode* temp = *root; 29 | *root = (*root)->next; 30 | int x = (*temp).data; 31 | free(temp); 32 | return x; 33 | } 34 | } 35 | int peek(StackNode* root){ 36 | if(isEmpty(root)){ 37 | printf("Stack Underflow\n"); 38 | return INT_MIN; 39 | } else { 40 | return root->data; 41 | } 42 | } 43 | //Driver Code 44 | int main(){ 45 | StackNode* stk = NULL; 46 | push(&stk,10); 47 | push(&stk, 15); 48 | push(&stk, 20); 49 | cout<< pop(&stk) << "popped from the stack\n"; 50 | cout<< peek(stk) << "is the top element now\n"; 51 | while(!isEmpty(stk)){ 52 | cout< adj[]; 7 | 8 | @SuppressWarnings("unchecked") Graph(int v) 9 | { 10 | V = v; 11 | adj = new LinkedList[v]; 12 | for (int i = 0; i < v; ++i) 13 | adj[i] = new LinkedList(); 14 | } 15 | 16 | void addEdge(int v, int w) 17 | { 18 | adj[v].add(w); // Add w to v's list. 19 | } 20 | 21 | void DFSUtil(int v, boolean visited[]) 22 | { 23 | visited[v] = true; 24 | System.out.print(v + " "); 25 | 26 | Iterator i = adj[v].listIterator(); 27 | while (i.hasNext()) 28 | { 29 | int n = i.next(); 30 | if (!visited[n]) 31 | DFSUtil(n, visited); 32 | } 33 | } 34 | 35 | void DFS(int v) 36 | { 37 | boolean visited[] = new boolean[V]; 38 | DFSUtil(v, visited); 39 | } 40 | 41 | 42 | public static void main(String args[]) 43 | { 44 | Graph g = new Graph(4); 45 | 46 | g.addEdge(0, 1); 47 | g.addEdge(0, 2); 48 | g.addEdge(1, 2); 49 | g.addEdge(2, 0); 50 | g.addEdge(2, 3); 51 | g.addEdge(3, 3); 52 | 53 | System.out.println( 54 | "Following is Depth First Traversal " 55 | + "(starting from vertex 2)"); 56 | 57 | g.DFS(2); 58 | } 59 | } -------------------------------------------------------------------------------- /BinarySearch/CountOccurancesUsingBinarySearch.cpp: -------------------------------------------------------------------------------- 1 | // Program to count occurances of a number in Sorted Array Using binary Search 2 | #include 3 | #include 4 | using namespace std; 5 | int a[1000000]; 6 | int count = 0; 7 | 8 | int getLeft(int low, int high, int key) { 9 | int mid , ans = -1; 10 | while (low <= high) { 11 | mid = low + (high - low) / 2; 12 | 13 | if (a[mid] > key) 14 | high = mid - 1; 15 | else if (a[mid] < key ) 16 | low = mid + 1; 17 | else { 18 | ans = mid; 19 | high = mid - 1; 20 | } 21 | } 22 | return ans; 23 | } 24 | 25 | int getRight(int low, int high, int key) { 26 | int mid , ans = -1; 27 | while (low <= high) { 28 | mid = low + (high - low) / 2; 29 | if (a[mid] > key) { 30 | high = mid - 1; 31 | } 32 | else if (a[mid] < key) { 33 | low = mid + 1; 34 | } 35 | else { 36 | ans = mid; 37 | low = mid + 1; 38 | } 39 | } 40 | return ans; 41 | } 42 | 43 | void countOccurances(int n, int key) { 44 | int x = getLeft(0, n - 1, key); 45 | int y = getRight(0, n - 1, key); 46 | cout << "Left Position :" << x << endl; 47 | cout << "Right Position :" << y << endl; 48 | if (x != -1 && y != -1 && a[x] == key && a[y] == key) 49 | cout << (y - x + 1) << endl; 50 | else 51 | cout << "Not found !" << endl; 52 | 53 | } 54 | 55 | int main() { 56 | int t, n, i, key; 57 | cin >> t; 58 | while (t--) { 59 | cin >> n; 60 | for (i = 0; i < n; i++) scanf("%d", &a[i]); 61 | count = 0; 62 | cin >> key; 63 | countOccurances(n, key); 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Stacks/stock_span.cpp: -------------------------------------------------------------------------------- 1 | // Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day. 2 | 3 | // The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backward) 4 | // for which the stock price was less than or equal to today's price. 5 | 6 | 7 | #include 8 | using namespace std; 9 | 10 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); 11 | #define external_io() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 12 | #define secsort() bool sortbysec(const pair &a, const pair &b){return (a.second < b.second);} 13 | #define MOD 1000000007 14 | #define MAXN 100005 15 | 16 | vector solve(vector arr){ 17 | stack > stk; 18 | vector sol(arr.size()); 19 | stk.push(make_pair(arr[0], 1)); 20 | sol[0] = 1; 21 | for (int i = 1; i < arr.size(); i++){ 22 | pair temp = {arr[i], 1}; 23 | while(!stk.empty() && stk.top().first>n; 37 | vector arr(n); 38 | for(int i=0; i>arr[i]; 40 | } 41 | vector sol =solve(arr); 42 | for(int i=0; i 3 | using namespace std; 4 | #define MAX 1000 5 | 6 | //Defining a class to implement functions like push,pop,peek etc 7 | class Stack { 8 | int top; 9 | public: 10 | int arr[MAX]; 11 | Stack(){ 12 | top = -1; 13 | }; 14 | bool push(int x); 15 | int pop(); 16 | int peek(); 17 | bool isEmpty(); 18 | }; 19 | 20 | //Pushing in a stack 21 | bool Stack::push(int val){ 22 | if(top>=(MAX-1)){ 23 | printf("Stack Overflow\n"); 24 | return false; 25 | } else { 26 | arr[++top] = val; 27 | printf("%d pushed into the stack!\n",val); 28 | return true; 29 | } 30 | } 31 | 32 | //Popping from a stack 33 | int Stack::pop(){ 34 | if(top<0){ 35 | printf("Stack Underflow\n"); 36 | return 0; 37 | } else { 38 | int x = arr[top--]; 39 | return x; 40 | } 41 | } 42 | 43 | //Check isEmpty 44 | bool Stack::isEmpty(){ 45 | return (top<0); 46 | } 47 | 48 | //Peek the stack 49 | int Stack::peek(){ 50 | if(top<0){ 51 | printf("Stack is empty\n"); 52 | return 0; 53 | } else { 54 | int x =arr[top]; 55 | return x; 56 | } 57 | } 58 | 59 | //Driver Code 60 | int main(){ 61 | class Stack stk; 62 | stk.push(10); 63 | stk.push(20); 64 | stk.push(30); 65 | printf("Elements present in the Stack\n"); 66 | while(!stk.isEmpty()){ 67 | printf("%d ",stk.peek()); 68 | stk.pop(); 69 | }printf("\n"); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Greedy/DIJKSTRAS_ALGO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define V 9 5 | 6 | int minDistance(int dist[], int sptSet[]) 7 | { 8 | int min = INT_MAX, min_index; 9 | 10 | for (int v = 0; v < V; v++) 11 | if (sptSet[v] == 0 && dist[v] <= min) 12 | min = dist[v], min_index = v; 13 | 14 | return min_index; 15 | } 16 | 17 | void printSolution(int dist[]) 18 | { 19 | printf("Vertex \t\t Distance from Source\n"); 20 | for (int i = 0; i < V; i++) 21 | printf("%d \t\t %d\n", i, dist[i]); 22 | } 23 | 24 | void dijkstra(int graph[V][V], int src) 25 | { 26 | int dist[V]; 27 | 28 | int sptSet[V]; 29 | 30 | for (int i = 0; i < V; i++) 31 | dist[i] = INT_MAX, sptSet[i] = 0; 32 | 33 | dist[src] = 0; 34 | 35 | for (int count = 0; count < V - 1; count++) { 36 | 37 | int u = minDistance(dist, sptSet); 38 | 39 | sptSet[u] = 1; 40 | 41 | for (int v = 0; v < V; v++) 42 | if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX 43 | && dist[u] + graph[u][v] < dist[v]) 44 | dist[v] = dist[u] + graph[u][v]; 45 | } 46 | 47 | printSolution(dist); 48 | } 49 | 50 | int main() 51 | { 52 | int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, 53 | { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, 54 | { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, 55 | { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, 56 | { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, 57 | { 0, 0, 4, 14, 10, 0, 2, 0, 0 }, 58 | { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, 59 | { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, 60 | { 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; 61 | 62 | dijkstra(graph, 0); 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Math/MatrixExponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define REP(i, n) for (int i = 1; i <= n; i++) 3 | #define mod 1000000007 4 | #define ll long long 5 | #define N 101 6 | 7 | using namespace std; 8 | 9 | /* struct Optimizer{ 10 | Optimizer(){ 11 | ios_base::sync_with_stdio(false); 12 | cin.tie(NULL); 13 | } 14 | }; 15 | 16 | Optimizer opt; */ 17 | 18 | int arr[N][N], I[N][N]; 19 | 20 | void mul(int A[N][N] , int B[N][N] , int dim){ 21 | int res[dim+1][dim+1]; 22 | 23 | REP(i, dim) REP(j, dim) { 24 | res[i][j] = 0; 25 | REP(k, dim) res[i][j] += A[i][k] * B[k][j]; 26 | } 27 | 28 | REP(i, dim) REP(j, dim) A[i][j] = res[i][j]; 29 | } 30 | 31 | void power(int A[N][N], int dim, int n){ 32 | REP(i, dim) REP(j, dim){ 33 | if(i == j) I[i][j] = 1; 34 | else I[i][j] = 0; 35 | } 36 | 37 | // Complexity of O(M^3 * N) 38 | /* REP(i, n) 39 | mul(I, A, dim); */ 40 | 41 | //Complexity of O(M^3 * logN) 42 | while(n){ 43 | if(n % 2) mul(I, A, dim), n--; 44 | 45 | else mul(A, A, dim), n /= 2; 46 | } 47 | 48 | REP(i, dim) REP(j, dim) A[i][j] = I[i][j]; 49 | } 50 | 51 | void printMat(int A[][N], int dim){ 52 | REP(i, dim) { 53 | REP(j, dim) cout << A[i][j] << " "; 54 | cout << "\n"; 55 | } 56 | } 57 | 58 | int main(){ 59 | 60 | int t, dim, n; cin >> t; 61 | while (t--){ 62 | cin >> dim >> n; 63 | 64 | REP(i, dim) REP(j, dim) cin >> arr[i][j]; 65 | 66 | power(arr, dim, n); 67 | printMat(arr, dim); 68 | } 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Graph/Dijkstra's Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int findMinVertex(int* distance, bool* visited, int n){ 5 | int minVertex = -1; 6 | for(int i = 0; i < n; i++){ 7 | if(!visited[i] && (minVertex == -1 || distance[i] < distance[minVertex])){ 8 | minVertex = i; 9 | } 10 | } 11 | return minVertex; 12 | } 13 | void dijkstra(int** edges, int n){ 14 | int* distance = new int[n]; 15 | bool* visited = new bool[n]; 16 | for(int i = 0; i < n; i++){ 17 | distance[i] = INT_MAX; 18 | visited[i] = false; 19 | } 20 | distance[0] = 0; 21 | for(int i = 0; i < n - 1; i++){ 22 | int minVertex = findMinVertex(distance, visited, n); 23 | visited[minVertex] = true; 24 | for(int j = 0; j < n; j++){ 25 | if(edges[minVertex][j] != 0 && !visited[j]){ 26 | int dist = distance[minVertex] + edges[minVertex][j]; 27 | if(dist < distance[j]){ 28 | distance[j] = dist; 29 | } 30 | } 31 | } 32 | } 33 | for(int i = 0; i < n; i++){ 34 | cout << i << " " << distance[i] << endl; 35 | } 36 | delete [] visited; 37 | delete [] distance; 38 | } 39 | int main() { 40 | int n; 41 | int e; 42 | cin >> n >> e; 43 | int** edges = new int*[n]; 44 | for (int i = 0; i < n; i++) { 45 | edges[i] = new int[n]; 46 | for (int j = 0; j < n; j++) { 47 | edges[i][j] = 0; 48 | } 49 | } 50 | for (int i = 0; i < e; i++) { 51 | int f, s, weight; 52 | cin >> f >> s >> weight; 53 | edges[f][s] = weight; 54 | edges[s][f] = weight; 55 | } 56 | cout << endl; 57 | dijkstra(edges, n); 58 | for (int i = 0; i < n; i++) { 59 | delete [] edges[i]; 60 | } 61 | delete [] edges; 62 | } 63 | -------------------------------------------------------------------------------- /Graph/Kruskal's Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int findMinVertex(int* distance, bool* visited, int n){ 5 | int minVertex = -1; 6 | for(int i = 0; i < n; i++){ 7 | if(!visited[i] && (minVertex == -1 || distance[i] < distance[minVertex])){ 8 | minVertex = i; 9 | } 10 | } 11 | return minVertex; 12 | } 13 | void dijkstra(int** edges, int n){ 14 | int* distance = new int[n]; 15 | bool* visited = new bool[n]; 16 | for(int i = 0; i < n; i++){ 17 | distance[i] = INT_MAX; 18 | visited[i] = false; 19 | } 20 | distance[0] = 0; 21 | for(int i = 0; i < n - 1; i++){ 22 | int minVertex = findMinVertex(distance, visited, n); 23 | visited[minVertex] = true; 24 | for(int j = 0; j < n; j++){ 25 | if(edges[minVertex][j] != 0 && !visited[j]){ 26 | int dist = distance[minVertex] + edges[minVertex][j]; 27 | if(dist < distance[j]){ 28 | distance[j] = dist; 29 | } 30 | } 31 | } 32 | } 33 | for(int i = 0; i < n; i++){ 34 | cout << i << " " << distance[i] << endl; 35 | } 36 | delete [] visited; 37 | delete [] distance; 38 | } 39 | int main() { 40 | int n; 41 | int e; 42 | cin >> n >> e; 43 | int** edges = new int*[n]; 44 | for (int i = 0; i < n; i++) { 45 | edges[i] = new int[n]; 46 | for (int j = 0; j < n; j++) { 47 | edges[i][j] = 0; 48 | } 49 | } 50 | for (int i = 0; i < e; i++) { 51 | int f, s, weight; 52 | cin >> f >> s >> weight; 53 | edges[f][s] = weight; 54 | edges[s][f] = weight; 55 | } 56 | cout << endl; 57 | dijkstra(edges, n); 58 | for (int i = 0; i < n; i++) { 59 | delete [] edges[i]; 60 | } 61 | delete [] edges; 62 | } 63 | -------------------------------------------------------------------------------- /Back-tracking/nqueen.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | bool issafe(int **arr,int x,int y,int n) 6 | { 7 | for(int i=0;i=0 &&col>=0) 14 | { 15 | if(arr[row][col]==1) 16 | return false; 17 | row--;col--; 18 | } row=x;col=y; 19 | 20 | while(row>=0 && col=n) 31 | { 32 | return true; 33 | } 34 | for(int col=0;col>n; 50 | int **arr=new int *[n]; 51 | for(int i=0;i 3 | using namespace std; 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | 9 | Node(int x) 10 | { 11 | data = x; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | void append(struct Node **headRef, int newData) 17 | { 18 | struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); 19 | struct Node *last = *headRef; 20 | new_node->data = newData; 21 | new_node->next = NULL; 22 | if (*headRef == NULL) 23 | { 24 | *headRef = new_node; 25 | return; 26 | } 27 | while (last->next != NULL) 28 | last = last->next; 29 | last->next = new_node; 30 | return; 31 | } 32 | 33 | int sumOfLastN_Nodes(struct Node *head, int n); 34 | 35 | // Driver program to test above 36 | int main() 37 | { 38 | int t; 39 | cin >> t; 40 | while (t--) 41 | { 42 | struct Node *head = NULL; 43 | int n, m, tmp; 44 | cin >> m >> n; 45 | while (m--) 46 | { 47 | cin >> tmp; 48 | append(&head, tmp); 49 | } 50 | cout << sumOfLastN_Nodes(head, n) << endl; 51 | } 52 | return 0; 53 | } 54 | int sumOfLastN_Nodes(struct Node *head, int n) 55 | { 56 | int l = 0; 57 | struct Node *ptr = head; 58 | while (ptr != NULL) 59 | { 60 | l++; 61 | ptr = ptr->next; 62 | } 63 | int cnt = l - n, pos = 0, s = 0; 64 | ptr = head; 65 | while (ptr != NULL) 66 | { 67 | if (pos >= cnt) 68 | { 69 | s = s + ptr->data; 70 | } 71 | pos++; 72 | ptr = ptr->next; 73 | } 74 | return s; 75 | } -------------------------------------------------------------------------------- /Strings/binsearchstr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define fastIO \ 4 | ios::sync_with_stdio(false); \ 5 | cin.tie(NULL); \ 6 | cout.tie(NULL); 7 | int main() 8 | { 9 | fastIO int t; 10 | cin >> t; 11 | while (t--) 12 | { 13 | int n; 14 | cin >> n; 15 | string row1, row2; 16 | cin >> row1 >> row2; 17 | int c = 0; 18 | bool flag = false; 19 | if (row2[0] == '1' && row1[1] == '1') 20 | { 21 | row1[1] = '2'; 22 | c++; 23 | } 24 | else if (row2[0] == '1' && row1[0] == '0') 25 | { 26 | row1[0] = '2'; 27 | c++; 28 | } 29 | for (int i = 1; i <= (n - 2); i++) 30 | { 31 | if (row2[i] == '1') 32 | { 33 | if (row1[i - 1] == '1') 34 | { 35 | row1[i - 1] = '2'; 36 | c++; 37 | } 38 | else if (row1[i + 1] == '1') 39 | { 40 | row1[i + 1] = '2'; 41 | c++; 42 | } 43 | else if (row1[i] == '0') 44 | { 45 | row1[i] = '2'; 46 | c++; 47 | } 48 | } 49 | } 50 | if (row2[n - 1] == '1' && row1[n - 2] == '1') 51 | { 52 | row1[n - 2] = '2'; 53 | c++; 54 | } 55 | else if (row2[n - 1] == '1' && row1[n - 1] == '0') 56 | { 57 | row1[n - 1] = '2'; 58 | c++; 59 | } 60 | cout << c << endl; 61 | } 62 | return 0; 63 | } -------------------------------------------------------------------------------- /CP-Programming/Badge.cpp: -------------------------------------------------------------------------------- 1 | //https://codeforces.com/problemset/problem/1020/B 2 | 3 | #include 4 | using namespace std; 5 | 6 | #define ll long long 7 | #define int long long 8 | #define pb push_back 9 | #define ppb pop_back 10 | #define mp make_pair 11 | #define lb lower_bound 12 | #define ub upper_bound 13 | #define all(a) (a).begin(),(a).end() 14 | #define FOR(i, a, n) for(int i = a; i < (n); i++) 15 | #define r0 return 0; 16 | #define endl '\n' 17 | #define INF (int)1e15 18 | #define SIZE 100010 19 | // Debugger 20 | string to_string(string s) { return '"' + s + '"';} 21 | string to_string(bool f) { if(f) return "True"; else return "False";} 22 | string to_string(const char* s) { return to_string((string) s);} 23 | 24 | struct Optimizer{ 25 | Optimizer(){ 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(NULL); 28 | } 29 | }; 30 | 31 | Optimizer opt; 32 | 33 | vector graph[SIZE]; 34 | bool visited[SIZE]; 35 | vector results; 36 | 37 | void dfs(int node){ 38 | visited[node] = true; 39 | 40 | for (int i = 0; i < graph[node].size(); ++i){ 41 | int next = graph[node][i]; 42 | if (!visited[next]) 43 | dfs(next); 44 | 45 | else if (visited[next]) 46 | results.push_back(next); 47 | } 48 | } 49 | 50 | void solve () { 51 | int n; cin >> n; 52 | 53 | FOR(i, 1, n+1){ 54 | int p; cin >> p; 55 | graph[i].push_back(p); 56 | } 57 | 58 | FOR (i, 1, n+1){ 59 | memset(visited, 0, n+2); 60 | visited[i] = true; 61 | dfs(graph[i][0]); 62 | } 63 | // cout << "Visited: "; 64 | // for (int i = 0; i < n+1; i++) cout << visited[i] << " "; 65 | // cout << "\n\nResults: "; 66 | for (auto result: results) cout << result << " "; 67 | 68 | } 69 | 70 | signed main(){ 71 | 72 | solve(); 73 | 74 | r0 75 | } -------------------------------------------------------------------------------- /Arrays/MergeSort.java: -------------------------------------------------------------------------------- 1 | package algorithm; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class MyMergeSort> { 7 | private T[] a; 8 | 9 | public MyMergeSort(T[] b) { 10 | this.a = b; 11 | } 12 | 13 | public MyMergeSort() { 14 | } 15 | 16 | public void merge_sort() { 17 | merge_sort(a, 0, a.length - 1); 18 | } 19 | public void merge_sort(T[] a) { 20 | merge_sort(a, 0, a.length); 21 | } 22 | 23 | public void merge_sort(T[] a, int left, int right) { 24 | if(left < right) { 25 | int mid = left + (right - left)/2; 26 | merge_sort(a, left, mid); 27 | merge_sort(a, mid + 1, right); 28 | merge(a, left, mid, right); 29 | } 30 | } 31 | 32 | private void merge(T[] a, int left, int mid, int right) { 33 | int n1 = mid - left + 1; 34 | int n2 = right - mid; 35 | List l = new ArrayList<>(); 36 | List r = new ArrayList<>(); 37 | 38 | for(int i=0;i 7 | using namespace std; 8 | void display(int arr[], int n) 9 | { 10 | for (int i = 0; i < n; i++) 11 | { 12 | cout << arr[i] << " "; 13 | } 14 | cout << "\n"; 15 | } 16 | //Implementing merge sort technique 17 | void merge(int arr[], int l, int m, int r) 18 | { 19 | int i, j, k; 20 | int n1 = m - l + 1; 21 | int n2 = r - m; 22 | int L[n1], R[n2]; 23 | for (int i = 0; i < n1; i++) 24 | { 25 | L[i] = arr[l + i]; 26 | } 27 | for (int j = 0; j < n2; j++) 28 | { 29 | R[j] = arr[m + 1 + j]; 30 | } 31 | i = 0; 32 | j = 0; 33 | k = l; 34 | while (i < n1 && j < n2) 35 | { 36 | if (L[i] < R[j]) 37 | { 38 | arr[k] = L[i]; 39 | i++; 40 | } 41 | else 42 | { 43 | arr[k] = R[j]; 44 | j++; 45 | } 46 | k++; 47 | } 48 | while (i < n1) 49 | { 50 | arr[k] = L[i]; 51 | i++; 52 | k++; 53 | } 54 | while (j < n2) 55 | { 56 | arr[k] = R[j]; 57 | j++; 58 | k++; 59 | } 60 | } 61 | void mergesort(int arr[], int l, int r) 62 | { 63 | if (l < r) 64 | { 65 | int mid = l + (r - l) / 2; 66 | mergesort(arr, l, mid); 67 | mergesort(arr, mid + 1, r); 68 | merge(arr, l, mid, r); 69 | } 70 | } 71 | int main() 72 | { 73 | int n; 74 | cin >> n; 75 | int arr[n]; 76 | for (auto &x : arr) 77 | cin >> x; 78 | display(arr, n); 79 | mergesort(arr, 0, n); 80 | display(arr, n); 81 | cout << "Enter the value to search" << endl; 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Back-tracking/RatInMaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSafe (int **arr, int x, int y, int n){ 5 | 6 | if (x < n && y < n && arr[x][y] == 1){ 7 | return true; 8 | } 9 | return false; 10 | } 11 | 12 | bool ratInMaze(int **arr, int x, int y, int n, int **resultArr){ 13 | 14 | if (x == n-1 && y == n-1){ 15 | resultArr[x][y] = 1; 16 | return true; 17 | } 18 | 19 | if (isSafe(arr, x, y, n)){ 20 | resultArr[x][y] = 1; 21 | if (ratInMaze (arr, x+1, y, n, resultArr)){ 22 | return true; 23 | } 24 | if (ratInMaze (arr, x, y+1, n, resultArr)){ 25 | return true; 26 | } 27 | 28 | resultArr[x][y] = 0; //Backtracking 29 | return false; 30 | } 31 | 32 | return false; 33 | 34 | } 35 | 36 | int main() { 37 | 38 | int n; cin >> n; 39 | 40 | int** arr = new int*[n]; 41 | for (int i = 0; i < n; i++){ 42 | arr[i] = new int[n]; 43 | } 44 | 45 | for (int i = 0; i < n; i++){ 46 | for (int j = 0; j < n; j++){ 47 | cin >> arr[i][j]; 48 | } 49 | } 50 | 51 | cout << "\n"; 52 | 53 | int **resultArr = new int*[n]; 54 | for (int i = 0; i < n; i++){ 55 | resultArr[i] = new int[n]; 56 | for (int j = 0; j < n; j++){ 57 | resultArr[i][j] = 0; 58 | } 59 | } 60 | 61 | 62 | if(ratInMaze(arr, 0, 0, n, resultArr)){ 63 | for (int i = 0; i < n; i++){ 64 | for (int j = 0; j < n; j++){ 65 | cout << resultArr[i][j] <<" "; 66 | } 67 | cout << "\n"; 68 | } 69 | } 70 | 71 | return 0; 72 | } 73 | 74 | /* 75 | 76 | 1 0 1 0 1 77 | 1 1 1 1 1 78 | 0 1 0 1 0 79 | 1 0 0 1 1 80 | 1 1 1 0 1 81 | 82 | */ -------------------------------------------------------------------------------- /Lists/RevKnodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node 4 | { 5 | public: 6 | int data; 7 | node *next; 8 | 9 | node(int val) 10 | { 11 | data = val; 12 | next = NULL; 13 | } 14 | }; 15 | void print(node *&head) 16 | { 17 | node *ptr = head; 18 | while (ptr != NULL) 19 | { 20 | cout << ptr->data << " "; 21 | ptr = ptr->next; 22 | } 23 | cout << endl; 24 | } 25 | node* revKnode(node* &head,int k){ 26 | node* preptr = NULL; 27 | node* currptr = head; 28 | node* nextptr; 29 | int count=0; 30 | while(countnext; 32 | currptr->next = preptr; 33 | preptr = currptr; 34 | currptr = nextptr; 35 | count++; 36 | } 37 | //Recursive Call for rest of LL 38 | if(nextptr!=NULL){ 39 | head->next = revKnode(nextptr,k); 40 | } 41 | return preptr; 42 | 43 | } 44 | 45 | void insert(node *&head, int val, int pos) 46 | { 47 | node *n = new node(val); 48 | if (pos == 1) 49 | { 50 | head = n; 51 | n->next = NULL; 52 | } 53 | else 54 | { 55 | node *temp = head; 56 | node *ptr; 57 | while (pos > 1) 58 | { 59 | ptr = temp; 60 | temp = temp->next; 61 | pos--; 62 | } 63 | n->next = ptr->next; 64 | ptr->next = n; 65 | } 66 | } 67 | int main() 68 | { 69 | node *head = NULL; 70 | insert(head, 10, 1); 71 | insert(head, 20, 2); 72 | insert(head, 40, 3); 73 | insert(head, 50, 4); 74 | insert(head, 60, 3); 75 | //insertSorted(head, 30); 76 | //insertSorted(head, 95); 77 | print(head); 78 | node *head2 = revKnode(head,2); 79 | print(head2); 80 | return 0; 81 | } -------------------------------------------------------------------------------- /Graph/Prim's Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int findMinVertex(int* weights, bool* visited, int n){ 5 | int minVertex = -1; 6 | for(int i = 0; i < n; i++){ 7 | if(!visited[i] && (minVertex == - 1 || weights[i] < weights[minVertex])){ 8 | minVertex = i; 9 | } 10 | } 11 | return minVertex; 12 | } 13 | void prims(int** edges, int n){ 14 | int* parent = new int[n]; 15 | int* weights = new int[n]; 16 | bool* visited = new bool[n]; 17 | for(int i = 0; i < n; i++){ 18 | visited[i] = false; 19 | weights[i] = INT_MAX; 20 | } 21 | parent[0] = -1; 22 | weights[0] = 0; 23 | for(int i = 0; i < n - 1; i++){ 24 | // Find Min Vertex 25 | int minVertex = findMinVertex(weights, visited, n); 26 | visited[minVertex] = true; 27 | // Explore un visted neighbours 28 | for(int j = 0; j < n; j++){ 29 | if(edges[minVertex][j] != 0 && !visited[j]){ 30 | if(edges[minVertex][j] < weights[j]){ 31 | weights[j] = edges[minVertex][j]; 32 | parent[j] = minVertex; 33 | } 34 | } 35 | } 36 | } 37 | for(int i = 1; i < n; i++){ 38 | if(parent[i] < i){ 39 | cout << parent[i]<<" " << i << " " << weights[i] << endl; 40 | }else{ 41 | cout << i << " " << parent[i] << " " << weights[i] << endl; 42 | } 43 | } 44 | } 45 | int main() { 46 | int n; 47 | int e; 48 | cin >> n >> e; 49 | int** edges = new int*[n]; 50 | for (int i = 0; i < n; i++) { 51 | edges[i] = new int[n]; 52 | for (int j = 0; j < n; j++) { 53 | edges[i][j] = 0; 54 | } 55 | } 56 | for (int i = 0; i < e; i++) { 57 | int f, s, weight; 58 | cin >> f >> s >> weight; 59 | edges[f][s] = weight; 60 | edges[s][f] = weight; 61 | } 62 | cout << endl; 63 | prims(edges, n); 64 | for (int i = 0; i < n; i++) { 65 | delete [] edges[i]; 66 | } 67 | delete [] edges; 68 | } 69 | -------------------------------------------------------------------------------- /Stacks/ApplicationOfStacks/In-Post.cpp: -------------------------------------------------------------------------------- 1 | //Program to convert an infix expression to postfix expression using stack 2 | #include 3 | #include 4 | using namespace std; 5 | int precision(char ch) 6 | { 7 | if (ch == '^') 8 | { 9 | return 3; 10 | } 11 | else if (ch == '*' || ch == '/') 12 | { 13 | return 2; 14 | } 15 | else if (ch == '+' || ch == '-') 16 | { 17 | return 1; 18 | } 19 | else 20 | { 21 | return -1; 22 | } 23 | } 24 | string InfixToPostfix(string req) 25 | { 26 | string res; 27 | stack stk; 28 | for (int i = 0; i < req.length(); i++) 29 | { 30 | if ((req[i] >= 'a' && req[i] <= 'z') || (req[i] >= 'A' && req[i] <= 'Z') || (req[i] >= '0' && req[i] <= '9')) 31 | { 32 | res += req[i]; 33 | } 34 | else if (req[i] == '(') 35 | { 36 | stk.push(req[i]); 37 | } 38 | else if (req[i] == ')') 39 | { 40 | while (!stk.empty() && stk.top() != '(') 41 | { 42 | res += stk.top(); 43 | stk.pop(); 44 | } 45 | if (!stk.empty()) 46 | { 47 | stk.pop(); 48 | } 49 | } 50 | else 51 | { 52 | while (!stk.empty() && precision(req[i]) <= precision(stk.top())) 53 | { 54 | res += stk.top(); 55 | stk.pop(); 56 | } 57 | stk.push(req[i]); 58 | } 59 | } 60 | while (!stk.empty()) 61 | { 62 | res += stk.top(); 63 | stk.pop(); 64 | } 65 | return res; 66 | } 67 | //Driver Code 68 | int main() 69 | { 70 | string var; 71 | cin >> var; 72 | cout << InfixToPostfix(var) << endl; 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Lists/ReverseList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node { 4 | public: 5 | int data; 6 | node* next; 7 | 8 | node(int val){ 9 | data = val; 10 | next = NULL; 11 | } 12 | }; 13 | void print(node* &head){ 14 | node* ptr=head; 15 | while(ptr!=NULL){ 16 | cout<data<<" "; 17 | ptr = ptr->next; 18 | }cout<next; 28 | cur->next = pre; 29 | pre = cur; 30 | cur = then; 31 | } 32 | return pre; 33 | } 34 | //Recursive Method 35 | node* Recursive_reverse(node* &head){ 36 | if(head==NULL || head->next==NULL){ 37 | return head; 38 | } 39 | node* newhead = Recursive_reverse(head->next); 40 | head->next->next = head; 41 | head->next = NULL; 42 | return newhead; 43 | } 44 | void insert(node *&head, int val, int pos) 45 | { 46 | node *n = new node(val); 47 | if (pos == 1) 48 | { 49 | head = n; 50 | n->next = NULL; 51 | } 52 | else 53 | { 54 | node *temp = head; 55 | node *ptr; 56 | while (pos > 1) 57 | { 58 | ptr = temp; 59 | temp = temp->next; 60 | pos--; 61 | } 62 | n->next = ptr->next; 63 | ptr->next = n; 64 | } 65 | } 66 | int main() 67 | { 68 | node *head = NULL; 69 | insert(head, 10, 1); 70 | insert(head, 20, 2); 71 | insert(head, 40, 3); 72 | insert(head, 50, 4); 73 | insert(head, 60, 3); 74 | //insertSorted(head, 30); 75 | //insertSorted(head, 95); 76 | print(head); 77 | node *head2 = Recursive_reverse(head); 78 | print(head2); 79 | return 0; 80 | } -------------------------------------------------------------------------------- /two_stack_in_a_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class twoStacks 6 | { 7 | int *arr; 8 | int size; 9 | int top1, top2; 10 | public: 11 | twoStacks(int n=100){size = n; arr = new int[n]; top1 = -1; top2 = size;} 12 | 13 | void push1(int x); 14 | void push2(int x); 15 | int pop1(); 16 | int pop2(); 17 | }; 18 | 19 | 20 | 21 | int main() 22 | { 23 | 24 | int T; 25 | cin>>T; 26 | while(T--) 27 | { 28 | twoStacks *sq = new twoStacks(); 29 | 30 | int Q; 31 | cin>>Q; 32 | while(Q--){ 33 | int stack_no; 34 | cin>>stack_no; 35 | int QueryType=0; 36 | cin>>QueryType; 37 | 38 | if(QueryType==1) 39 | { 40 | int a; 41 | cin>>a; 42 | if(stack_no ==1) 43 | sq->push1(a); 44 | else if(stack_no==2) 45 | sq->push2(a); 46 | }else if(QueryType==2){ 47 | if(stack_no==1) 48 | cout<pop1()<<" "; 49 | else if(stack_no==2) 50 | cout<pop2()<<" "; 51 | 52 | } 53 | } 54 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | const ll is_query = -(1LL<<62); 5 | struct Line { 6 | ll m, b; 7 | mutable function succ; 8 | bool operator<(const Line& rhs) const { 9 | if (rhs.b != is_query) return m < rhs.m; 10 | const Line* s = succ(); 11 | if (!s) return 0; 12 | ll x = rhs.m; 13 | return b - s->b < (s->m - m) * x; 14 | } 15 | }; 16 | struct HullDynamic : public multiset { // will maintain upper hull for maximum 17 | bool bad(iterator y) { 18 | auto z = next(y); 19 | if (y == begin()) { 20 | if (z == end()) return 0; 21 | return y->m == z->m && y->b <= z->b; 22 | } 23 | auto x = prev(y); 24 | if (z == end()) return y->m == x->m && y->b <= x->b; 25 | return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m); 26 | } 27 | void insert_line(ll m, ll b) { 28 | auto y = insert({ m, b }); 29 | y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; 30 | if (bad(y)) { erase(y); return; } 31 | while (next(y) != end() && bad(next(y))) erase(next(y)); 32 | while (y != begin() && bad(prev(y))) erase(prev(y)); 33 | } 34 | ll eval(ll x) { 35 | auto l = *lower_bound((Line) { x, is_query }); 36 | return l.m * x + l.b; 37 | } 38 | }; 39 | void solve() 40 | { 41 | ll n,c; 42 | cin>>n>>c; 43 | ll h[n]; 44 | ll dp[n]; 45 | for(int i=0;i>h[i]; 47 | HullDynamic e; 48 | e.insert_line(2ll*h[0],-(h[0]*h[0])); 49 | for(int i=1;i>t; 60 | while(t--) 61 | solve(); 62 | } 63 | 64 | -------------------------------------------------------------------------------- /CP-Programming/Pair-Of-Roses.md: -------------------------------------------------------------------------------- 1 | ## Problem Name: Pair of Roses 2 | ### Problem Difficulty: None 3 | #### Problem Constraints: 4 | 1≤ T ≤100
5 | 2 ≤ N ≤ 10000
6 | Price[i]<1000001 7 | 8 | 9 | #### Problem Description: 10 | Deepak has a limited amount of money that he can spend on his girlfriend. So he decides to buy two roses 11 | for her. Since roses are of varying sizes, their prices are different. Deepak wishes to completely spend that fixed amount of money on buying roses for her.
12 | As he wishes to spend all the money, he should choose a pair of roses whose prices when summed up are equal to the money that he has.
13 | Help Deepak choose such a pair of roses for his girlfriend.

14 | NOTE:
If there are multiple solutions print the solution that minimizes the difference between the prices i and j. After each test case, you must print a blank line. 15 | 16 | #### Input Format: 17 | Format: The first line indicates the number of test cases T.
18 | Then, in the next line, the number of available roses, N is given.
19 | The next line will have N integers, representing the price of each rose, a rose that costs less than 1000001.
20 | Then there is another line with an integer M, representing how much money Deepak has.
21 | There is a blank line after each test case. 22 | 23 | * Sample Input: 24 |
25 | 2
26 | 2
27 | 40 40
28 | 80
29 | 
30 | 5
31 | 10 2 6 8 4
32 | 10
33 | 
34 | 35 | #### Output Format: 36 | For each test case, you must print the message: ‘Deepak should buy roses whose prices are i and 37 | j.’, where i and j are the prices of the roses whose sum is equal do M and i ≤ j. You can consider that it 38 | is always possible to find a solution. If there are multiple solutions print the solution that minimizes 39 | the difference between the prices i and j. 40 | 41 | * Sample Output: 42 |
 
43 | Deepak should buy roses whose prices are 40 and 40.
44 | Deepak should buy roses whose prices are 4 and 6.
45 | 
-------------------------------------------------------------------------------- /CP-Programming/integerToRoman.cpp: -------------------------------------------------------------------------------- 1 | // Given an integer less than 10000, return the number in its Roman type. 2 | // Symbol Value 3 | // I 1 4 | // V 5 5 | // X 10 6 | // L 50 7 | // C 100 8 | // D 500 9 | // M 1000 10 | // R for others (unkonwn) 11 | 12 | #include 13 | using namespace std; 14 | 15 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); 16 | #define external_io() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 17 | 18 | 19 | void allot(string &l, string &m, string &h, int n){ 20 | if(n==1){ 21 | l="I"; 22 | m="V"; 23 | h="X"; 24 | } 25 | if(n==10){ 26 | l="X"; 27 | m="L"; 28 | h="C"; 29 | } 30 | if(n==100){ 31 | l="C"; 32 | m="D"; 33 | h="M"; 34 | } 35 | if(n==1000){ 36 | l="M"; 37 | m="R"; //don't know 38 | h="R"; //don't know 39 | } 40 | return; 41 | } 42 | 43 | string intToRoman(int num) { 44 | string sol=""; 45 | int c=num; 46 | int n=10000; 47 | string l, m, h; 48 | while(n>1){ 49 | n/=10; 50 | allot(l,m,h,n); 51 | c= num/n; 52 | if(c==0); 53 | else if(c<4){ 54 | int t=c; 55 | while(t--){ 56 | sol+=l; 57 | } 58 | } 59 | else if(c==4){ 60 | sol+=l; 61 | sol+=m; 62 | } 63 | else if(c==5){ 64 | sol+=m; 65 | } 66 | else if(c<9) { 67 | sol+=m; 68 | int t=c%5; 69 | while(t--){ 70 | sol+=l; 71 | } 72 | } 73 | else if (c==9) { 74 | sol+=l; 75 | sol+=h; 76 | } 77 | num=num%n; 78 | } 79 | return sol; 80 | } 81 | 82 | int main(){ 83 | fast_cin(); 84 | int n; 85 | cin>>n; 86 | string sol = intToRoman(n); 87 | cout << sol; 88 | return 0; 89 | } -------------------------------------------------------------------------------- /BinarySearch/Algorihtm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Binary Search Algorithm 3 | Space Complex: O(1) 4 | Time Complex: O(log n) 5 | 6 | Merge Sort Algorithm 7 | Space Complex: O(1) 8 | Time Complex: O(n) 9 | */ 10 | #include 11 | using namespace std; 12 | void display(int arr[],int n){ 13 | for(int i=0;i>n; 77 | int arr[n]; 78 | for(auto &x: arr) 79 | cin>>x; 80 | display(arr,n); 81 | mergesort(arr,0,n); 82 | display(arr,n); 83 | cout<<"Enter the value to search"<>val; 85 | cout< 2 | using namespace std; 3 | #define sync ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 4 | class Node{ 5 | public: 6 | int data; 7 | Node* next; 8 | }; 9 | void push(Node* head){ 10 | while(head->next!=NULL) 11 | head=head->next; 12 | Node* node = new Node(); 13 | int d;cin>>d; 14 | head->next=node; 15 | node->data=d; 16 | node->next=NULL; 17 | } 18 | void peek(Node* head){ 19 | if(head==NULL) 20 | cout<<"Empty"<next!=NULL) 23 | head=head->next; 24 | cout<data<next==NULL) 31 | { 32 | delete head; head=NULL; 33 | } 34 | else{ 35 | while(head->next->next!=NULL) 36 | head=head->next; 37 | delete head->next; 38 | head->next=NULL; 39 | } 40 | 41 | } 42 | void print(Node* head){ 43 | while(head->next!=NULL) 44 | { 45 | cout<data<<"-->"; 46 | head=head->next; 47 | } 48 | cout<data<>c; 54 | while(c!=0){ 55 | if(c==1) 56 | { 57 | if(head==NULL){ 58 | head=new Node(); 59 | int d;cin>>d;head->data=d;head->next=NULL; 60 | } 61 | else 62 | push(head); 63 | } 64 | if(c==2){ 65 | peek(head); 66 | } 67 | if(c==3){ 68 | if(head==NULL) 69 | cout<<"Empty"<next!=NULL){ 72 | pop(head); 73 | } 74 | 75 | else{ 76 | pop(head); 77 | head=NULL; 78 | } 79 | } 80 | 81 | } 82 | if(c==4){ 83 | if(head!=NULL) 84 | print(head); 85 | else 86 | cout<<"Empty"<>c; 89 | } 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Trees/BuildTree.cpp: -------------------------------------------------------------------------------- 1 | // Given the Preorder and Inorder traversal sequence of a Binary Tree, build the tree. 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Definition for a binary tree node. 7 | struct TreeNode { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | }; 15 | 16 | class Solution { 17 | public: 18 | TreeNode * build(vector &preorder, int ip, int jp, vector &inorder, int ii, int ji){ 19 | if(jp& preorder, vector& inorder) { 37 | return build(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1); 38 | } 39 | }; 40 | 41 | int main(){ 42 | int n; 43 | cout<<"Enter the number of elements in the tree: "; 44 | cin>>n; 45 | vector preorder(n); 46 | vector inorder(n); 47 | cout << "\nPreorder traversal:"; 48 | for(int i=0; i>preorder[i]; 50 | } 51 | cout << "\nInorder traversal:\n"; 52 | for(int i=0; i>inorder[i]; 54 | } 55 | Solution sol; 56 | TreeNode* root = sol.buildTree(preorder, inorder); 57 | 58 | // level order traversal of obtained tree 59 | queue q; 60 | q.push(root); 61 | while(!q.empty()){ 62 | TreeNode* temp = q.front(); 63 | q.pop(); 64 | cout<val<<" "; 65 | if(temp->left) q.push(temp->left); 66 | if(temp->right) q.push(temp->right); 67 | } 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Dynamic programming/Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | problem statement 2 | https://leetcode.com/problems/longest-palindromic-substring/ 3 | 4 | Video solution 5 | https://www.youtube.com/watch?v=WpYHNHofwjc 6 | 7 | class Solution { 8 | public: 9 | string longestPalindrome(string s) { 10 | 11 | // we are checking if the string is empty or contains only 1 character then we will return empty string 12 | if(s.empty() || s.length()<1) 13 | return ""; 14 | 15 | int maxLenBegin=0; 16 | int maxLen=1; 17 | int mid=0; 18 | while(mid < s.length()) 19 | { 20 | 21 | int midBegin = mid; 22 | int midEnd = mid; 23 | 24 | //EVEN - for even length string we will check if midEnd+1 is equal to midEnd then we will increament the midEnd 25 | while(midEnd < s.length() && s[midEnd] == s[midEnd+1]) 26 | midEnd++; 27 | 28 | mid = midEnd + 1; 29 | 30 | //ODD - now we will defin two variable left and right and we will assign midBegin to left and midEnd to right 31 | // if the length of string is odd then both left and right will be having same value because midBegin will be == midEnd. 32 | int left = midBegin; 33 | int right = midEnd; 34 | 35 | // now the time to expand to left and right side from the middle of the string with the basic conditions as below 36 | while(left - 1 >= 0 && right + 1 < s.length() && s[left - 1] == s[right + 1]) 37 | { 38 | left--; 39 | right++; 40 | } 41 | // when the above condition stop it will be having some pallindromic string indexes in left and right 42 | // to get the len we will subtract the start aka " left" and end aka "right" here : 43 | int currLen = right - left + 1; 44 | 45 | if(currLen > maxLen) 46 | { 47 | maxLenBegin = left; 48 | maxLen = currLen; 49 | } 50 | } 51 | //in c++ substr(startingIndex, Ending Index) is to get the substring fo a string with indexs 52 | return s.substr(maxLenBegin, maxLen); 53 | } 54 | }; -------------------------------------------------------------------------------- /Dynamic programming/Grid1.cpp: -------------------------------------------------------------------------------- 1 | // Rishabh Agarwal 2 | #include 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e9+7; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vector>v; 35 | vector::iterator it; 36 | mapmp; 37 | 38 | void primeFactors(ll n) 39 | { 40 | while (n % 2 == 0) 41 | { 42 | mp[2]++; 43 | n = n/2; 44 | } 45 | for (ll i = 3; i <= sqrt(n); i = i + 2) 46 | { 47 | while (n % i == 0) 48 | { 49 | mp[i]++; 50 | n = n/i; 51 | } 52 | } 53 | if (n > 2){ 54 | mp[n]++; 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | ios_base::sync_with_stdio(false); 61 | cin.tie(NULL); 62 | cout.tie(NULL); 63 | ll t; 64 | //cin>>t; 65 | t=1; 66 | //ll c=1; 67 | while(t--){ 68 | ll h,w; 69 | cin>>h>>w; 70 | char a[h][w]; 71 | for(ll x=0; x>a[x][y]; 74 | } 75 | } 76 | ll dp[h+1][w+1]; 77 | for(ll x=0; x<=h; x++){ 78 | for(ll y=0; y<=w; y++){ 79 | if(x-1>=0&&y-1>=0&&a[x-1][y-1]=='#'){ 80 | dp[x][y]=0; 81 | } 82 | else if(x==0||y==0){ 83 | dp[x][y]=0; 84 | } 85 | else{ 86 | dp[x][y]=dp[x-1][y]+dp[x][y-1]; 87 | } 88 | dp[1][1]=1; 89 | dp[x][y]%=mod; 90 | } 91 | } 92 | cout< adj[]; //Adjacency Lists 10 | 11 | // Constructor 12 | Graph(int v) 13 | { 14 | V = v; 15 | adj = new LinkedList[v]; 16 | for (int i=0; i queue = new LinkedList(); 35 | 36 | // Mark the current node as visited and enqueue it 37 | visited[s]=true; 38 | queue.add(s); 39 | 40 | while (queue.size() != 0) 41 | { 42 | // Dequeue a vertex from queue and print it 43 | s = queue.poll(); 44 | System.out.print(s+" "); 45 | 46 | // Get all adjacent vertices of the dequeued vertex s 47 | // If a adjacent has not been visited, then mark it 48 | // visited and enqueue it 49 | Iterator i = adj[s].listIterator(); 50 | while (i.hasNext()) 51 | { 52 | int n = i.next(); 53 | if (!visited[n]) 54 | { 55 | visited[n] = true; 56 | queue.add(n); 57 | } 58 | } 59 | } 60 | } 61 | 62 | // Driver method to 63 | public static void main(String args[]) 64 | { 65 | Graph g = new Graph(4); 66 | 67 | g.addEdge(0, 1); 68 | g.addEdge(0, 2); 69 | g.addEdge(1, 2); 70 | g.addEdge(2, 0); 71 | g.addEdge(2, 3); 72 | g.addEdge(3, 3); 73 | 74 | System.out.println("Following is Breadth First Traversal "+ 75 | "(starting from vertex 2)"); 76 | 77 | g.BFS(2); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Dynamic programming/Longest_Path.cpp: -------------------------------------------------------------------------------- 1 | // Rishabh Agarwal 2 | #include 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e18; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vector> adj(100000); 35 | vector visited; 36 | vector ans; 37 | 38 | void dfs(ll v) { 39 | visited[v] = true; 40 | for (ll u : adj[v]) { 41 | if (!visited[u]) 42 | dfs(u); 43 | } 44 | ans.push_back(v); 45 | } 46 | 47 | void topological_sort(ll n) { 48 | visited.assign(n, false); 49 | ans.clear(); 50 | for (ll i = 0; i < n; ++i) { 51 | if (!visited[i]) 52 | dfs(i); 53 | } 54 | reverse(ans.begin(), ans.end()); 55 | } 56 | 57 | int main() 58 | { 59 | ios_base::sync_with_stdio(false); 60 | cin.tie(NULL); 61 | cout.tie(NULL); 62 | ll t; 63 | //cin>>t; 64 | t=1; 65 | //ll c=1; 66 | while(t--){ 67 | ll n,m; 68 | cin>>n>>m; 69 | vector>adj2(n+1); 70 | for(ll x=0; x>y>>z; 73 | adj[y-1].push_back(z-1); 74 | adj2[z].push_back(y); 75 | } 76 | topological_sort(n); 77 | for(ll x=0; x 3 | using namespace std; 4 | #define IOSfaststream \ 5 | ios::sync_with_stdio(false); \ 6 | cin.tie(NULL); \ 7 | cout.tie(NULL); 8 | typedef long long int lli; 9 | #define ln(x) x.length() 10 | #define YY cout << "YES" << endl 11 | #define NN cout << "NO" << endl 12 | #define all(x) x.begin(), x.end() 13 | #define auto_input(v, x) \ 14 | for (auto &x : v) \ 15 | { \ 16 | cin >> x; \ 17 | } 18 | #define displayst(v, x) \ 19 | for (auto x : v) \ 20 | { \ 21 | cout << x << " "; \ 22 | } 23 | #define displayMaps(v, x) \ 24 | for (auto x : v) \ 25 | { \ 26 | cout << x.first << " " << x.second << "\n"; \ 27 | } 28 | #define vec(v, n) vector v(n) 29 | #define mapit map 30 | #define f(i, a, b) for (int i = a; i < b; i++) 31 | #define sf scanf 32 | #define pf printf 33 | const lli MOD = 1e9 + 7; 34 | int main() 35 | { 36 | IOSfaststream 37 | #ifndef ONLINE_JUDGE 38 | freopen("input.txt", "r", stdin); 39 | freopen("output.txt", "w", stdout); 40 | #endif 41 | int t; 42 | cin >> t; 43 | while (t--) 44 | { 45 | stack stk; 46 | int n; 47 | cin >> n; 48 | int c = n; 49 | for (int i = 1; i <= n; i++) 50 | { 51 | { 52 | for (int j = 1; j <= i; j++) 53 | { 54 | stk.push(')'); 55 | } 56 | for (int j = 1; j <= i; j++) 57 | { 58 | stk.push('('); 59 | } 60 | for (int j = 1; j <= (n - i); j++) 61 | { 62 | stk.push(')'); 63 | } 64 | for (int j = 1; j <= (n - i); j++) 65 | { 66 | stk.push('('); 67 | } 68 | } 69 | while (!stk.empty()) 70 | { 71 | cout << stk.top(); 72 | stk.pop(); 73 | } 74 | cout << "\n"; 75 | } 76 | } 77 | return 0; 78 | } -------------------------------------------------------------------------------- /BinarySearch/Recrsive_Algo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Binary Search Algorithm 3 | Space Complex: O(1) 4 | Time Complex: O(log n) 5 | 6 | Merge Sort Algorithm 7 | Space Complex: O(1) 8 | Time Complex: O(n) 9 | */ 10 | #include 11 | using namespace std; 12 | void display(int arr[], int n) 13 | { 14 | for (int i = 0; i < n; i++) 15 | { 16 | cout << arr[i] << " "; 17 | } 18 | cout << "\n"; 19 | } 20 | //Implementing Binary search technique 21 | int binarySearch(int arr[], int l, int r, int val){ 22 | if (r >= l) 23 | { 24 | int mid = l + (r - l) / 2; 25 | if (arr[mid] == val) 26 | return mid; 27 | if (arr[mid] > val) 28 | return binarySearch(arr, l, mid - 1, val); 29 | return binarySearch(arr, mid + 1, r, val); 30 | } 31 | return -1; 32 | } 33 | 34 | //Implementing merge sort technique 35 | void merge(int arr[], int l, int m, int r) 36 | { 37 | int i, j, k; 38 | int n1 = m - l + 1; 39 | int n2 = r - m; 40 | int L[n1], R[n2]; 41 | for (int i = 0; i < n1; i++) 42 | { 43 | L[i] = arr[l + i]; 44 | } 45 | for (int j = 0; j < n2; j++) 46 | { 47 | R[j] = arr[m + 1 + j]; 48 | } 49 | i = 0; 50 | j = 0; 51 | k = l; 52 | while (i < n1 && j < n2) 53 | { 54 | if (L[i] < R[j]) 55 | { 56 | arr[k] = L[i]; 57 | i++; 58 | } 59 | else 60 | { 61 | arr[k] = R[j]; 62 | j++; 63 | } 64 | k++; 65 | } 66 | while (i < n1) 67 | { 68 | arr[k] = L[i]; 69 | i++; 70 | k++; 71 | } 72 | while (j < n2) 73 | { 74 | arr[k] = R[j]; 75 | j++; 76 | k++; 77 | } 78 | } 79 | void mergesort(int arr[], int l, int r) 80 | { 81 | if (l < r) 82 | { 83 | int mid = l + (r - l) / 2; 84 | mergesort(arr, l, mid); 85 | mergesort(arr, mid + 1, r); 86 | merge(arr, l, mid, r); 87 | } 88 | } 89 | int main() 90 | { 91 | int n; 92 | cin >> n; 93 | int arr[n]; 94 | for (auto &x : arr) 95 | cin >> x; 96 | display(arr, n); 97 | mergesort(arr, 0, n); 98 | display(arr, n); 99 | cout << "Enter the value to search" << endl; 100 | int val; 101 | cin >> val; 102 | cout << binarySearch(arr, 0, n, val) + 1 << endl; 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /Arrays/topKfrequent.cpp: -------------------------------------------------------------------------------- 1 | // Given an array of integers and a number K, return the top K most frequent elements in the array. 2 | // If the frequency for two or more elements is same, return them in sorted order. 3 | // input : n (number of integers), k (number of elements to return), followed by n integers 4 | // output: k most frequent integeres 5 | // Example 6 | // input: 7 | // 7 2 8 | // 1 2 3 1 3 3 2 9 | // output: 10 | // 3 2 11 | 12 | #include 13 | using namespace std; 14 | 15 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); 16 | #define external_io() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 17 | 18 | vector topKFrequent (vector & nums, int k){ 19 | map freq; 20 | vector sol(k); 21 | multiset> multi; 22 | for(int i=0; i temp; 28 | temp.first= i->second; 29 | temp.second = i->first; 30 | multi.insert(temp); 31 | } 32 | 33 | int j = 0; 34 | for (auto i = multi.rbegin(); i != multi.rend() && jsecond); 36 | j++; 37 | } 38 | return sol; 39 | } 40 | 41 | vector topKFrequent_fast (vector & nums, int k){ 42 | map freq; 43 | vector sol(k); 44 | multiset > multi; 45 | for(int i=0; i temp; 52 | temp.first= i->second; 53 | temp.second = i->first; 54 | multi.insert(temp); 55 | } 56 | 57 | for( ; i!=freq.end(); i++){ 58 | pair temp; 59 | temp.first= i->second; 60 | temp.second = i->first; 61 | multi.insert(temp); 62 | auto it=multi.begin(); 63 | multi.erase(it); 64 | } 65 | j = 0; 66 | for (auto it = multi.rbegin(); it != multi.rend(); it++){ 67 | sol[j]=(it->second); 68 | j++; 69 | } 70 | return sol; 71 | } 72 | 73 | 74 | int main(){ 75 | fast_cin(); 76 | int n,k; 77 | cin>>n>>k; 78 | vector arr(n); 79 | for(int i=0; i>arr[i]; 81 | } 82 | vector sol = topKFrequent_fast(arr, k); 83 | for(int i=0; i 3 | // #include 4 | using namespace std; 5 | #define fio ios_base::sync_with_stdio(false) 6 | 7 | #define ll long long int 8 | 9 | #define s(x) scanf("%lld",&x) 10 | #define s2(x,y) s(x)+s(y) 11 | #define s3(x,y,z) s(x)+s(y)+s(z) 12 | 13 | #define p(x) printf("%lld\n",x) 14 | #define p2(x,y) p(x)+p(y) 15 | #define p3(x,y,z) p(x)+p(y)+p(z) 16 | #define F(i,a,b) for(ll i = (ll)(a); i <= (ll)(b); i++) 17 | #define RF(i,a,b) for(ll i = (ll)(a); i >= (ll)(b); i--) 18 | 19 | #define ff first 20 | #define ss second 21 | #define mp(x,y) make_pair(x,y) 22 | #define pll pair 23 | #define pb push_back 24 | 25 | ll inf = 1e18; 26 | ll mod = 1e9 + 7 ; 27 | ll gcd(ll a , ll b){return b==0?a:gcd(b,a%b);} 28 | 29 | int main() 30 | { 31 | ll n; 32 | ll ct=0; 33 | ll t; 34 | s(t); 35 | while(t--){ 36 | cin>>n; 37 | ct++; 38 | // Accept the input in a vector 39 | vectorar(n); 40 | for(ll i=0;i>ar[i]; 41 | 42 | // Sort the vector in ascending order 43 | sort(ar.begin(), ar.end()); 44 | 45 | // Accept the money Deepak has 46 | ll tar; 47 | s(tar); 48 | 49 | // Since in cases of multiple solutions, we have to output that one which 50 | // has the minimum difference betweeen the two prices of the roses, so we 51 | // are maintaining the difference for every solution and will output the one 52 | // with minimum difference. Initially, the minimum difference is assumed to be 53 | // infinity. 54 | 55 | ll curDif=INT_MAX; 56 | ll p1,p2; 57 | 58 | for(ll i=0;itar)break; 60 | 61 | // toFind is the value of the price of the second rose in the pair 62 | ll tofind=tar-ar[i]; 63 | 64 | // ind is the index of the toFind variable in the sorted array if it exists 65 | ll ind = lower_bound(ar.begin()+i+1,ar.end(),tofind)-ar.begin(); 66 | 67 | // if ind is greater than n, it indicates that the second rose of the pair 68 | // does not exists, so continue finding another pair. 69 | if(ind>=n)continue; 70 | 71 | // if we have found the second rose of the pair, we will check for the minimum difference condition 72 | // and then assign the value of the two prices in p1 and p2. 73 | if(ar[ind]==tofind){ 74 | if(ar[ind]-ar[i] 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e9+7; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vectorv; 35 | vector::iterator it; 36 | mapmp; 37 | 38 | void primeFactors(ll n) 39 | { 40 | while (n % 2 == 0) 41 | { 42 | mp[2]++; 43 | n = n/2; 44 | } 45 | for (ll i = 3; i <= sqrt(n); i = i + 2) 46 | { 47 | while (n % i == 0) 48 | { 49 | mp[i]++; 50 | n = n/i; 51 | } 52 | } 53 | if (n > 2){ 54 | mp[n]++; 55 | } 56 | } 57 | 58 | 59 | 60 | int main() 61 | { 62 | ios_base::sync_with_stdio(false); 63 | cin.tie(NULL); 64 | cout.tie(NULL); 65 | ll t; 66 | //cin>>t; 67 | t=1; 68 | //ll c=1; 69 | while(t--){ 70 | ll n,k; 71 | cin>>n>>k; 72 | ll a[n]; 73 | for(ll x=0; x>a[x]; 75 | } 76 | ll dp[n+1][k+1]; 77 | for(ll x=0; x<=n; x++){ 78 | for(ll y=0; y<=k; y++){ 79 | if(x==0||y==0){ 80 | dp[x][y]=1; 81 | } 82 | else if(y-a[x-1]-1<0){ 83 | dp[x][y]=dp[x][y-1]+dp[x-1][y]; 84 | } 85 | else{ 86 | dp[x][y]=dp[x][y-1]+dp[x-1][y]-dp[x-1][y-a[x-1]-1]; 87 | if(dp[x-1][y]-dp[x-1][y-a[x-1]-1]<0){ 88 | dp[x][y]+=mod; 89 | } 90 | } 91 | dp[x][y]%=mod; 92 | } 93 | } 94 | ll ans=dp[n][k]; 95 | if(k-1>=0){ 96 | ans-=dp[n][k-1]; 97 | } 98 | if(ans<0){ 99 | ans+=mod; 100 | } 101 | cout< 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e9+7; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vector>v; 35 | vector::iterator it; 36 | mapmp; 37 | 38 | void primeFactors(ll n) 39 | { 40 | while (n % 2 == 0) 41 | { 42 | mp[2]++; 43 | n = n/2; 44 | } 45 | for (ll i = 3; i <= sqrt(n); i = i + 2) 46 | { 47 | while (n % i == 0) 48 | { 49 | mp[i]++; 50 | n = n/i; 51 | } 52 | } 53 | if (n > 2){ 54 | mp[n]++; 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | ios_base::sync_with_stdio(false); 61 | cin.tie(NULL); 62 | cout.tie(NULL); 63 | ll t; 64 | //cin>>t; 65 | t=1; 66 | //ll c=1; 67 | while(t--){ 68 | ll n; 69 | cin>>n; 70 | long double p[n]; 71 | for(ll x=0; x>p[x]; 73 | } 74 | long double dp[n+1][n+1]; 75 | //memset(dp,0,sizeof(dp)); 76 | for(ll x=0; x<=n; x++){ 77 | for(ll y=0; y<=n; y++){ 78 | dp[x][y]=0.00000000; 79 | if(x==0){ 80 | dp[x][y]=0; 81 | } 82 | else if(y==0){ 83 | dp[x][y]=p[x-1]*dp[x-1][y]; 84 | } 85 | else if(x>=y){ 86 | dp[x][y]=dp[x-1][y]*p[x-1]+dp[x-1][y-1]*(1.0000-p[x-1]); 87 | } 88 | else{ 89 | continue; 90 | } 91 | dp[1][0]=p[0]; 92 | dp[1][1]=1.000000000-p[0]; 93 | //cout< 2 | using namespace std; 3 | 4 | // Defining the maximum size of the stack to be 10 5 | #define MAX 10 6 | 7 | float evaluatePostfix(); 8 | 9 | // Defining a class to store the member variables 10 | // and member functions required 11 | class Stack { 12 | float stk[MAX]; 13 | 14 | public: 15 | int top; 16 | Stack(): top(-1) {} 17 | void push(float ); 18 | float pop(); 19 | void display(); 20 | }; 21 | 22 | 23 | /* 24 | ALGORITHM FOR PUSHING AN ELEMENT IN THE STACK: 25 | 1) IF TOP = MAX-1 26 | WRITE Overflow 27 | EXIT 28 | 2) TOP = TOP + 1 29 | 3) STACK[TOP] = VALUE 30 | 4) EXIT 31 | */ 32 | 33 | void Stack :: push(float value) { 34 | // When the size of stack has reached maximum, 35 | // we cannot insert any element 36 | if(top == MAX-1) { 37 | cout<<"The Stack is full !\n"; 38 | } 39 | else { 40 | // Incrementing top 41 | top++; 42 | 43 | // Store the value in the stack at top position 44 | stk[top] = value; 45 | } 46 | } 47 | 48 | 49 | /* 50 | ALGORITHM FOR POPPING AN ELEMENT FROM THE STACK: 51 | 1) IF TOP = -1 52 | WRITE Underflow 53 | RETURN -1 54 | EXIT 55 | 2) VALUE = STK[TOP] 56 | 3) TOP = TOP - 1 57 | 4) RETURN VALUE 58 | 5) EXIT 59 | */ 60 | 61 | float Stack :: pop() { 62 | // When the size of stack is -1, the stack is empty 63 | if(top == -1) { 64 | return -1; 65 | } 66 | else { 67 | // Store the top value 68 | float value = stk[top]; 69 | 70 | // Decrement top 71 | top --; 72 | 73 | return value; 74 | } 75 | } 76 | 77 | 78 | float evaluatePostfix() { 79 | Stack s; 80 | string exp; 81 | int i=0; 82 | float op1, op2, value; 83 | 84 | cout<<"Enter the experssion: "; 85 | getline(cin, exp); 86 | 87 | while(exp[i] != '\0') { 88 | if(isdigit(exp[i])) { 89 | s.push((float)exp[i]-'0'); 90 | i++; 91 | } 92 | else { 93 | op2 = s.pop(); 94 | op1 = s.pop(); 95 | 96 | switch(exp[i]) { 97 | case '+': value = op1 + op2; 98 | break; 99 | 100 | case '-': value = op1 - op2; 101 | break; 102 | 103 | case '*': value = op1 * op2; 104 | break; 105 | 106 | case '/': value = op1 / op2; 107 | break; 108 | 109 | case '%': value = (int)op1 % (int)op2; 110 | break; 111 | } 112 | s.push(value); 113 | } 114 | i++; 115 | } 116 | return s.pop(); 117 | } 118 | 119 | int main() { 120 | int option; 121 | float result; 122 | 123 | result = evaluatePostfix(); 124 | cout<<"The result is "< 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e9+7; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vector>v; 35 | vector::iterator it; 36 | mapmp; 37 | 38 | void primeFactors(ll n) 39 | { 40 | while (n % 2 == 0) 41 | { 42 | mp[2]++; 43 | n = n/2; 44 | } 45 | for (ll i = 3; i <= sqrt(n); i = i + 2) 46 | { 47 | while (n % i == 0) 48 | { 49 | mp[i]++; 50 | n = n/i; 51 | } 52 | } 53 | if (n > 2){ 54 | mp[n]++; 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | ios_base::sync_with_stdio(false); 61 | cin.tie(NULL); 62 | cout.tie(NULL); 63 | ll t; 64 | //cin>>t; 65 | t=1; 66 | //ll c=1; 67 | while(t--){ 68 | ll n,k; 69 | cin>>n>>k; 70 | ll a[n]; 71 | for(ll x=0; x>a[x]; 73 | } 74 | ll dp[n+1][k+1]; 75 | memset(dp,-1,sizeof(dp)); 76 | for(ll y=1; y<=k; y++){ 77 | ll flag=-1; 78 | for(ll x=0; x 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e18; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vectorv; 35 | vector::iterator it; 36 | mapmp; 37 | 38 | void primeFactors(ll n) 39 | { 40 | // Print the number of 2s that divide n 41 | while (n % 2 == 0) 42 | { 43 | //cout << 2 << " "; 44 | mp[2]++; 45 | n = n/2; 46 | } 47 | 48 | // n must be odd at this point. So we can skip 49 | // one element (Note i = i +2) 50 | for (ll i = 3; i <= sqrt(n); i = i + 2) 51 | { 52 | // While i divides n, print i and divide n 53 | while (n % i == 0) 54 | { 55 | //cout << i << " "; 56 | mp[i]++; 57 | n = n/i; 58 | } 59 | } 60 | 61 | // This condition is to handle the case when n 62 | // is a prime number greater than 2 63 | if (n > 2){ 64 | mp[n]++; 65 | //cout << n << " "; 66 | } 67 | } 68 | 69 | ll max(ll a, ll b) { return (a > b)? a : b; } 70 | 71 | ll knapSack(ll sum, ll wt[], ll val[], ll n,ll W) 72 | { 73 | ll i, w; 74 | ll K[n+1][sum+1]; 75 | //memset(K,INF,sizeof(K)); 76 | for (i = 0; i <= n; i++) 77 | { 78 | for (w = 0; w <= sum; w++) 79 | { 80 | K[i][w]=INF; 81 | if (w==0) 82 | K[i][w] = 0; 83 | else if(i==0) 84 | continue; 85 | else if (val[i-1] <= w) 86 | K[i][w] = min(wt[i-1] + K[i-1][w-val[i-1]], K[i-1][w]); 87 | else 88 | K[i][w] = K[i-1][w]; 89 | 90 | //cout<=0; x--){ 95 | if(K[n][x]<=W) 96 | return x; 97 | } 98 | } 99 | 100 | int main() 101 | { 102 | ios_base::sync_with_stdio(false); 103 | cin.tie(NULL); 104 | cout.tie(NULL); 105 | ll t; 106 | //cin>>t; 107 | t=1; 108 | //ll c=1; 109 | while(t--){ 110 | ll n,w; 111 | cin>>n>>w; 112 | ll we[n],va[n]; 113 | ll sum=0; 114 | for(ll x=0; x>we[x]>>va[x]; 116 | sum=sum+va[x]; 117 | } 118 | cout< 3 | #define F first 4 | #define S second 5 | #define MAX 10000003 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | const ll mod = 1e18; 10 | const ll INF= 1e18; 11 | long double PI=3.1415926; 12 | 13 | ll power(ll a,ll b){ 14 | if(b==0){ 15 | return 1; 16 | } 17 | ll temp=power(a,b/2)%mod; 18 | if(b%2==0){ 19 | return (temp*temp)%mod; 20 | } 21 | else{ 22 | return ((a*temp)%mod*temp)%mod; 23 | } 24 | } 25 | 26 | ll gcd(ll a, ll b) 27 | { 28 | if (b == 0) 29 | return a; 30 | return gcd(b, a % b); 31 | 32 | } 33 | 34 | vectorv; 35 | vector::iterator it; 36 | mapmp; 37 | 38 | void primeFactors(ll n) 39 | { 40 | // Print the number of 2s that divide n 41 | while (n % 2 == 0) 42 | { 43 | //cout << 2 << " "; 44 | mp[2]++; 45 | n = n/2; 46 | } 47 | 48 | // n must be odd at this point. So we can skip 49 | // one element (Note i = i +2) 50 | for (ll i = 3; i <= sqrt(n); i = i + 2) 51 | { 52 | // While i divides n, print i and divide n 53 | while (n % i == 0) 54 | { 55 | //cout << i << " "; 56 | mp[i]++; 57 | n = n/i; 58 | } 59 | } 60 | 61 | // This condition is to handle the case when n 62 | // is a prime number greater than 2 63 | if (n > 2){ 64 | mp[n]++; 65 | //cout << n << " "; 66 | } 67 | } 68 | 69 | ll dp[3001][3001]; 70 | 71 | void solve(string a, string b){ 72 | ll an=a.length(); 73 | ll bn=b.length(); 74 | for(ll x=0; x<=an; x++){ 75 | for(ll y=0; y<=bn; y++){ 76 | dp[x][y]=0; 77 | } 78 | } 79 | for(ll x=1; x<=an; x++){ 80 | for(ll y=1; y<=bn; y++){ 81 | dp[x][y]=max(dp[x-1][y],dp[x][y-1]); 82 | if(a[x-1]==b[y-1]){ 83 | if(dp[x][y]<=dp[x-1][y-1]+1){ 84 | dp[x][y]=dp[x-1][y-1]+1; 85 | } 86 | } 87 | } 88 | } 89 | } 90 | 91 | int main() 92 | { 93 | ios_base::sync_with_stdio(false); 94 | cin.tie(NULL); 95 | cout.tie(NULL); 96 | ll t; 97 | //cin>>t; 98 | t=1; 99 | //ll c=1; 100 | while(t--){ 101 | string s,t; 102 | cin>>s>>t; 103 | solve(s,t); 104 | string out=""; 105 | 106 | ll x=s.length(); 107 | ll y=t.length(); 108 | while(x>0&&y>0){ 109 | if(s[x-1]==t[y-1]){ 110 | out+=s[x-1]; 111 | x--; 112 | y--; 113 | } 114 | else{ 115 | if(dp[x-1][y]>dp[x][y-1]){ 116 | x--; 117 | } 118 | else{ 119 | y--; 120 | } 121 | } 122 | } 123 | 124 | reverse(out.begin(),out.end()); 125 | cout< path = new Vector(); 24 | 25 | Queue q = new LinkedList(); // queue to maintain states 26 | q.add(new Pair( 0, 0 )); // Initialing with initial state 27 | 28 | while (!q.isEmpty()) { 29 | 30 | Pair u = q.peek(); // current state 31 | 32 | q.poll(); // pop off used state 33 | 34 | if ((u.first > a || u.second > b || 35 | u.first < 0 || u.second < 0)) 36 | continue; 37 | 38 | if (m[u.first][u.second] > -1) 39 | continue; 40 | 41 | path.add(new Pair( u.first, u.second )); 42 | 43 | m[u.first][u.second] = 1; 44 | 45 | if (u.first == target || u.second == target) { 46 | isSolvable = true; 47 | if (u.first == target) { 48 | if (u.second != 0) 49 | path.add(new Pair(u.first, 0 )); 50 | } 51 | else { 52 | if (u.first != 0) 53 | path.add(new Pair( 0, u.second )); 54 | } 55 | int sz = path.size(); 56 | for (int i = 0; i < sz; i++) 57 | System.out.println("(" + path.get(i).first 58 | + ", " + path.get(i).second + ")"); 59 | break; 60 | } 61 | 62 | q.add(new Pair( u.first, b )); // fill Jug2 63 | q.add(new Pair( a, u.second )); // fill Jug1 64 | 65 | for (int ap = 0; ap <= Math.max(a, b); ap++) { 66 | 67 | int c = u.first + ap; 68 | int d = u.second - ap; 69 | 70 | if (c == a || (d == 0 && d >= 0)) 71 | q.add(new Pair( c, d )); 72 | c = u.first - ap; 73 | d = u.second + ap; 74 | 75 | if ((c == 0 && c >= 0) || d == b) 76 | q.add(new Pair( c, d )); 77 | } 78 | 79 | q.add(new Pair( a, 0 )); // Empty Jug2 80 | q.add(new Pair( 0, b )); // Empty Jug1 81 | } 82 | 83 | if (!isSolvable) 84 | System.out.print("No solution"); 85 | } 86 | 87 | public static void main(String args[]) 88 | { 89 | int Jug1 = 4, Jug2 = 3, target = 2; 90 | 91 | System.out.println("Path from initial state " + 92 | "to solution state ::"); 93 | 94 | WaterJugProblem object = new WaterJugProblem(); 95 | 96 | object.BFS(Jug1, Jug2, target); 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Dynamic programming/Word Wrap using DP.cpp: -------------------------------------------------------------------------------- 1 | //Solving WordWrap using Dynamic Programming 2 | #include 3 | #include 4 | using namespace std; 5 | #define INF INT_MAX 6 | 7 | int printSolution (int p[], int n); 8 | 9 | void printArray(int arr[20][20]) 10 | { 11 | int n; 12 | for(int i=0;i= 0) 51 | lc[i][j] = 0; 52 | else 53 | lc[i][j] = extras[i][j]*extras[i][j]; 54 | } 55 | 56 | } 57 | cout<<"#################### Array OF COST ############################"; 58 | for(int i=1;i<=n;i++){ 59 | cout<<"\n"; 60 | for(int j=1;j<=n;j++){ 61 | if(lc[i][j]== INF ){ 62 | lc[i][j]= 999; 63 | cout<>n; 129 | cout<<"enter the strings"<>s[i]; 133 | } 134 | for(int i=0;i>M; 141 | int n1 = sizeof(l)/sizeof(l[0]); 142 | solveWordWrap (l, n, M); 143 | return 0; 144 | } 145 | 146 | -------------------------------------------------------------------------------- /Arrays/Nut and Bolt Matching Problem.cpp: -------------------------------------------------------------------------------- 1 | // Nut and Bolt Matching Problem based on QuickSort Approach - Google Interview 2 | #include 3 | #include 4 | #include 5 | using namespace std ; 6 | 7 | class Bolt; //Forward Declaration 8 | class Nut{ 9 | int Size; 10 | public: 11 | Nut(int Size){ 12 | this->Size = Size; 13 | } 14 | int getSize(){ 15 | return this->Size; 16 | } 17 | friend int::compare(const Nut*,const Bolt*); 18 | friend int::compare(const Bolt*,const Nut*); 19 | }; 20 | 21 | class Bolt{ 22 | int Size; 23 | 24 | public: 25 | Bolt(int Size){ 26 | this->Size = Size; 27 | } 28 | int getSize(){ 29 | return this->Size; 30 | } 31 | friend int::compare(const Nut*,const Bolt*); 32 | friend int::compare(const Bolt*,const Nut*); 33 | }; 34 | 35 | // Return -1 Nut < Bolt , 0 equal , else 1 36 | int compare(const Nut* n,const Bolt*b){ 37 | if( n->Size < b->Size) 38 | return -1; 39 | else if(n->Size==b->Size) 40 | return 0; 41 | else 42 | return 1; 43 | } 44 | //Return -1 Bolt < Nut , 0 equal , else 1 45 | int compare(const Bolt*b,const Nut*n){ 46 | if( b->Size < n->Size) 47 | return -1; 48 | else if(n->Size==b->Size) 49 | return 0; 50 | else 51 | return 1; 52 | } 53 | //Since we need two types of partition function , hence better to use a template 54 | template 55 | int partition(X** a,int low,int high,Y*b){ 56 | int i,j,k; 57 | i = low - 1; 58 | 59 | for(j=low;j>n; 95 | Nut **nuts; 96 | Bolt **bolts; 97 | 98 | cout<<"Enter Nut Sizes ,then Bolt Sizes :"<>s; 105 | nuts[i] = new Nut(s); 106 | } 107 | for(i=0;i>s; 109 | bolts[i] = new Bolt(s); 110 | } 111 | matchNutsBolts(nuts,bolts,0,n-1); 112 | 113 | cout<<"BOLTS : "; 114 | for(i=0;igetSize()<<" "; 116 | } 117 | cout<getSize()<<" " ; 120 | } 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /Lists/LinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node { 4 | public: 5 | int data; 6 | node* next; 7 | 8 | //Constructor 9 | node(int val){ 10 | data = val; 11 | next = NULL; 12 | } 13 | }; 14 | void print(node* head){ 15 | //node* n = new node(val); 16 | node* temp= head; 17 | while(temp!=NULL){ 18 | cout << temp->data <<"->"; 19 | temp = temp->next; 20 | }cout<<"NULL"; 21 | cout<next!= NULL) 29 | { 30 | //cout << temp->data << endl; 31 | temp = temp->next; 32 | } 33 | temp->next = n; 34 | } else { 35 | head = n; 36 | return; 37 | } 38 | } 39 | void inserthead(node* &head,int val){ 40 | node* n = new node(val); 41 | n->next = head; 42 | head = n; 43 | } 44 | */ 45 | void insert(node* &head,int val,int pos){ 46 | node* n = new node(val); 47 | if(pos==1){ 48 | n->next = head; 49 | head = n; 50 | } else { 51 | node* temp = head; 52 | node* ptr; 53 | while (pos > 1) 54 | { 55 | ptr = temp; 56 | temp = temp->next; 57 | pos--; 58 | } 59 | ptr->next = n; 60 | n->next = temp; 61 | //n->next = ptr->next; 62 | //ptr->next = n; 63 | } 64 | } 65 | void insertSorted(node* &head,int val){ 66 | node* n = new node(val); 67 | node* temp = head; 68 | if(val<(head->data)){ 69 | n->next = head; 70 | head = n; 71 | } else { 72 | node* ptr; 73 | while (val > (temp->data)) 74 | { 75 | ptr = temp; 76 | temp = temp->next; 77 | if(temp==NULL){ 78 | break; 79 | } 80 | } 81 | n->next = ptr->next; 82 | ptr->next = n; 83 | } 84 | } 85 | void deletion(node* &head,int val){ 86 | node* temp=head; 87 | if(temp==NULL){ 88 | return; 89 | } 90 | if(temp->next==NULL){ 91 | node *rem = head; 92 | head = temp->next; 93 | delete rem; 94 | return; 95 | } 96 | if(temp->data==val){ 97 | node* rem = head; 98 | head = temp->next; 99 | delete rem; 100 | } else { 101 | while(temp->next->data!=val){ 102 | temp = temp->next; 103 | } 104 | node* rem = temp->next; 105 | temp->next = temp->next->next; 106 | delete rem; 107 | } 108 | } 109 | void deleteAlt(node* &head) 110 | { 111 | node *ptr = head; 112 | while (ptr != NULL) 113 | { 114 | node *ptr2 = ptr->next; 115 | ptr = ptr2->next; 116 | ptr2 = NULL; 117 | } 118 | } 119 | int main(){ 120 | node *head=NULL; 121 | insert(head,10,1); 122 | insert(head, 20, 2); 123 | insert(head, 40, 3); 124 | insert(head, 50, 4); 125 | insert(head,60,5); 126 | insert(head,70,6); 127 | //insertSorted(head, 30); 128 | //insertSorted(head, 95); 129 | print(head); 130 | deleteAlt(head); 131 | print(head); 132 | return 0; 133 | } -------------------------------------------------------------------------------- /Strings/Pursuit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://codeforces.com/problemset/problem/1530/C 3 | */ 4 | #include 5 | using namespace std; 6 | #define lli long long int 7 | #define ll long long 8 | #define ld long double 9 | #define fast \ 10 | ios::sync_with_stdio(false); \ 11 | cin.tie(NULL); \ 12 | cout.tie(NULL); 13 | #define all(x) (x).begin(), (x).end() 14 | #define loop(i, a, b) for (lli i = a; i < b; i++) 15 | #define revloop(i, a, b) for (lli i = a; i >= b; i--) 16 | #define pb push_back 17 | #define pf printf 18 | #define sf scanf 19 | #define ff first 20 | #define ss second 21 | #define YY cout << "YES" << endl 22 | #define NN cout << "NO" << endl 23 | #define input(arr, n) \ 24 | for (ll i1 = 0; i1 < n; i1++) \ 25 | cin >> arr[i1] 26 | #define sz(x) x.size() 27 | #define ln(x) x.length() 28 | typedef vector vectorlli; 29 | typedef vector vectorint; 30 | typedef vector vectorchar; 31 | typedef vector vectorst; 32 | typedef vector vectorll; 33 | typedef pair pairlli; 34 | typedef pair pairll; 35 | typedef map maplli; 36 | typedef map mapll; 37 | typedef map mapch; 38 | typedef map mapst; 39 | const lli mod = 1e9 + 7; 40 | const lli inf = 1e18; 41 | int check(int n) 42 | { 43 | return n - floor((double)n / 4); 44 | } 45 | int main() 46 | { 47 | int t; 48 | cin >> t; 49 | while (t--) 50 | { 51 | lli n; 52 | cin >> n; 53 | lli bk = n; 54 | vectorlli a, b, c, d; 55 | loop(i, 0, n) 56 | { 57 | lli x; 58 | cin >> x; 59 | a.pb(x); 60 | } 61 | loop(i, 0, n) 62 | { 63 | lli x; 64 | cin >> x; 65 | b.pb(x); 66 | } 67 | sort(all(a), greater()); 68 | sort(all(b), greater()); 69 | lli s = 0, s1 = 0; 70 | loop(i, 0, n) 71 | { 72 | s = s + a[i]; 73 | c.pb(s); 74 | s1 = s1 + b[i]; 75 | d.pb(s1); 76 | } 77 | lli k = check(n); 78 | lli maxc = c[n - 1], maxd = d[n - 1]; 79 | if (c[k - 1] >= d[k - 1]) 80 | { 81 | cout << 0 << endl; 82 | } 83 | else 84 | { 85 | int cnt = 0, v1 = c[k - 1], v2 = d[k - 1]; 86 | while (v1 < v2) 87 | { 88 | n++; 89 | k = check(n); 90 | cnt++; 91 | if ((k - 1 - cnt) > (n - 1)) 92 | { 93 | v1 = maxc + (100 * cnt); 94 | } 95 | else if ((k - 1 - cnt) < 0) 96 | { 97 | v1 = c[0] + (100 * cnt); 98 | } 99 | else 100 | { 101 | v1 = c[k - 1 - cnt] + (100 * cnt); 102 | } 103 | if ((k) > bk) 104 | { 105 | v2 = maxd; 106 | } 107 | else 108 | { 109 | v2 = d[k - 1]; 110 | } 111 | } 112 | cout << cnt << endl; 113 | } 114 | } 115 | return 0; 116 | } -------------------------------------------------------------------------------- /Back-tracking/Solve the Sudoku.cpp: -------------------------------------------------------------------------------- 1 | problem statemnt : 2 | leet 3 | httpsleetcode.comproblemssudoku-solver 4 | gfg 5 | https://practice.geeksforgeeks.org/problems/solve-the-sudoku-1587115621/1 6 | 7 | 8 | Video link solution 9 | https://www.youtube.com/watch?v=uyetDh-DyDg&t=1185s 10 | 11 | #include 12 | using namespace std; 13 | // UNASSIGNED is used for empty cells in sudoku grid 14 | #define UNASSIGNED 0 15 | 16 | // N is used for the size of Sudoku grid. 17 | // Size will be NxN 18 | #define N 9 19 | 20 | 21 | // } Driver Code Ends 22 | 23 | 24 | class Solution { 25 | public: 26 | 27 | bool check(int grid[N][N],int n1,int x1,int x2) 28 | { 29 | 30 | for(int i=0;i<9;i++) 31 | { 32 | if(grid[x1][i]==n1) 33 | return false; 34 | } 35 | 36 | for(int i=0;i<9;i++) 37 | { 38 | if(grid[i][x2]==n1) 39 | return false; 40 | } 41 | 42 | //cout<>t; 157 | while(t--) 158 | { 159 | int grid[N][N]; 160 | 161 | for(int i=0;i<9;i++) 162 | for(int j=0;j<9;j++) 163 | cin>>grid[i][j]; 164 | 165 | Solution ob; 166 | 167 | if (ob.SolveSudoku(grid) == true) 168 | ob.printGrid(grid); 169 | else 170 | cout << "No solution exists"; 171 | 172 | cout<> suggestedProducts(String[] products, String searchWord) { 8 | 9 | List> res = new ArrayList<>(); 10 | if (products == null || searchWord == null) { 11 | return res; 12 | } 13 | 14 | Arrays.sort(products); 15 | List proList = Arrays.asList(products); 16 | TreeMap proToIndex = new TreeMap<>(); 17 | for (int i = 0; i < products.length; i++) { 18 | proToIndex.put(products[i], i); 19 | } 20 | 21 | String key = ""; 22 | for (char c : searchWord.toCharArray()) { 23 | key = key + c; 24 | String ceil = proToIndex.cellingKey(key); 25 | String floor = proToIndex.floorKey(key + "~"); // can find key 26 | if (cell == null || floor == null) { 27 | break; 28 | } 29 | 30 | res.add(proList.subList(proToIndex.get(ceil), Math.min(proToIndex.get(ceil) + 3, proToIndex.get(floor) + 1))); 31 | } 32 | 33 | while (res.size() < searchWord.length()) { 34 | res.add(new Array()); 35 | } 36 | 37 | return res; 38 | } 39 | } 40 | 41 | 42 | /* 43 | *Approach #2 Tire Tree 44 | */ 45 | 46 | class Solution { 47 | private class TrieNode { 48 | boolean end = false; 49 | String str = null; 50 | int count = 0; 51 | TrieNode[] children = new TrieNode[26]; 52 | } 53 | 54 | private class Trie { 55 | TrieNode root = new TrieNode(); 56 | public void insert(String[] products) { 57 | for (String str : products) { 58 | insertWord(str); 59 | } 60 | } 61 | 62 | public void insertWord(String word) { 63 | TrieNode node = root; 64 | for (char c : word.toCharArray()) { 65 | if (node.children[c - 'a'] == null) { 66 | node.children[c - 'a'] = new TrieNode(); 67 | } 68 | node = node.children[c - 'a']; 69 | } 70 | if (node.end != true) { 71 | node.end = true; 72 | node.str = word; 73 | } 74 | node.count++; 75 | } 76 | 77 | public List> searchWord(String word) { 78 | List> result = new ArrayList<>(); 79 | for (int i = 1; i <= word.length(); i++) { 80 | result.add(search(word.substring(0, i))); 81 | } 82 | return result; 83 | } 84 | 85 | private List search(String pattern) { 86 | List result = new ArrayList<>(); 87 | TrieNode node = root; 88 | for (char c : pattern.toCharArray()) { 89 | if (node.children[c - 'a'] == null) { 90 | return result; 91 | } 92 | node = node.children[c - 'a']; 93 | } 94 | 95 | Solution(node, result); 96 | return result; 97 | } 98 | 99 | private void Solution(TrieNode root, List result) { 100 | if (root.end) { 101 | for (int i = 0; i < root.count; i++) { 102 | result.add(root.str); 103 | if (result.size() == 3) { 104 | return; 105 | } 106 | } 107 | } 108 | 109 | for (TrieNode node : root.children) { 110 | if (node != null) { 111 | Solution(node, result); 112 | } 113 | if (result.size() == 3) { 114 | return; 115 | } 116 | } 117 | 118 | } 119 | } 120 | 121 | public List> suggestedProducts(String[] products, String searchWord) { 122 | Trie trie = new Trie(); 123 | trie.insert(products); 124 | return trie.searchWord(searchWord); 125 | } 126 | } -------------------------------------------------------------------------------- /File-Ops/FileOps.c: -------------------------------------------------------------------------------- 1 | //List of Headers included 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //Design of the structure 9 | struct details 10 | { 11 | char *name; 12 | char *status; 13 | int hour; 14 | int min; 15 | int sec; 16 | }; 17 | int main() 18 | { 19 | //Opening the file to read the data from the CSV file 20 | FILE *fp = fopen("sheet.csv", "r"); 21 | //Initializing an array of structures to store the data 22 | struct details dt[132]; 23 | int mx = 0; 24 | if (!fp) 25 | { 26 | //If no such file present shall show error msg in console 27 | printf("No file found!\n"); 28 | } 29 | else 30 | { 31 | char buffer[1024]; 32 | char *a, *b, *c; 33 | int row = 0, col = 0, i = 0; 34 | //Loop to get the data row=wise from csv file 35 | while (fgets(buffer, sizeof(buffer), fp)) 36 | { 37 | { 38 | //Subrata Nandi,Joined"3/6/20212:52:54 PM" 39 | col = 0; 40 | row++; 41 | if (row == 1) 42 | continue; 43 | char *value = strtok(buffer, ","); //strtok function of extract the pure data required from the csv unicode data 44 | while (value) 45 | { 46 | if (col == 0) 47 | { 48 | //helper variable to get the name 49 | a = value; 50 | } 51 | if (col == 1) 52 | { 53 | //helper variable ti get the statuses 54 | b = (value); 55 | } 56 | if (col == 3) 57 | { 58 | //helper variable to get the exact time i.e HH:MM:SS 59 | c = (value); 60 | } 61 | //TO get new parts of the buffer char array everytime 62 | value = strtok(NULL, ","); 63 | col++; 64 | } 65 | //Allocarion of memory for each structure to fill in the name, status, time 66 | //For Name 67 | dt[i].name = (char *)malloc(strlen(a) + 1); 68 | strcpy(dt[i].name, a); 69 | 70 | //storing maxlength to fix displaying of data 71 | mx = (mx > strlen(dt[i].name)) ? (mx) : (strlen(dt[i].name)); 72 | 73 | //For Status 74 | dt[i].status = (char *)malloc(strlen(b) + 1); 75 | strcpy(dt[i].status, b); 76 | 77 | //For time in separated manner as hours,minutes,seconds 78 | //For hours 79 | char *hr = (char *)calloc(1, &c[2] - &c[1] + 1); 80 | memcpy(hr, &c[1], &c[2] - &c[1]); 81 | //Conversion of string->int 82 | dt[i].hour = atoi(hr); 83 | 84 | //For minutes 85 | char *mn = (char *)calloc(1, &c[5] - &c[3] + 1); 86 | memcpy(mn, &c[3], &c[5] - &c[3]); 87 | dt[i].min = atoi(mn); 88 | 89 | //For seconds 90 | char *sc = (char *)calloc(1, &c[8] - &c[6] + 1); 91 | memcpy(sc, &c[6], &c[8] - &c[6]); 92 | dt[i].sec = atoi(sc); 93 | } 94 | i = i + 1; 95 | } 96 | int cnt = 0, s = 0, tt = (2 * 3600 + 60 * 50); 97 | //Headers of each Columns 98 | printf("%s", "Name of Attendees"); 99 | /* 100 | for(int k=0;k<(mx-strlen(dt[i].name));k++){ 101 | printf(" "); 102 | } 103 | */ 104 | printf("\t\t%s \t\t%s\n", ("Time Left/Joined"), ("Percentage of attendance in class")); 105 | 106 | //Looping via each struct 107 | for (int i = 0; i < 131; i++) 108 | { 109 | int start = 0, start2 = 0, end = 0, end2 = 0; 110 | //Removing multiple occurances of same names who had diff statuses by using a pair-wise Linear Search Algo 111 | if (strcmp(dt[i].name, dt[i + 1].name) != 0) 112 | { 113 | if (strcmp(dt[i].status, "Joined") == 0) 114 | { 115 | s = s + ((5 * 3600) - ((dt[i].sec) + ((dt[i].min + 1) * 60) + (dt[i].hour * 3600))); 116 | } 117 | printf("%s", dt[i].name); 118 | for (int k = 0; k < (mx - strlen(dt[i].name)); k++) 119 | { 120 | printf(" "); 121 | } 122 | printf("\t%d \t\t\t%f\n", cnt + 1, ((float)100 * s / tt)); 123 | //Restroing defaults for next iteration 124 | cnt = 0; 125 | s = 0; 126 | } 127 | else 128 | { 129 | 130 | //Calculations of time of presence by a simple model on seconds theory by using 00:00:00 as reference 131 | if (strcmp(dt[i].status, "Joined") == 0 && strcmp(dt[i + 1].status, "Left") == 0) 132 | { 133 | s = s + (((dt[i + 1].sec) + ((dt[i + 1].min) * 60) + (dt[i + 1].hour * 3600)) - ((dt[i].sec) + ((dt[i].min) * 60) + (dt[i].hour * 3600))); 134 | } 135 | cnt++; 136 | } 137 | } 138 | //Closing the wkstation... 139 | fclose(fp); 140 | } 141 | //End 142 | return 0; 143 | } -------------------------------------------------------------------------------- /File-Ops/sheet.csv: -------------------------------------------------------------------------------- 1 | Full Name,User Action,Timestamp 2 | Subrata Nandi,Joined,"3/6/2021, 2:52:54 PM" 3 | Subrata Nandi,Left,"3/6/2021, 4:02:57 PM" 4 | Subrata Nandi,Joined,"3/6/2021, 4:08:19 PM" 5 | SANGAM KUMAR,Joined,"3/6/2021, 2:55:48 PM" 6 | Harshit Surana,Joined,"3/6/2021, 2:57:39 PM" 7 | ASIM SEN,Joined,"3/6/2021, 2:57:44 PM" 8 | ANKIT VISHWAKARMA,Joined,"3/6/2021, 2:58:06 PM" 9 | ANKIT VISHWAKARMA,Left,"3/6/2021, 2:59:33 PM" 10 | ANKIT VISHWAKARMA,Joined,"3/6/2021, 3:00:52 PM" 11 | POPURI CHARAN,Joined,"3/6/2021, 2:58:20 PM" 12 | POPURI CHARAN,Left,"3/6/2021, 3:47:49 PM" 13 | POPURI CHARAN,Joined,"3/6/2021, 3:48:17 PM" 14 | TANU PRIYA,Joined,"3/6/2021, 2:58:24 PM" 15 | AQUIB ZAMAN,Joined,"3/6/2021, 2:58:38 PM" 16 | VISHAL MAHAVAR,Joined,"3/6/2021, 2:58:39 PM" 17 | DEEPAK KUMAR,Joined,"3/6/2021, 2:58:48 PM" 18 | DEEPAK KUMAR,Left,"3/6/2021, 3:59:40 PM" 19 | DEVA VENKATA SAI RITHISH PUNNA,Joined,"3/6/2021, 2:59:52 PM" 20 | TURPATI SAI BALAJI,Joined,"3/6/2021, 3:00:16 PM" 21 | ISHAN DUTTA,Joined,"3/6/2021, 3:00:19 PM" 22 | ISHAN DUTTA,Left,"3/6/2021, 3:56:46 PM" 23 | ISHAN DUTTA,Joined,"3/6/2021, 4:13:43 PM" 24 | TANMOY GORAI,Joined,"3/6/2021, 3:00:27 PM" 25 | TANMOY GORAI,Left,"3/6/2021, 3:18:01 PM" 26 | TANMOY GORAI,Joined,"3/6/2021, 3:18:15 PM" 27 | TANMOY GORAI,Left,"3/6/2021, 3:21:43 PM" 28 | TANMOY GORAI,Joined,"3/6/2021, 3:21:52 PM" 29 | TANMOY GORAI,Left,"3/6/2021, 3:23:25 PM" 30 | TANMOY GORAI,Joined,"3/6/2021, 3:23:57 PM" 31 | TANMOY GORAI,Left,"3/6/2021, 3:47:53 PM" 32 | TANMOY GORAI,Joined,"3/6/2021, 3:48:02 PM" 33 | TANMOY GORAI,Left,"3/6/2021, 4:08:23 PM" 34 | TANMOY GORAI,Joined,"3/6/2021, 4:08:28 PM" 35 | NUTETI USHODAYA,Joined,"3/6/2021, 3:00:51 PM" 36 | NUTETI USHODAYA,Left,"3/6/2021, 3:07:15 PM" 37 | NUTETI USHODAYA,Joined,"3/6/2021, 3:07:43 PM" 38 | AYUSH KUMAR GUPTA,Joined,"3/6/2021, 3:01:12 PM" 39 | AYUSH KUMAR GUPTA,Left,"3/6/2021, 3:54:19 PM" 40 | Swarnabh Paul,Joined,"3/6/2021, 3:01:36 PM" 41 | KSHITIJ SHRIVASTAVA,Joined,"3/6/2021, 3:01:45 PM" 42 | ATUL KUMAR,Joined,"3/6/2021, 3:02:04 PM" 43 | ATUL KUMAR,Left,"3/6/2021, 3:02:12 PM" 44 | ATUL KUMAR,Joined,"3/6/2021, 3:11:27 PM" 45 | GULLA PRAMILA RATNA KUMARI,Joined,"3/6/2021, 3:02:26 PM" 46 | GULLA PRAMILA RATNA KUMARI,Left,"3/6/2021, 3:42:10 PM" 47 | GULLA PRAMILA RATNA KUMARI,Joined,"3/6/2021, 3:42:25 PM" 48 | PRIETY DAS,Joined,"3/6/2021, 3:02:32 PM" 49 | PRIETY DAS,Left,"3/6/2021, 3:20:37 PM" 50 | PRIETY DAS,Joined,"3/6/2021, 3:25:03 PM" 51 | PRIETY DAS,Left,"3/6/2021, 3:27:43 PM" 52 | PRASANN GUPTA,Joined,"3/6/2021, 3:02:53 PM" 53 | PRASANN GUPTA,Left,"3/6/2021, 3:16:13 PM" 54 | PRASANN GUPTA,Joined,"3/6/2021, 3:17:35 PM" 55 | NEELAPU ASHA RISHITHA,Joined,"3/6/2021, 3:02:53 PM" 56 | SOHIT KUMAR SINGH,Joined,"3/6/2021, 3:03:12 PM" 57 | SHASHWAT GUPTA,Joined,"3/6/2021, 3:03:24 PM" 58 | Satyam Vivek,Joined,"3/6/2021, 3:03:44 PM" 59 | ABANTIKA BASU,Joined,"3/6/2021, 3:03:48 PM" 60 | VORUGANTI GAYATRI DEEPSHIKHA,Joined,"3/6/2021, 3:03:51 PM" 61 | ANKITA SINGH,Joined,"3/6/2021, 3:03:56 PM" 62 | ANKITA SINGH,Left,"3/6/2021, 4:10:46 PM" 63 | POONAM KISKU,Joined,"3/6/2021, 3:03:58 PM" 64 | ALLU KUMARA SAI SUBRAHMANYAM,Joined,"3/6/2021, 3:04:06 PM" 65 | VIKAS KUMAR,Joined,"3/6/2021, 3:04:06 PM" 66 | SUBHADIP RUDRA,Joined,"3/6/2021, 3:04:26 PM" 67 | GUDEY SONALI,Joined,"3/6/2021, 3:04:48 PM" 68 | AKASH MAJHI,Joined,"3/6/2021, 3:04:51 PM" 69 | SUSHANTA SAREN,Joined,"3/6/2021, 3:05:16 PM" 70 | SUSHANTA SAREN,Left,"3/6/2021, 4:09:04 PM" 71 | UDDAGIRI SAI VENKAT,Joined,"3/6/2021, 3:05:53 PM" 72 | UDDAGIRI SAI VENKAT,Left,"3/6/2021, 3:24:45 PM" 73 | UDDAGIRI SAI VENKAT,Joined,"3/6/2021, 3:32:15 PM" 74 | VIPUL DEV HEMBROM,Joined,"3/6/2021, 3:06:46 PM" 75 | ABHISHEK BASU,Joined,"3/6/2021, 3:07:15 PM" 76 | SUBHASH KUMAR,Joined,"3/6/2021, 3:09:30 PM" 77 | ARKA SETH,Joined,"3/6/2021, 3:09:53 PM" 78 | ARKA SETH,Left,"3/6/2021, 3:12:17 PM" 79 | ARKA SETH,Joined,"3/6/2021, 3:16:56 PM" 80 | ARKA SETH,Left,"3/6/2021, 3:21:36 PM" 81 | ARKA SETH,Joined,"3/6/2021, 3:33:59 PM" 82 | ARKA SETH,Left,"3/6/2021, 3:46:45 PM" 83 | ARKA SETH,Joined,"3/6/2021, 3:46:50 PM" 84 | DODDA CHANDU,Joined,"3/6/2021, 3:10:11 PM" 85 | ROHITH CHEKURI,Joined,"3/6/2021, 3:10:15 PM" 86 | ADITYA GUPTA,Joined,"3/6/2021, 3:10:26 PM" 87 | SHADAB AHMAD,Joined,"3/6/2021, 3:10:32 PM" 88 | BORRA DAIVALEELA,Joined,"3/6/2021, 3:10:45 PM" 89 | Sanipalli Renuka,Joined,"3/6/2021, 3:12:06 PM" 90 | RAM SHARMA,Joined,"3/6/2021, 3:14:49 PM" 91 | RAM SHARMA,Left,"3/6/2021, 3:55:17 PM" 92 | MADDU AMAR SRI LAKSHMI PRASANNA TRINATH,Joined,"3/6/2021, 3:15:29 PM" 93 | MADDU AMAR SRI LAKSHMI PRASANNA TRINATH,Left,"3/6/2021, 3:33:43 PM" 94 | Sreeshanth Ganduri,Joined,"3/6/2021, 3:16:26 PM" 95 | JAMPALA HARIKA,Joined,"3/6/2021, 3:16:40 PM" 96 | ANKITA MANDAL,Joined,"3/6/2021, 3:17:09 PM" 97 | ANKITA MANDAL,Left,"3/6/2021, 3:52:00 PM" 98 | ARIJIT BISWAS,Joined,"3/6/2021, 3:17:51 PM" 99 | NALLA YOSHITA,Joined,"3/6/2021, 3:20:04 PM" 100 | NALLA YOSHITA,Left,"3/6/2021, 4:09:55 PM" 101 | NALLA YOSHITA,Joined,"3/6/2021, 4:10:39 PM" 102 | PRAVEEN KUMAR RAVINDRAN SIBANIRANI,Joined,"3/6/2021, 3:20:13 PM" 103 | PRAVEEN KUMAR RAVINDRAN SIBANIRANI,Left,"3/6/2021, 3:22:55 PM" 104 | PRAVEEN KUMAR RAVINDRAN SIBANIRANI,Joined,"3/6/2021, 3:23:39 PM" 105 | ARVIND SAHU,Joined,"3/6/2021, 3:22:01 PM" 106 | ARVIND SAHU,Left,"3/6/2021, 4:25:56 PM" 107 | BOKKA SREE,Joined,"3/6/2021, 3:23:07 PM" 108 | BISHWAJIT GHOSH,Joined,"3/6/2021, 3:32:23 PM" 109 | BISHWAJIT GHOSH,Left,"3/6/2021, 4:08:27 PM" 110 | BISHWAJIT GHOSH,Joined,"3/6/2021, 4:16:11 PM" 111 | PUTHA REDDY,Joined,"3/6/2021, 3:36:49 PM" 112 | SAGAR AGARWAL,Joined,"3/6/2021, 3:42:57 PM" 113 | KANCHARLA SASHIDHAR CHOWDARY,Joined,"3/6/2021, 3:43:28 PM" 114 | PRAKHAR SRIVASTAVA,Joined,"3/6/2021, 3:44:32 PM" 115 | ANUPAM DAS,Joined,"3/6/2021, 3:55:40 PM" 116 | SUDIPAN DATTA,Joined,"3/6/2021, 3:55:52 PM" 117 | SUDIPAN DATTA,Left,"3/6/2021, 4:26:11 PM" 118 | AMAN KUMAR,Joined,"3/6/2021, 3:58:08 PM" 119 | BHUKYA RAHUL NAIK,Joined,"3/6/2021, 4:08:19 PM" 120 | BHUKYA RAHUL NAIK,Left,"3/6/2021, 4:22:46 PM" 121 | AMAN KUMAR,Joined,"3/6/2021, 4:08:19 PM" 122 | SUDEV DHALI,Joined,"3/6/2021, 4:08:19 PM" 123 | SUMAN SAHU,Joined,"3/6/2021, 4:08:19 PM" 124 | DEBOLINA CHOWDHURY,Joined,"3/6/2021, 4:09:45 PM" 125 | Anurag Mondal,Joined,"3/6/2021, 4:10:30 PM" 126 | Anurag Mondal,Left,"3/6/2021, 4:16:50 PM" 127 | Nikhil Kumar,Joined,"3/6/2021, 4:10:48 PM" 128 | SATYAM YADAV,Joined,"3/6/2021, 4:14:08 PM" 129 | ANSHIKA CHAUDHARY,Joined,"3/6/2021, 4:15:41 PM" 130 | VIKAS KUMAR,Joined,"3/6/2021, 4:17:04 PM" 131 | ANWER SAYEED,Joined,"3/6/2021, 4:18:51 PM" 132 | TILAKRAJ NARAYANAN,Joined,"3/6/2021, 4:28:02 PM" 133 | -------------------------------------------------------------------------------- /Arrays/heap_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int size,data_generated=0; 7 | int check(int data_generated) 8 | { 9 | if(data_generated == 0) 10 | return 0; 11 | else 12 | return 1; 13 | } 14 | 15 | void max_heapify(int arr[], int i,int size) 16 | { 17 | int temp,largest; 18 | //int largest=i; 19 | int l = 2 * i; 20 | int r = 2 * i + 1; 21 | 22 | if (l <= size && arr[l] > arr[i]) 23 | largest = l; 24 | else 25 | largest=i; 26 | 27 | if (r <= size && arr[r] > arr[largest]) 28 | largest = r; 29 | 30 | if (largest != i) 31 | { 32 | temp=arr[i]; 33 | arr[i]=arr[largest]; 34 | arr[largest]=temp; 35 | max_heapify(arr, largest, size); 36 | } 37 | } 38 | 39 | void build_max_heap(int arr[],int size) 40 | { 41 | for (int i = size / 2 ; i >= 1; i--) 42 | max_heapify(arr, i, size); 43 | } 44 | 45 | void heapSort(int arr[], int size) 46 | { 47 | int temp; 48 | build_max_heap(arr,size); 49 | 50 | for (int i = size; i >= 2; i--) 51 | { 52 | temp=arr[1]; 53 | arr[1]=arr[i]; 54 | arr[i]=temp; 55 | size= size - 1; 56 | max_heapify(arr, 1, size); 57 | } 58 | } 59 | 60 | void asce(int arr[],int size) 61 | { 62 | for (int i = 1; i <= size; ++i){ 63 | for (int j = i + 1; j <= size; ++j){ 64 | if (arr[i] > arr[j]){ 65 | int temp = arr[i]; 66 | arr[i] = arr[j]; 67 | arr[j] = temp; 68 | } 69 | } 70 | } 71 | } 72 | 73 | void desc(int arr[],int size) 74 | { 75 | for (int i = 1; i <= size; ++i){ 76 | for (int j = i + 1; j <= size; ++j){ 77 | if (arr[i] < arr[j]){ 78 | int temp = arr[i]; 79 | arr[i] = arr[j]; 80 | arr[j] = temp; 81 | } 82 | } 83 | } 84 | } 85 | void print_array(int arr[],int n) 86 | { 87 | for (int i = 1; i <= n; i++) 88 | printf("%d ",arr[i]); 89 | printf("\n"); 90 | } 91 | 92 | int heap_extract_max(int arr[],int size) 93 | { 94 | if(size<1) 95 | printf("Heap underflow\n"); 96 | int max=arr[1]; 97 | arr[1]=arr[size]; 98 | size=size-1; 99 | max_heapify(arr,1,size); 100 | return max; 101 | } 102 | 103 | void heap_increase_key(int arr[],int i,int key) 104 | { 105 | if(key1 && arr[i/2] Exit\n"); 165 | printf("1 --> random array input\n"); 166 | printf("2 --> Display the array. \n"); 167 | printf("3 --> Sorted array using Max-Heap Sort.\n"); 168 | printf("4 --> Sort array in descending order\n"); 169 | printf("5 --> TC to sort ascending of random data\n"); 170 | printf("6 --> TC to sort ascending of data already sorted in ascending order\n"); 171 | printf("7 --> TC to sort ascending of data already sorted in descending order\n"); 172 | printf("8 --> TC of all cases\n"); 173 | printf("9 --> Extract largest element\n"); 174 | printf("10 --> Replace value at a node with new value\n"); 175 | printf("11 --> Insert a new element\n"); 176 | printf("12 --> Delete an element\n"); 177 | 178 | srand(time(0)); 179 | size= rand() % 100; 180 | int arr[size],key; 181 | while(1) 182 | { 183 | printf("\n which operation to be done \n"); 184 | scanf("%d",&x); 185 | switch (x) 186 | { 187 | case 0: 188 | exit(0); 189 | break; 190 | case 1: 191 | printf("size = %d\n", size); 192 | for (int i = 1; i <= size; i++) 193 | { 194 | arr[i] = rand() % 1000; 195 | } 196 | printf("Random data generated successfully \n"); 197 | data_generated=1; 198 | break; 199 | case 2: 200 | if(check(data_generated) == 1) 201 | { 202 | print_array(arr, size); 203 | } 204 | else 205 | { 206 | printf("First generate random data \n"); 207 | } 208 | break; 209 | case 3: 210 | if(check(data_generated) == 1) 211 | { 212 | heapSort(arr, size); 213 | printf("After sorting the array in ascending order \n"); 214 | print_array(arr, size); 215 | } 216 | else 217 | { 218 | printf("First generate random data \n"); 219 | } 220 | break; 221 | case 4: 222 | if(check(data_generated) == 1) 223 | { 224 | desc(arr, size); 225 | printf("After sorting the array in descending order \n"); 226 | print_array(arr, size); 227 | } 228 | else 229 | { 230 | printf("First generate random data \n"); 231 | } 232 | break; 233 | case 5: 234 | if(check(data_generated) == 1) 235 | { 236 | start=clock(); 237 | heapSort(arr, size); 238 | end=clock(); 239 | t_random= end - start; 240 | printf("Time taken in ms of rand: %f\n", t_random); 241 | } 242 | else 243 | { 244 | printf("First generate random data \n"); 245 | } 246 | break; 247 | 248 | case 6: 249 | if(check(data_generated) == 1) 250 | { 251 | asce(arr, size); 252 | 253 | start=clock(); 254 | heapSort(arr, size); 255 | end=clock(); 256 | t_ascending= end - start; 257 | printf("Time taken in ms of ascending data: %f\n", t_ascending); 258 | } 259 | else 260 | { 261 | printf("First generate random data \n"); 262 | } 263 | break; 264 | case 7: 265 | if(check(data_generated) == 1) 266 | { 267 | desc(arr, size); 268 | 269 | start=clock(); 270 | heapSort(arr, size); 271 | end=clock(); 272 | t_desc= end - start; 273 | printf("Time taken in ms of descending data: %f\n", t_desc); 274 | 275 | } 276 | else 277 | { 278 | printf("First generate random data \n"); 279 | } 280 | break; 281 | case 8: 282 | if(check(data_generated) == 1) 283 | { 284 | heapSort(arr, size); 285 | 286 | start=clock(); 287 | heapSort(arr, size); 288 | end=clock(); 289 | t_ascending= end - start; 290 | 291 | 292 | desc(arr, size); 293 | 294 | start=clock(); 295 | heapSort(arr, size); 296 | end=clock(); 297 | t_desc= end - start; 298 | 299 | 300 | start=clock(); 301 | heapSort(arr, size); 302 | end=clock(); 303 | t_random= end - start; 304 | 305 | printf("Value of n Ascending Data Descending Data Random Data \n"); 306 | printf("%d \t %f \t %f \t %f ",size, t_ascending, t_desc, t_random); 307 | } 308 | else 309 | { 310 | printf("First generate random data \n"); 311 | } 312 | 313 | break; 314 | 315 | case 9: 316 | if(check(data_generated) == 1) 317 | { 318 | build_max_heap(arr, size); 319 | print_array(arr, size); 320 | printf("\n"); 321 | maximum=heap_extract_max(arr, size); 322 | printf("Extracted Largest element %d\n", maximum); 323 | size--; 324 | } 325 | else 326 | { 327 | printf("First generate random data \n"); 328 | } 329 | break; 330 | case 10: 331 | if(check(data_generated) == 1) 332 | { 333 | build_max_heap(arr, size); 334 | printf("\n"); 335 | print_array(arr, size); 336 | printf("\n"); 337 | printf("Enter the key value to be changed :- "); 338 | scanf("%d",&key_old); 339 | index=search(arr, key_old, size); 340 | printf("Enter new key value :- "); 341 | scanf("%d",&key_new); 342 | heap_increase_key(arr, index, key_new); 343 | build_max_heap(arr, size); 344 | } 345 | else 346 | { 347 | printf("First generate random data \n"); 348 | } 349 | break; 350 | case 11: 351 | if(check(data_generated) == 1) 352 | { 353 | build_max_heap(arr, size); 354 | printf("Enter the element to be inserted :- "); 355 | scanf("%d",&key); 356 | max_heap_insert(arr, key, size); 357 | size++; 358 | } 359 | else 360 | { 361 | printf("First generate random data \n"); 362 | } 363 | break; 364 | case 12: 365 | if(check(data_generated) == 1) 366 | { 367 | build_max_heap(arr, size); 368 | printf("Array data \n"); 369 | print_array(arr, size); 370 | printf("Enter the element to be deleted :- "); 371 | scanf("%d",&delete_element); 372 | flag_value=delete_ele(arr, size, delete_element); 373 | if(flag_value == 1) 374 | { 375 | printf("Element deleted succesfully\n"); 376 | size--; 377 | } 378 | else 379 | printf("Element not found \n"); 380 | build_max_heap(arr, size); 381 | } 382 | else 383 | { 384 | printf("First generate random data \n"); 385 | } 386 | break; 387 | default: 388 | printf("enter correct no \n"); 389 | break; 390 | } 391 | } 392 | } 393 | --------------------------------------------------------------------------------