formatting source-code for d7af6fdc8c

This commit is contained in:
github-actions
2020-05-29 23:26:30 +00:00
parent edb3d51ec2
commit 7ad1f171c1
176 changed files with 5342 additions and 4288 deletions

View File

@@ -1,9 +1,9 @@
//0-1 Knapsack problem - Dynamic programming
// 0-1 Knapsack problem - Dynamic programming
//#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//void Print(int res[20][20], int i, int j, int capacity)
// void Print(int res[20][20], int i, int j, int capacity)
//{
// if(i==0 || j==0)
// {
@@ -30,42 +30,43 @@ using namespace std;
int Knapsack(int capacity, int n, int weight[], int value[])
{
int res[20][20];
for (int i = 0; i < n + 1; ++i)
{
for (int j = 0; j < capacity + 1; ++j)
{
if (i == 0 || j == 0)
res[i][j] = 0;
else if (weight[i - 1] <= j)
res[i][j] = max(value[i - 1] + res[i - 1][j - weight[i - 1]], res[i - 1][j]);
else
res[i][j] = res[i - 1][j];
}
}
// Print(res, n, capacity, capacity);
// cout<<"\n";
return res[n][capacity];
int res[20][20];
for (int i = 0; i < n + 1; ++i)
{
for (int j = 0; j < capacity + 1; ++j)
{
if (i == 0 || j == 0)
res[i][j] = 0;
else if (weight[i - 1] <= j)
res[i][j] = max(value[i - 1] + res[i - 1][j - weight[i - 1]],
res[i - 1][j]);
else
res[i][j] = res[i - 1][j];
}
}
// Print(res, n, capacity, capacity);
// cout<<"\n";
return res[n][capacity];
}
int main()
{
int n;
cout << "Enter number of items: ";
cin >> n;
int weight[n], value[n];
cout << "Enter weights: ";
for (int i = 0; i < n; ++i)
{
cin >> weight[i];
}
cout << "Enter values: ";
for (int i = 0; i < n; ++i)
{
cin >> value[i];
}
int capacity;
cout << "Enter capacity: ";
cin >> capacity;
cout << Knapsack(capacity, n, weight, value);
return 0;
int n;
cout << "Enter number of items: ";
cin >> n;
int weight[n], value[n];
cout << "Enter weights: ";
for (int i = 0; i < n; ++i)
{
cin >> weight[i];
}
cout << "Enter values: ";
for (int i = 0; i < n; ++i)
{
cin >> value[i];
}
int capacity;
cout << "Enter capacity: ";
cin >> capacity;
cout << Knapsack(capacity, n, weight, value);
return 0;
}

View File

