diff --git a/Graph Algorithm/Kruskals.cpp b/Graph Algorithm/Kruskals.cpp index 93a591d..2191a8c 100644 --- a/Graph Algorithm/Kruskals.cpp +++ b/Graph Algorithm/Kruskals.cpp @@ -1,84 +1,84 @@ -#include - -using namespace std; - -bool comp(vector& v1 , vector& v2 ){ - return v1[0] > v2[0]; -} - -class Solution{ - -public: - - class dsu{ - public: - vector parent; - vector size; - - dsu(int sz){ - parent.resize(sz); - size.resize(sz); - - for(int i = 0 ; i < sz ; i++ ) - parent[i] = i , size[i] = 1; - } - - int find(int n){ - if( parent[n] == n ) return n; - - return parent[n] = find( parent[n] ); - } - - int find_iterative( int n ){ - while( parent[n] != n ){ - n = parent[n]; - } - return n; - } - - void unite(int a, int b){ - - int par_a = find(a); - int par_b = find(b); - - if( par_a == par_b ) - return; - - if( size[par_a] < size[par_b] ) swap(par_a, par_b); - - size[par_a] += size[par_b]; - - parent[par_b] = par_a; - } - - bool isSame(int a , int b){ - return find(a) == find(b); - } - }; - - - - //Solution function template... - int maxNumEdgesToRemove(int n, vector>& edges) { - - sort( edges.begin() , edges.end() , comp ); - - - } - -}; - -int main( int argc, char* argv[] ){ - - ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); - - Solution sol; - - //solution function call... - - - //Extract result and print the solution.. - - - return 0; +#include + +using namespace std; + +bool comp(vector& v1 , vector& v2 ){ + return v1[0] > v2[0]; +} + +class Solution{ + +public: + + class dsu{ + public: + vector parent; + vector size; + + dsu(int sz){ + parent.resize(sz); + size.resize(sz); + + for(int i = 0 ; i < sz ; i++ ) + parent[i] = i , size[i] = 1; + } + + int find(int n){ + if( parent[n] == n ) return n; + + return parent[n] = find( parent[n] ); + } + + int find_iterative( int n ){ + while( parent[n] != n ){ + n = parent[n]; + } + return n; + } + + void unite(int a, int b){ + + int par_a = find(a); + int par_b = find(b); + + if( par_a == par_b ) + return; + + if( size[par_a] < size[par_b] ) swap(par_a, par_b); + + size[par_a] += size[par_b]; + + parent[par_b] = par_a; + } + + bool isSame(int a , int b){ + return find(a) == find(b); + } + }; + + + + //Solution function template... + int maxNumEdgesToRemove(int n, vector>& edges) { + + sort( edges.begin() , edges.end() , comp ); + + + } + +}; + +int main( int argc, char* argv[] ){ + + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + Solution sol; + + //solution function call... + + + //Extract result and print the solution.. + + + return 0; } \ No newline at end of file diff --git a/Graph Algorithm/dsu.cpp b/Graph Algorithm/dsu.cpp index d1e79f0..0e8867a 100644 --- a/Graph Algorithm/dsu.cpp +++ b/Graph Algorithm/dsu.cpp @@ -1,45 +1,45 @@ -class dsu{ - public: - vector parent; - vector size; - - dsu(int sz){ - parent.resize(sz); - size.resize(sz); - - for(int i = 0 ; i < sz ; i++ ) - parent[i] = i , size[i] = 1; - } - - int find(int n){ - if( parent[n] == n ) return n; - - return parent[n] = find( parent[n] ); - } - - int find_iterative( int n ){ - while( parent[n] != n ){ - n = parent[n]; - } - return n; - } - - void unite(int a, int b){ - - int par_a = find(a); - int par_b = find(b); - - if( par_a == par_b ) - return; - - if( size[par_a] < size[par_b] ) swap(par_a, par_b); - - size[par_a] += size[par_b]; - - parent[par_b] = par_a; - } - - bool isSame(int a , int b){ - return find(a) == find(b); - } +class dsu{ + public: + vector parent; + vector size; + + dsu(int sz){ + parent.resize(sz); + size.resize(sz); + + for(int i = 0 ; i < sz ; i++ ) + parent[i] = i , size[i] = 1; + } + + int find(int n){ + if( parent[n] == n ) return n; + + return parent[n] = find( parent[n] ); + } + + int find_iterative( int n ){ + while( parent[n] != n ){ + n = parent[n]; + } + return n; + } + + void unite(int a, int b){ + + int par_a = find(a); + int par_b = find(b); + + if( par_a == par_b ) + return; + + if( size[par_a] < size[par_b] ) swap(par_a, par_b); + + size[par_a] += size[par_b]; + + parent[par_b] = par_a; + } + + bool isSame(int a , int b){ + return find(a) == find(b); + } }; \ No newline at end of file diff --git a/LeetcodeTemplate.cpp b/LeetcodeTemplate.cpp index 1fc15ce..71b043e 100644 --- a/LeetcodeTemplate.cpp +++ b/LeetcodeTemplate.cpp @@ -1,27 +1,27 @@ -#include - -using namespace std; - -class Solution{ - -public: - - //Solution function template... - -}; - -int main( int argc, char* argv[] ){ - - ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); - - Solution sol; - - //solution function call... - - - - //Extract result and print the solution.. - - - return 0; +#include + +using namespace std; + +class Solution{ + +public: + + //Solution function template... + +}; + +int main( int argc, char* argv[] ){ + + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + Solution sol; + + //solution function call... + + + + //Extract result and print the solution.. + + + return 0; } \ No newline at end of file diff --git a/Number Theory/Catalan_number.cpp b/Number Theory/Catalan_number.cpp index 514d26f..9b633f6 100644 --- a/Number Theory/Catalan_number.cpp +++ b/Number Theory/Catalan_number.cpp @@ -1,32 +1,32 @@ -#include - -using namespace std; - -/* cantalan Number using Iterative dp */ -int catalanNumber( int n ){ - - vector dp(n+1); - - dp[0] = dp[1] = 1; - - for(int i = 2 ; i <= n ; i++ ){ - - for(int j = 0 ; j <= i ; j++ ){ - - dp[i] += dp[j-1]*dp[i-j]; - } - } - - return dp[n]; -} - -/* - Formula for Catalan Number - (2n-C-n)/(n+1) -*/ - -int main(int argc, char* argv[] ){ - - cout << catalanNumber(2) << '\n'; - return 0; +#include + +using namespace std; + +/* cantalan Number using Iterative dp */ +int catalanNumber( int n ){ + + vector dp(n+1); + + dp[0] = dp[1] = 1; + + for(int i = 2 ; i <= n ; i++ ){ + + for(int j = 0 ; j <= i ; j++ ){ + + dp[i] += dp[j-1]*dp[i-j]; + } + } + + return dp[n]; +} + +/* + Formula for Catalan Number + (2n-C-n)/(n+1) +*/ + +int main(int argc, char* argv[] ){ + + cout << catalanNumber(2) << '\n'; + return 0; } \ No newline at end of file diff --git a/Number Theory/Catalan_number_formula.cpp b/Number Theory/Catalan_number_formula.cpp index cbffb76..093a3d7 100644 --- a/Number Theory/Catalan_number_formula.cpp +++ b/Number Theory/Catalan_number_formula.cpp @@ -1,69 +1,69 @@ -typedef long long int ll; - -ll mod = 998244353; - -vector factorial; -void calculateFactorial(ll n){ - factorial.resize(n+1); - - factorial[0] = factorial[1] = 1; - - for( ll i = 2 ; i <= n ; i++ ){ - factorial[i] = ( factorial[i-1] * i ) % mod; - } -} - -template -T modMul(T a,T b,T M=(ll)1e9+7){ - return ((a%M)*(b%M))%M; -} - -template -inline T power(T a, T b, T M) -{ - T x = 1; - while (b) - { - if (b & 1) x = modMul(x,a,M); - a = modMul(a,a,M); - b >>= 1; - } - return x; -} - -/* Using Fermat little Theorem */ -ll inverse(ll n ){ - return power( n , mod - 2 , mod ); -} - -vector inverse_factorial; -void calculateInverseFactorial(ll n){ - - inverse_factorial.resize(n+1); - inverse_factorial[0] = inverse_factorial[1] = 1; - - for( ll i = 2 ; i <= n ; i++ ){ - inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod; - } -} - -ll nCr( ll n , ll r ){ - - // -- n! -- - // | ------------- | % mod - // -- n! * (n - r)! -- - - return ( ( ( factorial[n] * inverse_factorial[r] ) % mod - ) * inverse_factorial[n-r] ) % mod; -} - -/* Catalan Number */ -ll Cn( ll n ){ - - // -- 2n! -- - // | --------------- | % mod - // -- n! * n! * (n+1) -- - - return ( ( ( ( ( factorial[2*n] * inverse_factorial[n] ) % mod ) - * inverse_factorial[n] ) % mod ) * inverse( n + 1 ) ) % mod; +typedef long long int ll; + +ll mod = 998244353; + +vector factorial; +void calculateFactorial(ll n){ + factorial.resize(n+1); + + factorial[0] = factorial[1] = 1; + + for( ll i = 2 ; i <= n ; i++ ){ + factorial[i] = ( factorial[i-1] * i ) % mod; + } +} + +template +T modMul(T a,T b,T M=(ll)1e9+7){ + return ((a%M)*(b%M))%M; +} + +template +inline T power(T a, T b, T M) +{ + T x = 1; + while (b) + { + if (b & 1) x = modMul(x,a,M); + a = modMul(a,a,M); + b >>= 1; + } + return x; +} + +/* Using Fermat little Theorem */ +ll inverse(ll n ){ + return power( n , mod - 2 , mod ); +} + +vector inverse_factorial; +void calculateInverseFactorial(ll n){ + + inverse_factorial.resize(n+1); + inverse_factorial[0] = inverse_factorial[1] = 1; + + for( ll i = 2 ; i <= n ; i++ ){ + inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod; + } +} + +ll nCr( ll n , ll r ){ + + // -- n! -- + // | ------------- | % mod + // -- n! * (n - r)! -- + + return ( ( ( factorial[n] * inverse_factorial[r] ) % mod + ) * inverse_factorial[n-r] ) % mod; +} + +/* Catalan Number */ +ll Cn( ll n ){ + + // -- 2n! -- + // | --------------- | % mod + // -- n! * n! * (n+1) -- + + return ( ( ( ( ( factorial[2*n] * inverse_factorial[n] ) % mod ) + * inverse_factorial[n] ) % mod ) * inverse( n + 1 ) ) % mod; } \ No newline at end of file diff --git a/Number Theory/nCr_formula.cpp b/Number Theory/nCr_formula.cpp index b5fb9ed..744e3fc 100644 --- a/Number Theory/nCr_formula.cpp +++ b/Number Theory/nCr_formula.cpp @@ -1,56 +1,56 @@ -ll mod = 998244353; - -vector factorial; -void calculateFactorial(ll n){ - factorial.resize(n+1); - - factorial[0] = factorial[1] = 1; - - for( ll i = 2 ; i <= n ; i++ ){ - factorial[i] = ( factorial[i-1] * i ) % mod; - } -} - -template -T modMul(T a,T b,T M=(ll)1e9+7){ - return ((a%M)*(b%M))%M; -} - -template -inline T power(T a, T b, T M) -{ - T x = 1; - while (b) - { - if (b & 1) x = modMul(x,a,M); - a = modMul(a,a,M); - b >>= 1; - } - return x; -} - -/* Using Fermat little Theorem */ -ll inverse(ll n ){ - return power( n , mod - 2 , mod ); -} - -vector inverse_factorial; -void calculateInverseFactorial(ll n){ - - inverse_factorial.resize(n+1); - inverse_factorial[0] = inverse_factorial[1] = 1; - - for( ll i = 2 ; i <= n ; i++ ){ - inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod; - } -} - -ll nCr( ll n , ll r ){ - - // -- n! -- - // | ------------- | % mod - // -- n! * (n - r)! -- - - return ( ( ( factorial[n] * inverse_factorial[r] ) % mod - ) * inverse_factorial[n-r] ) % mod; +ll mod = 998244353; + +vector factorial; +void calculateFactorial(ll n){ + factorial.resize(n+1); + + factorial[0] = factorial[1] = 1; + + for( ll i = 2 ; i <= n ; i++ ){ + factorial[i] = ( factorial[i-1] * i ) % mod; + } +} + +template +T modMul(T a,T b,T M=(ll)1e9+7){ + return ((a%M)*(b%M))%M; +} + +template +inline T power(T a, T b, T M) +{ + T x = 1; + while (b) + { + if (b & 1) x = modMul(x,a,M); + a = modMul(a,a,M); + b >>= 1; + } + return x; +} + +/* Using Fermat little Theorem */ +ll inverse(ll n ){ + return power( n , mod - 2 , mod ); +} + +vector inverse_factorial; +void calculateInverseFactorial(ll n){ + + inverse_factorial.resize(n+1); + inverse_factorial[0] = inverse_factorial[1] = 1; + + for( ll i = 2 ; i <= n ; i++ ){ + inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod; + } +} + +ll nCr( ll n , ll r ){ + + // -- n! -- + // | ------------- | % mod + // -- n! * (n - r)! -- + + return ( ( ( factorial[n] * inverse_factorial[r] ) % mod + ) * inverse_factorial[n-r] ) % mod; } \ No newline at end of file diff --git a/Range Queries/FenwickTree.cpp b/Range Queries/FenwickTree.cpp index 30affbb..d7f6942 100644 --- a/Range Queries/FenwickTree.cpp +++ b/Range Queries/FenwickTree.cpp @@ -1,68 +1,68 @@ -#include - -using namespace std; - - - -class Fenwick_Tree{ - -public: - - // 1 based indexing... - vector tree; - int sz; - - void init(int n, const vector& arr){ - tree.resize(n+1,0); - - this->sz = n; - - for(int i = 1 ; i <= sz ; i++ ){ - - update(i, arr[i-1]); - } - } - - int sum(int k){ - int s = 0; - - while(k >= 1){ - - s += tree[k]; - - k -= k & -k; - } - - return s; - } - - void update(int k, int val){ - - while( k <= sz ){ - tree[k] += val; - - k += k & -k; - } - } - - void print(){ - - for(int i = 1 ; i <= sz ; i++ ){ - cout << tree[i] << ' '; - } - cout << '\n'; - } - - -}; - - -int main(){ - - ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); - - - - - return 0; +#include + +using namespace std; + + + +class Fenwick_Tree{ + +public: + + // 1 based indexing... + vector tree; + int sz; + + void init(int n, const vector& arr){ + tree.resize(n+1,0); + + this->sz = n; + + for(int i = 1 ; i <= sz ; i++ ){ + + update(i, arr[i-1]); + } + } + + int sum(int k){ + int s = 0; + + while(k >= 1){ + + s += tree[k]; + + k -= k & -k; + } + + return s; + } + + void update(int k, int val){ + + while( k <= sz ){ + tree[k] += val; + + k += k & -k; + } + } + + void print(){ + + for(int i = 1 ; i <= sz ; i++ ){ + cout << tree[i] << ' '; + } + cout << '\n'; + } + + +}; + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + + + + return 0; } \ No newline at end of file diff --git a/Range Queries/SegmentTree.cpp b/Range Queries/SegmentTree.cpp index 906bc2e..71774ec 100644 --- a/Range Queries/SegmentTree.cpp +++ b/Range Queries/SegmentTree.cpp @@ -1,89 +1,89 @@ -#include - -using namespace std; - -class SegmentTree{ - - - int sz; - vector tree; - -public: - - SegmentTree(const vector& arr){ - - this->sz = arr.size(); - - tree.resize(2*this->sz+1,0); - - init(arr); - - } - - void init(const vector& arr){ - - for(int i = sz-1 ; i >= 0 ; i-- ) - tree[i+sz] = arr[i]; - - for(int i = sz - 1 ; i >= 1 ; i-- ){ - - tree[i] = tree[2*i] + tree[2*i+1]; - - } - - } - - - void print(){ - - cout << "Size : " << sz << '\n'; - - for(int i = 0 ; i <= 2*sz ; i++ ){ - - cout << tree[i] << ' '; - } - cout << '\n'; - } - - int sum( int a, int b){ - - int s = 0; - a += sz , b += sz; - - while( a <= b ){ - - if( a%2 == 1 ) s += tree[a++]; - - if( b%2 == 0 ) s += tree[b--]; - - a /= 2 , b /= 2; - } - return s; - } - - void add(int k, int x) { - k += sz; - tree[k] += x; - for (k /= 2; k >= 1; k /= 2) { - tree[k] = tree[2*k]+tree[2*k+1]; - } -} - - - -}; - -int main(){ - - vector arr = {5,8,6,3,2,7,2,6}; - - SegmentTree stree(arr); - - - stree.print(); - - stree.add(7,1); - cout << "(2,7) : " << stree.sum(2,7) << '\n'; - - return 0; +#include + +using namespace std; + +class SegmentTree{ + + + int sz; + vector tree; + +public: + + SegmentTree(const vector& arr){ + + this->sz = arr.size(); + + tree.resize(2*this->sz+1,0); + + init(arr); + + } + + void init(const vector& arr){ + + for(int i = sz-1 ; i >= 0 ; i-- ) + tree[i+sz] = arr[i]; + + for(int i = sz - 1 ; i >= 1 ; i-- ){ + + tree[i] = tree[2*i] + tree[2*i+1]; + + } + + } + + + void print(){ + + cout << "Size : " << sz << '\n'; + + for(int i = 0 ; i <= 2*sz ; i++ ){ + + cout << tree[i] << ' '; + } + cout << '\n'; + } + + int sum( int a, int b){ + + int s = 0; + a += sz , b += sz; + + while( a <= b ){ + + if( a%2 == 1 ) s += tree[a++]; + + if( b%2 == 0 ) s += tree[b--]; + + a /= 2 , b /= 2; + } + return s; + } + + void add(int k, int x) { + k += sz; + tree[k] += x; + for (k /= 2; k >= 1; k /= 2) { + tree[k] = tree[2*k]+tree[2*k+1]; + } +} + + + +}; + +int main(){ + + vector arr = {5,8,6,3,2,7,2,6}; + + SegmentTree stree(arr); + + + stree.print(); + + stree.add(7,1); + cout << "(2,7) : " << stree.sum(2,7) << '\n'; + + return 0; } \ No newline at end of file diff --git a/Range Queries/minSparseTable.cpp b/Range Queries/minSparseTable.cpp index 4e3ae89..bb1a587 100644 --- a/Range Queries/minSparseTable.cpp +++ b/Range Queries/minSparseTable.cpp @@ -1,49 +1,49 @@ -#include - -using namespace std; - - -//Sparse Table for minimum value query. - -vector> table; - -void precomputeSparseTable(int n, vector& arr){ - - int lPower = log2(n); - - table.resize(n+1,vector(lPower+1)); - - for(int p = 0 ; p <= lPower ; p++ ){ - for(int i = 0 ; i + pow(2,p) - 1 <= n ; i++ ){ - if( p == 0 ){ - table[i][p] = arr[i]; - continue; - } - - table[i][p] = min(table[i][p-1],table[i+pow(2,p-1)][p-1]); - } - } - - // for(int i = 0 ; i <= n ; i++ ){ - // cout << i << " : "; - // for(int p = 0 ; p <= lPower ; p++ ){ - // cout << table[i][p] << " "; - // } - // cout << endl; - // } -} - -int main(int argc,char* argv[]){ - - ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); - - int n,q; - cin>>n>>q; - - vector arr(n); - for(int i = 0 ; i < n ; i++ ){ - cin>>arr[i]; - } - - precomputeSparseTable(n,arr); +#include + +using namespace std; + + +//Sparse Table for minimum value query. + +vector> table; + +void precomputeSparseTable(int n, vector& arr){ + + int lPower = log2(n); + + table.resize(n+1,vector(lPower+1)); + + for(int p = 0 ; p <= lPower ; p++ ){ + for(int i = 0 ; i + pow(2,p) - 1 <= n ; i++ ){ + if( p == 0 ){ + table[i][p] = arr[i]; + continue; + } + + table[i][p] = min(table[i][p-1],table[i+pow(2,p-1)][p-1]); + } + } + + // for(int i = 0 ; i <= n ; i++ ){ + // cout << i << " : "; + // for(int p = 0 ; p <= lPower ; p++ ){ + // cout << table[i][p] << " "; + // } + // cout << endl; + // } +} + +int main(int argc,char* argv[]){ + + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + int n,q; + cin>>n>>q; + + vector arr(n); + for(int i = 0 ; i < n ; i++ ){ + cin>>arr[i]; + } + + precomputeSparseTable(n,arr); } \ No newline at end of file diff --git a/Testing/generator.cpp b/Testing/generator.cpp index df13d08..723c2ac 100644 --- a/Testing/generator.cpp +++ b/Testing/generator.cpp @@ -1,49 +1,49 @@ -#include - -using namespace std; - -int main(int argc, char* argv[]){ - - int n = atoi(argv[1]); - // cin>>n; - - int arr[n]; - for(int i = 1 ; i <= n ; i++ ) - arr[i-1] = i; - - int idx = 0; - int cnt = 1; - - int elemLeft = n; - - cout << "Operation:\n"; - - while( true ){ - // cout << "Size : " << elemLeft << '\n'; - if( elemLeft <= 0 ) - break; - - if( cnt%2 == 0 ){ - while( arr[idx] == -1 ) - idx = (idx+1)%n; - - cout << arr[idx] << ' '; - arr[idx] = -1; - - elemLeft--; - if( elemLeft <= 0 )break; - - while( arr[idx] == -1 ) - idx = (idx+1)%n; - cnt = 1; - - // idx = (idx+1)%n; - continue; - } - - idx = (idx+1)%n; - cnt++; - } - cout << '\n'; - return 0; +#include + +using namespace std; + +int main(int argc, char* argv[]){ + + int n = atoi(argv[1]); + // cin>>n; + + int arr[n]; + for(int i = 1 ; i <= n ; i++ ) + arr[i-1] = i; + + int idx = 0; + int cnt = 1; + + int elemLeft = n; + + cout << "Operation:\n"; + + while( true ){ + // cout << "Size : " << elemLeft << '\n'; + if( elemLeft <= 0 ) + break; + + if( cnt%2 == 0 ){ + while( arr[idx] == -1 ) + idx = (idx+1)%n; + + cout << arr[idx] << ' '; + arr[idx] = -1; + + elemLeft--; + if( elemLeft <= 0 )break; + + while( arr[idx] == -1 ) + idx = (idx+1)%n; + cnt = 1; + + // idx = (idx+1)%n; + continue; + } + + idx = (idx+1)%n; + cnt++; + } + cout << '\n'; + return 0; } \ No newline at end of file diff --git a/Testing/solution.cpp b/Testing/solution.cpp index cf451eb..af4cfd6 100644 --- a/Testing/solution.cpp +++ b/Testing/solution.cpp @@ -1,9 +1,9 @@ -#include - -using namespace std; - -int main(int argc, char* argv[] ){ - - - return 0; +#include + +using namespace std; + +int main(int argc, char* argv[] ){ + + + return 0; } \ No newline at end of file diff --git a/playground/IOFiles/correct_answer.txt b/playground/IOFiles/correct_answer.txt new file mode 100644 index 0000000..e69de29 diff --git a/playground/IOFiles/output.txt b/playground/IOFiles/output.txt new file mode 100644 index 0000000..e69de29 diff --git a/playground/IOFiles/testcase.txt b/playground/IOFiles/testcase.txt new file mode 100644 index 0000000..e69de29 diff --git a/playground/Makefile b/playground/Makefile new file mode 100644 index 0000000..445f3dd --- /dev/null +++ b/playground/Makefile @@ -0,0 +1,59 @@ +BUILD_DIR = build +OBJS_DIR = $(BUILD_DIR)/objs + +SOLUTION_BINARY = $(BUILD_DIR)/solution +BRUTEFORCE_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/bruteForce) +GENERATOR_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/generator) +CODEDIRS = . ./Stress_Testing +INCDIRS = . + + +CXX = g++ +DEPFLAGS = -MP -MMD +CXXFLAGS = -Wall -Wextra -g \ + $(foreach D,$(INCDIRS), -I$(D)) \ + $(DEPFLAGS) + +CXXFILES = $(foreach D, \ + $(CODEDIRS), \ + $(wildcard $(D)/*.cpp)) + +OBJECTS = $(patsubst %.cpp, $(OBJS_DIR)/%.o, $(notdir $(CXXFILES))) +DEPFILES = $(patsubst %.cpp, $(OBJS_DIR)/%.d, $(notdir $(CXXFILES))) + +all: $(SOLUTION_BINARY) \ + $(GENERATOR_BINARY) \ + $(BRUTEFORCE_BINARY) + + +$(SOLUTION_BINARY): $(OBJS_DIR)/solution.o | $(OBJS_DIR) + $(CXX) -o $@ $< + +$(GENERATOR_BINARY): $(OBJS_DIR)/generator.o | $(OBJS_DIR) + $(CXX) -o $@ $< + +$(BRUTEFORCE_BINARY): $(OBJS_DIR)/bruteForce.o | $(OBJS_DIR) + $(CXX) -o $@ $< + + +$(OBJS_DIR)/%.o: %.cpp | $(OBJS_DIR) + $(CXX) $(CXXFLAGS) -c -o $@ $< + +$(OBJS_DIR)/%.o: Stress_Testing/%.cpp | $(OBJS_DIR) + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + rm -f $(SOLUTION_BINARY) \ + $(BRUTEFORCE_BINARY) \ + $(GENERATOR_BINARY) \ + $(OBJECTS) \ + $(DEPFILES) + +help: + @echo "make clean: To remove executable, objects and dependency files." + @echo "make $(SOLUTION_BINARY): To build $(SOLUTION_BINARY) only." + @echo "make all: To build for Stress_Testing." + +-include $(DEPFILES) + +.PHONY: clean help all diff --git a/playground/Stress_Testing/bruteForce.cpp b/playground/Stress_Testing/bruteForce.cpp new file mode 100644 index 0000000..c81c6dc --- /dev/null +++ b/playground/Stress_Testing/bruteForce.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const int N = int(2e5) + 9; + +int n, k, h; +int need = int(1e9); +int cnt[N]; + +int main() { + scanf("%d %d", &n, &k); + for(int i = 0; i < n; ++i){ + int x; + scanf("%d", &x); + h = max(h, x); + need = min(need, x); + ++cnt[x]; + } + + int pos = N - 1; + int res = 0; + long long sum = 0; + int c = 0; + while(true){ + long long x = sum - c * 1LL * (pos - 1); + if(x > k){ + ++res; + h = pos; + sum = pos * 1LL * c; + } + + --pos; + if(pos == need) break; + c += cnt[pos]; + sum += cnt[pos] * 1LL * pos; + } + + if(h != need) ++res; + + cout << res << endl; +} \ No newline at end of file diff --git a/playground/Stress_Testing/generator.cpp b/playground/Stress_Testing/generator.cpp new file mode 100644 index 0000000..45cc19f --- /dev/null +++ b/playground/Stress_Testing/generator.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int rnd(int a,int b){ + return a + rand() % ( b - a + 1 ); +} + +int main(int argc, char* argv[]){ + + int seed = atoi(argv[1]); + + srand(seed); + + int n = rnd(1,20); + + int k = rnd(20, 40); + + cout << n << ' ' << k << '\n'; + + int val = 0; + for(int i = 0 ; i < n ; i++ ){ + + val = rnd(0,50); + + cout << val << ' '; + } + + cout << '\n'; + return 0; +} \ No newline at end of file diff --git a/playground/run.sh b/playground/run.sh new file mode 100644 index 0000000..02b4ef1 --- /dev/null +++ b/playground/run.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +#Creating .txt files +TESTCASE_FILE=IOFiles/testcase.txt +OUTPUT_FILE=IOFiles/output.txt +CORRECT_ANSWER_FILE=IOFiles/correct_answer.txt + +if ! [ -f $TESTCASE_FILE ]; then + echo "No $TESTCASE_FILE detected" + exit 1 +fi + +if ! [ -f $OUTPUT_FILE ]; then + echo "No $OUTPUT_FILE detected" + exit 1 +fi + +if ! [ -f $CORRECT_ANSWER_FILE ]; then + echo "No $CORRECT_ANSWER_FILE detected" + exit 1 +fi + + +#Executables. + +SOLUTION=build/solution + +make $SOLUTION + +# Read and split test cases and answers based on empty lines +echo "Running test cases..." +pass_count=0 +fail_count=0 + +# Separate test cases and expected outputs by empty lines +mapfile -t test_cases < <(awk -v RS= '1' "$TESTCASE_FILE") +mapfile -t expected_outputs < <(awk -v RS= '1' "$CORRECT_ANSWER_FILE") + +# Ensure number of testcase is same as number of output. +if [ ${#test_cases[@]} -ne ${#expected_outputs[@]} ]; then + echo "The TestCase and answer count should be same." + exit 2 +fi + +for (( test_num = 0 ; test_num < ${#test_cases[@]} ; test_num++)) +do + test_case=${test_cases[test_num]} + expected_output=${expected_outputs[test_num]} + + if ! echo "$test_case" | ./$SOLUTION > "$OUTPUT_FILE"; then + echo -e "\033[0;31mTest #$(($test_num+1)): Execution failed\033[0m" + echo "solution executable didn't executed successfully" + let fail_count++ + continue + fi + + test_output=$(<"$OUTPUT_FILE") + + if diff -q -Z <(echo "$test_output") <(echo "$expected_output") > /dev/null; then + echo -e "\033[0;32mTest #$(($test_num+1)): PASS\033[0m" + let pass_count++ + else + echo -e "\033[0;31mTest #$(($test_num+1)): FAIL\033[0m" + echo "Test case:" + echo $test_case + echo "Your output:" + echo $test_output + echo "Correct output:" + echo $expected_output + let fail_count++ + fi +done + +#Sumary +echo -e "\033[0;32mPassed test: $pass_count\033[0m" +echo -e "\033[0;31mFailed test: $fail_count\033[0m" + +# Exit with appropriate status +if [ $fail_count -eq 0 ]; then + exit 0 +else + exit 3 +fi \ No newline at end of file diff --git a/playground/solution.cpp b/playground/solution.cpp new file mode 100644 index 0000000..0b9524b --- /dev/null +++ b/playground/solution.cpp @@ -0,0 +1,9 @@ +#include + +using namespace std; + +int main(){ + int a,b; + cin >> a >> b; + cout << a << ' ' << b << '\n'; +} \ No newline at end of file diff --git a/playground/stress.sh b/playground/stress.sh new file mode 100644 index 0000000..4995c73 --- /dev/null +++ b/playground/stress.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +if [ $# -ne 1 ]; then + echo "Provide the limit of stress testing" + echo "$0 'test case limit'" + echo "$0 -1 (for unlimited)" + exit 1 +fi + +#Required tools +SOLUTION=build/solution +GENERATOR=build/generator +BRUTEFORCE=build/bruteForce + +#Build necessary things +make all + +#Files +TESTCASE_FILE=IOFiles/testcase.txt +OUTPUT_FILE=IOFiles/output.txt +CORRECT_ANSWER_FILE=IOFiles/correct_answer.txt + +pass_count=0 + +while (( 1 )) +do + ./generator > $TESTCASE_FILE + ./SOLUTION < $TESTCASE_FILE > $OUTPUT_FILE + ./BRUTEFORCE < $TESTCASE_FILE > $CORRECT_ANSWER_FILE + + if diff -q -Z $OUTPUT_FILE $CORRECT_ANSWER_FILE >> /dev/null; then + echo -e "\033[0;31mTest #$(($pass_count+1)): FAIL\033[0m" + echo "Test case:" + cat $TESTCASE_FILE + echo "Your output:" + cat $OUTPUT_FILE + echo "Correct output:" + cat $CORRECT_ANSWER_FILE + break + fi + + echo -e "\033[0;32mPassed test: #$(($pass_count+1))\033[0m" + + if [ $1 -ne -1 ] || [ $pass_count -lt $1 ]; then + break + fi +done