-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from hardik302001/master
ISSUE RESOLVED: Put the random files into folders #206
- Loading branch information
Showing
8 changed files
with
291 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include<bits/stc++.h> | ||
|
||
|
||
|
||
int uniquePaths(int m, int n) { | ||
vector< vector<int>>dp(m,vector<int>(n)); | ||
for(int i = 0;i<m;i++){ | ||
dp[i][n-1] = 1; | ||
} | ||
for(int j = 0;j<n;j++){ | ||
dp[m-1][j] = 1; | ||
} | ||
for(int i = m-2;i>=0;i--){ | ||
for(int j = n-2;j>=0;j--){ | ||
dp[i][j]= dp[i+1][j] + dp[i][j+1]; | ||
} | ||
} | ||
|
||
|
||
return dp[0][0]; | ||
} | ||
|
||
|
||
int main(){ | ||
|
||
return 0; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// C++ program of a space optimized DP solution for | ||
// 0-1 knapsack problem. | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
// val[] is for storing maximum profit for each weight | ||
// wt[] is for storing weights | ||
// n number of item | ||
// W maximum capacity of bag | ||
// mat[2][W+1] to store final result | ||
int KnapSack(int val[], int wt[], int n, int W) | ||
{ | ||
// matrix to store final result | ||
int mat[2][W+1]; | ||
memset(mat, 0, sizeof(mat)); | ||
|
||
// iterate through all items | ||
int i = 0; | ||
while (i < n) // one by one traverse each element | ||
{ | ||
int j = 0; // traverse all weights j <= W | ||
|
||
// if i is odd that mean till now we have odd | ||
// number of elements so we store result in 1th | ||
// indexed row | ||
if (i%2!=0) | ||
{ | ||
while (++j <= W) // check for each value | ||
{ | ||
if (wt[i] <= j) // include element | ||
mat[1][j] = max(val[i] + mat[0][j-wt[i]], | ||
mat[0][j] ); | ||
else // exclude element | ||
mat[1][j] = mat[0][j]; | ||
} | ||
|
||
} | ||
|
||
// if i is even that mean till now we have even number | ||
// of elements so we store result in 0th indexed row | ||
else | ||
{ | ||
while(++j <= W) | ||
{ | ||
if (wt[i] <= j) | ||
mat[0][j] = max(val[i] + mat[1][j-wt[i]], | ||
mat[1][j]); | ||
else | ||
mat[0][j] = mat[1][j]; | ||
} | ||
} | ||
i++; | ||
} | ||
|
||
// Return mat[0][W] if n is odd, else mat[1][W] | ||
return (n%2 != 0)? mat[0][W] : mat[1][W]; | ||
} | ||
|
||
// Driver program to test the cases | ||
int main() | ||
{ | ||
int val[] = {7, 8, 4}, wt[] = {3, 8, 6}, W = 10, n = 3; | ||
cout << KnapSack(val, wt, n, W) << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int max(int a, int b); | ||
|
||
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */ | ||
int lcs( char *X, char *Y, int m, int n ) | ||
{ | ||
if (m == 0 || n == 0) | ||
return 0; | ||
if (X[m-1] == Y[n-1]) | ||
return 1 + lcs(X, Y, m-1, n-1); | ||
else | ||
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); | ||
} | ||
|
||
/* Utility function to get max of 2 integers */ | ||
int max(int a, int b) | ||
{ | ||
return (a > b)? a : b; | ||
} | ||
|
||
/* Driver code */ | ||
int main() | ||
{ | ||
char X[] = "AGGTAB"; | ||
char Y[] = "GXTXAYB"; | ||
|
||
int m = strlen(X); | ||
int n = strlen(Y); | ||
|
||
cout<<"Length of LCS is "<< lcs( X, Y, m, n ) ; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// C++ Program to find the maximum for | ||
// each and every contiguous subarray of size k. | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Method to find the maximum for each | ||
// and every contiguous subarray of size k. | ||
void printKMax(int arr[], int n, int k) | ||
{ | ||
int j, max; | ||
|
||
for (int i = 0; i <= n - k; i++) | ||
{ | ||
max = arr[i]; | ||
|
||
for (j = 1; j < k; j++) | ||
{ | ||
if (arr[i + j] > max) | ||
max = arr[i + j]; | ||
} | ||
cout << max << " "; | ||
} | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
int k = 3; | ||
printKMax(arr, n, k); | ||
return 0; | ||
} |
70 changes: 35 additions & 35 deletions
70
maximum_fixed_point.cpp → Searching Algo/maximum_fixed_point.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
// C++ implementation of the above approach | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Function to find the maximum index | ||
// i such that arr[i] is equal to i | ||
void findLargestIndex(int arr[], int n) | ||
{ | ||
|
||
// Traversing the array from | ||
// backwards | ||
for (int i = n - 1; i >= 0; i--) { | ||
|
||
// If arr[i] is equal to i | ||
if (arr[i] == i) { | ||
cout << i << endl; | ||
return; | ||
} | ||
} | ||
|
||
// If there is no such index | ||
cout << -1 << endl; | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
// Given Input | ||
int arr[] = { -10, -5, 0, 3, 7 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
|
||
// Function Call | ||
findLargestIndex(arr, n); | ||
return 0; | ||
} | ||
// C++ implementation of the above approach | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Function to find the maximum index | ||
// i such that arr[i] is equal to i | ||
void findLargestIndex(int arr[], int n) | ||
{ | ||
|
||
// Traversing the array from | ||
// backwards | ||
for (int i = n - 1; i >= 0; i--) { | ||
|
||
// If arr[i] is equal to i | ||
if (arr[i] == i) { | ||
cout << i << endl; | ||
return; | ||
} | ||
} | ||
|
||
// If there is no such index | ||
cout << -1 << endl; | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
// Given Input | ||
int arr[] = { -10, -5, 0, 3, 7 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
|
||
// Function Call | ||
findLargestIndex(arr, n); | ||
return 0; | ||
} |
60 changes: 30 additions & 30 deletions
60
palindrome_check_using_stl.cpp → ...Algorithms/palindrome_check_using_stl.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
// C++ program for the above approach | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Function to check whether string | ||
// is palindrome | ||
string isPalindrome(string S) | ||
{ | ||
// Iterate over the range [0, N/2] | ||
for (int i = 0; i < S.length() / 2; i++) { | ||
|
||
// If S[i] is not equal to | ||
// the S[N-i-1] | ||
if (S[i] != S[S.length() - i - 1]) { | ||
// Return No | ||
return "No"; | ||
} | ||
} | ||
// Return "Yes" | ||
return "Yes"; | ||
} | ||
|
||
// Driver Code | ||
int main() | ||
{ | ||
string S = "ABCDCBA"; | ||
cout << isPalindrome(S); | ||
|
||
return 0; | ||
} | ||
// C++ program for the above approach | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Function to check whether string | ||
// is palindrome | ||
string isPalindrome(string S) | ||
{ | ||
// Iterate over the range [0, N/2] | ||
for (int i = 0; i < S.length() / 2; i++) { | ||
|
||
// If S[i] is not equal to | ||
// the S[N-i-1] | ||
if (S[i] != S[S.length() - i - 1]) { | ||
// Return No | ||
return "No"; | ||
} | ||
} | ||
// Return "Yes" | ||
return "Yes"; | ||
} | ||
|
||
// Driver Code | ||
int main() | ||
{ | ||
string S = "ABCDCBA"; | ||
cout << isPalindrome(S); | ||
|
||
return 0; | ||
} |