├── Advanced Graphs ├── Airport2.cpp ├── Airportwrong.cpp ├── Bottom.cpp ├── Connectedhorses.cpp ├── Dominos.cpp ├── Kingdom.cpp ├── Matrix.cpp ├── Monk.cpp ├── Newyear.cpp ├── Permutationsswap.cpp ├── Test.txt └── a.out ├── Assignment1 ├── Collecting the balls.cpp ├── Power.cpp ├── Sortingskills.cpp ├── Test.txt └── a.out ├── Backtracking ├── Crossword.cpp ├── Fillthematrix.cpp ├── Nqueens.cpp ├── Rateinamaze.cpp ├── Test.txt ├── a.out └── check.cpp ├── Bit Manipulation ├── Test.txt └── Unset.cpp ├── C++ tricks for competitive programming (for C++ 11) - GeeksforGeeks.pdf ├── Computational Geometry └── areaOfConvexPolygon.cpp ├── Cpp-STL-cheat-sheet.pdf ├── DP1(Very important) ├── Adjacentbit ├── Adjacentbit.cpp ├── Allzeroes ├── Allzeroes.cpp ├── AlphaCode-Question.cpp ├── Boredom.cpp ├── Hasanandtrip.cpp ├── Loot.cpp ├── Maximumsum.cpp ├── Mergegrid.cpp ├── Royandcoin.cpp ├── SamAndSubstring.cpp ├── Test.txt ├── Vanya and gcd ├── Vanya and gcd.cpp ├── a.out ├── angrychildren ├── angrychildren.cpp ├── coinchange.cpp ├── jonsnow.cpp ├── longestbitonicsubarray.cpp ├── minchoc.cpp ├── mincount.cpp ├── spreadsheet.cpp └── staircase.cpp ├── DPandbitmasking ├── Candy.cpp ├── Countingstrings.cpp ├── Diliema.cpp ├── Job.cpp ├── Mehta.cpp ├── Stringmaker.cpp ├── Test.txt └── a.out ├── Dynamic programming 2 ├── Balikavadhu.cpp ├── Charlie.cpp ├── Editdistance.cpp ├── Knapsack.cpp ├── LCS.cpp ├── Maxprofit.cpp ├── Miserman.cpp ├── Party.cpp ├── Shortestsubsequence.cpp ├── Smallestsuperseq.cpp ├── Subsetsum.cpp ├── Test.txt ├── a.out └── maxprofit.cpp ├── Game Theory ├── game_of_othello.cpp └── grundy_numbers.cpp ├── Graphs1 ├── 3cycle.cpp ├── Allconnectedcomponents.cpp ├── Board.cpp ├── Connected_component_DFS.cpp ├── Get_Path_DFS.cpp ├── Islandsc.cpp ├── Test.txt ├── a.out ├── bfs.cpp ├── codingninjas.cpp ├── getpathbfs.cpp ├── haspath.cpp ├── isconnected.cpp └── largestpiece.cpp ├── Greedy ├── Activityselection.cpp ├── FractionalKnapsack.cpp ├── Nikunjanddonuts.cpp ├── Perimeter ├── Perimeter.cpp ├── Problemdiscussion.cpp ├── Test.txt ├── Weightedjob.cpp ├── Winninglottery.cpp ├── a.out └── minabsdiff.cpp ├── Interview Prep Books ├── Algorithms_3rd.pdf ├── Cracking the Coding Interview 189 Programming Questions and Solutions.pdf └── epilight_cpp_new.pdf ├── Language-Tools ├── 3-zero-sum.cpp ├── Different-names ├── Different-names.cpp ├── Duplicateinarray.cpp ├── Extractunique ├── Extractunique.cpp ├── Find-the-Unique-Element ├── Find-the-Unique-Element.cpp ├── Love-for-characters ├── Love-for-characters.cpp ├── Pairsumtozero ├── Pairsumtozero.cpp ├── Repeat-and-Missing-Number -Array.cpp ├── Rotatematrix.cpp ├── Tripletsum ├── Tripletsum.cpp ├── Warm-reception ├── Warm-reception.cpp ├── a.out ├── tell-thepositions ├── tell-thepositions.cpp └── test.txt ├── Leetcode ├── 718.cpp ├── Test.txt └── codenation1.cpp ├── Modulo Arithmetic ├── Balancesbst.cpp └── test.txt ├── Number Theory ├── Test.txt ├── a.out └── prime.cpp ├── Prerequisites ├── Eveenodd.cpp ├── chakri.cpp ├── marbles.cpp └── pre3.cpp ├── README.md ├── ReturnKeypadCode.cpp ├── Searching and sorting applications ├── Aggressive cows ├── Aggressive cows.cpp ├── Distrributecandies ├── Distrributecandies.cpp ├── Inversion Count ├── Inversion Count.cpp ├── Momos market ├── Momos market.cpp ├── Murderer ├── Murderer.cpp ├── Tajmahalentry.cpp ├── Test.txt └── a.out ├── Segment Trees ├── Horriblequeries.cpp ├── Maxpairsim.cpp ├── Maxsum.cpp ├── Minimuminsub.cpp ├── Test.txt ├── VasyaandRhezo.cpp ├── a.out ├── evenodd.cpp ├── segsqr.cpp └── twovsthreee.cpp ├── String ├── 0000000000000183s.zip ├── KMP ├── KMP.cpp ├── Zalgo.cpp ├── a.out ├── input.txt ├── output.txt └── palindromecount.cpp ├── Time-complexity-analysis ├── K-Concatenation ├── K-Concatenation.cpp └── Mindbedingproduct.cpp ├── Tries ├── Test.txt ├── autocomplete.cpp ├── help pradyumna.cpp ├── maximumxorsub.cpp └── maximumxorsubOriginal.cpp └── adhoc problems ├── Circularlist.cpp ├── Intrestingsequences.cpp ├── Lightupbulbs.cpp ├── Test.txt ├── a.out └── winning.cpp /Advanced Graphs/Dominos.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | Dominos are lots of fun. Children like to stand the tiles on their side in long lines. When one domino falls, it knocks down the next one, which knocks down the one after that, all the way down the line. 9 | However, sometimes a domino fails to knock the next one down. In that case, we have to knock it down by hand to get the dominos falling again. 10 | Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall. 11 | Input 12 | The first line of input contains one integer specifying the number of test cases to follow.Each test case begins with a line containing two integers,each no larger than 100 000. The first integer n is the number of domino tiles and the second integer m is the number of lines to follow in the test case. The domino tiles are numbered from 1 to n. 13 | Each of the following lines contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well. 14 | Output 15 | For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall. 16 | Sample Input 17 | 1 18 | 3 2 19 | 1 2 20 | 2 3 21 | Sample Output 22 | 1 23 | */ 24 | 25 | #include 26 | 27 | using namespace std; 28 | 29 | void dfs1(vector* graph, int start, bool* visited, stack &s){ 30 | visited[start] = 1; 31 | 32 | for (int i = 0; i < graph[start].size(); ++i) 33 | { 34 | if (visited[graph[start].at(i)] == 0) 35 | { 36 | dfs1(graph, graph[start].at(i), visited, s); 37 | } 38 | } 39 | 40 | //Will put in stack in the last 41 | s.push(start); 42 | 43 | return; 44 | } 45 | 46 | void dfs2(vector* graph, int start, bool* visited){ 47 | visited[start] = 1; 48 | for (int i = 0; i < graph[start].size(); ++i) 49 | { 50 | int v = graph[start].at(i); 51 | if (visited[graph[start].at(i)] == 0) 52 | { 53 | dfs2(graph, v, visited); 54 | } 55 | } 56 | 57 | return; 58 | } 59 | 60 | void kosaraju(vector* graph, vector* grapht, int n){ 61 | bool* visited = new bool[n]; 62 | for (int i = 0; i < n; ++i) 63 | { 64 | visited[i] = 0; 65 | } 66 | stack s; 67 | 68 | for (int i = 0; i < n; ++i) 69 | { 70 | if (visited[i]==0) 71 | { 72 | dfs1(graph, i, visited, s); 73 | } 74 | } 75 | 76 | //Step 2 77 | int count = 0; 78 | for (int i = 0; i < n; ++i) 79 | { 80 | visited[i] = 0; 81 | } 82 | 83 | while(!s.empty()) 84 | { 85 | int v = s.top(); 86 | s.pop(); 87 | if (visited[v] == 0) 88 | { 89 | count++; 90 | dfs2(graph, v, visited); 91 | } 92 | } 93 | 94 | cout << count << '\n'; 95 | 96 | 97 | } 98 | 99 | int main( int argc , char ** argv ) 100 | { 101 | ios_base::sync_with_stdio(false) ; 102 | cin.tie(NULL) ; 103 | 104 | int t; 105 | cin>>t; 106 | while(t--){ 107 | int n, m; 108 | cin>>n>>m; 109 | 110 | vector* graph = new vector[n]; 111 | vector* grapht = new vector[n]; 112 | for (int i = 0; i < m; ++i) 113 | { 114 | int u, v; 115 | cin>>u>>v; 116 | graph[u-1].push_back(v-1); 117 | grapht[v-1].push_back(u-1); 118 | } 119 | 120 | kosaraju(graph, grapht, n); 121 | 122 | } 123 | 124 | 125 | return 0 ; 126 | 127 | 128 | 129 | } 130 | -------------------------------------------------------------------------------- /Advanced Graphs/Matrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Fill The Matrix 11 | A matrix B (consisting of integers) of dimension N × N is said to be good if there exists an array A (consisting of integers) such that B[i][j] = |A[i] - A[j]|, where |x| denotes absolute value of integer x. 12 | 13 | You are given a partially filled matrix B of dimension N × N. Q of the entries of this matrix are filled by either 0 or 1. You have to identify whether it is possible to fill the remaining entries of matrix B (the entries can be filled by any integer, not necessarily by 0 or 1) such that the resulting fully filled matrix B is good. 14 | Input 15 | The first line of the input contains an integer T denoting the number of test cases. 16 | 17 | The first line of each test case contains two space separated integers N, Q. 18 | 19 | Each of the next Q lines contain three space separated integers i, j, val, which means that B[i][j] is filled with value val. 20 | Output 21 | For each test case, output "yes" or "no" (without quotes) in a single line corresponding to the answer of the problem. 22 | Constraints 23 | 1 ≤ T ≤ 10^6 24 | 2 ≤ N ≤ 10^5 25 | 1 ≤ Q ≤ 10^6 26 | 1 ≤ i, j ≤ N 27 | 0 ≤ val ≤ 1 28 | Sum of each of N, Q over all test cases doesn't exceed 106 29 | Input 30 | 4 31 | 2 2 32 | 1 1 0 33 | 1 2 1 34 | 2 3 35 | 1 1 0 36 | 1 2 1 37 | 2 1 0 38 | 3 2 39 | 2 2 0 40 | 2 3 1 41 | 3 3 42 | 1 2 1 43 | 2 3 1 44 | 1 3 1 45 | Output 46 | yes 47 | no 48 | yes 49 | no 50 | 51 | */ 52 | 53 | 54 | /* 55 | 56 | There can be a really easy greedy solution the problem.We can initialise the array A by -1. 57 | There is crucial observation that the array A can be constructed with only two values 0 and 1. 58 | Sort the index value pairs.Now if both the given index are unassigned,assign them according to 59 | the value given.Like 1 3 1 would mean 1 and 3 would be different .So initialise a[1] with 0 and 60 | a[3]=a[1]XOR val.Keep doing this and if at some point when both the indices are intialised with 61 | values!= -1 ,then check if they satisfy the given value,if not break and print -1. 62 | 63 | 64 | 1)I'll initialize an array of size n with -1 65 | 2)I'll then iterate over it and start with setting the first A[i] = 0 and then dfs will 66 | set the rest 67 | 3)Now in dfs i'll set the A[j](as already set A[i]) as A[i]^value if A[j]==-1 68 | 4)If it's not then it has to be equal to A[i]^value else return false 69 | 70 | */ 71 | 72 | #include 73 | using namespace std; 74 | const int N = 1e5+5; 75 | bool error; int col[N]; 76 | vector > adj[N]; 77 | void dfs(int start){ 78 | for(auto edge: adj[start]){ 79 | int next = edge.first; 80 | if(col[next] == -1){ 81 | col[next] = col[start]^edge.second; 82 | dfs(next); 83 | } 84 | else if(col[next] != col[start]^edge.second){ 85 | error = true; 86 | } 87 | } 88 | } 89 | int main(){ 90 | ios_base::sync_with_stdio(false); 91 | cin.tie(0); cout.tie(0); 92 | int t; cin>>t; 93 | while(t--){ 94 | int n,m; 95 | cin>>n>>m; 96 | error = false; 97 | for(int i=1;i<=n;i++){ 98 | adj[i].clear(); 99 | col[i] = -1; 100 | } 101 | while(m--){ 102 | int a,b,c; 103 | cin>>a>>b>>c; 104 | adj[a].push_back({b,c}); 105 | adj[b].push_back({a,c}); 106 | } 107 | for(int i=1;i<=n;i++){ 108 | if(col[i] == -1){ 109 | col[i] = 0; 110 | dfs(i); 111 | } 112 | } 113 | if(error) cout<<"no"<adj[]) 45 | { 46 | queueq; 47 | dp[1]=0; 48 | q.push(1); 49 | while(!q.empty()) 50 | { 51 | int idx=q.front(); 52 | q.pop(); 53 | for(int i=0;i>t; 69 | while(t--) 70 | { 71 | cin>>n>>m; 72 | memset(dp,-1,sizeof(dp)); 73 | vectoradj[n+1]; 74 | while(m--) 75 | { 76 | int x,y; 77 | cin>>x>>y; 78 | adj[x].push_back(y); 79 | adj[y].push_back(x); 80 | } 81 | cout< 10 | using namespace std; 11 | 12 | int main(){ 13 | int n, t; 14 | cin >> n >> t; 15 | 16 | // int **graph = new int*[n]; 17 | // for(int i = 0; i < n; i++){ 18 | // graph[i] = new int[n]; 19 | // for(int j = 0; j < n; j++) graph[i][j] = 0; 20 | // } 21 | 22 | int position = 0; 23 | 24 | for(int i = 0; i < n-1; i++){ 25 | if(position == t-1){ 26 | cout << "YES" << endl; 27 | return 0; 28 | } 29 | int a; 30 | cin >> a; 31 | // graph[i][i+a] = 1; 32 | if(i == position) position += a; 33 | } 34 | 35 | cout << "NO" << endl; 36 | } 37 | -------------------------------------------------------------------------------- /Advanced Graphs/Test.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 5 6 100 3 | 1 2 5 4 | 1 3 5 5 | 1 4 5 6 | 1 5 5 7 | 3 4 8 8 | 4 5 8 -------------------------------------------------------------------------------- /Advanced Graphs/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Advanced Graphs/a.out -------------------------------------------------------------------------------- /Assignment1/Collecting the balls.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | There are ‘n’ number of balls in a container. Mr. Sharma and Singh want to took balls out from 12 | the container. At each step, Mr. Sharma took ‘k’ balls out of the box and Mr. Singh took one-tenth 13 | of the remaining balls. Suppose there are 29 balls at the moment and k=4. Then, Mr. Sharma will take 4 14 | balls and Mr. Singh will take 2 balls (29-4 = 25; 25/10 = 2). If there are less than ‘k’ balls 15 | remaining at some moment, then Mr. Sharma will take all the balls which will get the container 16 | empty. The process will last till the container becomes empty. Your task is to choose minimal ‘k’ 17 | for Mr. Sharma such that Mr. Sharma will take at least half of the balls from the container. 18 | Input Format: 19 | Only line of input contains a single integer ‘n’. 20 | Output Format: 21 | Print a single integer denoting the minimal value of ‘k’. 22 | Constraints: 23 | 1 <= n <= 10^18 24 | Time Limit: 1 second 25 | Sample Input: 26 | 68 27 | Sample Output: 28 | 3 29 | Explanation: 30 | 68-3 = 65; 65/10 = 6; 65-6 = 59 31 | 59-3 = 56; 56/10 = 5; 56-5 = 51 32 | 51-3 = 48; 48/10 = 4; 48-4 = 44 33 | 44-3 = 41; 41/10 = 4; 41-4 = 37 34 | ….. 35 | ….. 36 | ….. 37 | 6-3 = 3; 3/10 = 0; 3-0 = 3 38 | 3-3 = 0; 0/10 = 0; 0-0 = 0 39 | */ 40 | 41 | 42 | #include 43 | 44 | using namespace std; 45 | 46 | long long helper(long long start, long long end, long long N, long long &ans){ 47 | // cout << "start: "< end) 51 | { 52 | return ans; 53 | } 54 | 55 | long long n = N; 56 | 57 | long long mid = (start+end)/2; 58 | //cout << "mid; "<=k && n>0){ 65 | sharma += k; 66 | n = n-k; 67 | singh += (n)/10; 68 | n = n-(n)/10; 69 | } 70 | 71 | sharma += n; 72 | // cout << "sharma: " <>n; 99 | 100 | cout << helper(1, n, n, n) << '\n'; 101 | 102 | 103 | return 0 ; 104 | 105 | 106 | 107 | } 108 | -------------------------------------------------------------------------------- /Assignment1/Power.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Find power of a number 11 | Send Feedback 12 | Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer. 13 | Input format : 14 | Two integers x and n (separated by space) 15 | Output Format : 16 | x^n (i.e. x raise to the power n) 17 | Sample Input 1 : 18 | 3 4 19 | Sample Output 1 : 20 | 81 21 | Sample Input 2 : 22 | 2 5 23 | Sample Output 2 : 24 | 32 25 | */ 26 | 27 | 28 | #include 29 | 30 | using namespace std; 31 | int power(int x, int n){ 32 | if (n==0) 33 | { 34 | return 1; 35 | } 36 | 37 | return x*power(x, n-1); 38 | 39 | 40 | } 41 | 42 | int main( int argc , char ** argv ) 43 | { 44 | ios_base::sync_with_stdio(false) ; 45 | cin.tie(NULL) ; 46 | 47 | int x, n; 48 | cin>>x>>n; 49 | 50 | cout << power(x,n) << '\n'; 51 | 52 | 53 | return 0 ; 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Assignment1/Sortingskills.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | There is a company named James Peterson & Co. The company has ‘n’ employees. 11 | The employees have skills from 0 to n-1. All the employees have distinct skills. 12 | The manager of James Peterson & Co. wants to sort the employees on the basis of their 13 | skills in ascending order. He is only allowed to swap two employees which are adjacent to each 14 | other. He is given the skills of employees in an array of size ‘n’. He can swap the skills as 15 | long as the absolute difference between their skills is 1. You need to help the manager out and 16 | tell whether it is possible to sort the skills of employees or not. 17 | Input Format: 18 | First Line will have an integer ‘t’ denoting the no. of test cases. 19 | First line of each test case contains an integer ‘n’ denoting the no. of employees in the company. 20 | Second line of each test case contains ‘n’ distinct integers in the range [0, n-1]. 21 | Output Format: 22 | For each test case, print “Yes” if it is possible to sort the skills otherwise “No”. 23 | Constraints: 24 | 1 <= t <= 10 25 | 1 <= n <= 10^5 26 | Sample Input: 27 | 2 28 | 4 29 | 1 0 3 2 30 | 3 31 | 2 1 0 32 | Sample Output: 33 | Yes 34 | No 35 | Explanation: 36 | In first T.C., [1, 0, 3, 2] -> [0, 1, 3, 2] -> [0, 1, 2, 3] 37 | In second T.C., [2, 1, 0] -> [1, 2, 0] OR [2, 1, 0] -> [2, 0, 1] So, it is impossible to sort. 38 | 39 | */ 40 | 41 | 42 | #include 43 | 44 | using namespace std; 45 | 46 | bool skillSort(vector &skills, int start, int end){ 47 | // cout << start<<" " <= end) 49 | { 50 | return 1; 51 | } 52 | 53 | int n = skills.size(); 54 | int mid = (start+end)/2; 55 | 56 | int left = skillSort(skills, start, mid); 57 | int right = skillSort(skills, mid+1, end); 58 | 59 | if (left!=1 || right != 1) 60 | { 61 | return 0; 62 | } 63 | 64 | if (skills[mid+1]>skills[mid]) 65 | { 66 | return 1; 67 | } 68 | 69 | // cout << "mid "<1) 73 | { 74 | return 0; 75 | } 76 | 77 | if (skills[mid+1]>t; 94 | 95 | while(t--){ 96 | int n; 97 | cin>>n; 98 | //cout << "n "< skills; 101 | for (int i = 0; i < n; ++i) 102 | { 103 | int temp; 104 | cin>>temp; 105 | skills.push_back(temp); 106 | } 107 | 108 | if (skillSort(skills, 0, n-1)==1) 109 | { 110 | cout << "Yes" << '\n'; 111 | }else{ 112 | cout << "No" << '\n'; 113 | } 114 | 115 | } 116 | 117 | 118 | 119 | return 0 ; 120 | 121 | 122 | 123 | } 124 | -------------------------------------------------------------------------------- /Assignment1/Test.txt: -------------------------------------------------------------------------------- 1 | 68 -------------------------------------------------------------------------------- /Assignment1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Assignment1/a.out -------------------------------------------------------------------------------- /Backtracking/Nqueens.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | The very famous Nqueen problem. Quite straightforward. 10 | */ 11 | 12 | #include 13 | 14 | using namespace std; 15 | 16 | int board[11][11]; 17 | 18 | bool isPossible(int n,int row,int col){ 19 | 20 | // Same Column 21 | for(int i=row-1;i>=0;i--){ 22 | if(board[i][col] == 1){ 23 | return false; 24 | } 25 | } 26 | //Upper Left Diagonal 27 | for(int i=row-1,j=col-1;i>=0 && j>=0 ; i--,j--){ 28 | if(board[i][j] ==1){ 29 | return false; 30 | } 31 | } 32 | 33 | // Upper Right Diagonal 34 | 35 | for(int i=row-1,j=col+1;i>=0 && j 11 | using namespace std; 12 | 13 | 14 | void printSolution(int** solution,int n){ 15 | for(int i=0;i=n || x<0 || y>=n || y<0 || maze[x][y] ==0 || solution[x][y] ==1){ 32 | return; 33 | } 34 | solution[x][y] = 1; 35 | mazeHelp(maze,n,solution,x-1,y); 36 | mazeHelp(maze,n,solution,x+1,y); 37 | mazeHelp(maze,n,solution,x,y-1); 38 | mazeHelp(maze,n,solution,x,y+1); 39 | solution[x][y] = 0; 40 | } 41 | void ratInAMaze(int maze[][20], int n){ 42 | 43 | int** solution = new int*[n]; 44 | for(int i=0;i 27 | 28 | using namespace std; 29 | 30 | int turnOffIthBit(int n, int i){ 31 | // k must be greater than 0 32 | if (i < 0) return n; 33 | 34 | // Do & of n with a number with all set bits except 35 | // the k'th bit 36 | return (n & ~(1 << (i))); 37 | 38 | } 39 | 40 | 41 | int main( int argc , char ** argv ) 42 | { 43 | ios_base::sync_with_stdio(false) ; 44 | cin.tie(NULL) ; 45 | 46 | int n, i; 47 | 48 | cin >> n >> i; 49 | 50 | cout<< turnOffIthBit(n, i) < 30 | #define endl '\n' 31 | using namespace std; 32 | class point 33 | { 34 | public: 35 | double x, y; 36 | }; 37 | class polygon 38 | { 39 | public: 40 | point*points; 41 | polygon(int number_of_points) 42 | { 43 | points=new point [number_of_points]; 44 | } 45 | 46 | }; 47 | double area(polygon p, int n) 48 | { 49 | double total_area=0; 50 | for(int i=1; i>n; 67 | polygon p(n); 68 | for(int i=0; i>p.points[i].x; 71 | } 72 | for(int i=0; i>p.points[i].y; 75 | } 76 | //area 77 | cout<<(int)area(p, n)< 47 | 48 | using namespace std; 49 | const int M = 1000000007; 50 | 51 | long long go(long long n, long long k){ 52 | //vector>> dp(n+1, vector>(k+1, vector(2,0))); 53 | 54 | long long dp[n + 1][k + 1][2]; 55 | memset(dp, 0, sizeof(dp)); 56 | dp[1][0][0] = 1; 57 | dp[1][0][1] = 1; 58 | 59 | for (int i = 2; i < n+1; ++i) 60 | { 61 | for (int j = 0; j <= k; ++j) 62 | { 63 | // if(j==0) 64 | // { 65 | // dp[i][j][1]=dp[i-1][j][0]%(1000000007); 66 | // dp[i][j][0]=(dp[i-1][j][0]+dp[i-1][j][1])%(1000000007); 67 | // } 68 | // else 69 | // { 70 | // dp[i][j][0]=(dp[i-1][j][0]+dp[i-1][j][1])%(1000000007); 71 | // dp[i][j][1]=(dp[i-1][j-1][1]+dp[i-1][j][0])%(1000000007); 72 | // } 73 | 74 | dp[i][j][0] = (dp[i-1][j][0]%M + dp[i-1][j][1]%M )%M; 75 | if(j>0) 76 | dp[i][j][1] = dp[i-1][j-1][1]%M; 77 | dp[i][j][1] = (dp[i][j][1]%M + dp[i-1][j][0]%M )%M; 78 | } 79 | } 80 | 81 | return (dp[n][k][0]) + dp[n][k][1]; 82 | 83 | } 84 | 85 | 86 | int main( int argc , char ** argv ) 87 | { 88 | ios_base::sync_with_stdio(false) ; 89 | cin.tie(NULL) ; 90 | 91 | long long p; 92 | cin>>p; 93 | while(p--){ 94 | long long s, n, k; 95 | cin>>s>>n>>k; 96 | 97 | cout << s << " " << go(n, k) << '\n'; 98 | } 99 | 100 | 101 | return 0 ; 102 | 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /DP1(Very important)/Allzeroes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/DP1(Very important)/Allzeroes -------------------------------------------------------------------------------- /DP1(Very important)/Allzeroes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | Given a n*m matrix which contains only 0s and 1s, find out the size of maximum square sub-matrix with all 0s. You need to return the size of square with all 0s. 10 | Input format : 11 | Line 1 : n and m (space separated positive integers) 12 | Next n lines : m elements of each row (separated by space). 13 | Output Format: 14 | Line 1 : Size of maximum square sub-matrix 15 | Sample Input : 16 | 3 3 17 | 1 1 0 18 | 1 1 1 19 | 1 1 1 20 | Sample Output : 21 | 1 22 | */ 23 | 24 | #include 25 | 26 | using namespace std; 27 | 28 | int lef(int** arr, int m, int n){ 29 | int ans = 0; 30 | for (int i = n-1; i >= 0; --i) 31 | { 32 | if (arr[m][i] != 0) 33 | { 34 | return ans; 35 | } 36 | 37 | if (arr[m][i] == 0) 38 | { 39 | ans++; 40 | } 41 | } 42 | 43 | return ans; 44 | 45 | } 46 | 47 | int up(int** arr, int m, int n){ 48 | int ans = 0; 49 | for (int i = m-1; i >= 0; --i) 50 | { 51 | if (arr[i][n] != 0) 52 | { 53 | return ans; 54 | } 55 | 56 | if (arr[i][n] == 0) 57 | { 58 | ans++; 59 | } 60 | } 61 | 62 | return ans; 63 | 64 | } 65 | 66 | 67 | int findMaxSquareWithAllZeros(int** arr, int m, int n){ 68 | 69 | vector> dp(m, (vector(n, 0))); 70 | 71 | for (int i = 0; i < n; ++i) 72 | { 73 | if (arr[0][i] == 0) 74 | { 75 | dp[0][i] = 1; 76 | }else{ 77 | dp[0][i] = 0; 78 | } 79 | } 80 | 81 | for (int j = 0; j < m; ++j) 82 | { 83 | dp[j][0] = (arr[j][0] == 0); 84 | } 85 | 86 | for (int i = 1; i < m; ++i) 87 | { 88 | for (int j = 1; j < n; ++j) 89 | { 90 | if (arr[i][j] == 0) 91 | { 92 | int dig = dp[i-1][j-1]; 93 | int upper = up(arr, i, j); 94 | int left = lef(arr, i, j); 95 | int z = min(dig, min(upper, left)); 96 | dp[i][j] = z+1; 97 | 98 | }else{ 99 | dp[i][j] = 0; 100 | } 101 | 102 | } 103 | } 104 | 105 | int max = 0; 106 | for (int i = 0; i < m; ++i) 107 | { 108 | for (int j = 0; j < n; ++j) 109 | { 110 | max = std::max(max, dp[i][j]); 111 | } 112 | } 113 | 114 | return max; 115 | 116 | 117 | } 118 | int main( int argc , char ** argv ) 119 | { 120 | ios_base::sync_with_stdio(false) ; 121 | cin.tie(NULL) ; 122 | 123 | 124 | 125 | 126 | return 0 ; 127 | 128 | 129 | 130 | } 131 | -------------------------------------------------------------------------------- /DP1(Very important)/AlphaCode-Question.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages: 11 | Alice: “Let’s just use a very simple code: We’ll assign ‘A’ the code word 1, ‘B’ will be 2, and so on 12 | down to ‘Z’ being assigned 26.” 13 | 14 | Bob: “That’s a stupid code, Alice. Suppose I send you the word ‘BEAN’ encoded as 25114. You could 15 | decode that in many different ways!” 16 | 17 | Alice: “Sure you could, but what words would you get? Other than ‘BEAN’, you’d get ‘BEAAD’, 18 | ‘YAAD’, ‘YAN’, ‘YKD’ and ‘BEKD’. I think you would be able to figure out the correct decoding. 19 | And why would you send me the word ‘BEAN’ anyway?” 20 | 21 | Bob: “OK, maybe that’s a bad example, but I bet you that if you got a string of length 5000 there 22 | would be tons of different decodings and with that many you would find at least two different 23 | ones that would make sense.” 24 | 25 | Alice: “How many different decodings?” 26 | 27 | Bob: “Jillions!” 28 | For some reason, Alice is still unconvinced by Bob’s argument, so she requires a program that 29 | will determine how many decodings there can be for a given string using her code. 30 | Input 31 | Input will consist of multiple input sets. Each set will consist of a single line of at most 5000 32 | digits representing a valid encryption (for example, no line will begin with a 0). There will be 33 | no spaces between the digits. An input line of ‘0’ will terminate the input and should not be 34 | processed. 35 | Output 36 | For each input set, output the number of possible decodings for the input string. Prlong long your 37 | answer taking modulo "10^9+7" 38 | Sample Input: 39 | 25114 40 | 1111111111 41 | 3333333333 42 | 0 43 | Sample Output: 44 | 6 45 | 89 46 | 1 47 | 48 | */ 49 | 50 | #include 51 | 52 | using namespace std; 53 | 54 | long long go(string a){ 55 | long long n = a.size(); 56 | //cout << a[n] << '\n'; 57 | 58 | vector dp(n+2,0); 59 | 60 | dp[n] = 1; 61 | if(a.at(n-1)=='0'){ 62 | dp[n] = 0; 63 | } 64 | dp[n+1] = 0; 65 | 66 | for (long long i = n-1; i > 0; --i) 67 | { 68 | // 1) if a[i-1] is in range (1 to 9) 69 | if (a.at(i-1) == '0') 70 | dp.at(i)=0; 71 | else 72 | dp.at(i)=dp.at(i+1); 73 | 74 | // 2) if a[i-1]*10+a[i] is in range (10 to 26) 75 | 76 | if ((a[i-1]-'0')*10+(a[i]-'0')<=26 && (a[i-1]-'0')*10+(a[i]-'0')>=10) 77 | { 78 | if(i!=n-1){ 79 | dp.at(i) += dp.at(i+2); 80 | }else{ 81 | dp.at(i)++; 82 | } 83 | dp.at(i)%=1000000007; 84 | } 85 | 86 | // both 1) and 2) conditions calculate answer for (1 to 9) and (10 to 26) 87 | // overall answer for (1 to 26) is calculated 88 | } 89 | 90 | return dp.at(1)%1000000007;; 91 | 92 | } 93 | 94 | 95 | int main( int argc , char ** argv ) 96 | { 97 | ios_base::sync_with_stdio(false) ; 98 | cin.tie(NULL) ; 99 | 100 | std::vector v; 101 | string a; 102 | cin>>a; 103 | 104 | while(a!="0"){ 105 | v.push_back(a); 106 | cin>>a; 107 | } 108 | 109 | for (long long i = 0; i < v.size(); ++i) 110 | { 111 | cout << go(v.at(i)) << '\n'; 112 | } 113 | return 0 ; 114 | } -------------------------------------------------------------------------------- /DP1(Very important)/Boredom.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Gary is bored and wants to play an interesting but tough game . So he figured out a new board game called "destroy the neighbours" . In this game there are N integers on a board. In one move, he can pick any integer x from the board and then all the integers with value x+1 or x-1 gets destroyed .This move will give him x points. 12 | He plays the game until the board becomes empty . But as he want show this game to his friend Steven, he wants to learn techniques to maximise the points to show off . Can you help Gary in finding out the maximum points he receive grab from the game ? 13 | Input Format : 14 | Line 1 : Integer N 15 | Line 2 : A list of N integers 16 | Output Format : 17 | Maximum points Gary can recieve from the Game setup 18 | Constraints : 19 | 1<=N<=10^5 20 | 1<=A[i]<=1000 21 | Sample Input : 22 | 2 23 | 1 2 24 | Sample Output : 25 | 2 26 | */ 27 | #include 28 | 29 | using namespace std; 30 | 31 | int n; 32 | vectorA; 33 | int freq[1002],dp[1002]; 34 | int solve(int n,vectorA){ 35 | 36 | for(int i=0;i>n; 59 | for(int i=0;i>x; 63 | A.push_back(x); 64 | } 65 | cout< 33 | 34 | using namespace std; 35 | 36 | class city{ 37 | public: 38 | int x; 39 | int y; 40 | int happiness; 41 | }; 42 | double distance(city a, city b){ 43 | double ans = 0; 44 | long long X = (long long)pow(a.x-b.x, 2); 45 | long long Y = (long long)pow(a.y-b.y, 2); 46 | 47 | ans = (double)sqrt(X+Y); 48 | return ans; 49 | } 50 | double go(vector v){ 51 | int n = v.size(); 52 | vector dp(n, 0); 53 | dp[0] = v[0].happiness; 54 | 55 | for (int i = 1; i < n; ++i) 56 | { 57 | double temp = -1e9; 58 | for (int j = 0; j >n; 79 | std::vector v; 80 | for (int i = 0; i < n; ++i) 81 | { 82 | int a; 83 | int b; 84 | int c; 85 | cin>>a>>b>>c; 86 | city temp; 87 | temp.x = a; 88 | temp.y = b; 89 | temp.happiness = c; 90 | v.push_back(temp); 91 | } 92 | 93 | //cout << distance(v[0], v[1]) << '\n'; 94 | 95 | cout < 29 | 30 | using namespace std; 31 | 32 | int getMaxMoney(int arr[], int n){ 33 | std::vector dp(n, 0); 34 | 35 | if (n==1) 36 | { 37 | return arr[0]; 38 | } 39 | if (n==2) 40 | { 41 | return max(arr[0], arr[1]); 42 | } 43 | 44 | dp[0] = arr[0]; 45 | dp[1] = max(arr[1], arr[0]); 46 | 47 | for (int i = 2; i < n; ++i) 48 | { 49 | dp[i] =max(dp[i-1], arr[i] + dp[i-2]); 50 | } 51 | 52 | return *max_element(dp.begin(), dp.end()); 53 | 54 | } 55 | 56 | int main( int argc , char ** argv ) 57 | { 58 | ios_base::sync_with_stdio(false) ; 59 | cin.tie(NULL) ; 60 | 61 | int n; 62 | cin >> n; 63 | int arr[10000]; 64 | for(int i=0; i> arr[i]; 66 | } 67 | cout << getMaxMoney(arr, n); 68 | 69 | 70 | 71 | return 0 ; 72 | 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /DP1(Very important)/Maximumsum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | PROBLEM STATEMENT 9 | Given a 2D array, find the maximum sum rectangle in it. In other words find maximum sum over all rectangles in the matrix. 10 | Input 11 | First line contains 2 numbers n and m denoting number of rows and number of columns. Next n lines contain m space separated integers denoting elements of matrix nxm. 12 | Output 13 | Output a single integer, maximum sum rectangle. 14 | Constraints 15 | 1<=n,m<=100 16 | Sample Input 17 | 4 5 18 | 1 2 -1 -4 -20 19 | -8 -3 4 2 1 20 | 3 8 10 1 3 21 | -4 -1 1 7 -6 22 | Sample Output 23 | 29 24 | */ 25 | 26 | #include 27 | 28 | using namespace std; 29 | 30 | int kadane(vector v){ 31 | int n = v.size(); 32 | int maxtillnow = 0; 33 | int maxendinghere = 0; 34 | 35 | 36 | for (int i = 0; i < n; ++i) 37 | { 38 | maxendinghere = max(v[i], v[i]+maxendinghere); 39 | 40 | maxtillnow = max(maxtillnow, maxendinghere); 41 | 42 | } 43 | 44 | return maxtillnow; 45 | 46 | } 47 | 48 | int go(vector> arr, int m, int n){ 49 | 50 | int ans = INT_MIN; 51 | 52 | for (int i = 0; i < n; ++i) 53 | { 54 | std::vector sum(m, 0); 55 | 56 | for (int j = i; j < n; ++j) 57 | { 58 | for (int k = 0; k < m; ++k) 59 | { 60 | sum[k] += arr[k][j]; 61 | } 62 | 63 | int temp = kadane(sum); 64 | ans = max(ans, temp); 65 | } 66 | } 67 | 68 | return ans; 69 | 70 | } 71 | 72 | 73 | int main( int argc , char ** argv ) 74 | { 75 | ios_base::sync_with_stdio(false) ; 76 | cin.tie(NULL) ; 77 | 78 | int m, n; 79 | cin>>m>>n; 80 | 81 | vector> matrix(m, vector(n, 0)); 82 | for (int i = 0; i < m; ++i) 83 | { 84 | for (int j = 0; j < n; ++j) 85 | { 86 | cin>>matrix[i][j]; 87 | } 88 | } 89 | 90 | cout << go(matrix, m, n) < 46 | 47 | using namespace std; 48 | 49 | 50 | int main( int argc , char ** argv ) 51 | { 52 | ios_base::sync_with_stdio(false) ; 53 | cin.tie(NULL) ; 54 | 55 | int n, m; 56 | cin>>n>>m; 57 | vector s(n+1, 0); 58 | vector e(n+1, 0); 59 | 60 | vector buffer(n+1, 0); 61 | 62 | 63 | for (int i = 0; i < m; ++i) 64 | { 65 | int a, b; 66 | cin>>a>>b; 67 | 68 | s[a]++; 69 | e[b]++; 70 | } 71 | 72 | for (int i = 1; i < n+1; ++i) 73 | { 74 | buffer[i] = s[i] - e[i-1] + buffer[i-1]; 75 | } 76 | 77 | //Will store ki i coins kitne boxes me hai 78 | vector coins(n+1,0); 79 | for (int i = 1; i < n+1; ++i) 80 | { 81 | coins[buffer[i]]++; 82 | } 83 | 84 | for (int i = n-1; i >= 0; --i) 85 | { 86 | coins[i] += coins[i+1]; 87 | } 88 | 89 | int q; 90 | cin>>q; 91 | 92 | for (int i = 0; i < q; ++i) 93 | { 94 | int temp; 95 | cin>>temp; 96 | cout << coins[temp] << '\n'; 97 | } 98 | 99 | 100 | 101 | return 0 ; 102 | 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /DP1(Very important)/SamAndSubstring.cpp: -------------------------------------------------------------------------------- 1 | /*Hackerrank problem "sam and substring"*/ 2 | 3 | int substrings(string n) { 4 | 5 | long long total_sum = n[0]-'0'; 6 | long long last_value = n[0] - '0'; 7 | 8 | for(int i=1; i 29 | 30 | using namespace std; 31 | int go(vector v){ 32 | int n = v.size(); 33 | vector> dp(101, vector(n, 0)); 34 | 35 | for (int i = 0; i < n; ++i) 36 | { 37 | dp[v[i]][i] = 1; 38 | } 39 | 40 | for (int i = 0; i < n; ++i) 41 | { 42 | for (int j = 0; j < i; ++j) 43 | { 44 | if (v[i]>v[j]) 45 | { 46 | for (int k = 1; k < 101; ++k) 47 | { 48 | int gcn = __gcd(k, v[i]); 49 | dp[gcn][i] = (dp[gcn][i]+dp[k][j])%1000000007; 50 | } 51 | } 52 | } 53 | } 54 | 55 | int ans = 0; 56 | 57 | // for (int i = 1; i < 101; ++i) 58 | // { 59 | // for (int j = 0; j < n; ++j) 60 | // { 61 | // cout<>n; 86 | vector v(n, 0); 87 | 88 | for (int i = 0; i < n; ++i) 89 | { 90 | cin>>v[i]; 91 | } 92 | 93 | cout << go(v) << '\n'; 94 | 95 | 96 | 97 | return 0 ; 98 | 99 | 100 | 101 | } 102 | -------------------------------------------------------------------------------- /DP1(Very important)/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/DP1(Very important)/a.out -------------------------------------------------------------------------------- /DP1(Very important)/angrychildren: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/DP1(Very important)/angrychildren -------------------------------------------------------------------------------- /DP1(Very important)/angrychildren.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has N packets of candies and would like to distribute one packet to each of the K children in the village (each packet may contain different number of candies). To avoid a fight between the children, he would like to pick K out of N packets such that the unfairness is minimized. 10 | Suppose the K packets have (x1, x2, x3,....xk) candies in them, where xi denotes the number of candies in the ith packet, then we define unfairness as 11 | unfairness=0; 12 | for(i=0;i 46 | 47 | using namespace std; 48 | 49 | long go(vector candies, long n, long k){ 50 | //size is n+1 51 | 52 | sort(candies.begin(), candies.end()); 53 | 54 | long sum = 0; 55 | long buffer = 0; 56 | for (long i = 1; i <=k; ++i) 57 | { 58 | sum += i*candies[i]; 59 | sum -= (k-i+1)*candies[i]; 60 | 61 | buffer += candies[i]; 62 | } 63 | 64 | 65 | long ans = sum; 66 | for (long i = 2; i <= n-k+1; ++i) 67 | { 68 | sum = sum - 2*(buffer - candies[i-1])+(k-1)*candies[i-1]+(k-1)*candies[i+k-1]; 69 | 70 | buffer = buffer - candies[i-1]+candies[i+k-1]; 71 | 72 | 73 | ans = min(ans, sum); 74 | } 75 | 76 | return ans; 77 | } 78 | 79 | int main( int argc , char ** argv ) 80 | { 81 | ios_base::sync_with_stdio(false) ; 82 | cin.tie(NULL) ; 83 | 84 | long n, k; 85 | cin>>n>>k; 86 | 87 | vector candies(n+1, 0); 88 | for (int i = 1; i <= n; ++i) 89 | { 90 | cin>>candies[i]; 91 | } 92 | 93 | cout << go(candies, n, k) << '\n'; 94 | 95 | return 0 ; 96 | 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /DP1(Very important)/coinchange.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | You are given an infinite supply of coins of each of denominations D = {D0, D1, D2, D3, ...... Dn-1}. You need to figure out the total number of ways W, in which you can make change for Value V using coins of denominations D. 11 | Note : Return 0, if change isn't possible. 12 | Input Format 13 | Line 1 : Integer n i.e. total number of denominations 14 | Line 2 : N integers i.e. n denomination values 15 | Line 3 : Value V 16 | Output Format 17 | Line 1 : Number of ways i.e. W 18 | Constraints : 19 | 1<=n<=10 20 | 1<=V<=1000 21 | Sample Input 1 : 22 | 3 23 | 1 2 3 24 | 4 25 | Sample Output 26 | 4 27 | Sample Output Explanation : 28 | Number of ways are - 4 total i.e. (1,1,1,1), (1,1, 2), (1, 3) and (2, 2). 29 | 30 | 31 | */ 32 | 33 | 34 | #include 35 | 36 | using namespace std; 37 | int countWaysToMakeChange(int S[], int m, int n){ 38 | int i, j, x, y; 39 | 40 | // We need n+1 rows as the table 41 | // is constructed in bottom up 42 | // manner using the base case 0 43 | // value case (n = 0) 44 | int table[n + 1][m]; 45 | 46 | // Fill the enteries for 0 47 | // value case (n = 0) 48 | for (i = 0; i < m; i++) 49 | table[0][i] = 1; 50 | 51 | // Fill rest of the table entries 52 | // in bottom up manner 53 | for (i = 1; i < n + 1; i++) 54 | { 55 | for (j = 0; j < m; j++) 56 | { 57 | // Count of solutions including S[j] 58 | x = (i-S[j] >= 0) ? table[i - S[j]][j] : 0; 59 | 60 | // Count of solutions excluding S[j] 61 | y = (j >= 1) ? table[i][j - 1] : 0; 62 | 63 | // total count 64 | table[i][j] = x + y; 65 | } 66 | } 67 | return table[n][m - 1]; 68 | 69 | } 70 | 71 | 72 | int main( int argc , char ** argv ) 73 | { 74 | ios_base::sync_with_stdio(false) ; 75 | cin.tie(NULL) ; 76 | int numDenominations; 77 | cin >> numDenominations; 78 | int* denominations = new int[numDenominations]; 79 | for(int i = 0; i < numDenominations; i++){ 80 | cin >> denominations[i]; 81 | } 82 | int value; 83 | cin >> value; 84 | cout << countWaysToMakeChange(denominations, numDenominations, value)< 38 | 39 | using namespace std; 40 | 41 | int longestBitonicSubarray(int *arr, int n) { 42 | int i, j; 43 | 44 | /* Allocate memory for LIS[] and initialize LIS values as 1 for 45 | all indexes */ 46 | int *lis = new int[n]; 47 | for (i = 0; i < n; i++) 48 | lis[i] = 1; 49 | 50 | /* Compute LIS values from left to right */ 51 | for (i = 1; i < n; i++) 52 | for (j = 0; j < i; j++) 53 | if (arr[i] > arr[j] && lis[i] < lis[j] + 1) 54 | lis[i] = lis[j] + 1; 55 | 56 | /* Allocate memory for lds and initialize LDS values for 57 | all indexes */ 58 | int *lds = new int [n]; 59 | for (i = 0; i < n; i++) 60 | lds[i] = 1; 61 | 62 | /* Compute LDS values from right to left */ 63 | for (i = n-2; i >= 0; i--) 64 | for (j = n-1; j > i; j--) 65 | if (arr[i] > arr[j] && lds[i] < lds[j] + 1) 66 | lds[i] = lds[j] + 1; 67 | 68 | 69 | /* Return the maximum value of lis[i] + lds[i] - 1*/ 70 | int max = lis[0] + lds[0] - 1; 71 | for (i = 1; i < n; i++) 72 | if (lis[i] + lds[i] - 1 > max) 73 | max = lis[i] + lds[i] - 1; 74 | return max; 75 | 76 | } 77 | int main( int argc , char ** argv ) 78 | { 79 | ios_base::sync_with_stdio(false) ; 80 | cin.tie(NULL) ; 81 | int n, input[100000]; 82 | cin>>n; 83 | for(int i=0; i>input[i]; 85 | } 86 | cout< 33 | 34 | using namespace std; 35 | 36 | int getMin(int *arr, int n){ 37 | //left case 38 | 39 | std::vector dp(n, 0); 40 | dp[0] = 1; 41 | for (int i = 1; i < n; ++i) 42 | { 43 | if(arr[i]>arr[i-1]) 44 | dp[i] = dp[i-1]+1; 45 | else 46 | dp[i] = 1; // made changes here 47 | } 48 | 49 | 50 | dp[n-1] = max(dp[n-1], 1); 51 | 52 | for (int i = n-2; i >= 0; --i) 53 | { 54 | if(arr[i+1]> n; 77 | int *arr = new int[n]; 78 | for(int i = 0; i < n; i++){ 79 | cin >> arr[i]; 80 | } 81 | 82 | cout << getMin(arr, n); 83 | delete []arr; 84 | 85 | 86 | 87 | return 0 ; 88 | 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /DP1(Very important)/mincount.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given an integer N, find and return the count of minimum numbers, sum of whose squares is equal to N. 11 | That is, if N is 4, then we can represent it as : {1^2 + 1^2 + 1^2 + 1^2} and {2^2}. Output will be 1, as 1 is the minimum count of numbers required. 12 | Note : x^y represents x raise to the power y. 13 | Input Format : 14 | Integer N 15 | Output Format : 16 | Required minimum count 17 | Constraints : 18 | 1 <= N <= 1000 19 | Sample Input 1 : 20 | 12 21 | Sample Output 1 : 22 | 3 23 | Sample Output 1 Explanation : 24 | 12 can be represented as : 25 | 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 26 | 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 2^2 27 | 1^1 + 1^1 + 1^1 + 1^1 + 2^2 + 2^2 28 | 2^2 + 2^2 + 2^2 29 | As we can see, the output should be 3. 30 | Sample Input 2 : 31 | 9 32 | Sample Output 2 : 33 | 1 34 | */ 35 | 36 | 37 | #include 38 | 39 | using namespace std; 40 | 41 | int minCount(int n){ 42 | //non Recursive solution 43 | 44 | std::vector dp(n+1, 0); 45 | dp[0] = 0; 46 | dp[1] = 1; 47 | for (int i = 1; i <= n; ++i) 48 | { 49 | int count = INT_MAX; 50 | for (int j = 1; j <= i; ++j) 51 | { 52 | if (i-j*j<0) 53 | { 54 | break; 55 | } 56 | 57 | count = min(count, dp[i-j*j]); 58 | } 59 | dp[i] = 1+count; 60 | } 61 | 62 | return dp[n]; 63 | 64 | } 65 | 66 | int main( int argc , char ** argv ) 67 | { 68 | ios_base::sync_with_stdio(false) ; 69 | cin.tie(NULL) ; 70 | 71 | int num; 72 | cin >> num; 73 | cout << minCount(num)< 48 | 49 | using namespace std; 50 | 51 | 52 | int main( int argc , char ** argv ) 53 | { 54 | ios_base::sync_with_stdio(false) ; 55 | cin.tie(NULL) ; 56 | 57 | int m, n; 58 | cin>>m>>n; 59 | vector oldinput(n,0); 60 | vector newinput(n,0); 61 | 62 | vector oldsortedfrom(n,0); 63 | vector newsortedfrom(n,0); 64 | 65 | vector rowsortedfrom(m, 0); 66 | //cout <>newinput[j]; 75 | 76 | if(oldinput[j]<=newinput[j]) 77 | newsortedfrom[j] = oldsortedfrom[j]; 78 | else 79 | newsortedfrom[j] = i; 80 | 81 | minimum = min(minimum, newsortedfrom[j]); 82 | } 83 | // for(int j=0;j>q; 97 | while(q--){ 98 | int left, right; 99 | cin>>left>>right; 100 | 101 | if(rowsortedfrom[right-1]<=left-1) 102 | cout << "Yes" << '\n'; 103 | else 104 | cout << "No" << '\n'; 105 | } 106 | 107 | 108 | return 0 ; 109 | 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /DP1(Very important)/staircase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | PROBLEM STATEMENT 9 | A child is running up a staircase with n steps and can hop either 1 step, 2 steps or 3 steps 10 | at a time. Implement a method to count how many possible ways the child can run up to the stairs. 11 | You need to return all possible number of ways. 12 | Time complexity of your code should be O(n). 13 | Input format : 14 | Integer n (No. of steps) 15 | Constraints : 16 | n <= 70 17 | Sample Input 1: 18 | 4 19 | Sample Output 1: 20 | 7 21 | 22 | */ 23 | 24 | #include 25 | 26 | using namespace std; 27 | long staircase(int n){ 28 | long* arr = new long[n+1]; 29 | arr[0] = 0; 30 | arr[1] = 1; 31 | arr[2] = 2; 32 | arr[3] = 4; 33 | 34 | for (int i = 4; i < n+1; ++i) 35 | { 36 | arr[i] = arr[i-1] + arr[i-2] + arr[i-3]; 37 | } 38 | 39 | return arr[n]; 40 | 41 | } 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | 48 | int n; 49 | cin >> n ; 50 | cout << staircase(n) << endl; 51 | 52 | 53 | return 0 ; 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /DPandbitmasking/Candy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Gary is a teacher at XYZ school. To reward his N students he bought a packet of N candies all with different flavours. But the problem is some students like certain flavour while some doesn't. Now Gary wants to know the number of ways he can distribute these N candies to his N students such that every student gets exactly one candy he likes. 11 | Input Format : 12 | Line 1 : An integer N (1<= N <= 16) denoting number of students and candies. 13 | Next N lines : N integers describing the preferences of one student. 1 at i'th (0 <= i < N) position denotes that this student likes i'th candy , 0 means he doesn't. 14 | Assume input to be 0-indexed based. 15 | Output Format : 16 | Return the number of ways Gary can distribute these N candies to his N students such that every student gets exactly one candy he likes.`` 17 | Sample Input: 18 | 3 19 | 1 1 1 20 | 1 1 1 21 | 1 1 1 22 | Sample Output: 23 | 6 24 | */ 25 | 26 | #include 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef unordered_map umapii; 31 | typedef unordered_map umapib; 32 | typedef unordered_map umapsi; 33 | typedef unordered_map umapss; 34 | typedef map mapsi; 35 | typedef map, int> mappiii; 36 | typedef map mapii; 37 | typedef pair pii; 38 | typedef pair pll; 39 | typedef unordered_set useti; 40 | 41 | #define uset unordered_set 42 | #define it iterator 43 | #define mp make_pair 44 | #define pb push_back 45 | #define all(x) (x).begin(), (x).end() 46 | #define f first 47 | #define s second 48 | #define MOD 1000000007 49 | #define MAXN 16 50 | 51 | int go(int like[][MAXN], int n, int student, int mask, int* dp){ 52 | 53 | int ans = 0; 54 | 55 | if (student>=n) 56 | { 57 | return 1; 58 | } 59 | 60 | if (dp[mask]!=-1) 61 | { 62 | return dp[mask]; 63 | } 64 | 65 | for (int i = 0; i < n; ++i) 66 | { 67 | if (!(mask&(1<>n; 94 | for(int i = 0; i < n ; i++){ 95 | for(int j = 0; j < n; j++){ 96 | cin>>like[i][j]; 97 | } 98 | } 99 | cout< String 't' is lexicographical larger than string 's'. 12 | -> When you write both 's' and 't' in the reverse order, 't' is still lexicographical larger than 's'. 13 | Find out number of such strings 't'. As the answer could be very large, take modulo 10^9 + 7. 14 | */ 15 | 16 | #include 17 | using namespace std; 18 | 19 | typedef long long ll; 20 | typedef unordered_map umapii; 21 | typedef unordered_map umapib; 22 | typedef unordered_map umapsi; 23 | typedef unordered_map umapss; 24 | typedef map mapsi; 25 | typedef map, int> mappiii; 26 | typedef map mapii; 27 | typedef pair pii; 28 | typedef pair pll; 29 | typedef unordered_set useti; 30 | 31 | #define uset unordered_set 32 | #define it iterator 33 | #define mp make_pair 34 | #define pb push_back 35 | #define all(x) (x).begin(), (x).end() 36 | #define f first 37 | #define s second 38 | #define MOD 1000000007 39 | 40 | #include 41 | using namespace std; 42 | long long ans[100001],mod=1e9+7; 43 | int go(string s){ 44 | string str=""; 45 | int n=s.length(); 46 | for(int i=n-1;i>=1;i--) 47 | str.push_back(s.at(i)); 48 | for(int i=0;i>s; 77 | int ans = countStrings(s); 78 | cout<&v) 32 | { 33 | if(!(mask&(mask-1))&&mask) 34 | // everything was recongnised successfully when mask contains only one bit set 35 | return 0; 36 | 37 | if(pos == -1||mask == 0) 38 | return 100000; 39 | 40 | if(dp[pos][mask]) 41 | return dp[pos][mask]; 42 | 43 | int newmask1 = 0,newmask2 = 0,touches = 0; 44 | //if we touch the v[any][pos],mask will split. 45 | for(int i = 0;i>i)&1) 48 | { 49 | touches++; 50 | if(v[i][pos] == '0') 51 | { 52 | newmask1|= (1< v) 64 | { 65 | return find_touches(v[0].size()-1,(1< 14 | using namespace std; 15 | 16 | typedef long long ll; 17 | typedef unordered_map umapii; 18 | typedef unordered_map umapib; 19 | typedef unordered_map umapsi; 20 | typedef unordered_map umapss; 21 | typedef map mapsi; 22 | typedef map, int> mappiii; 23 | typedef map mapii; 24 | typedef pair pii; 25 | typedef pair pll; 26 | typedef unordered_set useti; 27 | 28 | #define uset unordered_set 29 | #define it iterator 30 | #define mp make_pair 31 | #define pb push_back 32 | #define all(x) (x).begin(), (x).end() 33 | #define f first 34 | #define s second 35 | #define MOD 1000000007 36 | 37 | 38 | int mincost(int cost[4][4], int n, int p, int mask, int* dp){ 39 | 40 | if (p>=n) 41 | { 42 | return 0; 43 | } 44 | 45 | if (dp[mask]!=INT_MAX) 46 | { 47 | return dp[mask]; 48 | } 49 | 50 | int minimum = INT_MAX; 51 | for (int i = 0; i < n; ++i) 52 | { 53 | if (!(mask&(1< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | #define rep(i,a,b) for(int i = a; i < b; i++) 13 | #define S(x) scanf("%d",&x) 14 | #define P(x) printf("%d\n",x) 15 | typedef long long int LL; 16 | typedef pair pii; 17 | const int MAXN = 2001; 18 | LL P[11] = {1,2,3,5,7,11,13,17,19,23,29}; 19 | LL dp[2][MAXN][MAXN]; 20 | int profit[MAXN]; 21 | int weight[MAXN]; 22 | vector v; 23 | int main() { 24 | int N,W; 25 | scanf("%d%d",&N,&W); 26 | assert(N >= 1 && N <= 2000); 27 | assert(W >= 1 && W <= 2000); 28 | rep(i,1,N+1) { 29 | scanf("%d%d",&profit[i], &weight[i]); 30 | assert(profit[i] >= 1 && profit[i] <= 1000000000); 31 | assert(weight[i] >= 1 && weight[i] <= 2000); 32 | v.push_back(make_pair(profit[i], weight[i])); 33 | } 34 | sort(v.begin(), v.end()); 35 | rep(i,1,N+1) { 36 | profit[i] = v[i-1].first; 37 | weight[i] = v[i-1].second; 38 | } 39 | rep(p,0,11) rep(i,1,N+1) rep(j,1,W+1) { 40 | if(!p) { 41 | dp[0][i][j] = max(dp[0][i-1][j], dp[0][i][j-1]); 42 | if(j >= weight[i]) 43 | dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][j-weight[i]]+profit[i]); 44 | } else { 45 | int cur = p&1; 46 | int prev = 1-cur; 47 | dp[cur][i][j] = dp[prev][i][j]; 48 | dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i-1][j]); 49 | dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i][j-1]); 50 | if(j >= weight[i]) { 51 | dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i-1][j-weight[i]]+profit[i]); 52 | dp[cur][i][j] = max(dp[cur][i][j], dp[prev][i-1][j-weight[i]]+P[p]*profit[i]); 53 | } 54 | } 55 | } 56 | cout << dp[0][N][W] << endl; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /DPandbitmasking/Stringmaker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | According to Ancient Ninjas , string making is an art form . There are various methods of string making , one of them uses previously generated strings to make the new one . Suppose you have two strings A and B , to generate a new string C , you can pick a subsequence of characters from A and a subsequence of characters from B and then merge these two subsequences to form the new string. 11 | Though while merging the two subsequences you can not change the relative order of individual subsequences. What this means is - suppose there two characters Ai and Aj in the subsequence chosen from A , where i < j , then after merging if i acquires position k and j acquires p then k