@@ -1,21 +1,23 @@
// Program to check whether a number is an armstrong number or not
#include <iostream>
using std::cout;
using std::cin;
using std::cout;
int main() {
int n, k, d, s = 0;
cout << "Enter a number:";
cin >> n;
k = n;
while (k != 0) {
d = k % 10;
s += d * d * d;
k /= 10;
}
if (s == n)
cout << n << "is an armstrong number";
else
cout << n << "is not an armstrong number";
int main()
{
int n, k, d, s = 0;
cout << "Enter a number:";
cin >> n;
k = n;
while (k != 0)
{
d = k % 10;
s += d * d * d;
k /= 10;
}
if (s == n)
cout << n << "is an armstrong number";
else
cout << n << "is not an armstrong number";
}

View File

@@ -1,128 +1,129 @@
#include <iostream>
#include <limits.h>
#include <iostream>
using namespace std;
//Wrapper class for storing an edge
// Wrapper class for storing an edge
class Edge
{
public:
int src, dst, weight;
public:
int src, dst, weight;
};
//Wrapper class for storing a graph
// Wrapper class for storing a graph
class Graph
{
public:
int vertexNum, edgeNum;
Edge *edges;
public:
int vertexNum, edgeNum;
Edge *edges;
//Constructs a graph with V vertices and E edges
Graph(int V, int E)
{
this->vertexNum = V;
this->edgeNum = E;
this->edges = (Edge *)malloc(E * sizeof(Edge));
}
// Constructs a graph with V vertices and E edges
Graph(int V, int E)
{
this->vertexNum = V;
this->edgeNum = E;
this->edges = (Edge *)malloc(E * sizeof(Edge));
}
//Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
static int edgeInd = 0;
if (edgeInd < this->edgeNum)
{
Edge newEdge;
newEdge.src = src;
newEdge.dst = dst;
newEdge.weight = weight;
this->edges[edgeInd++] = newEdge;
}
}
// Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
static int edgeInd = 0;
if (edgeInd < this->edgeNum)
{
Edge newEdge;
newEdge.src = src;
newEdge.dst = dst;
newEdge.weight = weight;
this->edges[edgeInd++] = newEdge;
}
}
};
//Utility function to print distances
// Utility function to print distances
void print(int dist[], int V)
{
cout << "\nVertex Distance" << endl;
for (int i = 0; i < V; i++)
{
if (dist[i] != INT_MAX)
cout << i << "\t" << dist[i] << endl;
else
cout << i << "\tINF" << endl;
}
cout << "\nVertex Distance" << endl;
for (int i = 0; i < V; i++)
{
if (dist[i] != INT_MAX)
cout << i << "\t" << dist[i] << endl;
else
cout << i << "\tINF" << endl;
}
}
//The main function that finds the shortest path from given source
//to all other vertices using Bellman-Ford.It also detects negative
//weight cycle
// The main function that finds the shortest path from given source
// to all other vertices using Bellman-Ford.It also detects negative
// weight cycle
void BellmanFord(Graph graph, int src)
{
int V = graph.vertexNum;
int E = graph.edgeNum;
int dist[V];
int V = graph.vertexNum;
int E = graph.edgeNum;
int dist[V];
//Initialize distances array as INF for all except source
//Intialize source as zero
for (int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = 0;
// Initialize distances array as INF for all except source
// Intialize source as zero
for (int i = 0; i < V; i++) dist[i] = INT_MAX;
dist[src] = 0;
//Calculate shortest path distance from source to all edges
//A path can contain maximum (|V|-1) edges
for (int i = 0; i <= V - 1; i++)
for (int j = 0; j < E; j++)
{
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
// Calculate shortest path distance from source to all edges
// A path can contain maximum (|V|-1) edges
for (int i = 0; i <= V - 1; i++)
for (int j = 0; j < E; j++)
{
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
dist[v] = dist[u] + w;
}
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
dist[v] = dist[u] + w;
}
//Iterate inner loop once more to check for negative cycle
for (int j = 0; j < E; j++)
{
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
// Iterate inner loop once more to check for negative cycle
for (int j = 0; j < E; j++)
{
int u = graph.edges[j].src;
int v = graph.edges[j].dst;
int w = graph.edges[j].weight;
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
{
cout << "Graph contains negative weight cycle. Hence, shortest distance not guaranteed." << endl;
return;
}
}
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
{
cout << "Graph contains negative weight cycle. Hence, shortest "
"distance not guaranteed."
<< endl;
return;
}
}
print(dist, V);
print(dist, V);
return;
return;
}
//Driver Function
// Driver Function
int main()
{
int V, E, gsrc;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V, E);
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;
G.addEdge(src, dst, weight);
}
cout << "\nEnter source: ";
cin >> gsrc;
BellmanFord(G, gsrc);
int V, E, gsrc;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V, E);
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;
G.addEdge(src, dst, weight);
}
cout << "\nEnter source: ";
cin >> gsrc;
BellmanFord(G, gsrc);
return 0;
return 0;
}

View File

