formatting source-code for 153fb7b8a5

This commit is contained in:
github-actions
2020-05-30 04:02:09 +00:00
parent 92fe9495ec
commit 8a2de9842b
175 changed files with 1671 additions and 3460 deletions

View File

@@ -28,13 +28,10 @@ using namespace std;
// }
//}
int Knapsack(int capacity, int n, int weight[], int value[])
{
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)
{
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)
@@ -48,20 +45,17 @@ int Knapsack(int capacity, int n, int weight[], int value[])
// cout<<"\n";
return res[n][capacity];
}
int main()
{
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)
{
for (int i = 0; i < n; ++i) {
cin >> weight[i];
}
cout << "Enter values: ";
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
cin >> value[i];
}
int capacity;

View File

@@ -4,14 +4,12 @@
using std::cin;
using std::cout;
int main()
{
int main() {
int n, k, d, s = 0;
cout << "Enter a number:";
cin >> n;
k = n;
while (k != 0)
{
while (k != 0) {
d = k % 10;
s += d * d * d;
k /= 10;

View File

@@ -4,33 +4,28 @@
using namespace std;
// Wrapper class for storing an edge
class Edge
{
class Edge {
public:
int src, dst, weight;
};
// Wrapper class for storing a graph
class Graph
{
class Graph {
public:
int vertexNum, edgeNum;
Edge *edges;
// Constructs a graph with V vertices and E edges
Graph(int V, int E)
{
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)
{
void addEdge(int src, int dst, int weight) {
static int edgeInd = 0;
if (edgeInd < this->edgeNum)
{
if (edgeInd < this->edgeNum) {
Edge newEdge;
newEdge.src = src;
newEdge.dst = dst;
@@ -41,11 +36,9 @@ class Graph
};
// Utility function to print distances
void print(int dist[], int V)
{
void print(int dist[], int V) {
cout << "\nVertex Distance" << endl;
for (int i = 0; i < V; i++)
{
for (int i = 0; i < V; i++) {
if (dist[i] != INT_MAX)
cout << i << "\t" << dist[i] << endl;
else
@@ -56,8 +49,7 @@ void print(int dist[], int V)
// 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)
{
void BellmanFord(Graph graph, int src) {
int V = graph.vertexNum;
int E = graph.edgeNum;
int dist[V];
@@ -70,8 +62,7 @@ void BellmanFord(Graph graph, int src)
// 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++)
{
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;
@@ -81,14 +72,12 @@ void BellmanFord(Graph graph, int src)
}
// Iterate inner loop once more to check for negative cycle
for (int j = 0; j < E; j++)
{
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])
{
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
cout << "Graph contains negative weight cycle. Hence, shortest "
"distance not guaranteed."
<< endl;
@@ -102,8 +91,7 @@ void BellmanFord(Graph graph, int src)
}
// Driver Function
int main()
{
int main() {
int V, E, gsrc;
int src, dst, weight;
cout << "Enter number of vertices: ";
@@ -111,8 +99,7 @@ int main()
cout << "Enter number of edges: ";
cin >> E;
Graph G(V, E);
for (int i = 0; i < E; i++)
{
for (int i = 0; i < E; i++) {
cout << "\nEdge " << i + 1 << "\nEnter source: ";
cin >> src;
cout << "Enter destination: ";

View File

@@ -11,8 +11,7 @@ using namespace std;
int *cat; // global array to hold catalan numbers
unsigned long int catalan_dp(int n)
{
unsigned long int catalan_dp(int n) {
/** Using the tabulation technique in dynamic programming,
this function computes the first `n+1` Catalan numbers
@@ -29,8 +28,7 @@ unsigned long int catalan_dp(int n)
cat[0] = cat[1] = 1;
// Compute the remaining numbers from index 2 to index n, using tabulation
for (int i = 2; i <= n; i++)
{
for (int i = 2; i <= n; i++) {
cat[i] = 0;
for (int j = 0; j < i; j++)
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
@@ -40,8 +38,7 @@ unsigned long int catalan_dp(int n)
return cat[n];
}
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
int n;
cout << "Enter n: ";
cin >> n;
@@ -49,8 +46,7 @@ int main(int argc, char *argv[])
cat = new int[n + 1];
cout << "Catalan numbers from 0 to " << n << " are:\n";
for (int i = 0; i <= n; i++)
{
for (int i = 0; i <= n; i++) {
cout << "catalan (" << i << ") = " << catalan_dp(i) << endl;
// NOTE: Since `cat` is a global array, calling `catalan_dp`
// repeatedly will not recompute the the values already computed

View File

@@ -3,8 +3,7 @@
using namespace std;
// Function to find the Minimum number of coins required to get Sum S
int findMinCoins(int arr[], int n, int N)
{
int findMinCoins(int arr[], int n, int N) {
// dp[i] = no of coins required to get a total of i
int dp[N + 1];
@@ -12,15 +11,13 @@ int findMinCoins(int arr[], int n, int N)
dp[0] = 0;
for (int i = 1; i <= N; i++)
{
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++)
{
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]];
@@ -36,8 +33,7 @@ int findMinCoins(int arr[], int n, int N)
return dp[N];
}
int main()
{
int main() {
// No of Coins We Have
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);

View File

@@ -5,23 +5,19 @@ the pieces.*/
#include <iostream>
using namespace std;
int cutrod(int p[], int n)
{
int cutrod(int p[], int n) {
int r[n + 1];
r[0] = 0;
for (int j = 0; j < n; j++)
{
for (int j = 0; j < n; j++) {
int q = INT_MIN;
for (int i = 0; i <= j; i++)
{
for (int i = 0; i <= j; i++) {
q = max(q, p[i] + r[j - i]);
}
r[j + 1] = q;
}
return r[n];
}
int main()
{
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};
cout << cutrod(price, 30);

View File

@@ -22,8 +22,7 @@ int min(int x, int y, int z) { return min(min(x, y), z); }
* str1 to str2.
* O(3^m)
*/
int editDist(string str1, string str2, int m, int n)
{
int editDist(string str1, string str2, int m, int n) {
if (m == 0)
return n;
if (n == 0)
@@ -45,16 +44,13 @@ int editDist(string str1, string str2, int m, int n)
/* A DP based program
* O(m x n)
*/
int editDistDP(string str1, string str2, int m, int n)
{
int editDistDP(string str1, string str2, int m, int n) {
// Create Table for SubProblems
int dp[m + 1][n + 1];
// Fill d[][] in bottom up manner
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
// If str1 empty. Then add all of str2
if (i == 0)
dp[i][j] = j;
@@ -78,8 +74,7 @@ int editDistDP(string str1, string str2, int m, int n)
return dp[m][n];
}
int main()
{
int main() {
string str1 = "sunday";
string str2 = "saturday";

View File

@@ -6,30 +6,24 @@
#include <iostream>
using namespace std;
int eggDrop(int n, int k)
{
int eggDrop(int n, int k) {
int eggFloor[n + 1][k + 1];
int result;
for (int i = 1; i <= n; i++)
{
for (int i = 1; i <= n; i++) {
eggFloor[i][1] = 1; // n eggs..1 Floor
eggFloor[i][0] = 0; // n eggs..0 Floor
}
// Only one egg available
for (int j = 1; j <= k; j++)
{
for (int j = 1; j <= k; j++) {
eggFloor[1][j] = j;
}
for (int i = 2; i <= n; i++)
{
for (int j = 2; j <= k; j++)
{
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= k; j++) {
eggFloor[i][j] = INT_MAX;
for (int x = 1; x <= j; x++)
{
for (int x = 1; x <= j; x++) {
// 1+max(eggBreak[one less egg, lower floors],
// eggDoesntBreak[same # of eggs, upper floors]);
result = 1 + max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]);
@@ -42,8 +36,7 @@ int eggDrop(int n, int k)
return eggFloor[n][k];
}
int main()
{
int main() {
int n, k;
cout << "Enter number of eggs and floors: ";
cin >> n >> k;

View File

@@ -1,20 +1,17 @@
#include <iostream>
using namespace std;
int fib(int n)
{
int fib(int n) {
int res[3];
res[0] = 0;
res[1] = 1;
for (int i = 2; i <= n; i++)
{
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 main(int argc, char const *argv[]) {
int n;
cout << "Enter n: ";
cin >> n;

View File

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

View File

@@ -1,12 +1,10 @@
#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;
@@ -17,14 +15,12 @@ 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

@@ -3,8 +3,7 @@ using namespace std;
int max(int a, int b) { return (a > b) ? a : b; }
int main()
{
int main() {
char str1[] = "DEFBCD";
char str2[] = "ABDEFJ";
int i, j, k;
@@ -13,10 +12,8 @@ int main()
// cout<<n<<" "<<m<<"\n";
int a[m][n];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i == 0 || j == 0)
a[i][j] = 0;
@@ -37,12 +34,9 @@ int main()
int ma = -1;
int indi, indj;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (a[i][j] > ma)
{
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;

View File

@@ -2,69 +2,50 @@
#include <iostream>
using namespace std;
void Print(int trace[20][20], int m, int n, string a)
{
if (m == 0 || n == 0)
{
void Print(int trace[20][20], int m, int n, string a) {
if (m == 0 || n == 0) {
return;
}
if (trace[m][n] == 1)
{
if (trace[m][n] == 1) {
Print(trace, m - 1, n - 1, a);
cout << a[m - 1];
}
else if (trace[m][n] == 2)
{
} else if (trace[m][n] == 2) {
Print(trace, m - 1, n, a);
}
else if (trace[m][n] == 3)
{
} else if (trace[m][n] == 3) {
Print(trace, m, n - 1, a);
}
}
int lcs(string a, string b)
{
int lcs(string a, string b) {
int m = a.length(), n = b.length();
int res[m + 1][n + 1];
int trace[20][20];
// fills up the arrays with zeros.
for (int i = 0; i < m + 1; i++)
{
for (int j = 0; j < n + 1; j++)
{
for (int i = 0; i < m + 1; i++) {
for (int j = 0; j < n + 1; j++) {
res[i][j] = 0;
trace[i][j] = 0;
}
}
for (int i = 0; i < m + 1; ++i)
{
for (int j = 0; j < n + 1; ++j)
{
if (i == 0 || j == 0)
{
for (int i = 0; i < m + 1; ++i) {
for (int j = 0; j < n + 1; ++j) {
if (i == 0 || j == 0) {
res[i][j] = 0;
trace[i][j] = 0;
}
else if (a[i - 1] == b[j - 1])
{
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.
}
else
{
if (res[i - 1][j] > res[i][j - 1])
{
} 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.
}
else
{
} else {
res[i][j] = res[i][j - 1];
trace[i][j] =
3; // means trace the matrix in left direction.
@@ -76,8 +57,7 @@ int lcs(string a, string b)
return res[m][n];
}
int main()
{
int main() {
string a, b;
cin >> a >> b;
cout << lcs(a, b);

View File

@@ -1,37 +1,30 @@
// 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(int a[], int n) {
int lis[n];
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
lis[i] = 1;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < i; ++j)
{
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)
{
for (int i = 0; i < n; ++i) {
res = max(res, lis[i]);
}
return res;
}
int main(int argc, char const *argv[])
{
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)
{
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << LIS(a, n) << endl;

View File

@@ -5,24 +5,19 @@
#include <iostream>
using namespace std;
int LIS(int arr[], int n)
{
int LIS(int arr[], int n) {
set<int> active; // The current built LIS.
active.insert(arr[0]);
// Loop through every element.
for (int i = 1; i < n; ++i)
{
for (int i = 1; i < n; ++i) {
auto get = active.lower_bound(arr[i]);
if (get == active.end())
{
if (get == active.end()) {
active.insert(arr[i]);
} // current element is the greatest so LIS increases by 1.
else
{
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
if (val > arr[i])
{
if (val > arr[i]) {
// else we remove the bigger element and add a smaller element
// (which is arr[i]) and continue;
active.erase(get);
@@ -32,15 +27,13 @@ int LIS(int arr[], int n)
}
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: ";
cin >> n;
int a[n];
cout << "Enter array elements: ";
for (int i = 0; i < n; ++i)
{
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << LIS(a, n) << endl;

View File

@@ -9,8 +9,7 @@ int dp[MAX][MAX];
// Function to find the most efficient way to multiply the given sequence of
// matrices
int MatrixChainMultiplication(int dim[], int i, int j)
{
int MatrixChainMultiplication(int dim[], int i, int j) {
// base case: one matrix
if (j <= i + 1)
return 0;
@@ -21,13 +20,11 @@ int MatrixChainMultiplication(int dim[], int i, int j)
// if dp[i][j] is not calculated (calculate it!!)
if (dp[i][j] == 0)
{
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++)
{
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);
@@ -48,8 +45,7 @@ int MatrixChainMultiplication(int dim[], int i, int j)
}
// main function
int main()
{
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};

View File

@@ -35,8 +35,7 @@
// 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;
@@ -50,8 +49,7 @@ int main()
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
@@ -59,8 +57,7 @@ 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];
}
@@ -69,8 +66,7 @@ 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;

View File

@@ -29,14 +29,11 @@ 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.
@@ -47,8 +44,7 @@ void depth_first_search(int u)
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;
@@ -58,8 +54,7 @@ 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);