Skip to content

Commit

Permalink
Merge pull request #217 from hardik302001/master
Browse files Browse the repository at this point in the history
ISSUE RESOLVED: Put the random files into folders #206
  • Loading branch information
siddharth25pandey authored Oct 1, 2021
2 parents 7f45df0 + c9ea2b3 commit feee041
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 66 deletions.
27 changes: 27 additions & 0 deletions DP - LEETCODE/unique_paths.cpp
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.
65 changes: 65 additions & 0 deletions DP/knapsack.cpp
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;
}
35 changes: 35 additions & 0 deletions DP/longest_subsequence.cpp
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;
}
67 changes: 66 additions & 1 deletion Function overloading/factorial_of_big_numbers.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
<<<<<<< HEAD:hack.cpp
/ C++ program to compute factorial of big numbers
#include<iostream>
using namespace std;

// Maximum number of digits in output
#define MAX 500

int multiply(int x, int res[], int res_size);

// This function finds factorial of large numbers
// and prints them
void factorial(int n)
{
int res[MAX];

// Initialize result
res[0] = 1;
int res_size = 1;

// Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
for (int x=2; x<=n; x++)
res_size = multiply(x, res, res_size);

cout << "Factorial of given number is \n";
for (int i=res_size-1; i>=0; i--)
cout << res[i];
}


int multiply(int x, int res[], int res_size)
{
int carry = 0; // Initialize carry

// One by one multiply n with individual digits of res[]
for (int i=0; i<res_size; i++)
{
int prod = res[i] * x + carry;

// Store last digit of 'prod' in res[]
res[i] = prod % 10;

// Put rest in carry
carry = prod/10;
}

// Put carry in res and increase result size
while (carry)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}

// Driver program
int main()
{
factorial(100);
factorial(50);
return 0;
}
=======
/ C++ program to compute factorial of big numbers
#include<iostream>
using namespace std;
Expand Down Expand Up @@ -58,4 +122,5 @@ int main()
{
factorial(100);
return 0;
}
}
>>>>>>> upstream/master:Function overloading/factorial_of_big_numbers.cpp
33 changes: 33 additions & 0 deletions SLIDING WINDOW/sliding_window.cpp
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 maximum_fixed_point.cpp → Searching Algo/maximum_fixed_point.cpp
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;
}
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;
}

0 comments on commit feee041

Please sign in to comment.