31 | using namespace std; 32 | 33 | typedef long long ll; 34 | #define MOD 1000000007 35 | ll dp[100][100][100]; 36 | 37 | int memsolver(string a,string b,string c){ 38 | ll x = a.size(); 39 | ll y = b.size(); 40 | ll z = c.size(); 41 | 42 | ll ans = 0; 43 | 44 | if (z == 0) 45 | { 46 | return 1; 47 | } 48 | 49 | if (x<=0 && y<=0) 50 | { 51 | return 0; 52 | } 53 | 54 | if (dp[x][y][z]!=-1) 55 | { 56 | return dp[x][y][z]; 57 | } 58 | 59 | for (ll i = 0; i < a.size(); ++i) 60 | { 61 | if (a[i]==c[0]) 62 | { 63 | ans+=memsolver(a.substr(i+1), b, c.substr(1)); 64 | } 65 | } 66 | 67 | 68 | 69 | for (ll i = 0; i < b.size(); ++i) 70 | { 71 | if (b[i] == c[0]) 72 | { 73 | ans+=memsolver(a, b.substr(i+1), c.substr(1)); 74 | } 75 | } 76 | 77 | dp[x][y][z] = ans%MOD; 78 | return ans%MOD; 79 | } 80 | 81 | int solve(string a,string b,string c) 82 | { 83 | memset(dp, -1, sizeof(dp)); 84 | return memsolver(a, b, c); 85 | } 86 | 87 | int main( int argc , char ** argv ) 88 | { 89 | ios_base::sync_with_stdio(false) ; 90 | cin.tie(NULL) ; 91 | 92 | string a,b,c; 93 | cin>>a>>b>>c; 94 | 95 | cout< 24 | using namespace std; 25 | 26 | int minSalary(int *assistant, int *captain, int n, int x, int **memo){ 27 | if(n == 0) return 0; 28 | 29 | if(memo[n][x] != -1) return memo[n][x]; 30 | 31 | if(x == 0){ 32 | int answer = assistant[0]+minSalary(assistant+1, captain+1, n-1, x+1, memo); 33 | memo[n][x] = answer; 34 | return answer; 35 | } 36 | else if(x == n){ 37 | int answer = captain[0]+minSalary(assistant+1, captain+1, n-1, x-1, memo); 38 | memo[n][x] = answer; 39 | return answer; 40 | } 41 | else{ 42 | int a = assistant[0]+minSalary(assistant+1, captain+1, n-1, x+1, memo); 43 | int b = captain[0]+minSalary(assistant+1, captain+1, n-1, x-1, memo); 44 | int answer = min(a, b); 45 | memo[n][x] = answer; 46 | return answer; 47 | } 48 | } 49 | 50 | int main(){ 51 | int n; 52 | cin >> n; 53 | 54 | int *captain = new int[n]; 55 | int *assistant = new int[n]; 56 | for(int i = 0; i < n; i++){ 57 | cin >> captain[i] >> assistant[i]; 58 | } 59 | 60 | int **memo = new int *[n+1]; 61 | for(int i = 0; i <= n; i++){ 62 | memo[i] = new int[(n/2)+1]; 63 | for(int j = 0; j <= n/2; j++) memo[i][j] = -1; 64 | } 65 | 66 | cout << minSalary(assistant, captain, n, 0, memo) << endl; 67 | } 68 | -------------------------------------------------------------------------------- /Dynamic programming 2/Editdistance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | Given two strings s and t of lengths m and n respectively, find the Edit Distance between the 11 | strings. Edit Distance of two strings is minimum number of steps required to make one string 12 | equal to other. In order to do so you can perform following three operations only : 13 | 1. Delete a character 14 | 15 | 2. Replace a character with another one 16 | 17 | 3. Insert a character 18 | Note - Strings don't contain spaces 19 | Input Format : 20 | Line 1 : String s 21 | Line 2 : String t 22 | Output Format : 23 | Line 1 : Edit Distance value 24 | Constraints 25 | 1<= m,n <= 20 26 | Sample Input 1 : 27 | abc 28 | dc 29 | Sample Output 1 : 30 | 2 31 | */ 32 | 33 | #include 34 | 35 | using namespace std; 36 | 37 | int editDistance(string s1, string s2){ 38 | int m = s1.size(); 39 | int n = s2.size(); 40 | 41 | vector>dp(m+1, vector(n+1, 0)); 42 | 43 | for (int i = 0; i < m+1; ++i) 44 | { 45 | dp[i][n] = m-i; 46 | } 47 | 48 | 49 | for (int i = 0; i < n+1; ++i) 50 | { 51 | dp[m][i] = n-i; 52 | } 53 | 54 | //dp[m][n] = 0; 55 | 56 | for (int i = m-1; i >= 0; --i) 57 | { 58 | for (int j = n-1; j >= 0; --j) 59 | { 60 | if(s1[i]==s2[j]){ 61 | dp[i][j] = dp[i+1][j+1]; 62 | }else{ 63 | 64 | dp[i][j] = min(1+dp[i+1][j+1], min(1+dp[i+1][j], 1+dp[i][j+1])); 65 | } 66 | } 67 | } 68 | 69 | // for (int i = 0; i < m+1; ++i) 70 | // { 71 | // for (int j = 0; j < n+1; ++j) 72 | // { 73 | // cout<> s1; 94 | cin >> s2; 95 | 96 | cout << editDistance(s1,s2) << endl; 97 | 98 | return 0 ; 99 | 100 | 101 | 102 | } 103 | -------------------------------------------------------------------------------- /Dynamic programming 2/Knapsack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | A thief robbing a store and can carry a maximal weight of W into his knapsack. There are N items and ith item weigh wi and is of value vi. What is the maximum value V, that thief can take ? 10 | Space complexity should be O(n). 11 | Input Format : 12 | Line 1 : N i.e. number of items 13 | Line 2 : N Integers i.e. weights of items separated by space 14 | Line 3 : N Integers i.e. values of items separated by space 15 | Line 4 : Integer W i.e. maximum weight thief can carry 16 | Output Format : 17 | Line 1 : Maximum value V 18 | Constraints 19 | 1 <= N <= 10^4 20 | 1<= wi <= 100 21 | 1 <= vi <= 100 22 | Sample Input 1 : 23 | 4 24 | 1 2 4 5 25 | 5 4 8 6 26 | 5 27 | Sample Output : 28 | 13 29 | */ 30 | 31 | 32 | #include 33 | 34 | using namespace std; 35 | 36 | int knapSack(int W, int wt[], int val[], int n) 37 | { 38 | int i, w; 39 | int K[n+1][W+1]; 40 | 41 | // Build table K[][] in bottom up manner 42 | for (i = 0; i <= n; i++) 43 | { 44 | for (w = 0; w <= W; w++) 45 | { 46 | if (i==0 || w==0) 47 | K[i][w] = 0; 48 | else if (wt[i-1] <= w) 49 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 50 | else 51 | K[i][w] = K[i-1][w]; 52 | } 53 | } 54 | 55 | return K[n][W]; 56 | } 57 | 58 | int knapsack(int* weights, int* values, int n, int maxWeight){ 59 | 60 | return knapSack(maxWeight, weights, values, n); 61 | 62 | 63 | } 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /Dynamic programming 2/LCS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | Given 2 strings of S1 and S2 with lengths m and n respectively, find the length of longest common subsequence. 10 | A subsequence of a string S whose length is n, is a string containing characters in same relative order as they are present in S, but not necessarily contiguous. 11 | Subsequences contain all the strings of length varying from 0 to n. E.g. subsequences of string "abc" are - "",a,b,c,ab,bc,ac,abc. 12 | Input Format : 13 | Line 1 : String S1 14 | Line 2 : String s2 15 | Output Format : 16 | Line 1 : Length of lcs 17 | Sample Input : 18 | adebc 19 | dcadb 20 | Sample Output : 21 | 3 22 | */ 23 | 24 | #include 25 | 26 | using namespace std; 27 | 28 | int lcs(string s1, string s2){ 29 | 30 | int m = s1.size(); 31 | int n = s2.size(); 32 | 33 | vector> dp(m, vector (n,0)); 34 | 35 | if (s1[0] == s2[0]) 36 | { 37 | dp[0][0] = 1; 38 | } 39 | 40 | for (int i = 1; i < n; ++i) 41 | { 42 | if(s1[0] == s2[i]) 43 | dp[0][i] = 1; 44 | else 45 | dp[0][i] = dp[0][i-1]; 46 | } 47 | 48 | for (int i = 1; i < m; ++i) 49 | { 50 | if(s1[i] == s2[0]) 51 | dp[i][0] = 1; 52 | else 53 | dp[i][0] = dp[i-1][0]; 54 | } 55 | 56 | for (int i = 1; i < m; ++i) 57 | { 58 | for (int j = 1; j < n; ++j) 59 | { 60 | if(s1[i] == s2[j]) 61 | dp[i][j] = dp[i-1][j-1]+1; 62 | else 63 | dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 64 | } 65 | } 66 | 67 | // for (int i = 0; i < m; ++i) 68 | // { 69 | // for (int j = 0; j < n; ++j) 70 | // { 71 | // cout << dp[i][j]<<" " ; 72 | // } 73 | // cout <<'\n'; 74 | // } 75 | 76 | 77 | return dp[m-1][n-1]; 78 | 79 | 80 | } 81 | 82 | int main( int argc , char ** argv ) 83 | { 84 | ios_base::sync_with_stdio(false) ; 85 | cin.tie(NULL) ; 86 | 87 | string s1, s2; 88 | cin>>s1>>s2; 89 | 90 | cout << lcs(s1, s2) << '\n'; 91 | 92 | return 0 ; 93 | 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /Dynamic programming 2/Maxprofit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | Mike is a stock trader and makes a profit by buying and selling stocks. He buys a stock at a lower price and sells it at a higher price to book a profit. He has come to know the stock prices of a particular stock for n upcoming days in future and wants to calculate the maximum profit by doing the right transactions (single transaction = buying + selling). Can you help him maximize his profit? 10 | Note: A transaction starts after the previous transaction has ended. Two transactions can't overlap or run in parallel. 11 | The stock prices are given in the form of an array A for n days. 12 | Given the stock prices and a positive integer k, find and print the maximum profit Mike can make in at most k transactions. 13 | Input Format 14 | The first line of input contains an integer q denoting the number of queries. 15 | 16 | The first line of each test case contains a positive integer k, denoting the number of transactions. 17 | 18 | The second line of each test case contains a positive integer n, denoting the length of the array A. 19 | 20 | The third line of each test case contains n space-separated positive integers, denoting the prices of each day in the array A. 21 | Constraints 22 | 1<=q<=100 23 | 24 | 0 51 | using namespace std; 52 | typedef pair pii; 53 | #define f first 54 | #define s second 55 | 56 | int maxProfit(int *input, int id, int n, int k, bool buy, pii **memo){ 57 | if(k == 0 || id == n) return 0; 58 | 59 | if(buy){ 60 | if(memo[id][k].f != -1) return memo[id][k].f; 61 | } 62 | else{ 63 | if(memo[id][k].s != -1) return memo[id][k].s; 64 | } 65 | 66 | if(buy){ 67 | int a = maxProfit(input, id+1, n, k, false, memo)-input[id]; 68 | int b = maxProfit(input, id+1, n, k, true, memo); 69 | memo[id][k].f = max(a, b); 70 | return max(a, b); 71 | } 72 | else{ 73 | int a = input[id]+maxProfit(input, id+1, n, k-1, true, memo); 74 | int b = maxProfit(input, id+1, n, k, false, memo); 75 | memo[id][k].s = max(a, b); 76 | return max(a, b); 77 | } 78 | } 79 | 80 | int main(){ 81 | int q; 82 | cin >> q; 83 | while(q--){ 84 | int k, n; 85 | cin >> k >> n; 86 | 87 | int *input = new int[n]; 88 | for(int i = 0; i < n; i++) cin >> input[i]; 89 | 90 | pii **memo = new pii *[n+1]; 91 | for(int i = 0; i <= n; i++){ 92 | memo[i] = new pii[k+1]; 93 | for(int j = 0; j <= k; j++){ 94 | memo[i][j].f = -1; 95 | memo[i][j].s = -1; 96 | } 97 | } 98 | 99 | cout << maxProfit(input, 0, n, k, true, memo) << endl; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Dynamic programming 2/Miserman.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | Jack is a wise and miser man. Always tries to save his money. 9 | One day, he wants to go from city A to city B. Between A and B, there are N number of cities(including B and excluding A) and in each city there are M buses numbered from 1 to M. And the fare of each bus is different. Means for all N*M busses, fare (K) may be different or same. Now Jack has to go from city A to city B following these conditions: 10 | 1. At every city, he has to change the bus. 11 | 2. And he can switch to only those buses which have number either equal or 1 less or 1 greater to the previous. 12 | You are to help Jack to go from A to B by spending the minimum amount of money. 13 | N, M, K <= 100. 14 | Input 15 | Line 1: N M 16 | 17 | Line 2: NxM Grid 18 | 19 | Each row lists the fares the M busses to go form the current city to the next city. 20 | Output 21 | Single Line containing the minimum amount of fare that Jack has to give. 22 | Sample Input 23 | 5 5 24 | 1 3 1 2 6 25 | 10 2 5 4 15 26 | 10 9 6 7 1 27 | 2 7 1 5 3 28 | 8 2 6 1 9 29 | Sample Output 30 | 10 31 | */ 32 | 33 | #include 34 | using namespace std; 35 | 36 | int findFare(int **input, int n, int m, int row, int col, int **dp){ 37 | if(row == n) return 0; 38 | 39 | if(dp[row][col] != -1) return dp[row][col]; 40 | 41 | int a = INT_MAX, b = INT_MAX, c = INT_MAX; 42 | if(col-1 >= 0) a = input[row][col]+findFare(input, n, m, row+1, col-1, dp); 43 | b = input[row][col]+findFare(input, n, m, row+1, col, dp); 44 | if(col+1 < m) c = input[row][col]+findFare(input, n, m, row+1, col+1, dp); 45 | 46 | dp[row][col] = min(a, min(b, c)); 47 | return min(a, min(b, c)); 48 | } 49 | 50 | int main(){ 51 | int n, m; 52 | cin >> n >> m; 53 | 54 | int **input = new int *[n]; 55 | for(int i = 0; i < n; i++){ 56 | input[i] = new int[m]; 57 | for(int j = 0; j < m; j++) cin >> input[i][j]; 58 | } 59 | 60 | int **dp = new int *[n]; 61 | for(int i = 0; i < n; i++) dp[i] = new int[m]; 62 | 63 | int minFare = INT_MAX; 64 | for(int i = 0; i < m; i++){ 65 | for(int j = 0; j < n; j++){ 66 | for(int k = 0; k < m; k++) dp[j][k] = -1; 67 | } 68 | 69 | minFare = min(minFare, findFare(input, n, m, 0, i, dp)); 70 | } 71 | 72 | cout << minFare << endl; 73 | } 74 | -------------------------------------------------------------------------------- /Dynamic programming 2/Shortestsubsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | Gary has two string S and V. Now Gary wants to know the length shortest subsequence in S such that it is not a subsequence in V. 11 | Note: input data will be such so there will always be a solution. 12 | Input Format : 13 | Line 1 : String S of length N (1 <= N <= 1000) 14 | Line 2 : String V of length M (1 <= M <= 1000) 15 | Output Format : 16 | Length of shortest subsequence in S such that it is not a subsequence in V 17 | Sample Input : 18 | babab 19 | babba 20 | Sample Output : 21 | 3 22 | 23 | */ 24 | 25 | #include 26 | 27 | using namespace std; 28 | 29 | int solve(string s1,string s2) 30 | { 31 | int m =s1.size(); 32 | int n =s2.size(); 33 | 34 | vector> dp(m+1, vector(n+1, 0)); 35 | 36 | for (int i = 0; i < n+1; ++i) 37 | { 38 | dp[m][i] = n-i; 39 | } 40 | 41 | for (int i = 0; i < m+1; ++i) 42 | { 43 | dp[i][n] = m-i; 44 | } 45 | 46 | for (int i = m-1; i >= 0; --i) 47 | { 48 | for (int j = n-1; j >= 0; --j) 49 | { 50 | int k = j; 51 | 52 | while(k>S>>V; 85 | cout< 26 | 27 | using namespace std; 28 | 29 | int go(string s1, string s2, int i, int j, int** mem){ 30 | int m = s1.size(); 31 | int n = s2.size(); 32 | 33 | //cout << m<<" "< m-1) 36 | { 37 | return n-j; 38 | } 39 | 40 | if (j > n-1) 41 | { 42 | return m-i; 43 | } 44 | 45 | if (mem[i][j]!=-1) 46 | { 47 | return mem[i][j]; 48 | } 49 | 50 | if (s1[i] == s2[j]) 51 | { 52 | mem[i][j] = 1 + go(s1, s2, i+1, j+1, mem); 53 | return mem[i][j]; 54 | }else{ 55 | mem[i][j] = min(1+go(s1, s2, i+1, j, mem), 1+go(s1, s2, i, j+1, mem)); 56 | return mem[i][j]; 57 | } 58 | 59 | } 60 | 61 | int smallestSuperSequence(char str1[], int len1, char str2[], int len2) { 62 | string s1 = ""; 63 | string s2 = ""; 64 | 65 | for (int i = 0; i < len1; ++i) 66 | { 67 | s1 += str1[i]; 68 | } 69 | 70 | for (int i = 0; i < len2; ++i) 71 | { 72 | s2 += str2[i]; 73 | } 74 | 75 | int** mem = new int*[len1+1]; 76 | 77 | for (int i = 0; i < len1+1; ++i) 78 | { 79 | mem[i] = new int[len2+1]; 80 | for (int j = 0; j < len2+1; ++j) 81 | { 82 | mem[i][j] = -1; 83 | } 84 | } 85 | 86 | 87 | return go(s1, s2, 0, 0, mem); 88 | 89 | 90 | 91 | } 92 | 93 | int main( int argc , char ** argv ) 94 | { 95 | ios_base::sync_with_stdio(false) ; 96 | cin.tie(NULL) ; 97 | 98 | char str1[50], str2[50]; 99 | cin>>str1; 100 | cin>>str2; 101 | int len1 = strlen(str1); 102 | int len2 = strlen(str2); 103 | int min_len = smallestSuperSequence(str1, len1, str2, len2); 104 | cout< 27 | 28 | using namespace std; 29 | 30 | void go(vector arr, int n, int i , int sum){ 31 | vector> dp(n+1, vector(sum+1, 0)); 32 | 33 | for (int i = 0; i < n+1; ++i) 34 | { 35 | dp[i][0] = 1; 36 | } 37 | 38 | for (int i = 1; i < sum+1; ++i) 39 | { 40 | dp[n][i] = 0; 41 | } 42 | 43 | for (int j = 1; j < sum+1; ++j) 44 | { 45 | for (int i = n-1; i >= 0; --i) 46 | { 47 | bool c1 = 0; 48 | if (j-arr[i]>=0) 49 | { 50 | c1 = dp[i+1][j-arr[i]]; 51 | } 52 | 53 | if (c1 == 1) 54 | dp[i][j] = 1; 55 | else 56 | { 57 | dp[i][j] = dp[i+1][j]; 58 | 59 | } 60 | } 61 | } 62 | 63 | // for (int i = 0; i < n+1; ++i) 64 | // { 65 | // for (int j = 0; j < sum+1; ++j) 66 | // { 67 | // cout<>n; 91 | 92 | vector arr(n, 0); 93 | for (int i = 0; i < n; ++i) 94 | { 95 | cin>>arr[i]; 96 | } 97 | 98 | int sum; 99 | cin>>sum; 100 | 101 | go(arr, n, 0, sum); 102 | 103 | 104 | 105 | return 0 ; 106 | 107 | 108 | 109 | } 110 | -------------------------------------------------------------------------------- /Dynamic programming 2/Test.txt: -------------------------------------------------------------------------------- 1 | a 2 | ab -------------------------------------------------------------------------------- /Dynamic programming 2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Dynamic programming 2/a.out -------------------------------------------------------------------------------- /Dynamic programming 2/maxprofit.cpp: -------------------------------------------------------------------------------- 1 | jds -------------------------------------------------------------------------------- /Game Theory/grundy_numbers.cpp: -------------------------------------------------------------------------------- 1 | /*Calculate the Grundy Number for the given 'n' in the game. 2 | The game starts with a number- ‘n’ and the player to move divides the number- ‘n’ with 2, 3 or 6 and then takes the floor. If the integer becomes 0, it is removed. The last player to move wins. Which player wins the game? 3 | Input Format 4 | An Integer 'n' 5 | Output Format 6 | Grundy Number(n) 7 | Sample Input 1 - 8 | 10 9 | Sample Output 1- 10 | 0 11 | */ 12 | 13 | 14 | 15 | #include 16 | using namespace std; 17 | 18 | int mex(int a, int b, int c){ 19 | unordered_sets; 20 | s.insert(a); 21 | s.insert(b); 22 | s.insert(c); 23 | for(int i=0; i>n; 37 | cout<u,vectorv) 21 | { 22 | //Creating matrix 23 | int** matrix = new int*[n+1]; 24 | 25 | for(int i=0;i<=n;i++){ 26 | matrix[i] = new int[n+1]; 27 | 28 | for(int j=0;j<=n;j++){ 29 | matrix[i][j]=0; 30 | } 31 | 32 | } 33 | 34 | for(int i=0;i 10 | 11 | 12 | #include 13 | using namespace std; 14 | void connected(bool** edges, int n, int sv, int *visited) 15 | { 16 | int init=sv; 17 | 18 | while(init q; 25 | q.push(init); 26 | init++; 27 | vector v1; 28 | while(!q.empty()) 29 | { 30 | int x = q.front(); 31 | v1.push_back(x); 32 | 33 | q.pop(); 34 | 35 | for(int i=0;i> V >> E; 58 | 59 | bool** edges = new bool*[V]; 60 | for(int i=0; i>f; 70 | cin>>s; 71 | 72 | edges[f][s]=1; 73 | edges[s][f]=1; 74 | 75 | 76 | } 77 | 78 | int *visited = new int[V]; 79 | for(int i=0;i 33 | #define MAXN 51 34 | 35 | using namespace std; 36 | 37 | int helper(char board[][MAXN], int n, int m, int* visited){ 38 | for (int i = 0; i < n; ++i) 39 | { 40 | for (int j = 0; j < m; ++j) 41 | { 42 | if () 43 | { 44 | 45 | } 46 | } 47 | } 48 | } 49 | 50 | int solve(char board[][MAXN],int n, int m) 51 | { 52 | int* visited = new int[n](); 53 | 54 | return helper(board, n, m, visited); 55 | } 56 | 57 | int main( int argc , char ** argv ) 58 | { 59 | ios_base::sync_with_stdio(false) ; 60 | cin.tie(NULL) ; 61 | 62 | int N,M,i; 63 | char board[MAXN][MAXN]; 64 | cin>>N>>M; 65 | for(i = 0;i < N; i++){ 66 | cin>>board[i]; 67 | } 68 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void DFS(int **edges, bool *visited, int n, vector &ans, int start) 9 | { 10 | visited[start] = true; 11 | ans.push_back(start); 12 | 13 | for (int i = 0; i < n; ++i) 14 | { 15 | if (edges[start][i] == 1 && !visited[i]) 16 | DFS(edges, visited, n, ans, i); 17 | } 18 | } 19 | 20 | void Connected_Components(int **edges, int n) 21 | { 22 | bool *visited = new bool[n]; 23 | 24 | for (int i = 0; i < n; ++i) 25 | visited[i] = false; 26 | 27 | for (int i = 0; i < n; ++i) 28 | { 29 | if (!visited[i]) 30 | { 31 | // this vector creating againg and again 32 | vector ans; 33 | 34 | DFS(edges, visited, n, ans, i); 35 | sort(ans.begin(), ans.end()); 36 | 37 | for (int j = 0; j < ans.size(); ++j) 38 | { 39 | cout << ans[j] << " "; 40 | } 41 | cout << endl; 42 | } 43 | } 44 | delete[] visited; 45 | } 46 | 47 | int main() 48 | { 49 | 50 | // KNOCKCAT \\ 51 | 52 | int n; 53 | int e; 54 | 55 | cout << "Enter number of vertices & edges : " << endl; 56 | cin >> n >> e; 57 | 58 | // for storing we need to create a 2 d array n * n 59 | 60 | int **edges = new int *[n]; // dynamic array for storing edges 61 | 62 | for (int i = 0; i < n; ++i) 63 | { 64 | edges[i] = new int[n]; 65 | for (int j = 0; j < n; ++j) 66 | { 67 | edges[i][j] = 0; 68 | } 69 | } 70 | 71 | for (int i = 0; i < e; ++i) // e edges 72 | { 73 | int f, s; // first vertex second vertex 74 | cout << "Enter edge from first vertex to second vertex " << endl; 75 | 76 | cin >> f >> s; 77 | edges[f][s] = 1; 78 | edges[s][f] = 1; 79 | } 80 | 81 | cout << "Connected Components are " << endl; 82 | Connected_Components(edges, n); 83 | 84 | // deleting all the memory 85 | 86 | for (int i = 0; i < n; ++i) 87 | delete[] edges[i]; 88 | 89 | delete[] edges; 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /Graphs1/Get_Path_DFS.cpp: -------------------------------------------------------------------------------- 1 | // Get Path DFS 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | vector Get_Path(int **edges, int n, int s, int d, bool *visited) 8 | { 9 | if (s == d) 10 | { 11 | vector ans; 12 | visited[s] = true; 13 | ans.push_back(s); 14 | return ans; 15 | } 16 | 17 | visited[s] = true; 18 | 19 | for (int i = 0; i < n; ++i) 20 | { 21 | if (edges[s][i] == 1 && !visited[i]) 22 | { 23 | vector ans = Get_Path(edges, n, i, d, visited); 24 | 25 | if (!ans.empty()) 26 | { 27 | ans.push_back(s); 28 | return ans; 29 | } 30 | } 31 | } 32 | 33 | return {}; 34 | } 35 | 36 | int main() 37 | { 38 | // KNOCKCAT \\ 39 | 40 | int n, e; 41 | cout << "Enter number of vertices & edges : " << endl; 42 | cin >> n >> e; 43 | 44 | // for storing we need a 2d arary n*n; 45 | int **edges = new int *[n]; 46 | 47 | for (int i = 0; i < n; ++i) 48 | { 49 | edges[i] = new int[n]; 50 | for (int j = 0; j < n; ++j) 51 | edges[i][j] = 0; 52 | } 53 | 54 | for (int i = 0; i < e; ++i) // e edges 55 | { 56 | int f, s; // first vertex second vertes 57 | cout << "Enter edge from first vertex to second vertex" << endl; 58 | 59 | cin >> f >> s; 60 | edges[f][s] = 1; 61 | edges[s][f] = 1; 62 | } 63 | 64 | bool *visited = new bool[n]; 65 | 66 | for (int i = 0; i < n; ++i) 67 | visited[i] = false; 68 | 69 | int s, d; 70 | vector ans; 71 | cout << "Enter source vertex & destination vertex for path " << endl; 72 | cin >> s >> d; 73 | 74 | ans = Get_Path(edges, n, s, d, visited); 75 | 76 | cout << "Path from " << s << " to " << d << endl; 77 | 78 | if (!ans.empty()) 79 | { 80 | for (auto i : ans) 81 | cout << i << " "; 82 | } 83 | else 84 | cout << "No Path" << endl; 85 | 86 | // deleting memory 87 | for (int i = 0; i < n; ++i) 88 | delete[] edges[i]; 89 | 90 | delete[] edges; 91 | 92 | delete[] visited; 93 | 94 | return 0; 95 | } 96 | 97 | /* 98 | 99 | OUTPUT 100 | 101 | PS E:\DOCUMENTS\C-Plus-Plus-Programming\Graph> cd "e:\DOCUMENTS\C-Plus-Plus-Programming\Graph\" ; if ($?) { g++ Get_Path_DFS.cpp -o Get_Path_DFS } ; if ($?) { .\Get_Path_DFS } 102 | Enter number of vertices & edges : 103 | 4 4 104 | Enter edge from first vertex to second vertex 105 | 0 1 106 | Enter edge from first vertex to second vertex 107 | 0 3 108 | Enter edge from first vertex to second vertex 109 | 1 2 110 | Enter edge from first vertex to second vertex 111 | 2 3 112 | Enter source vertex & destination vertex for path 113 | 1 3 114 | Path from 1 to 3 115 | 3 0 1 116 | PS E:\DOCUMENTS\C-Plus-Plus-Programming\Graph> cd "e:\DOCUMENTS\C-Plus-Plus-Programming\Graph\" ; if ($?) { g++ Get_Path_DFS.cpp -o Get_Path_DFS } ; if ($?) { .\Get_Path_DFS } 117 | Enter number of vertices & edges : 118 | 6 3 119 | Enter edge from first vertex to second vertex 120 | 5 3 121 | Enter edge from first vertex to second vertex 122 | 0 1 123 | Enter edge from first vertex to second vertex 124 | 3 4 125 | Enter source vertex & destination vertex for path 126 | 0 3 127 | Path from 0 to 3 128 | No Path 129 | PS E:\DOCUMENTS\C-Plus-Plus-Programming\Graph> 130 | 131 | */ 132 | -------------------------------------------------------------------------------- /Graphs1/Islandsc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | An island is a small piece of land surrounded by water . A group of islands is said to be connected if we can reach from any given island to any other island in the same group . Given N islands (numbered from 1 to N) and two lists of size M (u and v) denoting island u[i] is connected to island v[i] and vice versa . Can you count the number of connected groups of islands. 9 | Constraints : 10 | 1<=N<=100 11 | 1<=M<=(N*(N-1))/2 12 | 1<=u[i],v[i]<=N 13 | Input Format : 14 | Line 1 : Two integers N and M 15 | Line 2 : List u of size of M 16 | Line 3 : List v of size of M 17 | Output Return Format : 18 | The count the number of connected groups of islands 19 | Sample Input : 20 | 2 1 21 | 1 22 | 2 23 | Sample Output : 24 | 1 25 | */ 26 | 27 | #include 28 | 29 | using namespace std; 30 | 31 | void DFS(int** arr, int* visited, int n, int sv){ 32 | 33 | visited[sv] = 1; 34 | 35 | for (int i = 0; i < n; ++i) 36 | { 37 | if (i==sv) 38 | { 39 | continue; 40 | } 41 | if (arr[sv][i] == 1 && visited[i] == 0) 42 | { 43 | DFS(arr, visited, n, i); 44 | } 45 | } 46 | 47 | return; 48 | } 49 | 50 | int solve(int n,int m,vectoru,vectorv) 51 | { 52 | int** arr = new int*[n]; 53 | for (int i = 0; i < n; ++i) 54 | { 55 | arr[i] = new int[n]; 56 | for (int j = 0; j < n; ++j) 57 | { 58 | arr[i][j] = 0; 59 | } 60 | } 61 | 62 | 63 | 64 | for (int i = 0; i < m; ++i) 65 | { 66 | arr[u[i]-1][v[i]-1] = 1; 67 | arr[v[i]-1][u[i]-1] = 1; 68 | } 69 | 70 | // for (int i = 0; i < n; ++i) 71 | // { 72 | // for (int j = 0; j < n; ++j) 73 | // { 74 | // cout<u,v; 106 | cin>>n>>m; 107 | for(int i=0;i>x; 111 | u.push_back(x); 112 | } 113 | for(int i=0;i>x; 117 | v.push_back(x); 118 | } 119 | cout< 32 | 33 | using namespace std; 34 | 35 | 36 | void print(int** edges, int n, int* visited, int sv){ 37 | //cout << sv << '\n'; 38 | visited[sv] = 1; 39 | 40 | queue temp; 41 | temp.push(sv); 42 | while(!temp.empty()){ 43 | int top = temp.front(); 44 | temp.pop(); 45 | cout << top << " "; 46 | //visited[temp] = 1; 47 | 48 | for (int i = 0; i < n; ++i) 49 | { 50 | if (i==top) 51 | { 52 | continue; 53 | } 54 | if (edges[top][i] == 1 && visited[i] == 0) 55 | { 56 | temp.push(i); 57 | visited[i] = 1; 58 | } 59 | } 60 | } 61 | 62 | return; 63 | } 64 | 65 | 66 | int main( int argc , char ** argv ) 67 | { 68 | ios_base::sync_with_stdio(false) ; 69 | cin.tie(NULL) ; 70 | 71 | int n, e; 72 | cin>>n>>e; 73 | 74 | int** edges = new int*[n]; 75 | for (int i = 0; i < n; ++i) 76 | { 77 | edges[i] = new int[n]; 78 | for (int j = 0; j < n; ++j) 79 | { 80 | edges[i][j] = 0; 81 | } 82 | } 83 | 84 | int* visited = new int[n]; 85 | 86 | for (int i = 0; i < n; ++i) 87 | { 88 | visited[i] = 0; 89 | } 90 | 91 | for (int i = 0; i < e; ++i) 92 | { 93 | int a, b; 94 | cin>>a>>b; 95 | 96 | edges[a][b] = 1; 97 | edges[b][a] = 1; 98 | } 99 | 100 | 101 | 102 | print(edges, n, visited, 0); 103 | 104 | 105 | 106 | 107 | return 0 ; 108 | 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /Graphs1/codingninjas.cpp: -------------------------------------------------------------------------------- 1 | bool check(char graph[][MAXN],int n,int m,bool visited[][MAXN],string s,int i,int j) 2 | { 3 | if(s[0]=='\0') 4 | return true; 5 | if(visited[i][j]==true) 6 | return false; 7 | visited[i][j]=true; 8 | bool ans=false; 9 | if(graph[i-1][j-1]==s[0]&&(i-1>=0&&j-1>=0)) 10 | { 11 | ans=check(graph,n,m,visited,s.substr(1),i-1,j-1); 12 | if(ans) 13 | return true; 14 | } 15 | if(graph[i-1][j]==s[0]&&i-1>=0) 16 | { ans=check(graph,n,m,visited,s.substr(1),i-1,j); 17 | if(ans) 18 | return true; 19 | } 20 | if(graph[i-1][j+1]==s[0]&&(i-1>=0&&j+1=0&&i+1=0) 47 | {ans = check(graph,n,m,visited,s.substr(1),i,j-1); 48 | if(ans) 49 | return true; 50 | } 51 | visited[i][j]=false; 52 | if(ans==false) 53 | return false; 54 | 55 | 56 | } 57 | int solve(char graph[][MAXN],int N, int M) 58 | { 59 | // Write your code here. 60 | bool visited[N][MAXN]={false}; 61 | string s="CODINGNINJA"; 62 | for(int k=0;k 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector* getPath(int** edges, int n, int sv, int ev) 7 | { queue bfsQ; 8 | bool * visited = new bool[n]; 9 | for (int i = 0; i < n; i++) { 10 | visited[i] = false; 11 | } 12 | 13 | bfsQ.push(sv); 14 | 15 | visited[sv] = true; 16 | bool done = false; 17 | unordered_map parent; 18 | while (!bfsQ.empty() && !done) { 19 | int front = bfsQ.front(); 20 | bfsQ.pop(); 21 | for (int i = 0; i < n; i++) { 22 | if (edges[front][i] && !visited[i]) { 23 | parent[i] = front; bfsQ.push(i); 24 | visited[i] = true; if (i == ev) { 25 | done = true; 26 | break; 27 | } 28 | } 29 | } 30 | } 31 | delete [] visited; 32 | if (!done) { 33 | return NULL; 34 | } else { 35 | vector* output = new vector(); 36 | int current = ev; 37 | output->push_back(ev); 38 | while (current != sv) { 39 | current = parent[current]; 40 | output->push_back(current); 41 | } return output; 42 | } 43 | } 44 | 45 | 46 | 47 | int main() { 48 | int n; 49 | int e; 50 | cin >> n >> e; 51 | int** edges = 52 | new int*[n]; 53 | for (int i = 0; i < n; i++) { 54 | edges[i] = new int[n]; 55 | for (int j = 0; j < n; j++) { 56 | edges[i][j] = 0; 57 | } 58 | } for (int i = 0; i < e; i++) { 59 | int f, s; 60 | cin >> f >> s; 61 | edges[f][s] = 1; edges[s][f] = 1; } 62 | int sv, ev; 63 | cin >> sv >> ev; 64 | vector* output = getPath(edges, n, sv, ev); 65 | if (output != NULL) { 66 | for (int i = 0; i < output->size(); i++) { 67 | cout << output->at(i) << " "; 68 | } delete output; 69 | } for (int i = 0; i < n; i++) { 70 | delete [] edges[i]; 71 | } delete [] edges; 72 | } -------------------------------------------------------------------------------- /Graphs1/haspath.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | Given an undirected graph G(V, E) and two vertices v1 and v2(as integers), check if there exists any path between them or not. Print true or false. 11 | V is the number of vertices present in graph G and vertices are numbered from 0 to V-1. 12 | E is the number of edges present in graph G. 13 | Input Format : 14 | Line 1: Two Integers V and E (separated by space) 15 | Next E lines : Two integers a and b, denoting that there exists an edge between vertex a and vertex b (separated by space) 16 | Line (E+2) : Two integers v1 and v2 (separated by space) 17 | Output Format : 18 | true or false 19 | Constraints : 20 | 2 <= V <= 1000 21 | 1 <= E <= 1000 22 | 0 <= v1, v2 <= V-1 23 | Sample Input 1 : 24 | 4 4 25 | 0 1 26 | 0 3 27 | 1 2 28 | 2 3 29 | 1 3 30 | Sample Output 1 : 31 | true 32 | Sample Input 2 : 33 | 6 3 34 | 5 3 35 | 0 1 36 | 3 4 37 | 0 3 38 | Sample Output 2 : 39 | false 40 | */ 41 | 42 | #include 43 | 44 | using namespace std; 45 | bool hasPath(int** edges, int n, int a, int b, int* visited){ 46 | //Base Case 47 | if (a==b) 48 | { 49 | return 1; 50 | } 51 | int result = 0; 52 | 53 | for (int i = 0; i < n; ++i) 54 | { 55 | if (edges[a][i] == 1 && visited[i] == 0) 56 | { 57 | visited[i] = 1; 58 | result = hasPath(edges, n, i, b, visited); 59 | if (result==1) 60 | { 61 | return 1; 62 | } 63 | } 64 | } 65 | 66 | // for (int i = 0; i < n; ++i) 67 | // { 68 | // cout << visited[i] << ' '; 69 | // } 70 | 71 | return result; 72 | 73 | } 74 | 75 | int main( int argc , char ** argv ) 76 | { 77 | ios_base::sync_with_stdio(false) ; 78 | cin.tie(NULL) ; 79 | 80 | int n, e; 81 | cin>>n>>e; 82 | 83 | int** edges = new int*[n]; 84 | 85 | for (int i = 0; i < n; ++i) 86 | { 87 | edges[i] = new int[n]; 88 | for (int j = 0; j < n; ++j) 89 | { 90 | edges[i][j] = 0; 91 | } 92 | 93 | } 94 | 95 | int* visited = new int[n]; 96 | for (int i = 0; i < n; ++i) 97 | { 98 | visited[i] = 0; 99 | } 100 | 101 | for (int i = 0; i < e; ++i) 102 | { 103 | int a, b; 104 | cin>>a>>b; 105 | 106 | edges[a][b] = 1; 107 | edges[b][a] = 1; 108 | } 109 | 110 | int c, d; 111 | cin>>c>>d; 112 | 113 | if(hasPath(edges, n, c, d, visited)){ 114 | cout << "true" << '\n'; 115 | }else{ 116 | cout << "false" << '\n'; 117 | } 118 | 119 | return 0 ; 120 | 121 | 122 | 123 | } 124 | -------------------------------------------------------------------------------- /Graphs1/isconnected.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | Given an undirected graph G(V,E), check if the graph G is connected graph or not. 9 | V is the number of vertices present in graph G and vertices are numbered from 0 to V-1. 10 | E is the number of edges present in graph G. 11 | Input Format : 12 | Line 1: Two Integers V and E (separated by space) 13 | Next 'E' lines, each have two space-separated integers, 'a' and 'b', denoting that there exists an edge between Vertex 'a' and Vertex 'b'. 14 | Output Format : 15 | "true" or "false" 16 | Constraints : 17 | 2 <= V <= 1000 18 | 1 <= E <= 1000 19 | Sample Input 1: 20 | 4 4 21 | 0 1 22 | 0 3 23 | 1 2 24 | 2 3 25 | Sample Output 1: 26 | true 27 | Sample Input 2: 28 | 4 3 29 | 0 1 30 | 1 3 31 | 0 3 32 | Sample Output 2: 33 | false 34 | Sample Output 2 Explanation 35 | The graph is not connected, even though vertices 0,1 and 3 are connected to each other but there isn’t any path from vertices 0,1,3 to vertex 2. 36 | */ 37 | 38 | #include 39 | 40 | using namespace std; 41 | 42 | void marker(int** edges, int n, int* visited, int sv){ 43 | if (n==1) 44 | { 45 | visited[sv] = 1; 46 | return; 47 | } 48 | 49 | visited[sv] = 1; 50 | 51 | for (int i = 0; i < n; ++i) 52 | { 53 | if (edges[sv][i]==1 && visited[i]==0) 54 | { 55 | marker(edges, n, visited, i); 56 | } 57 | } 58 | return; 59 | 60 | } 61 | 62 | int main( int argc , char ** argv ) 63 | { 64 | ios_base::sync_with_stdio(false) ; 65 | cin.tie(NULL) ; 66 | 67 | int n, e; 68 | cin>>n>>e; 69 | 70 | int** edges = new int*[n]; 71 | 72 | for (int i = 0; i < n; ++i) 73 | { 74 | edges[i] = new int[n]; 75 | for (int j = 0; j < n; ++j) 76 | { 77 | edges[i][j] = 0; 78 | } 79 | 80 | } 81 | 82 | int* visited = new int[n]; 83 | for (int i = 0; i < n; ++i) 84 | { 85 | visited[i] = 0; 86 | } 87 | 88 | for (int i = 0; i < e; ++i) 89 | { 90 | int a, b; 91 | cin>>a>>b; 92 | 93 | edges[a][b] = 1; 94 | edges[b][a] = 1; 95 | } 96 | 97 | 98 | marker(edges, n, visited, 0); 99 | 100 | for (int i = 0; i < n; ++i) 101 | { 102 | if (visited[i] == 0) 103 | { 104 | cout << "false" << '\n'; 105 | return 0; 106 | } 107 | 108 | } 109 | cout << "true" << '\n'; 110 | 111 | 112 | return 0 ; 113 | 114 | 115 | 116 | } -------------------------------------------------------------------------------- /Graphs1/largestpiece.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #include 5 | #include 6 | 7 | 8 | static int rowNbr[] = {-1, 1, 0, 0}; 9 | static int colNbr[] = {0, 0, 1, -1}; 10 | 11 | 12 | //To check if the index is an eligible index for our cake 13 | bool iseligible(char cake[][55], int x, int y, int** visited, int n) 14 | { 15 | // if(x == 0 && y == 4) 16 | // cout << "Chirag" << endl; 17 | //cout << x << " " << y << endl; 18 | if(x<0 || x>=n || y<0 || y>=n|| visited[x][y] == 1 || cake[x][y] == '0' ){ 19 | return 0; 20 | } 21 | else 22 | return 1; 23 | } 24 | 25 | //Returns the maximum peice of cake that can be taken starting from x,y 26 | int solver(int n, char cake[][55], int** visited, int x, int y){ 27 | int sum=0; 28 | 29 | for(int k=0;k<4;k++){ 30 | if(iseligible(cake,x+rowNbr[k], y+colNbr[k], visited, n)){ 31 | 32 | visited[x+rowNbr[k]][y+colNbr[k]]=1; 33 | sum=sum+solver(n, cake, visited, x+rowNbr[k], y+colNbr[k]); 34 | } 35 | } 36 | return 1+sum; 37 | } 38 | 39 | 40 | //passes index of each one to solver as starting point and returns max of all 41 | int solve(int n,char cake[][55]) 42 | { 43 | int** visited = new int*[n]; 44 | for(int i=0;i 26 | 27 | using namespace std; 28 | 29 | bool mySort(pair slot1, pair slot2){ 30 | return(slot1.first> time){ 34 | int count = 1; 35 | int n = time.size(); 36 | sort(time.begin(), time.end(), mySort); 37 | 38 | pair current = time.at(0); 39 | 40 | for (int i = 1; i < n; ++i) 41 | { 42 | if (current.second<=time.at(i).first) 43 | { 44 | count++; 45 | current = time.at(i); 46 | 47 | }else if (current.second>time.at(i).first) 48 | { 49 | if (time.at(i).second<=current.second) 50 | { 51 | current = time.at(i); 52 | } 53 | } 54 | } 55 | 56 | return count; 57 | 58 | } 59 | 60 | 61 | int main( int argc , char ** argv ) 62 | { 63 | ios_base::sync_with_stdio(false) ; 64 | cin.tie(NULL) ; 65 | 66 | int n; 67 | cin>>n; 68 | std::vector> time; 69 | 70 | while(n--){ 71 | int start, end; 72 | cin>>start>>end; 73 | pair a = make_pair(start, end); 74 | time.push_back(a); 75 | } 76 | 77 | cout << go(time) << '\n'; 78 | 79 | 80 | return 0 ; 81 | 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /Greedy/FractionalKnapsack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | You want to paint your house. The total area of your house is D units. There are a total of N 11 | workers. The ith worker is available after time Ti, has hiring cost Xi and speed Yi. This means he becomes 12 | available for hiring from time Ti and remains available after that. Once available, you can hire 13 | him with cost Xi, after which he will start painting the house immediately, covering exactly Yi 14 | units of house with paint per time unit. You may or may not hire a worker and can also hire or 15 | fire him at any later point of time. However, no more than 1 worker can be painting the house 16 | at a given time. 17 | Since you want the work to be done as fast as possible, figure out a way to hire the workers, such 18 | that your house gets painted at the earliest possible time, with minimum cost to spend for hiring workers. 19 | 20 | 21 | */ 22 | 23 | #include 24 | 25 | using namespace std; 26 | 27 | bool mySort(vector a, vector b){ 28 | 29 | if (a[0] == b[0]) { 30 | if (a[2] == b[2]) { 31 | return (a[1] < b[1]); 32 | } 33 | return (a[2] > b[2]); 34 | } 35 | return (a[0] < b[0]); 36 | } 37 | 38 | long long go(vector> worker, long long area){ 39 | long long n = worker.size(); 40 | sort(worker.begin(),worker.end(),mySort); 41 | 42 | long long cost = worker.at(0)[1]; 43 | long long area_done = 0; 44 | long long current_worker = 0; 45 | long long last = 0; 46 | 47 | for (int i = 1; i < n && area_done=area) 62 | { 63 | 64 | return cost; 65 | 66 | } 67 | 68 | if (worker.at(current_worker)[2]>n>>d; 92 | vector> worker; 93 | 94 | while(n--){ 95 | long long t,x,y; 96 | cin>>t>>x>>y; 97 | 98 | vector temp; 99 | temp.push_back(t); 100 | temp.push_back(x); 101 | temp.push_back(y); 102 | 103 | worker.push_back(temp); 104 | } 105 | 106 | cout << go(worker, d) << '\n'; 107 | 108 | 109 | 110 | return 0 ; 111 | 112 | 113 | 114 | } 115 | -------------------------------------------------------------------------------- /Greedy/Nikunjanddonuts.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Nikunj loves donuts, but he also likes to stay fit. He eats n donuts in one sitting, and each donut has a calorie count, ci. After eating a donut with k calories, he must walk at least 2^j x k(where j is the number donuts he has already eaten) miles to maintain his weight. 11 | Given the individual calorie counts for each of the n donuts, find and print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight. Note that he can eat the donuts in any order. 12 | Input 13 | The first line contains an integer, n, denoting the number of donuts. 14 | The second line contains n space-separated integers describing the respective calorie counts of each donut I, i.e ci. 15 | Output 16 | Print a long integer denoting the minimum number of miles Nikunj must walk to maintain his weight. 17 | Constraints 18 | 1 ≤ n ≤ 40 19 | 1 ≤ ci ≤ 1000 20 | Sample Input 21 | 3 22 | 1 3 2 23 | Sample Output 24 | 11 25 | */ 26 | 27 | 28 | #include 29 | 30 | using namespace std; 31 | 32 | long long go(vector donuts){ 33 | int n = donuts.size(); 34 | 35 | sort(donuts.begin(), donuts.end(), greater()); 36 | 37 | //int count = 0; 38 | long long ans = 0; 39 | for (int i = 0; i < n ; ++i) 40 | { 41 | ans+=pow(2, i)*donuts.at(i); 42 | } 43 | 44 | return ans; 45 | } 46 | 47 | 48 | int main( int argc , char ** argv ) 49 | { 50 | ios_base::sync_with_stdio(false) ; 51 | cin.tie(NULL) ; 52 | 53 | int n; 54 | cin>>n; 55 | 56 | std::vector donuts; 57 | while(n--){ 58 | int a; 59 | cin>>a; 60 | donuts.push_back(a); 61 | } 62 | 63 | cout << go(donuts) << '\n'; 64 | 65 | 66 | return 0 ; 67 | 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Greedy/Perimeter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Greedy/Perimeter -------------------------------------------------------------------------------- /Greedy/Perimeter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Aahad gives an array of integers and asks Harshit to find which three elements form a triangle (non-degenerate). The task seems easy to Harshit. 12 | So, Aahad adds some conditions to this task - 13 | 1. Find the triangle with maximum perimeter 14 | 2. If there are two or more combinations with same value of maximum perimeter, then find the one with the longest side. 15 | 3.If there are more than one combinations which satisfy all the above conditions the find with maximum longest minimum side. 16 | Input Format 17 | The First line contains no of elements of array: N 18 | Each T lines contains N space-separated integers: A [i] 19 | Output Format 20 | The output contains three space-separated elements that denote the length of the sides of triangle. If no such triangle is possible, then print -1. 21 | Constraints 22 | 1 =< N <= 10^5 23 | 1 <= A[i] <= 10^9 24 | Time Limit: 1 second 25 | Sample Input1: 26 | 5 27 | 1 1 1 3 3 28 | Sample Output1: 29 | 1 3 3 30 | Sample Input2: 31 | 3 32 | 2 2 4 33 | Sample Output3: 34 | -1 35 | Explaination 36 | In the First Sample case, the elements that form a triangle with maximum perimeter is 1,3,3. 37 | In the Second Sample case, the elements that can form a triangle are degenerate, so, we printed -1. 38 | 39 | */ 40 | 41 | #include 42 | 43 | using namespace std; 44 | void go(int* arr, int n){ 45 | sort(arr, arr+n, greater()); 46 | 47 | for (int i = 0; i < n-2; ++i) 48 | { 49 | int s1, s2, s3; 50 | 51 | s1 = arr[i]; 52 | s2 = arr[i+1]; 53 | s3 = arr[i+2]; 54 | 55 | if (s2+s3>s1 && s1+s2>s3 && s3+s1>s2) 56 | { 57 | cout << s3<<" "<>n; 78 | 79 | int* arr = new int[n]; 80 | 81 | for (int i = 0; i < n; ++i) 82 | { 83 | cin>>arr[i] 84 | } 85 | go(arr, n); 86 | 87 | delete [] arr; 88 | 89 | return 0 ; 90 | 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /Greedy/Problemdiscussion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Harshit gave Aahad an array of size N and asked to minimize the difference between the maximum 11 | value and minimum value by modifying the array under the condition that each array element either 12 | increase or decrease by k(only once). 13 | It seems difficult for Aahad so he asked for your help 14 | Input Format 15 | The First line contains two space-separated integers: N,K 16 | Next lines contain N space-separated integers denoting elements of the array 17 | Output Format 18 | The output contains a single integer denoting the minimum difference between maximum value and 19 | the minimum value in the array 20 | Constraints 21 | 1 =< N <= 10^5 22 | 1 <= Ai,K <= 10^9 23 | Sample Input1: 24 | 3 6 25 | 1 15 10 26 | Sample Output1: 27 | 5 28 | Explaination 29 | We change from 1 to 6, 15 to 9 and 10 to 4. Maximum difference is 5 (between 4 and 9). We can't 30 | get a lower difference. 31 | 32 | */ 33 | 34 | 35 | #include 36 | 37 | using namespace std; 38 | 39 | int getMinDiff(int arr[], int n, int k) 40 | { 41 | if (n == 1) 42 | return 0; 43 | 44 | // Sort all elements 45 | sort(arr, arr+n); 46 | 47 | // Initialize result 48 | int ans = arr[n-1] - arr[0]; 49 | 50 | // Handle corner elements 51 | int small = arr[0] + k; 52 | int big = arr[n-1] - k; 53 | if (small > big) 54 | swap(small, big); 55 | 56 | // Traverse middle elements 57 | for (int i = 1; i < n-1; i ++) 58 | { 59 | int subtract = arr[i] - k; 60 | int add = arr[i] + k; 61 | 62 | // If both subtraction and addition 63 | // do not change diff 64 | if (subtract >= small || add <= big) 65 | continue; 66 | 67 | // Either subtraction causes a smaller 68 | // number or addition causes a greater 69 | // number. Update small or big using 70 | // greedy approach (If big - subtract 71 | // causes smaller diff, update small 72 | // Else update big) 73 | if (big - subtract <= add - small) 74 | small = subtract; 75 | else 76 | big = add; 77 | } 78 | 79 | return min(ans, big - small); 80 | } 81 | 82 | int main( int argc , char ** argv ) 83 | { 84 | ios_base::sync_with_stdio(false) ; 85 | cin.tie(NULL) ; 86 | 87 | int n,k; 88 | cin>>n>>k; 89 | 90 | int* arr = new int[n]; 91 | 92 | for (int i = 0; i < n; ++i) 93 | { 94 | cin>>arr[i]; 95 | } 96 | 97 | cout << getMinDiff(arr, n, k) << '\n'; 98 | 99 | 100 | return 0 ; 101 | 102 | 103 | 104 | } 105 | -------------------------------------------------------------------------------- /Greedy/Test.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 3 10 20 3 | 1 2 50 4 | 6 19 100 5 | 2 100 200 6 | -------------------------------------------------------------------------------- /Greedy/Weightedjob.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | You are given N jobs where every job is represented as: 11 | 1.Start Time 12 | 2.Finish Time 13 | 3.Profit Associated 14 | Find the maximum profit subset of jobs such that no two jobs in the subset overlap. 15 | Input 16 | The first line of input contains one integer denoting N. 17 | Next N lines contains three space separated integers denoting the start time, finish time and the 18 | profit associated with the ith job. 19 | Output 20 | Output one integer, the maximum profit that can be achieved. 21 | Constraints 22 | 1 ≤ N ≤ 10^6 23 | 1 ≤ ai, di, p ≤ 10^6 24 | Sample Input 25 | 4 26 | 3 10 20 27 | 1 2 50 28 | 6 19 100 29 | 2 100 200 30 | Sample Output 31 | 250 32 | 33 | 34 | */ 35 | 36 | 37 | #include 38 | using namespace std; 39 | typedef long long ll; 40 | 41 | struct job{ 42 | ll start, finish, profit; 43 | 44 | job(ll s, ll f, ll p){ 45 | start = s; 46 | finish = f; 47 | profit = p; 48 | } 49 | }; 50 | 51 | bool compare(job a, job b){ 52 | return a.finish < b.finish; 53 | } 54 | 55 | ll search(vector *input, ll limit, ll si, ll ei){ 56 | if(si > ei) return -1; 57 | 58 | if(si == ei){ 59 | if((input->at(si)).finish <= limit) return si; 60 | else return -1; 61 | } 62 | 63 | ll mid = (si+ei)/2; 64 | if((input->at(mid)).finish <= limit){ 65 | ll answer = search(input, limit, mid+1, ei); 66 | if(answer == -1) return mid; 67 | else return answer; 68 | } 69 | else return search(input, limit, si, mid-1); 70 | } 71 | 72 | int main(){ 73 | ll n; 74 | cin >> n; 75 | 76 | vector input; 77 | for(ll i = 0; i < n; i++){ 78 | ll s, f, p; 79 | cin >> s >> f >> p; 80 | input.push_back(job(s, f, p)); 81 | } 82 | 83 | sort(input.begin(), input.end(), compare); 84 | 85 | ll *dp = new ll[n]; 86 | dp[0] = input[0].profit; 87 | for(ll i = 1; i < n; i++){ 88 | ll include = input[i].profit; 89 | 90 | ll id = -1; 91 | id = search(&input, input[i].start, 0, i-1); 92 | // for(ll j = i-1; j >= 0; j--){ 93 | // if(input[j].finish <= input[i].start){ 94 | // id = j; 95 | // break; 96 | // } 97 | // } 98 | 99 | if(id != -1){ 100 | include += dp[id]; 101 | } 102 | 103 | dp[i] = max(dp[i-1], include); 104 | } 105 | 106 | cout << dp[n-1] << endl; 107 | } -------------------------------------------------------------------------------- /Greedy/Winninglottery.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Harshit knows by his resources that this time the winning lottery number is the smallest 11 | number whose sum of the digits is S and the number of digits is D. You have to help Harshit 12 | and print the winning lottery number. 13 | Input Format 14 | The Input line contains two space-separated integers: S,D 15 | Output Format 16 | The output contains a single integer denoting the winning lottery number 17 | Constraints 18 | 1 <= D <= 1000 19 | 1 <= S <= 9*D 20 | Time Limit: 1 second 21 | Sample Input1: 22 | 9 2 23 | Sample Output1: 24 | 18 25 | Explanation 26 | There are many other possible numbers like 45, 54, 90, etc with the sum of digits as 9 and 27 | number of digits as 2. The smallest of them is 18. 28 | 29 | */ 30 | 31 | 32 | /* 33 | 34 | Name: Mehul Chaturvedi 35 | IIT-Guwahati 36 | 37 | */ 38 | 39 | /* 40 | PROBLEM STATEMENT 41 | Harshit knows by his resources that this time the winning lottery number is the smallest 42 | number whose sum of the digits is S and the number of digits is D. You have to help Harshit 43 | and print the winning lottery number. 44 | Input Format 45 | The Input line contains two space-separated integers: S,D 46 | Output Format 47 | The output contains a single integer denoting the winning lottery number 48 | Constraints 49 | 1 <= D <= 1000 50 | 1 <= S <= 9*D 51 | Time Limit: 1 second 52 | Sample Input1: 53 | 9 2 54 | Sample Output1: 55 | 18 56 | Explanation 57 | There are many other possible numbers like 45, 54, 90, etc with the sum of digits as 9 and 58 | number of digits as 2. The smallest of them is 18. 59 | 60 | */ 61 | 62 | 63 | #include 64 | 65 | using namespace std; 66 | 67 | void findSmallest(int m, int s) 68 | { 69 | // If sum of digits is 0, then a number is possible 70 | // only if number of digits is 1. 71 | if (s == 0) 72 | { 73 | (m == 1)? cout << 0 74 | : cout << "Not possible"; 75 | return ; 76 | } 77 | 78 | // Sum greater than the maximum possible sum. 79 | if (s > 9*m) 80 | { 81 | cout << "Not possible"; 82 | return ; 83 | } 84 | 85 | // Create an array to store digits of result 86 | int res[m]; 87 | 88 | // deduct sum by one to account for cases later 89 | // (There must be 1 left for the most significant 90 | // digit) 91 | s -= 1; 92 | 93 | // Fill last m-1 digits (from right to left) 94 | for (int i=m-1; i>0; i--) 95 | { 96 | // If sum is still greater than 9, 97 | // digit must be 9. 98 | if (s > 9) 99 | { 100 | res[i] = 9; 101 | s -= 9; 102 | } 103 | else 104 | { 105 | res[i] = s; 106 | s = 0; 107 | } 108 | } 109 | 110 | // Whatever is left should be the most significant 111 | // digit. 112 | res[0] = s + 1; // The initially subtracted 1 is 113 | // incorporated here. 114 | 115 | 116 | for (int i=0; i>s>>d; 128 | 129 | findSmallest(d,s); 130 | 131 | 132 | return 0 ; 133 | 134 | 135 | 136 | } 137 | -------------------------------------------------------------------------------- /Greedy/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Greedy/a.out -------------------------------------------------------------------------------- /Greedy/minabsdiff.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | Given an integer array A of size N, find and return the minimum absolute difference between 12 | any two elements in the array. 13 | We define the absolute difference between two elements ai, and aj (where i != j ) is |ai - aj|. 14 | Input format : 15 | Line 1 : Integer N, Array Size 16 | Line 2 : Array elements (separated by space) 17 | Output Format : 18 | Minimum difference 19 | */ 20 | 21 | int minAbsoluteDiff(int arr[], int n) { 22 | int minimum = INT_MIN; 23 | 24 | sort(arr, arr+n); 25 | 26 | for (int i = 0; i < n-1; ++i) 27 | { 28 | int temp = arr[i+1]-arr[i]; 29 | minimum = min(temp, minimum); 30 | } 31 | 32 | return minimum; 33 | 34 | } 35 | 36 | 37 | #include 38 | 39 | using namespace std; 40 | 41 | 42 | int main( int argc , char ** argv ) 43 | { 44 | ios_base::sync_with_stdio(false) ; 45 | cin.tie(NULL) ; 46 | 47 | 48 | 49 | 50 | return 0 ; 51 | 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Interview Prep Books/Algorithms_3rd.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Interview Prep Books/Algorithms_3rd.pdf -------------------------------------------------------------------------------- /Interview Prep Books/Cracking the Coding Interview 189 Programming Questions and Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Interview Prep Books/Cracking the Coding Interview 189 Programming Questions and Solutions.pdf -------------------------------------------------------------------------------- /Interview Prep Books/epilight_cpp_new.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Interview Prep Books/epilight_cpp_new.pdf -------------------------------------------------------------------------------- /Language-Tools/3-zero-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | PROBLEM STATEMENT 9 | Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? 10 | Find all unique triplets in the array which gives the sum of zero. 11 | 12 | Note: 13 | 14 | Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) 15 | The solution set must not contain duplicate triplets. For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: 16 | (-1, 0, 1) 17 | (-1, -1, 2) 18 | 19 | */ 20 | 21 | #include 22 | 23 | using namespace std; 24 | 25 | vector > threeSum(vector &A) { 26 | sort(A.begin(), A.end()); 27 | int n = A.size(); 28 | vector > result; 29 | int c = 0; 30 | for (auto i = 0; i0 && A[i]==A[i-1]) 34 | continue; 35 | 36 | while(l temp = {A[i], A[l], A[r]}; 42 | int s = result.size(); 43 | if(s>0 && result[s-1][0]==temp[0] && result[s-1][1]==temp[1] && result[s-1][2]==temp[2]); 44 | else 45 | result.push_back(temp); 46 | ++l; --r; 47 | } 48 | else if(sum>0) 49 | --r; 50 | else 51 | ++l; 52 | } 53 | } 54 | return result; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Language-Tools/Different-names: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/Different-names -------------------------------------------------------------------------------- /Language-Tools/Different-names.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | In Little Flowers Public School, there are many students with same first names. 10 | You are given a task to find the students with same names. 11 | You will be given a string comprising of all the names of students and you have 12 | to tell the name and count of those students having same. If all the names are unique, print -1 instead. 13 | Note: We don't have to mention names whose frequency is 1. 14 | 15 | */ 16 | 17 | #include 18 | 19 | using namespace std; 20 | 21 | 22 | int main( int argc , char ** argv ) 23 | { 24 | ios_base::sync_with_stdio(false) ; 25 | cin.tie(NULL) ; 26 | 27 | string names; 28 | getline(cin, names); 29 | //cout << names << '\n'; 30 | 31 | stringstream iss(names); 32 | 33 | unordered_map m1; 34 | string temp; 35 | 36 | while(iss>>temp){ 37 | m1[temp]++; 38 | } 39 | 40 | unordered_map :: iterator it=m1.begin(); 41 | int count = 0; 42 | for (it; it != m1.end(); ++it) 43 | { 44 | if(it->second>1){ 45 | cout << it->first<<" "<< it->second << '\n'; 46 | count++; 47 | } 48 | } 49 | if (count == 0) 50 | { 51 | cout << -1 << '\n'; 52 | } 53 | 54 | return 0 ; 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Language-Tools/Duplicateinarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given an array of integers of size n which contains numbers from 0 to n - 2. Each number is present at least once. That is, if n = 5, numbers from 0 to 3 is present in the given array at least once and one number is present twice. You need to find and return that duplicate number present in the array. 11 | Assume, duplicate number is always present in the array. 12 | Input format : 13 | Line 1 : Size of input array 14 | Line 2 : Array elements (separated by space) 15 | Output Format : 16 | Duplicate element 17 | 18 | */ 19 | 20 | 21 | #include 22 | 23 | using namespace std; 24 | int MissingNumber(int arr[], int size){ 25 | unordered_map m1; 26 | for (int i = 0; i < size; ++i) 27 | { 28 | m1[arr[i]]++; 29 | } 30 | 31 | for (int i = 0; i < size; ++i) 32 | { 33 | if (m1[arr[i]] == 2) 34 | { 35 | return arr[i]; 36 | } 37 | } 38 | 39 | return -1; 40 | } 41 | 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | int size; 48 | cin >> size; 49 | int *input = new int[1 + size]; 50 | 51 | for(int i = 0; i < size; i++) 52 | cin >> input[i]; 53 | 54 | cout << MissingNumber(input, size); 55 | 56 | delete [] input; 57 | 58 | return 0; 59 | 60 | 61 | 62 | return 0 ; 63 | 64 | 65 | 66 | } 67 | -------------------------------------------------------------------------------- /Language-Tools/Extractunique: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/Extractunique -------------------------------------------------------------------------------- /Language-Tools/Extractunique.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given a string, you need to remove all the duplicates. 11 | That means, the output string should contain each character only once. 12 | The respective order of characters should remain same. 13 | 14 | 15 | */ 16 | 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | 23 | char* uniqueChar(char *s){ 24 | unordered_map m1; 25 | 26 | char out[10000000]; 27 | int j= 0; 28 | for (int i = 0; i < strlen(s); ++i) 29 | { 30 | m1[s[i]]++; 31 | if (m1[s[i]]==1) 32 | { 33 | out[j] = s[i]; 34 | j++; 35 | } 36 | 37 | } 38 | out[j]='\0'; 39 | return out; 40 | 41 | } 42 | int main( int argc , char ** argv ) 43 | { 44 | ios_base::sync_with_stdio(false) ; 45 | cin.tie(NULL) ; 46 | 47 | char input[1000000]; 48 | cin >> input; 49 | cout << uniqueChar(input) << endl; 50 | 51 | return 0 ; 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Language-Tools/Find-the-Unique-Element: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/Find-the-Unique-Element -------------------------------------------------------------------------------- /Language-Tools/Find-the-Unique-Element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | Given an integer array of size 2N + 1. In this given array, N numbers are present twice and one number 12 | is present only once in the array.You need to find and return that number which is unique in the array. 13 | Note : Given array will always contain odd number of elements. 14 | 15 | */ 16 | 17 | 18 | #include 19 | 20 | using namespace std; 21 | 22 | int FindUnique(int arr[], int size){ 23 | unordered_map m1; 24 | 25 | for (int i = 0; i < size; ++i) 26 | { 27 | m1[arr[i]]++; 28 | } 29 | 30 | for (int i = 0; i < size; ++i) 31 | { 32 | if (m1[arr[i]]==1) 33 | { 34 | return arr[i]; 35 | } 36 | } 37 | 38 | return 0; 39 | 40 | 41 | } 42 | 43 | int main( int argc , char ** argv ) 44 | { 45 | ios_base::sync_with_stdio(false) ; 46 | cin.tie(NULL) ; 47 | 48 | int size; 49 | 50 | cin>>size; 51 | int *input=new int[1+size]; 52 | 53 | for(int i=0;i>input[i]; 55 | 56 | cout< 15 | 16 | using namespace std; 17 | 18 | 19 | int main( int argc , char ** argv ) 20 | { 21 | ios_base::sync_with_stdio(false) ; 22 | cin.tie(NULL) ; 23 | 24 | int n; 25 | cin>>n; 26 | string s; 27 | std::cin>>s; 28 | 29 | unordered_map m1; 30 | 31 | for (int i = 0; i < s.size(); ++i) 32 | { 33 | m1[s[i]]++; 34 | } 35 | 36 | cout << m1['a']<<" "< 19 | 20 | using namespace std; 21 | 22 | void PairSum(int *input, int n) { 23 | unordered_map m1; 24 | for (int i = 0; i < n; ++i) 25 | { 26 | if(m1[0-input[i]]==0){ 27 | m1[input[i]]++; 28 | 29 | }else{ 30 | m1[input[i]]++; 31 | } 32 | } 33 | 34 | unordered_map::iterator it=m1.begin(); 35 | while(it!=m1.end()){ 36 | int total = 0; 37 | int left = it->second; 38 | int right = m1[-it->first]; 39 | total = left*right; 40 | while(total>0){ 41 | cout << min(it->first, -it->first)<<" "<< max(it->first, -it->first) << '\n'; 42 | total--; 43 | } 44 | m1[it->first]=0; 45 | m1[-it->first]=0; 46 | it++; 47 | 48 | } 49 | 50 | } 51 | 52 | int main( int argc , char ** argv ) 53 | { 54 | ios_base::sync_with_stdio(false) ; 55 | cin.tie(NULL) ; 56 | 57 | int n; 58 | int arr[100000]; 59 | cin>>n; 60 | for(int i=0; i>arr[i]; 63 | } 64 | PairSum(arr, n); 65 | return 0; 66 | 67 | 68 | 69 | return 0 ; 70 | 71 | 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Language-Tools/Repeat-and-Missing-Number -Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | You are given a read only array of n integers from 1 to n. 12 | 13 | Each integer appears exactly once except A which appears twice and B which is missing. 14 | 15 | Return A and B. 16 | 17 | Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 18 | 19 | Note that in your output A should precede B. 20 | 21 | 22 | 23 | 24 | */ 25 | 26 | 27 | 28 | #include 29 | 30 | using namespace std; 31 | vector Solution::repeatedNumber(const vector &A) { 32 | long long int len = A.size(); 33 | long long int Sum_N = (len * (len+1) ) /2, Sum_NSq = (len * (len +1) *(2*len +1) )/6; 34 | long long int missingNumber=0, repeating=0; 35 | 36 | for(int i=0;i ans; 44 | ans.push_back(repeating); 45 | ans.push_back(missingNumber); 46 | return ans; 47 | 48 | 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Language-Tools/Rotatematrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/Rotatematrix.cpp -------------------------------------------------------------------------------- /Language-Tools/Tripletsum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/Tripletsum -------------------------------------------------------------------------------- /Language-Tools/Tripletsum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given a random integer array and a number x. Find and print the triplets of elements in the array which sum to x. 11 | While printing a triplet, print the smallest element first. 12 | That is, if a valid triplet is (6, 5, 10) print "5 6 10". 13 | There is no constraint that out of 5 triplets which have to be printed on 1st line. 14 | You can print triplets in any order, just be careful about the order of elements in a triplet. 15 | */ 16 | 17 | 18 | #include 19 | 20 | using namespace std; 21 | void FindTriplet(int arr[], int size, int x) { 22 | sort(arr, arr+size); 23 | //int end = size-1; 24 | for (int i = 0; i < size; i++) 25 | { 26 | int val = x-arr[i]; 27 | int j = i+1; 28 | int k = size-1; 29 | while(jval) 33 | { 34 | k--; 35 | 36 | }else if (arr[j]+arr[k]= j; ptr--) 55 | { 56 | if (arr[ptr] == arr[k]) 57 | { 58 | rightcount++; 59 | }else 60 | break; 61 | } 62 | 63 | int total = leftcount*rightcount; 64 | // cout<>size; 92 | 93 | int *arr=new int[1+size]; 94 | 95 | for(int i=0;i>arr[i]; 97 | cin>>x; 98 | 99 | FindTriplet(arr,size,x); 100 | 101 | return 0; 102 | 103 | 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Language-Tools/Warm-reception: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/Warm-reception -------------------------------------------------------------------------------- /Language-Tools/Warm-reception.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | There is only one beauty parlour in the town CodingNinjasLand. The receptionist at beauty parlour is flooded with appointment requests because “Hakori” festival is round the corner and everyone wants to look good on it. 11 | She needs your help. The problem is they don’t have chairs in reception. They are ordering chairs from NinjasKart. They don’t want to order more than required. You have to tell minimum number of chairs required such that none of the customer has to stand. 12 | 13 | */ 14 | 15 | 16 | #include 17 | 18 | using namespace std; 19 | 20 | class detail 21 | { 22 | public: 23 | int time; 24 | bool status; 25 | 26 | }; 27 | 28 | 29 | bool mysort(detail a, detail b){ 30 | return(a.time < b.time); 31 | } 32 | 33 | 34 | 35 | int main( int argc , char ** argv ) 36 | { 37 | ios_base::sync_with_stdio(false) ; 38 | cin.tie(NULL) ; 39 | 40 | int n; 41 | cin>>n; 42 | 43 | //Arrival 44 | std::vector people; 45 | for (int i = 0; i < n; ++i) 46 | { 47 | int temp_time; 48 | cin>>temp_time; 49 | detail temp; 50 | temp.time = temp_time; 51 | temp.status = 1; 52 | people.push_back(temp); 53 | } 54 | 55 | //Departure 56 | for (int i = 0; i < n; ++i) 57 | { 58 | int temp_time; 59 | cin>>temp_time; 60 | detail temp; 61 | temp.time = temp_time; 62 | temp.status = 0; 63 | people.push_back(temp); 64 | } 65 | 66 | //Sorting 67 | 68 | sort(people.begin(), people.end(), mysort); 69 | 70 | 71 | int ans = 0; 72 | int count = 0; 73 | 74 | vector :: iterator it = people.begin(); 75 | 76 | for (int i = 0; i < people.size(); ++i) 77 | { 78 | if (people.at(i).status == 1) 79 | { 80 | count++; 81 | }else{ 82 | count--; 83 | } 84 | ans = max(ans, count); 85 | } 86 | 87 | cout << ans << '\n'; 88 | 89 | 90 | return 0 ; 91 | 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Language-Tools/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/a.out -------------------------------------------------------------------------------- /Language-Tools/tell-thepositions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Language-Tools/tell-thepositions -------------------------------------------------------------------------------- /Language-Tools/tell-thepositions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | In a class there are ‘n’ number of students. They have three different subjects: Data Structures, Algorithm Design & Analysis and Operating Systems. 12 | Marks for each subject of all the students are provided to you. You have to tell the position of each student in the class. 13 | Print the names of each student according to their position in class. Tie is broken on the basis of their roll numbers. Between two students having same marks, 14 | the one with less roll number will have higher rank. The input is provided in order of roll number. 15 | 16 | */ 17 | 18 | 19 | #include 20 | 21 | using namespace std; 22 | 23 | class student{ 24 | public: 25 | string name; 26 | int marks; 27 | int roll; 28 | }; 29 | 30 | bool mycompare(student a, student b){ 31 | 32 | if (a.marks!=b.marks) 33 | { 34 | return (a.marks>b.marks); 35 | }else{ 36 | return(a.roll>n; 49 | 50 | //std::vector name; //ncross1 51 | std::vector stud; //ncross3 52 | 53 | for (int i = 0; i < n; ++i) 54 | { 55 | string temp_name; 56 | int sub1; 57 | int sub2; 58 | int sub3; 59 | cin>>temp_name>>sub1>>sub2>>sub3; 60 | 61 | int sum = sub1+sub2+sub3; 62 | 63 | student temp_sub; 64 | 65 | temp_sub.name = temp_name; 66 | temp_sub.marks = sum; 67 | temp_sub. roll = i+1; 68 | 69 | stud.push_back(temp_sub); 70 | } 71 | 72 | 73 | 74 | sort(stud.begin(), stud.end(), mycompare); 75 | 76 | for (int i = 0; i < n; ++i) 77 | { 78 | cout << i+1<<" "< 10 | 11 | using namespace std; 12 | int balancedBTs(int h) { 13 | 14 | if (h==0) 15 | { 16 | return 1; 17 | } 18 | if (h==1) 19 | { 20 | return 1; 21 | } 22 | 23 | long long x = balancedBTs(h-1)%1000000007; 24 | long long y = balancedBTs(h-2)%1000000007; 25 | long long temp = (long long)(((2*x)%1000000007)*(y%1000000007))+(long long)((x%1000000007)*(x%1000000007)); 26 | 27 | long long ans = 0; 28 | ans += ((temp%1000000007))%(1000000007); 29 | 30 | return ans; 31 | 32 | 33 | } 34 | 35 | int main( int argc , char ** argv ) 36 | { 37 | ios_base::sync_with_stdio(false) ; 38 | cin.tie(NULL) ; 39 | 40 | int h; 41 | cin >> h; 42 | int ans = balancedBTs(h); 43 | cout << ans << endl; 44 | 45 | 46 | return 0 ; 47 | 48 | 49 | 50 | } -------------------------------------------------------------------------------- /Modulo Arithmetic/test.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Modulo Arithmetic/test.txt -------------------------------------------------------------------------------- /Number Theory/Test.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /Number Theory/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Number Theory/a.out -------------------------------------------------------------------------------- /Number Theory/prime.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | */ 12 | 13 | #include 14 | using namespace std; 15 | 16 | typedef long long ll; 17 | typedef unordered_map umapii; 18 | typedef unordered_map umapib; 19 | typedef unordered_map umapsi; 20 | typedef unordered_map umapss; 21 | typedef map mapsi; 22 | typedef map, int> mappiii; 23 | typedef map mapii; 24 | typedef pair pii; 25 | typedef pair pll; 26 | typedef unordered_set useti; 27 | 28 | #define uset unordered_set 29 | #define it iterator 30 | #define mp make_pair 31 | #define pb push_back 32 | #define all(x) (x).begin(), (x).end() 33 | #define f first 34 | #define s second 35 | #define MOD 1000000007 36 | 37 | 38 | int main( int argc , char ** argv ) 39 | { 40 | ios_base::sync_with_stdio(false) ; 41 | cin.tie(NULL) ; 42 | 43 | int n; 44 | cin>>n; 45 | int count = 0; 46 | 47 | if (n>=2) 48 | { 49 | count++; 50 | } 51 | if (n>=3) 52 | { 53 | count++; 54 | } 55 | 56 | //cout << n << '\n'; 57 | for (int i = 2; i <= n; ++i) 58 | { 59 | int flag = 0; 60 | for (int j = 2; j*j <= i; ++j) 61 | { 62 | if (i%j==0) 63 | { 64 | flag = 0; 65 | break; 66 | }else{ 67 | flag = 1; 68 | } 69 | 70 | } 71 | if (flag == 1) 72 | { 73 | count++; 74 | } 75 | } 76 | 77 | cout < 5 | 6 | using namespace std; 7 | 8 | 9 | int main( int argc , char ** argv ) 10 | { 11 | ios_base::sync_with_stdio(false) ; 12 | cin.tie(NULL) ; 13 | 14 | int n; 15 | cin>> n; 16 | int* arr = new int[n]; 17 | int sum1=0, sum2=0; 18 | 19 | for (int i = 0; i < n; ++i) 20 | { 21 | int a; 22 | cin>>a; 23 | arr[i] = a; 24 | if(i%2==0 && a%2==0){ 25 | sum1+=a; 26 | }else if(i%2 != 0 && a%2!=0){ 27 | sum2+=a; 28 | } 29 | } 30 | 31 | cout << sum1<<" "< 10 | 11 | using namespace std; 12 | 13 | 14 | int main( int argc , char ** argv ) 15 | { 16 | ios_base::sync_with_stdio(false) ; 17 | cin.tie(NULL) ; 18 | 19 | int n; 20 | cin>>n; 21 | int a[n]; 22 | for(int i=0;i>a[i]; 24 | int profit=0; 25 | for(int i=0;i0) && ((a[j]-a[i])>profit)) 30 | { 31 | profit=a[j]-a[i]; 32 | } 33 | } 34 | } 35 | cout< 10 | 11 | using namespace std; 12 | 13 | 14 | int main( int argc , char ** argv ) 15 | { 16 | ios_base::sync_with_stdio(false) ; 17 | cin.tie(NULL) ; 18 | 19 | int n,sum; 20 | 21 | cin>> n>>sum; 22 | 23 | int* arr = new int[n]; 24 | for (int i = 0; i < n; ++i) 25 | { 26 | cin>>arr[i]; 27 | } 28 | 29 | int start=0; 30 | int end=0; 31 | int tempsum=arr[end]; 32 | 33 | while(1){ 34 | //tempsum=tempsum+arr[end]; 35 | cout << "temp"<sum) 60 | { if (startend){ 65 | end++; 66 | tempsum = arr[end]; 67 | } 68 | continue; 69 | }else{ 70 | cout << "false" << '\n'; 71 | return 0; 72 | } 73 | 74 | }else if (start == n-1) 75 | { 76 | cout << "false" << '\n'; 77 | return 0; 78 | } 79 | 80 | 81 | 82 | 83 | } 84 | 85 | 86 | 87 | 88 | 89 | 90 | return 0 ; 91 | 92 | 93 | 94 | } 95 | -------------------------------------------------------------------------------- /Prerequisites/pre3.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | int main( int argc , char ** argv ) 9 | { 10 | ios_base::sync_with_stdio(false) ; 11 | cin.tie(NULL) ; 12 | 13 | int n; 14 | cin>>n; 15 | 16 | int sum = 0; 17 | int** arr = new int*[n]; 18 | for (int i = 0; i < n; ++i) 19 | { 20 | arr[i]=new int[n]; 21 | for (int j = 0; j < n; ++j) 22 | { 23 | cin>>arr[i][j]; 24 | if(i==0 || j==0 || i==j || i==n-1 || j==n-1 || i+j==n-1){ 25 | sum=sum+arr[i][j]; 26 | } 27 | } 28 | } 29 | 30 | cout << sum << '\n'; 31 | 32 | 33 | 34 | return 0 ; 35 | 36 | 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Roadmap to crack FAANG interviews 2 | https://tinyurl.com/329muw7d 3 | 4 | # Coding-Ninjas Competitive Programming | 15 August’19 5 | 6 | This will have solutions to all the problems that are included in Coding Ninja's 2019 Competitive Programming Course. Star the repo if you like it. 7 | 8 | ## General Information 9 | 10 | The repository contains solutions to various problems on Coding Ninjas CP Course. 11 | **The code written is purely original & completely my own.** 12 | 13 | ## How to contribute? 14 | 15 | 1. Fork the repository 16 | 2. Do the desired changes (add/delete/modify) 17 | 3. Make a pull request 18 | 19 | ## When to contribute? 20 | 21 | 1. If there is no solution to a problem in the main branch. 22 | 2. If your solution is asymptotically faster than the one in the main branch. 23 | 3. If your algorithm is of the same time complexity but with reduced code size. In this case, comment out the original solution & make a pull request with your solution. 24 | 4. If you have another method of solving the problem which is asymptotically slower than the original method then, comment your solution in the main file & make a pull request. 25 | -------------------------------------------------------------------------------- /ReturnKeypadCode.cpp: -------------------------------------------------------------------------------- 1 | /*Given an integer n, using phone keypad find out all the possible strings that can be made using digits of input n. 2 | Return empty string for numbers 0 and 1. 3 | Note : 1. The order of strings are not important. 4 | 2. Input and output has already been managed for you. You just have to populate the output array and return the count of elements populated in the output array. 5 | Input Format : 6 | Integer n 7 | Output Format : 8 | All possible strings in different lines 9 | Constraints : 10 | 1 <= n <= 10^6 11 | Sample Input: 12 | 23 13 | Sample Output: 14 | ad 15 | ae 16 | af 17 | bd 18 | be 19 | bf 20 | cd 21 | ce 22 | cf 23 | */ 24 | 25 | 26 | 27 | #include 28 | using namespace std; 29 | 30 | int keypad(int num, string output[]){ 31 | /* Insert all the possible combinations of the integer number into the output string array. You do not need to 32 | print anything, just return the number of strings inserted into the array. 33 | */ 34 | string input; 35 | if(num == 0){ 36 | output[0] = ""; 37 | return 1; 38 | } 39 | 40 | int n = num%10; 41 | num = num/10; 42 | int smalloutputsize = keypad(num, output); 43 | switch(n){ 44 | case 2: input = "abc"; 45 | break; 46 | case 3: input = "def"; 47 | break; 48 | case 4: input = "ghi"; 49 | break; 50 | case 5: input = "jkl"; 51 | break; 52 | case 6: input = "mno"; 53 | break; 54 | case 7: input = "pqrs"; 55 | break; 56 | case 8: input = "tuv"; 57 | break; 58 | case 9: input = "wxyz"; 59 | break; 60 | 61 | } 62 | int ans_size=smalloutputsize*(input.size()); 63 | string temp[ans_size]; 64 | int k=0; 65 | for(int i=0; i 40 | 41 | using namespace std; 42 | 43 | int go(int c, vector barn){ 44 | 45 | sort(barn.begin(), barn.end()); 46 | 47 | int n = barn.size(); 48 | int start = 0; 49 | int end = barn[n-1]-barn[0]; 50 | int max_dist = end+start; 51 | int mid = max_dist/2; 52 | int ans = 0; 53 | 54 | 55 | 56 | while(start<=end){ 57 | int last_cow_at = 0; 58 | int cow = c-1; 59 | mid = max_dist/2; 60 | 61 | for (int i = 0; i < n; ++i) 62 | { 63 | 64 | if (barn[i]-barn[last_cow_at]>=mid) 65 | { 66 | 67 | cow--; 68 | last_cow_at = i; 69 | if (cow == 0) 70 | { 71 | ans = max(ans, mid); 72 | break; 73 | } 74 | 75 | } 76 | 77 | } 78 | if (cow==0) 79 | { 80 | start = mid+1; 81 | end = end; 82 | }else{ 83 | start = start; 84 | end = mid-1; 85 | } 86 | max_dist = end+start; 87 | 88 | } 89 | return ans; 90 | 91 | 92 | } 93 | 94 | 95 | int main( int argc , char ** argv ) 96 | { 97 | ios_base::sync_with_stdio(false) ; 98 | cin.tie(NULL) ; 99 | 100 | int t; 101 | cin >> t; 102 | 103 | while(t>0){ 104 | int n, c; 105 | cin>>n>>c; 106 | vector barn; 107 | 108 | while(n>0){ 109 | int b; 110 | cin>>b; 111 | barn.push_back(b); 112 | n--; 113 | } 114 | cout << go(c, barn) << '\n'; 115 | 116 | t--; 117 | } 118 | 119 | 120 | 121 | return 0 ; 122 | 123 | 124 | 125 | } 126 | -------------------------------------------------------------------------------- /Searching and sorting applications/Distrributecandies: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Searching and sorting applications/Distrributecandies -------------------------------------------------------------------------------- /Searching and sorting applications/Distrributecandies.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Shaky has N (1<=N<=50000) candy boxes each of them contains a non-zero number of candies 11 | (between 1 and 1000000000). 12 | Shaky want to distibute these candies among his K (1<=K<=1000000000) IIIT-Delhi students. 13 | He want to distibute them in a way such that: 14 | 1. All students get equal number of candies. 15 | 2. All the candies which a student get must be from a single box only. 16 | As he want to make all of them happy so he want to give as many candies as possible. 17 | Help Shaky in finding out what is the maximum number of candies which a student can get. 18 | 19 | */ 20 | 21 | 22 | #include 23 | using namespace std; 24 | 25 | 26 | int go(int* arr, int k,int n){ 27 | 28 | sort(arr,arr+n); 29 | 30 | int max_candies = arr[n-1]; 31 | int min_candies = 0; 32 | int mid ; 33 | int ans = 1; 34 | 35 | 36 | while(max_candies>=min_candies){ 37 | mid = (min_candies+max_candies)/2; 38 | int given=0; 39 | for(int i=0;i=k){ 45 | ans=max(ans,mid); 46 | min_candies=mid+1; 47 | } 48 | else{ 49 | max_candies=mid-1; 50 | } 51 | 52 | } 53 | 54 | return ans; 55 | 56 | } 57 | 58 | 59 | 60 | 61 | int main( ) 62 | { 63 | int t; 64 | cin>>t; 65 | 66 | while(t--){ 67 | int n, k; 68 | cin>>n>>k; 69 | int* arr=new int[n]; 70 | for(int i=0;i>arr[i]; 72 | cout << go(arr,k,n) << endl; 73 | } 74 | 75 | return 0 ; 76 | 77 | } 78 | -------------------------------------------------------------------------------- /Searching and sorting applications/Inversion Count: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Searching and sorting applications/Inversion Count -------------------------------------------------------------------------------- /Searching and sorting applications/Inversion Count.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | /* 8 | PROBLEM STATEMENT 9 | Let A[0 ... n-1] be an array of n distinct positive integers. 10 | If i < j and A[i] > A[j] then the pair (i, j) is called an inversion of A 11 | (where i and j are indexes of A). Given an integer array A, your task is to 12 | find the number of inversions in A. 13 | Input format : 14 | Line 1 : n, array size 15 | Line 2 : Array elements (separated by space). 16 | Output format : 17 | Count of inversions 18 | Constraints : 19 | 1 <= n <= 10^5 20 | 1 <= A[i] <= 10^9 21 | 22 | */ 23 | 24 | #include 25 | 26 | using namespace std; 27 | 28 | long long merge(int A[],int left,int mid,int right){ 29 | 30 | int i=left,j=mid,k=0; 31 | 32 | int temp[right-left+1]; 33 | long long count = 0; 34 | while(i left){ 58 | int mid = (left + right)/2; 59 | 60 | long long countLeft = merge_sort(A,left,mid); 61 | long long countRight = merge_sort(A,mid+1,right); 62 | long long myCount = merge(A,left,mid+1,right); 63 | 64 | return myCount + countLeft + countRight; 65 | } 66 | return count; 67 | 68 | } 69 | long long solve(int A[], int n) 70 | { 71 | long long ans = merge_sort(A,0,n-1); 72 | return ans; 73 | } 74 | 75 | 76 | #define MAXN 100005 77 | 78 | int main() 79 | { 80 | int n,A[MAXN]; 81 | cin>>n; 82 | for(int i = 0; i < n ; i++){ 83 | cin>>A[i]; 84 | } 85 | cout< 25 | 26 | using namespace std; 27 | long long merge(vector &v, long long start, long long mid, long long end){ 28 | 29 | long long sum = 0; 30 | long long i = start; 31 | long long j = mid+1; 32 | 33 | 34 | while(j<=end && i<=mid){ 35 | 36 | if (v.at(j)>v.at(i)) 37 | { 38 | 39 | sum += (end-j+1)*v.at(i); 40 | i++; 41 | }else{ 42 | j++; 43 | } 44 | 45 | } 46 | return sum; 47 | 48 | } 49 | 50 | long long go(vector &v, long long start, long long end){ 51 | 52 | if (start>=end) 53 | { 54 | return 0; 55 | } 56 | long long mid = (start+end)/2; 57 | 58 | long long left = go(v, start, mid); 59 | long long right = go(v, mid+1, end); 60 | 61 | sort(v.begin()+start, v.begin()+mid+1); 62 | sort(v.begin()+mid+1, v.begin()+end+1); 63 | 64 | return left+right+merge(v, start, mid, end); 65 | 66 | } 67 | 68 | 69 | int main( int argc , char ** argv ) 70 | { 71 | ios_base::sync_with_stdio(false) ; 72 | cin.tie(NULL) ; 73 | 74 | long long t; 75 | cin>>t; 76 | 77 | while(t--){ 78 | long long n; 79 | cin>>n; 80 | long long m = n; 81 | 82 | std::vector v; 83 | while(n--){ 84 | long long a; 85 | cin>>a; 86 | v.push_back(a); 87 | } 88 | 89 | cout << go(v, 0, m-1) << '\n'; 90 | } 91 | 92 | return 0 ; 93 | } 94 | -------------------------------------------------------------------------------- /Searching and sorting applications/Tajmahalentry.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Taj Mahal is one of the seven wonders of the world. Aahad loves to travel places 12 | and wants to visit Taj Mahal. He visited Agra to view Taj Mahal. There is a ticketing 13 | system at Taj Mahal. There are total ‘n’ windows which provide the tickets to get entry 14 | into Taj Mahal. There are ‘Ai’ people already present at each window to get the tickets. 15 | Each window gives ticket to one person in one minute. Initially, Aahad stands in front of 16 | the first window. After each minute, if he didn’t get the ticket, he moves on to the next 17 | window to get the ticket. If he is at window 1, he will move to 2. If at 2nd, he will move to 3rd. 18 | If he is at last window, he will move to 1st again and so on. 19 | Find the window number at which he will get the ticket. 20 | 21 | 22 | */ 23 | 24 | #include 25 | 26 | using namespace std; 27 | 28 | int go(vector window, int min_index){ 29 | int n = window.size(); 30 | int pos = window.at(min_index)%n; 31 | int min_value = window.at(min_index); 32 | //cout << min_index << '\n'; 33 | //cout << pos << '\n'; 34 | 35 | if (pos==min_index) 36 | { 37 | return min_index; 38 | } 39 | 40 | int cycles = min_value/n; 41 | 42 | for (int i = pos; i < n+pos; ++i) 43 | { 44 | window.at(i%n) = window.at(i%n) - abs(i-pos)-cycles*n-min_value; 45 | if (window.at(i%n)<=0) 46 | { 47 | return i%n; 48 | } 49 | } 50 | 51 | return 0; 52 | 53 | } 54 | 55 | 56 | int main( int argc , char ** argv ) 57 | { 58 | ios_base::sync_with_stdio(false) ; 59 | cin.tie(NULL) ; 60 | 61 | int n; 62 | cin>>n; 63 | //int m = n; 64 | int min_index= 0; 65 | int min_value = INT_MAX; 66 | int h = 0; 67 | 68 | std::vector window; 69 | while(n--){ 70 | int a; 71 | cin>>a; 72 | window.push_back(a); 73 | if (window.at(min_index)>window.at(h)) 74 | { 75 | min_index = h; 76 | min_value = window.at(min_index); 77 | } 78 | h++; 79 | } 80 | 81 | cout << go(window, min_index)+1 << '\n'; 82 | 83 | 84 | 85 | 86 | return 0 ; 87 | 88 | 89 | 90 | } 91 | -------------------------------------------------------------------------------- /Searching and sorting applications/Test.txt: -------------------------------------------------------------------------------- 1 | +-++++++++ 2 | +-++++++++ 3 | +-++++++++ 4 | +-----++++ 5 | +-+++-++++ 6 | +-+++-++++ 7 | +++++-++++ 8 | ++------++ 9 | +++++-++++ 10 | +++++-++++ 11 | LONDON;DELHI;ICELAND;ANKARA -------------------------------------------------------------------------------- /Searching and sorting applications/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Searching and sorting applications/a.out -------------------------------------------------------------------------------- /Segment Trees/Minimuminsub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | Range Minimum Query 10 | Given an array A of size N, there are two types of queries on this array. 11 | 1) q l r: In this query you need to print the minimum in the sub-array A[l:r]. 12 | 2) u x y: In this query you need to update A[x]=y. 13 | Input: 14 | First line of the test case contains two integers, N and Q, size of array A and number of queries. 15 | Second line contains N space separated integers, elements of A. 16 | Next Q lines contain one of the two queries. 17 | Output: 18 | For each type 1 query, print the minimum element in the sub-array A[l:r]. 19 | Contraints: 20 | 1≤N,Q,y≤10^5 21 | 1≤l,r,x≤N 22 | Sample Input : 23 | 5 5 24 | 1 5 2 4 3 25 | q 1 5 26 | q 1 3 27 | q 3 5 28 | u 3 6 29 | q 1 5 30 | Sample Output : 31 | 1 32 | 1 33 | 2 34 | 1 35 | */ 36 | 37 | #include 38 | 39 | using namespace std; 40 | 41 | int query(int* tree, int start, int end, int treeNode, int left, int right){ 42 | 43 | //Completely out 44 | if (left>end || right=left && end<=right) 51 | { 52 | return tree[treeNode]; 53 | } 54 | 55 | //Partially inside 56 | int mid = (start+end)/2; 57 | 58 | int l = query(tree, start, mid, 2*treeNode+1, left, right); 59 | int r= query(tree, mid+1, end, 2*treeNode+2, left, right); 60 | 61 | return min(l, r); 62 | } 63 | 64 | 65 | void update(int* arr, int* tree, int start, int end, int treeNode, int idx, int value){ 66 | int mid = (start+end)/2; 67 | 68 | if (start == end) 69 | { 70 | arr[idx] = value; 71 | tree[treeNode] = value; 72 | return; 73 | } 74 | 75 | if (idx<=mid) 76 | { 77 | update(arr, tree, start, mid, 2*treeNode+1, idx, value); 78 | }else{ 79 | 80 | update(arr, tree, mid+1, end, 2*treeNode+2, idx, value); 81 | } 82 | 83 | tree[treeNode] = min(tree[2*treeNode+1], tree[2*treeNode+2]); 84 | } 85 | 86 | 87 | void create(int* arr, int* tree, int start, int end, int treeNode){ 88 | if (start == end) 89 | { 90 | tree[treeNode] = arr[start]; 91 | return; 92 | } 93 | 94 | int mid = (start+end)/2; 95 | 96 | create(arr, tree, start, mid, 2*treeNode+1); 97 | create(arr, tree, mid+1, end, 2*treeNode+2); 98 | 99 | tree[treeNode] = min(tree[2*treeNode+1], tree[2*treeNode+2]); 100 | return; 101 | } 102 | 103 | int main( int argc , char ** argv ) 104 | { 105 | ios_base::sync_with_stdio(false) ; 106 | cin.tie(NULL) ; 107 | 108 | int n, q; 109 | cin>>n>>q; 110 | 111 | int* arr = new int[n]; 112 | for (int i = 0; i < n; ++i) 113 | { 114 | cin>>arr[i]; 115 | } 116 | 117 | int* tree = new int[4*n]; 118 | 119 | create(arr, tree, 0, n-1, 0); 120 | 121 | 122 | 123 | while(q--){ 124 | char a; 125 | int b, c; 126 | cin>>a>>b>>c; 127 | 128 | if (a=='q') 129 | { 130 | cout << query(tree, 0, n-1, 0, b-1, c-1) << '\n'; 131 | }else{ 132 | update(arr, tree, 0, n-1, 0, b-1, c); 133 | } 134 | 135 | } 136 | 137 | 138 | return 0 ; 139 | 140 | 141 | 142 | } 143 | -------------------------------------------------------------------------------- /Segment Trees/Test.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 1 8 4 6 8 3 | 4 8 6 3 7 4 | 4 5 | 1 4 6 | 2 4 7 | 3 4 8 | 1 5 -------------------------------------------------------------------------------- /Segment Trees/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Segment Trees/a.out -------------------------------------------------------------------------------- /String/0000000000000183s.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/String/0000000000000183s.zip -------------------------------------------------------------------------------- /String/KMP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/String/KMP -------------------------------------------------------------------------------- /String/KMP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | 11 | */ 12 | 13 | #include 14 | using namespace std; 15 | 16 | typedef long long ll; 17 | typedef unordered_map umapii; 18 | typedef unordered_map umapib; 19 | typedef unordered_map umapsi; 20 | typedef unordered_map umapss; 21 | typedef map mapsi; 22 | typedef map, int> mappiii; 23 | typedef map mapii; 24 | typedef pair pii; 25 | typedef pair pll; 26 | typedef unordered_set useti; 27 | 28 | #define uset unordered_set 29 | #define it iterator 30 | #define mp make_pair 31 | #define pb push_back 32 | #define all(x) (x).begin(), (x).end() 33 | #define f first 34 | #define s second 35 | #define MOD 1000000007 36 | 37 | // S and T - input strings 38 | // You need to check if string T is present in S or not 39 | 40 | int findString(char S[], char T[]) { 41 | int length = strlen(T); 42 | int n = strlen(S); 43 | 44 | 45 | vector pitable(length, 0); 46 | int i = 0; 47 | int j = i+1; 48 | 49 | while(j 14 | using namespace std; 15 | 16 | typedef long long ll; 17 | typedef unordered_map umapii; 18 | typedef unordered_map umapib; 19 | typedef unordered_map umapsi; 20 | typedef unordered_map umapss; 21 | typedef map mapsi; 22 | typedef map, int> mappiii; 23 | typedef map mapii; 24 | typedef pair pii; 25 | typedef pair pll; 26 | typedef unordered_set useti; 27 | 28 | #define uset unordered_set 29 | #define it iterator 30 | #define mp make_pair 31 | #define pb push_back 32 | #define all(x) (x).begin(), (x).end() 33 | #define f first 34 | #define s second 35 | #define MOD 1000000007 36 | 37 | 38 | int main( int argc , char ** argv ) 39 | { 40 | ios_base::sync_with_stdio(false) ; 41 | cin.tie(NULL) ; 42 | 43 | 44 | 45 | 46 | 47 | 48 | return 0 ; 49 | 50 | 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /String/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/String/a.out -------------------------------------------------------------------------------- /String/input.txt: -------------------------------------------------------------------------------- 1 | aaaa -------------------------------------------------------------------------------- /String/output.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | 1 4 | 1 5 | 2 6 | 2 7 | 3 8 | 3 9 | 4 10 | 4 11 | 0 12 | 2 13 | 3 14 | 0 15 | 2 16 | -------------------------------------------------------------------------------- /String/palindromecount.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | /* 9 | PROBLEM STATEMENT 10 | Given a string S, count and return the number of substrings of S that are palindrome. 11 | Single length substrings are also palindromes. You just need to calculate the count, not the substrings. 12 | Input Format : 13 | String S 14 | Output Format : 15 | count of palindrome substrings 16 | Constraints : 17 | 1 <= Length of S <= 1000 18 | Sample Input : 19 | aba 20 | Sample Output : 21 | 4 22 | **Note : Here 4 corresponds to ("a","b","a","aba"). 23 | */ 24 | 25 | #include 26 | using namespace std; 27 | 28 | typedef long long ll; 29 | typedef unordered_map umapii; 30 | typedef unordered_map umapib; 31 | typedef unordered_map umapsi; 32 | typedef unordered_map umapss; 33 | typedef map mapsi; 34 | typedef map, int> mappiii; 35 | typedef map mapii; 36 | typedef pair pii; 37 | typedef pair pll; 38 | typedef unordered_set useti; 39 | 40 | #define uset unordered_set 41 | #define it iterator 42 | #define mp make_pair 43 | #define pb push_back 44 | #define all(x) (x).begin(), (x).end() 45 | #define f first 46 | #define s second 47 | #define MOD 1000000007 48 | 49 | int countPalindromes(string s) 50 | { 51 | int count = 0; 52 | unordered_map m; 53 | for (int i = 0; i < s.length(); i++) { 54 | 55 | // check for odd length palindromes 56 | for (int j = 0; j <= i; j++) { 57 | 58 | if (!s[i + j]) 59 | break; 60 | 61 | if (s[i - j] == s[i + j]) { 62 | 63 | // check for palindromes of length 64 | // greater than 1 65 | if ((i + j + 1) - (i - j) > 1){ 66 | m[s.substr(i - j, 67 | (i + j + 1) - (i - j))]++; 68 | count++; 69 | } 70 | 71 | } else 72 | break; 73 | } 74 | 75 | // check for even length palindromes 76 | for (int j = 0; j <= i; j++) { 77 | if (!s[i + j + 1]) 78 | break; 79 | if (s[i - j] == s[i + j + 1]) { 80 | 81 | // check for palindromes of length 82 | // greater than 1 83 | if ((i + j + 2) - (i - j) > 1) 84 | m[s.substr(i - j, 85 | (i + j + 2) - (i - j))]++; 86 | count++; 87 | 88 | } else 89 | break; 90 | } 91 | } 92 | return count; 93 | } 94 | int countPalindromeSubstrings(char s[]) { 95 | string str; 96 | for (int i = 0; s[i]!='\0'; ++i) 97 | { 98 | str += s[i]; 99 | } 100 | 101 | return countPalindromes(str)+str.size(); 102 | 103 | } 104 | int main( int argc , char ** argv ) 105 | { 106 | ios_base::sync_with_stdio(false) ; 107 | cin.tie(NULL) ; 108 | 109 | char input[10000]; 110 | cin >> input; 111 | cout << countPalindromeSubstrings(input) << endl; 112 | 113 | 114 | 115 | 116 | 117 | return 0 ; 118 | 119 | 120 | 121 | } 122 | 123 | -------------------------------------------------------------------------------- /Time-complexity-analysis/K-Concatenation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Time-complexity-analysis/K-Concatenation -------------------------------------------------------------------------------- /Time-complexity-analysis/K-Concatenation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | typedef long long ll; 14 | 15 | ll kadane(int arr[], int n){ 16 | ll curr_sum = 0; 17 | ll max_so_far = INT_MIN; 18 | for (int i = 0; i < n; ++i) 19 | { 20 | curr_sum = curr_sum +arr[i]; 21 | if (max_so_far=0; --i) 50 | { 51 | curr_sufffix_sum += arr[i]; 52 | max_suffix_sum = max(max_suffix_sum, curr_sufffix_sum); 53 | } 54 | 55 | ll ans; 56 | if (totalSum<0) 57 | { 58 | ans=max(max_suffix_sum+max_suffix_sum, kadanes_sum); 59 | }else{ 60 | ans = max(max_suffix_sum+max_prefix_sum+totalSum*(k-2), kadanes_sum); 61 | } 62 | return ans; 63 | 64 | 65 | 66 | } 67 | int main( int argc , char ** argv ) 68 | { 69 | ios_base::sync_with_stdio(false) ; 70 | cin.tie(NULL) ; 71 | 72 | int t; 73 | cin>>t; 74 | while(t--){ 75 | int n,k; 76 | cin>>n>>k; 77 | int arr[n]; 78 | for (int i = 0; i < n; ++i) 79 | { 80 | std::cin>>arr[i]; 81 | } 82 | cout << maxSubarraySum(arr, n, k) << '\n'; 83 | 84 | } 85 | 86 | 87 | return 0 ; 88 | 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Time-complexity-analysis/Mindbedingproduct.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | 14 | int main( int argc , char ** argv ) 15 | { 16 | ios_base::sync_with_stdio(false) ; 17 | cin.tie(NULL) ; 18 | 19 | int n; 20 | cin>>n; 21 | int* arr=new int[n]; 22 | 23 | for (int i = 0; i < n; ++i) 24 | { 25 | cin>>arr[i]; 26 | } 27 | 28 | long double sum=0; 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | sum+=(long double)log10(arr[i]); 33 | } 34 | 35 | for (int i = 0; i < n; ++i) 36 | { 37 | cout << int(pow((long double)10.00, sum-log10(arr[i]))) << " "; 38 | } 39 | 40 | 41 | 42 | return 0 ; 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Tries/Test.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/Tries/Test.txt -------------------------------------------------------------------------------- /Tries/autocomplete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class trienode{ 4 | public: 5 | char letter; 6 | bool eow;//end of word 7 | trienode** children=new trienode* [26]; 8 | trienode() 9 | { 10 | letter='\0'; 11 | eow=false; 12 | for(int i=0;i<26;i++) 13 | children[i]=NULL; 14 | } 15 | }; 16 | void insert(string s,trienode* head) 17 | { 18 | trienode* curr= head; 19 | int i=0; 20 | for(i=0;ichildren[index]!=NULL) 24 | { 25 | curr=curr->children[index]; 26 | } 27 | else 28 | { 29 | curr->children[index]=new trienode(); 30 | curr->children[index]->letter=s[i]; 31 | curr=curr->children[index]; 32 | } 33 | } 34 | 35 | if(i==s.length()) 36 | { 37 | curr->eow=true; 38 | } 39 | 40 | } 41 | void dfs(trienode* root,string in) 42 | { if(root->eow) 43 | cout<children[i]) 47 | dfs(root->children[i],in+root->children[i]->letter); 48 | } 49 | 50 | } 51 | int complete(string search,trienode* head) 52 | { trienode* curr = head; 53 | for(int i=0;ichildren[index]!=NULL) 57 | { 58 | curr=curr->children[index]; 59 | } 60 | else 61 | { 62 | return 0; 63 | } 64 | } 65 | dfs(curr,search); 66 | return 1; 67 | } 68 | int main() 69 | {int n; 70 | cin>>n; 71 | string input; 72 | trienode* head=new trienode(); 73 | for(int i=0;i>input; 76 | insert(input,head); 77 | } 78 | /* for(int i=0;i<26;i++) 79 | { 80 | if(head->children[i]) 81 | { 82 | cout<children[i]->letter<>q; 87 | string search; 88 | int result; 89 | for(int i=0;i>search; 92 | result=complete(search,head); 93 | if(result==0) 94 | { 95 | cout<<"No suggestions"< 2 | using namespace std; 3 | class trienode{ 4 | public: 5 | char letter; 6 | bool eow;//end of word 7 | trienode** children=new trienode* [26]; 8 | trienode() 9 | { 10 | letter='\0'; 11 | eow=false; 12 | for(int i=0;i<26;i++) 13 | children[i]=NULL; 14 | } 15 | }; 16 | void insert(string s,trienode* head) 17 | { 18 | trienode* curr= head; 19 | int i=0; 20 | for(i=0;ichildren[index]!=NULL) 24 | { 25 | curr=curr->children[index]; 26 | } 27 | else 28 | { 29 | curr->children[index]=new trienode(); 30 | curr->children[index]->letter=s[i]; 31 | curr=curr->children[index]; 32 | } 33 | } 34 | 35 | if(i==s.length()) 36 | { 37 | curr->eow=true; 38 | } 39 | 40 | } 41 | void dfs(trienode* root,string in) 42 | { if(root->eow) 43 | cout<children[i]) 47 | dfs(root->children[i],in+root->children[i]->letter); 48 | } 49 | 50 | } 51 | int complete(string search,trienode* head) 52 | { trienode* curr = head; 53 | for(int i=0;ichildren[index]!=NULL) 57 | { 58 | curr=curr->children[index]; 59 | } 60 | else 61 | { 62 | return 0; 63 | } 64 | } 65 | dfs(curr,search); 66 | return 1; 67 | } 68 | int main() 69 | {int n; 70 | cin>>n; 71 | string input; 72 | trienode* head=new trienode(); 73 | for(int i=0;i>input; 76 | insert(input,head); 77 | } 78 | /* for(int i=0;i<26;i++) 79 | { 80 | if(head->children[i]) 81 | { 82 | cout<children[i]->letter<>q; 87 | string search; 88 | int result; 89 | for(int i=0;i>search; 92 | result=complete(search,head); 93 | if(result==0) 94 | { 95 | cout<<"No suggestions"< 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 12 | This list is circular, means that 11 will follow 0 again. You will be given the student number ‘i’ and 13 | some position ‘p’. You will have to tell that if the list will start from (i+1)th student, 14 | then which student will be at pth position. 15 | Input Format: 16 | First line will have an integer ‘t’, denoting the number of test cases. 17 | Next line will have two space separated integers denoting the value of ‘i’ and ‘p’ respectively. 18 | Output Format: 19 | Print ‘t’ lines containing single integer denoting the student number. 20 | Constraints: 21 | 1 <= t <= 10^5 22 | 0 <= i <= 11 23 | 1 <= p <= 12 24 | Sample Input: 25 | 2 26 | 2 3 27 | 5 8 28 | Sample Output: 29 | 5 30 | 1 31 | Explanation: 32 | First, list will start at 3. 3 -> 4 -> 5. Hence, 5 will be at third position. 33 | Second, list will start at 6. 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 0 -> 1. Hence, 1 will be at 8th 34 | position. 35 | */ 36 | 37 | 38 | #include 39 | 40 | using namespace std; 41 | 42 | int go(int i, int p){ 43 | int ans = (i+p)%12 44 | return ans; 45 | } 46 | 47 | 48 | int main( int argc , char ** argv ) 49 | { 50 | ios_base::sync_with_stdio(false) ; 51 | cin.tie(NULL) ; 52 | 53 | int t; 54 | cin>>t; 55 | 56 | while(t--){ 57 | int i, p; 58 | cin>>i>>p; 59 | 60 | cout << go(i, p) << '\n'; 61 | } 62 | 63 | 64 | return 0 ; 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /adhoc problems/Test.txt: -------------------------------------------------------------------------------- 1 | 5 1 10 2 | 01000 -------------------------------------------------------------------------------- /adhoc problems/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mehulcoder/Coding-ninjas-competitive/ec8bb8c01e582662e5a6198cfa0154c301206d51/adhoc problems/a.out -------------------------------------------------------------------------------- /adhoc problems/winning.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Name: Mehul Chaturvedi 4 | IIT-Guwahati 5 | 6 | */ 7 | 8 | 9 | /* 10 | PROBLEM STATEMENT 11 | Our college team is going to the sports fest to play a football match with our coach. There are 12 | n players in our team, numbered from 1 to n. 13 | The coach will know the position of another team hence create a winning strategy. He creates the 14 | position of every player in a specific order so that we will win and then he starts swapping two 15 | players at a time to form the positions. 16 | He swaps payers in such a way that it can't be understood by another team: 17 | 1. Any player can swap with the player directly at front him 18 | 2. One player can swap at most with two other players 19 | If the specific order is formed then our team will win otherwise we will lose 20 | Input Format 21 | The First line contains numbers of players in team: n 22 | The second line contains n space separated integers denoting the specific position of players: 23 | i-th integer denotes the position of Ai player in winning strategy 24 | Output Format 25 | If our team wins print "YES"(without quotes) and in next line print the minimum numbers of 26 | swapping required to form this specific order otherwise print "NO"(without quotes) 27 | Constraints 28 | 1 =< n <= 10^5 29 | 1 <= Ai <= n 30 | Sample Input1: 31 | 5 32 | 2 1 5 3 4 33 | Sample Output1: 34 | YES 35 | 3 36 | Sample Input2: 37 | 5 38 | 2 5 1 3 4 39 | Sample Output3: 40 | NO 41 | Explaination 42 | In the First Sample case, 43 | Initial state: 1 2 3 4 5 44 | Three moves required to form this order: 45 | 1 2 3 4 5 -> 1 2 3 5 4 -> 1 2 5 3 4 -> 2 1 5 3 4 46 | In the second case, no way to form this specific order 47 | */ 48 | 49 | #include 50 | 51 | using namespace std; 52 | //Returns -1 if not possible 53 | int go(int* arr, int n){ 54 | if(n==1){ 55 | if(arr[0]!=1){ 56 | return -1; 57 | }else{ 58 | return 0; 59 | } 60 | } 61 | if(n==0){ 62 | return 0; 63 | } 64 | 65 | int count = 0; 66 | int i = 0; 67 | 68 | for (int j = 0; j < n; ++j) 69 | { 70 | if(arr[j]>j+3 || arr[j]i+3 || arr[i]>n; 128 | 129 | int* arr = new int[n]; 130 | for (int i = 0; i < n; ++i) 131 | { 132 | cin>>arr[i]; 133 | } 134 | 135 | int ans = go(arr, n); 136 | if (ans==-1) 137 | { 138 | cout << "NO" << '\n'; 139 | }else{ 140 | cout << "YES" << '\n' << ans <