@@ -9,7 +9,7 @@
#include <iostream>
using namespace std;
int *cat; // global array to hold catalan numbers
int *cat; // global array to hold catalan numbers
unsigned long int catalan_dp(int n)
{
@@ -33,7 +33,7 @@ unsigned long int catalan_dp(int n)
{
cat[i] = 0;
for (int j = 0; j < i; j++)
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
}
// Return the result

View File

@@ -1,50 +1,52 @@
#include <iostream>
#include <climits>
#include <iostream>
using namespace std;
// Function to find the Minimum number of coins required to get Sum S
int findMinCoins(int arr[], int n, int N)
{
// dp[i] = no of coins required to get a total of i
int dp[N + 1];
// dp[i] = no of coins required to get a total of i
int dp[N + 1];
// 0 coins are needed for 0 sum
// 0 coins are needed for 0 sum
dp[0] = 0;
dp[0] = 0;
for (int i = 1; i <= N; i++)
{
// initialize minimum number of coins needed to infinity
dp[i] = INT_MAX;
int res = INT_MAX;
for (int i = 1; i <= N; i++)
{
// initialize minimum number of coins needed to infinity
dp[i] = INT_MAX;
int res = INT_MAX;
// do for each coin
for (int c = 0; c < n; c++)
{
if (i - arr[c] >= 0) // check if coins doesn't become negative by including it
res = dp[i - arr[c]];
// do for each coin
for (int c = 0; c < n; c++)
{
if (i - arr[c] >=
0) // check if coins doesn't become negative by including it
res = dp[i - arr[c]];
// if total can be reached by including current coin c,
// update minimum number of coins needed dp[i]
if (res != INT_MAX)
dp[i] = min(dp[i], res + 1);
}
}
// if total can be reached by including current coin c,
// update minimum number of coins needed dp[i]
if (res != INT_MAX)
dp[i] = min(dp[i], res + 1);
}
}
// The Minimum No of Coins Required for N = dp[N]
return dp[N];
// The Minimum No of Coins Required for N = dp[N]
return dp[N];
}
int main()
{
// No of Coins We Have
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
// No of Coins We Have
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
// Total Change Required
int N = 15;
// Total Change Required
int N = 15;
cout << "Minimum Number of Coins Required " << findMinCoins(arr, n, N) << "\n";
cout << "Minimum Number of Coins Required " << findMinCoins(arr, n, N)
<< "\n";
return 0;
return 0;
}

View File

@@ -1,6 +1,6 @@
/*Given a rod of length n inches and an array of prices that
contains prices of all pieces of size smaller than n. Determine
the maximum value obtainable by cutting up the rod and selling
/*Given a rod of length n inches and an array of prices that
contains prices of all pieces of size smaller than n. Determine
the maximum value obtainable by cutting up the rod and selling
the pieces.*/
#include <iostream>
@@ -22,7 +22,8 @@ int cutrod(int p[], int n)
}
int main()
{
int price[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
int price[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
cout << cutrod(price, 30);
return 0;
}

View File

@@ -7,7 +7,7 @@
* a. Insert
* b. Remove
* c. Replace
* All of the above operations are
* All of the above operations are
* of equal cost
*/
@@ -15,10 +15,7 @@
#include <string>
using namespace std;
int min(int x, int y, int z)
{
return min(min(x, y), z);
}
int min(int x, int y, int z) { return min(min(x, y), z); }
/* A Naive recursive C++ program to find
* minimum number of operations to convert
@@ -32,14 +29,14 @@ int editDist(string str1, string str2, int m, int n)
if (n == 0)
return m;
//If last characters are same then continue
//for the rest of them.
// If last characters are same then continue
// for the rest of them.
if (str1[m - 1] == str2[n - 1])
return editDist(str1, str2, m - 1, n - 1);
//If last not same, then 3 possibilities
//a.Insert b.Remove c. Replace
//Get min of three and continue for rest.
// If last not same, then 3 possibilities
// a.Insert b.Remove c. Replace
// Get min of three and continue for rest.
return 1 + min(editDist(str1, str2, m, n - 1),
editDist(str1, str2, m - 1, n),
editDist(str1, str2, m - 1, n - 1));
@@ -50,31 +47,30 @@ int editDist(string str1, string str2, int m, int n)
*/
int editDistDP(string str1, string str2, int m, int n)
{
//Create Table for SubProblems
// Create Table for SubProblems
int dp[m + 1][n + 1];
//Fill d[][] in bottom up manner
// Fill d[][] in bottom up manner
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
//If str1 empty. Then add all of str2
// If str1 empty. Then add all of str2
if (i == 0)
dp[i][j] = j;
//If str2 empty. Then add all of str1
// If str2 empty. Then add all of str1
else if (j == 0)
dp[i][j] = i;
//If character same. Recur for remaining
// If character same. Recur for remaining
else if (str1[i - 1] == str2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = 1 + min(dp[i][j - 1], //Insert
dp[i - 1][j], //Remove
dp[i - 1][j - 1] //Replace
dp[i][j] = 1 + min(dp[i][j - 1], // Insert
dp[i - 1][j], // Remove
dp[i - 1][j - 1] // Replace
);
}
}

View File

@@ -1,9 +1,9 @@
/* Function to get minimun number of trials needed
* in worst case with n eggs and k floors
/* Function to get minimun number of trials needed
* in worst case with n eggs and k floors
*/
#include <iostream>
#include <climits>
#include <iostream>
using namespace std;
int eggDrop(int n, int k)
@@ -13,8 +13,8 @@ int eggDrop(int n, int k)
for (int i = 1; i <= n; i++)
{
eggFloor[i][1] = 1; //n eggs..1 Floor
eggFloor[i][0] = 0; //n eggs..0 Floor
eggFloor[i][1] = 1; // n eggs..1 Floor
eggFloor[i][0] = 0; // n eggs..0 Floor
}
// Only one egg available

View File

@@ -2,23 +2,23 @@
using namespace std;
int fib(int n)
{
int res[3];
res[0] = 0;
res[1] = 1;
for (int i = 2; i <= n; i++)
{
res[2] = res[1] + res[0];
res[0] = res[1];
res[1] = res[2];
}
return res[1];
int res[3];
res[0] = 0;
res[1] = 1;
for (int i = 2; i <= n; i++)
{
res[2] = res[1] + res[0];
res[0] = res[1];
res[1] = res[2];
}
return res[1];
}
int main(int argc, char const *argv[])
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Fibonacci number is ";
cout << fib(n) << endl;
return 0;
int n;
cout << "Enter n: ";
cin >> n;
cout << "Fibonacci number is ";
cout << fib(n) << endl;
return 0;
}

View File

@@ -3,24 +3,24 @@ using namespace std;
int arr[1000000];
int fib(int n)
{
if (arr[n] == -1)
{
if (n <= 1)
arr[n] = n;
else
arr[n] = fib(n - 1) + fib(n - 2);
}
return arr[n];
if (arr[n] == -1)
{
if (n <= 1)
arr[n] = n;
else
arr[n] = fib(n - 1) + fib(n - 2);
}
return arr[n];
}
int main(int argc, char const *argv[])
{
int n;
cout << "Enter n: ";
cin >> n;
for (int i = 0; i < n + 1; ++i)
{
arr[i] = -1;
}
cout << "Fibonacci number is " << fib(n) << endl;
return 0;
int n;
cout << "Enter n: ";
cin >> n;
for (int i = 0; i < n + 1; ++i)
{
arr[i] = -1;
}
cout << "Fibonacci number is " << fib(n) << endl;
return 0;
}

View File

@@ -1,112 +1,110 @@
#include <iostream>
#include <limits.h>
#include <string.h>
#include <iostream>
using namespace std;
//Wrapper class for storing a graph
// Wrapper class for storing a graph
class Graph
{
public:
int vertexNum;
int **edges;
public:
int vertexNum;
int **edges;
//Constructs a graph with V vertices and E edges
Graph(int V)
{
this->vertexNum = V;
this->edges = (int **)malloc(V * sizeof(int *));
for (int i = 0; i < V; i++)
{
this->edges[i] = (int *)malloc(V * sizeof(int));
for (int j = 0; j < V; j++)
this->edges[i][j] = INT_MAX;
this->edges[i][i] = 0;
}
}
// Constructs a graph with V vertices and E edges
Graph(int V)
{
this->vertexNum = V;
this->edges = (int **)malloc(V * sizeof(int *));
for (int i = 0; i < V; i++)
{
this->edges[i] = (int *)malloc(V * sizeof(int));
for (int j = 0; j < V; j++) this->edges[i][j] = INT_MAX;
this->edges[i][i] = 0;
}
}
//Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
this->edges[src][dst] = weight;
}
// Adds the given edge to the graph
void addEdge(int src, int dst, int weight)
{
this->edges[src][dst] = weight;
}
};
//Utility function to print distances
// Utility function to print distances
void print(int dist[], int V)
{
cout << "\nThe Distance matrix for Floyd - Warshall" << endl;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i * V + j] != INT_MAX)
cout << dist[i * V + j] << "\t";
else
cout << "INF"
<< "\t";
}
cout << endl;
}
cout << "\nThe Distance matrix for Floyd - Warshall" << endl;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i * V + j] != INT_MAX)
cout << dist[i * V + j] << "\t";
else
cout << "INF"
<< "\t";
}
cout << endl;
}
}
//The main function that finds the shortest path from a vertex
//to all other vertices using Floyd-Warshall Algorithm.
// The main function that finds the shortest path from a vertex
// to all other vertices using Floyd-Warshall Algorithm.
void FloydWarshall(Graph graph)
{
int V = graph.vertexNum;
int dist[V][V];
int V = graph.vertexNum;
int dist[V][V];
//Initialise distance array
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph.edges[i][j];
// Initialise distance array
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++) dist[i][j] = graph.edges[i][j];
//Calculate distances
for (int k = 0; k < V; k++)
//Choose an intermediate vertex
// Calculate distances
for (int k = 0; k < V; k++)
// Choose an intermediate vertex
for (int i = 0; i < V; i++)
//Choose a source vertex for given intermediate
for (int i = 0; i < V; i++)
// Choose a source vertex for given intermediate
for (int j = 0; j < V; j++)
//Choose a destination vertex for above source vertex
for (int j = 0; j < V; j++)
// Choose a destination vertex for above source vertex
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
//If the distance through intermediate vertex is less than direct edge then update value in distance array
dist[i][j] = dist[i][k] + dist[k][j];
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX &&
dist[i][k] + dist[k][j] < dist[i][j])
// If the distance through intermediate vertex is less than
// direct edge then update value in distance array
dist[i][j] = dist[i][k] + dist[k][j];
//Convert 2d array to 1d array for print
int dist1d[V * V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist1d[i * V + j] = dist[i][j];
// Convert 2d array to 1d array for print
int dist1d[V * V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++) dist1d[i * V + j] = dist[i][j];
print(dist1d, V);
print(dist1d, V);
}
//Driver Function
// Driver Function
int main()
{
int V, E;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V);
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;
G.addEdge(src, dst, weight);
}
FloydWarshall(G);
int V, E;
int src, dst, weight;
cout << "Enter number of vertices: ";
cin >> V;
cout << "Enter number of edges: ";
cin >> E;
Graph G(V);
for (int i = 0; i < E; i++)
{
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";
cin >> dst;
cout << "Enter weight: ";
cin >> weight;
G.addEdge(src, dst, weight);
}
FloydWarshall(G);
return 0;
return 0;
}

View File

@@ -1,10 +1,12 @@
#include<iostream>
#include<climits>
#include <climits>
#include <iostream>
int maxSubArraySum(int a[], int size) {
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++) {
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
@@ -15,13 +17,14 @@ int maxSubArraySum(int a[], int size) {
return max_so_far;
}
int main() {
int main()
{
int n, i;
std::cout << "Enter the number of elements \n";
std::cin >> n;
int a[n]; // NOLINT
for (i = 0; i < n; i++) {
for (i = 0; i < n; i++)
{
std::cin >> a[i];
}
int max_sum = maxSubArraySum(a, n);

View File

@@ -1,65 +1,59 @@
#include <iosrteam>
using namespace std;
int max(int a,int b)
{
return (a > b) ? a : b;
}
int max(int a, int b) { return (a > b) ? a : b; }
int main()
{
char str1[]="DEFBCD";
char str2[]="ABDEFJ";
int i,j,k;
int n=strlen(str1)+1;
int m=strlen(str2)+1;
//cout<<n<<" "<<m<<"\n";
char str1[] = "DEFBCD";
char str2[] = "ABDEFJ";
int i, j, k;
int n = strlen(str1) + 1;
int m = strlen(str2) + 1;
// cout<<n<<" "<<m<<"\n";
int a[m][n];
for(i=0;i<m;i++)
for (i = 0; i < m; i++)
{
for(j=0;j<n;j++)
for (j = 0; j < n; j++)
{
if(i==0 || j==0)
a[i][j]=0;
if (i == 0 || j == 0)
a[i][j] = 0;
else if(str1[i-1] == str2[j-1])
a[i][j]=a[i-1][j-1]+1;
else if (str1[i - 1] == str2[j - 1])
a[i][j] = a[i - 1][j - 1] + 1;
else
a[i][j]=0;
a[i][j] = 0;
}
}
/*for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
}*/
int ma=-1;
int indi,indj;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
/*for(i=0;i<m;i++)
{
if(a[i][j]>ma)
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
}*/
int ma = -1;
int indi, indj;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (a[i][j] > ma)
{
ma=a[i][j];
indi=i;
indj=j;
ma = a[i][j];
indi = i;
indj = j;
}
}
}
cout<<str1<<"\n";
cout<<str2<<"\n";
cout << str1 << "\n";
cout << str2 << "\n";
cout<<"longest string size = "<<ma/*<<" "<<indi<<" "<<indj*/<<"\n";
for(i=indi-3;i<indi;i++)
cout<<str1[i];
cout<<"\n";
cout << "longest string size = " << ma /*<<" "<<indi<<" "<<indj*/ << "\n";
for (i = indi - 3; i < indi; i++) cout << str1[i];
cout << "\n";
}

View File

@@ -1,4 +1,4 @@
//Longest common subsequence - Dynamic Programming
// Longest common subsequence - Dynamic Programming
#include <iostream>
using namespace std;
@@ -52,19 +52,22 @@ int lcs(string a, string b)
else if (a[i - 1] == b[j - 1])
{
res[i][j] = 1 + res[i - 1][j - 1];
trace[i][j] = 1; // 1 means trace the matrix in upper left diagonal direction.
trace[i][j] = 1; // 1 means trace the matrix in upper left
// diagonal direction.
}
else
{
if (res[i - 1][j] > res[i][j - 1])
{
res[i][j] = res[i - 1][j];
trace[i][j] = 2; // 2 means trace the matrix in upwards direction.
trace[i][j] =
2; // 2 means trace the matrix in upwards direction.
}
else
{
res[i][j] = res[i][j - 1];
trace[i][j] = 3; // means trace the matrix in left direction.
trace[i][j] =
3; // means trace the matrix in left direction.
}
}
}

View File

@@ -1,39 +1,39 @@
//Program to calculate length of longest increasing subsequence in an array
// Program to calculate length of longest increasing subsequence in an array
#include <bits/stdc++.h>
using namespace std;
int LIS(int a[], int n)
{
int lis[n];
for (int i = 0; i < n; ++i)
{
lis[i] = 1;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
if (a[i] > a[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
}
}
int res = 0;
for (int i = 0; i < n; ++i)
{
res = max(res, lis[i]);
}
return res;
int lis[n];
for (int i = 0; i < n; ++i)
{
lis[i] = 1;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
if (a[i] > a[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
}
}
int res = 0;
for (int i = 0; i < n; ++i)
{
res = max(res, lis[i]);
}
return res;
}
int main(int argc, char const *argv[])
{
int n;
cout << "Enter size of array: ";
cin >> n;
int a[n];
cout << "Enter array elements: ";
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
cout << LIS(a, n) << endl;
return 0;
int n;
cout << "Enter size of array: ";
cin >> n;
int a[n];
cout << "Enter array elements: ";
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
cout << LIS(a, n) << endl;
return 0;
}

View File

@@ -1,4 +1,4 @@
//Program to calculate length of longest increasing subsequence in an array
// Program to calculate length of longest increasing subsequence in an array
// in O(n log n)
// tested on : https://cses.fi/problemset/task/1145/
@@ -7,30 +7,32 @@
using namespace std;
int LIS(int arr[], int n)
{
set < int > active; // The current built LIS.
set<int> active; // The current built LIS.
active.insert(arr[0]);
// Loop through every element.
for (int i = 1; i < n; ++i)
{
auto get = active.lower_bound(arr[i]);
if (get == active.end())
{
{
active.insert(arr[i]);
} // current element is the greatest so LIS increases by 1.
} // current element is the greatest so LIS increases by 1.
else
{
int val = * get; // we find the position where arr[i] will be in the LIS. If it is in the LIS already we do nothing
{
int val = *get; // we find the position where arr[i] will be in the
// LIS. If it is in the LIS already we do nothing
if (val > arr[i])
{
// else we remove the bigger element and add a smaller element (which is arr[i]) and continue;
{
// else we remove the bigger element and add a smaller element
// (which is arr[i]) and continue;
active.erase(get);
active.insert(arr[i]);
}
}
}
return active.size(); // size of the LIS.
return active.size(); // size of the LIS.
}
int main(int argc, char const * argv[])
int main(int argc, char const* argv[])
{
int n;
cout << "Enter size of array: ";

View File

@@ -1,5 +1,5 @@
#include <iostream>
#include <climits>
#include <iostream>
using namespace std;
#define MAX 10
@@ -7,56 +7,59 @@ using namespace std;
// dp table to store the solution for already computed sub problems
int dp[MAX][MAX];
// Function to find the most efficient way to multiply the given sequence of matrices
// Function to find the most efficient way to multiply the given sequence of
// matrices
int MatrixChainMultiplication(int dim[], int i, int j)
{
// base case: one matrix
if (j <= i + 1)
return 0;
// base case: one matrix
if (j <= i + 1)
return 0;
// stores minimum number of scalar multiplications (i.e., cost)
// needed to compute the matrix M[i+1]...M[j] = M[i..j]
int min = INT_MAX;
// stores minimum number of scalar multiplications (i.e., cost)
// needed to compute the matrix M[i+1]...M[j] = M[i..j]
int min = INT_MAX;
// if dp[i][j] is not calculated (calculate it!!)
// if dp[i][j] is not calculated (calculate it!!)
if (dp[i][j] == 0)
{
// take the minimum over each possible position at which the
// sequence of matrices can be split
if (dp[i][j] == 0)
{
// take the minimum over each possible position at which the
// sequence of matrices can be split
for (int k = i + 1; k <= j - 1; k++)
{
// recur for M[i+1]..M[k] to get a i x k matrix
int cost = MatrixChainMultiplication(dim, i, k);
for (int k = i + 1; k <= j - 1; k++)
{
// recur for M[i+1]..M[k] to get a i x k matrix
int cost = MatrixChainMultiplication(dim, i, k);
// recur for M[k+1]..M[j] to get a k x j matrix
cost += MatrixChainMultiplication(dim, k, j);
// recur for M[k+1]..M[j] to get a k x j matrix
cost += MatrixChainMultiplication(dim, k, j);
// cost to multiply two (i x k) and (k x j) matrix
cost += dim[i] * dim[k] * dim[j];
// cost to multiply two (i x k) and (k x j) matrix
cost += dim[i] * dim[k] * dim[j];
if (cost < min)
min = cost; // store the minimum cost
}
dp[i][j] = min;
}
if (cost < min)
min = cost; // store the minimum cost
}
dp[i][j] = min;
}
// return min cost to multiply M[j+1]..M[j]
return dp[i][j];
// return min cost to multiply M[j+1]..M[j]
return dp[i][j];
}
// main function
int main()
{
// Matrix i has Dimensions dim[i-1] & dim[i] for i=1..n
// input is 10 x 30 matrix, 30 x 5 matrix, 5 x 60 matrix
int dim[] = {10, 30, 5, 60};
int n = sizeof(dim) / sizeof(dim[0]);
// Matrix i has Dimensions dim[i-1] & dim[i] for i=1..n
// input is 10 x 30 matrix, 30 x 5 matrix, 5 x 60 matrix
int dim[] = {10, 30, 5, 60};
int n = sizeof(dim) / sizeof(dim[0]);
// Function Calling: MatrixChainMultiplications(dimensions_array, starting, ending);
// Function Calling: MatrixChainMultiplications(dimensions_array, starting,
// ending);
cout << "Minimum cost is " << MatrixChainMultiplication(dim, 0, n - 1) << "\n";
cout << "Minimum cost is " << MatrixChainMultiplication(dim, 0, n - 1)
<< "\n";
return 0;
return 0;
}

View File

@@ -1,41 +1,42 @@
/*
*this program is use to find any elemet in any row with variable array size
*aplication of pointer is use in it
*important point start from here to:
*the index value of array can be go to 1 to 100000
*check till array[1000]
*end here
*how to work example:
**Question:
***number of array 2
***quarry 3
***array 1 is {1 2 3 4 5}
***array 2 is {6 7}
****i) what is 2nd element in 1st array
****ii) what is 1st element in 2nd array
****iii) what is 5th element in 1st array
*****output:
*****Enter Number of array you want to Store : 2
*****Enter Number of Question or Quary you want to do Related to Array : 3
*****Enter number of element in 1 rows : 5
*****Enter the element of Array 1 2 3 4 5
*****Enter number of element in 2 rows : 2
*****Enter the element of Array 6 7
*****enter the number of row which element You want to find : 1
*****enter the position of element which You want to find : 2
*****The element is 2
*****enter the number of row which element You want to find : 2
*****enter the position of element which You want to find : 1
*****The element is 6
*****enter the number of row which element You want to find : 1
*****enter the position of element which You want to find : 5
*****The element is 5
*/
*this program is use to find any elemet in any row with variable array size
*aplication of pointer is use in it
*important point start from here to:
*the index value of array can be go to 1 to 100000
*check till array[1000]
*end here
*how to work example:
**Question:
***number of array 2
***quarry 3
***array 1 is {1 2 3 4 5}
***array 2 is {6 7}
****i) what is 2nd element in 1st array
****ii) what is 1st element in 2nd array
****iii) what is 5th element in 1st array
*****output:
*****Enter Number of array you want to Store : 2
*****Enter Number of Question or Quary you want to do Related to Array : 3
*****Enter number of element in 1 rows : 5
*****Enter the element of Array 1 2 3 4 5
*****Enter number of element in 2 rows : 2
*****Enter the element of Array 6 7
*****enter the number of row which element You want to find : 1
*****enter the position of element which You want to find : 2
*****The element is 2
*****enter the number of row which element You want to find : 2
*****enter the position of element which You want to find : 1
*****The element is 6
*****enter the number of row which element You want to find : 1
*****enter the position of element which You want to find : 5
*****The element is 5
*/
#include <iostream>
// this is main fuction
// ***
int main() {
int main()
{
int64_t r, mr = 0, x, q, i, z;
std::cout << "Enter Number of array you want to Store :";
std::cin >> x;
@@ -46,10 +47,11 @@ int main() {
// create a Array in run time because use can
// change the size of each array which he/she is going to store
// create a 2D array
int** ar = new int* [x]();
int** ar = new int*[x]();
// this for loop is use for entering different variable size array
// ***
for (r = 0; r < x; r++) {
for (r = 0; r < x; r++)
{
std::cout << "Enter number of element in " << r + 1 << " rows :";
std::cin >> mr;
// creating a 1D array
@@ -57,7 +59,8 @@ int main() {
std::cout << "Enter the element of Array ";
// this for loop is use for storing values in array
// ***
for (i = 0; i < mr; i++) {
for (i = 0; i < mr; i++)
{
// entering the value of rows in array in Horizontal
std::cin >> ac[i];
}
@@ -66,7 +69,8 @@ int main() {
}
// this for loop is use for display result of querry
// ***
for (z = 0; z < q; z++) {
for (z = 0; z < q; z++)
{
int64_t r1 = 0, q1 = 0;
std::cout << "enter the number of row which element you want to find :";
std::cin >> r1;
@@ -75,6 +79,6 @@ int main() {
std::cin >> q1;
q1 = q1 - 1;
// use this to find desire position of element in desire array
std::cout <<"The element is "<< ar[r1][q1] <<std::endl;
std::cout << "The element is " << ar[r1][q1] << std::endl;
}
}

View File

@@ -8,18 +8,18 @@
* 1 2
* 1 3
* 2 4
* which can be represented as
* 1
* / \
* 2 3
* |
* 4
*
* which can be represented as
* 1
* / \
* 2 3
* |
* 4
*
* Height of the tree : - 2
*/
*/
#include<iostream>
#include<vector>
#include <iostream>
#include <vector>
// global declarations
// no of nodes max limit.
@@ -29,22 +29,26 @@ std::vector<int> adj[MAX];
std::vector<bool> visited;
std::vector<int> dp;
void depth_first_search(int u) {
void depth_first_search(int u)
{
visited[u] = true;
int child_height = 1;
for (int v : adj[u]) {
if (!visited[v]) {
for (int v : adj[u])
{
if (!visited[v])
{
depth_first_search(v);
// select maximum sub-tree height from all children.
child_height = std::max(child_height, dp[v]+1);
child_height = std::max(child_height, dp[v] + 1);
}
}
// assigned the max child height to current visited node.
dp[u] = child_height;
}
int main() {
int main()
{
// number of nodes
int number_of_nodes;
std::cout << "Enter number of nodes of the tree : " << std::endl;
@@ -54,16 +58,17 @@ int main() {
int u, v;
// Tree contains exactly n-1 edges where n denotes the number of nodes.
std::cout << "Enter edges of the tree : " << std::endl;
for (int i = 0; i < number_of_nodes - 1; i++) {
for (int i = 0; i < number_of_nodes - 1; i++)
{
std::cin >> u >> v;
// undirected tree u -> v and v -> u.
adj[u].push_back(v);
adj[v].push_back(u);
}
// initialize all nodes as unvisited.
visited.assign(number_of_nodes+1, false);
visited.assign(number_of_nodes + 1, false);
// initialize depth of all nodes to 0.
dp.assign(number_of_nodes+1, 0);
dp.assign(number_of_nodes + 1, 0);
// function call which will initialize the height of all nodes.
depth_first_search(1);
std::cout << "Height of the Tree : " << dp[1] << std::endl;