From 3bf66528fca989ecdc6a1ad014187880b78026f9 Mon Sep 17 00:00:00 2001 From: Mehak Gupta <96685817+MehakGupta1103@users.noreply.github.com> Date: Wed, 12 Oct 2022 02:38:41 +0530 Subject: [PATCH] Top C2j ladder problems --- B_2_Wonderful_Coloring_-_2.cpp | 88 +++++++++++++++++++++++++++ B_Fun_with_Even_Subarrays.cpp | 44 ++++++++++++++ B_Funny_Permutation.cpp | 35 +++++++++++ B_GCD_Length.cpp | 36 +++++++++++ B_Minor_Reduction.cpp | 71 +++++++++++++++++++++ B_Mirror_in_the_String.cpp | 51 ++++++++++++++++ B_Ordinary_Numbers.cpp | 27 ++++++++ B_Permutation_Sort.cpp | 49 +++++++++++++++ B_Playing_with_GCD.cpp | 56 +++++++++++++++++ B_Prefix_Sum_Addicts.cpp | 77 +++++++++++++++++++++++ B_Roof_Construction.cpp | 59 ++++++++++++++++++ B_Sifid_and_Strange_Subsequences.cpp | 62 +++++++++++++++++++ B_TMT_Document.cpp | 46 ++++++++++++++ B_Tea_with_Tangerines.cpp | 48 +++++++++++++++ B_Two_Tables.cpp | 53 ++++++++++++++++ C_1_Good_Subarrays_Easy_Version.cpp | 82 +++++++++++++++++++++++++ C_Division_by_Two_and_Permutation.cpp | 59 ++++++++++++++++++ C_Even_Number_Addicts.cpp | 44 ++++++++++++++ C_Minimize_the_Thickness.cpp | 83 +++++++++++++++++++++++++ C_Paint_the_Array.cpp | 68 +++++++++++++++++++++ C_Pair_Programming.cpp | 52 ++++++++++++++++ C_Paprika_and_Permutation.cpp | 36 +++++++++++ C_Sum_of_Cubes.cpp | 47 ++++++++++++++ C_Yet_Another_Card_Deck.cpp | 41 +++++++++++++ D_Co-growing_Sequence.cpp | 43 +++++++++++++ D_Corrupted_Array.cpp | 69 +++++++++++++++++++++ D_Epic_Transformation.cpp | 64 +++++++++++++++++++ D_Palindromes_Coloring.cpp | 48 +++++++++++++++ E_Arranging_The_Sheep.cpp | 55 +++++++++++++++++ 29 files changed, 1593 insertions(+) create mode 100644 B_2_Wonderful_Coloring_-_2.cpp create mode 100644 B_Fun_with_Even_Subarrays.cpp create mode 100644 B_Funny_Permutation.cpp create mode 100644 B_GCD_Length.cpp create mode 100644 B_Minor_Reduction.cpp create mode 100644 B_Mirror_in_the_String.cpp create mode 100644 B_Ordinary_Numbers.cpp create mode 100644 B_Permutation_Sort.cpp create mode 100644 B_Playing_with_GCD.cpp create mode 100644 B_Prefix_Sum_Addicts.cpp create mode 100644 B_Roof_Construction.cpp create mode 100644 B_Sifid_and_Strange_Subsequences.cpp create mode 100644 B_TMT_Document.cpp create mode 100644 B_Tea_with_Tangerines.cpp create mode 100644 B_Two_Tables.cpp create mode 100644 C_1_Good_Subarrays_Easy_Version.cpp create mode 100644 C_Division_by_Two_and_Permutation.cpp create mode 100644 C_Even_Number_Addicts.cpp create mode 100644 C_Minimize_the_Thickness.cpp create mode 100644 C_Paint_the_Array.cpp create mode 100644 C_Pair_Programming.cpp create mode 100644 C_Paprika_and_Permutation.cpp create mode 100644 C_Sum_of_Cubes.cpp create mode 100644 C_Yet_Another_Card_Deck.cpp create mode 100644 D_Co-growing_Sequence.cpp create mode 100644 D_Corrupted_Array.cpp create mode 100644 D_Epic_Transformation.cpp create mode 100644 D_Palindromes_Coloring.cpp create mode 100644 E_Arranging_The_Sheep.cpp diff --git a/B_2_Wonderful_Coloring_-_2.cpp b/B_2_Wonderful_Coloring_-_2.cpp new file mode 100644 index 0000000..28f5738 --- /dev/null +++ b/B_2_Wonderful_Coloring_-_2.cpp @@ -0,0 +1,88 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n, k; cin >> n >> k; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + map colorct; + map m; + vector> v; + for(int i = 0; i < n; i++){ + v.push_back({a[i], i, 0}); + m[a[i]]++; + } + sort(v.begin(), v.end()); + int ct = 0; + for(auto &it: m){ + ct += min(k, it.second); + } + ct = ct/k; + + // cout << "the v" << endl; + // for(int i = 0; i < n; i++){ + // cout << v[i][0] << " " << v[i][1] << " " << v[i][2] << endl; + // } + + // cout << " theh count " << ct << endl; + + int color = 1; + v[0][2] = color; + int vict = 1; + colorct[color]++; + for(int i = 1; i < n; i++){ + if(v[i][0] == v[i-1][0]){ + vict++; + if(vict > k) continue; + } + else vict = 1; + if(vict > k) i++; + color++; + if(color > k) color = 1; + if(i> lol; + for(int i = 0; i < n; i++){ + lol.push_back({v[i][1], v[i][2]}); + } + sort(lol.begin(), lol.end()); + + for(int i = 0; i < n; i++){ + cout << lol[i].second << " "; + } + cout << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Fun_with_Even_Subarrays.cpp b/B_Fun_with_Even_Subarrays.cpp new file mode 100644 index 0000000..b841a59 --- /dev/null +++ b/B_Fun_with_Even_Subarrays.cpp @@ -0,0 +1,44 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + int cnt = 0, x = a[n-1], i = n-1, step = 0; + while(i >= 0){ + while(a[i]==x && i >= 0){ + cnt++, i--; + } + if(i<0) break; + step++; + i -= cnt; + cnt = cnt*2; + } + cout << step << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Funny_Permutation.cpp b/B_Funny_Permutation.cpp new file mode 100644 index 0000000..4097699 --- /dev/null +++ b/B_Funny_Permutation.cpp @@ -0,0 +1,35 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + if(n==3) cout << -1 << endl; + else{ + cout << n << " " << n-1 << " "; + for(int i = 1; i <= n-2; i++) cout << i << " "; + cout << endl; + } +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_GCD_Length.cpp b/B_GCD_Length.cpp new file mode 100644 index 0000000..74b0bc1 --- /dev/null +++ b/B_GCD_Length.cpp @@ -0,0 +1,36 @@ +#include +// #define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int a, b, c; + cin >> a >> b >> c; + int s = 7, t = 3, ans = 1; + ans *= pow(10, c-1); + int ans2 = ans; + while(ceil(log10(ans + 1)) < a) ans *= s; + while(ceil(log10(ans2 + 1)) < b) ans2 *= t; + cout << ans << " " << ans2 << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Minor_Reduction.cpp b/B_Minor_Reduction.cpp new file mode 100644 index 0000000..c8f0933 --- /dev/null +++ b/B_Minor_Reduction.cpp @@ -0,0 +1,71 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + string s; cin >> s; + int n = s.length(); + // cout << n << endl; + int flag = 0; + int num[n]; + loop{num[i] = s[i]-'0';} + for(int i = n-1; i > 0; i--){ + if(num[i]+num[i-1] >= 10){ + int temp = num[i]+num[i-1]; + num[i] = temp%10; + num[i-1] = temp / 10; + flag = 1; + break; + } + } + // cout << flag << endl; + if(!flag){ + // cout << "chala" << endl; + int index = -1; + int temp; + for(int i = 0; i < n-1; i++){ + temp = num[i]+num[i+1]; + // cout << temp << endl; + if(temp >= num[i]){ + index = i; + flag = 1; + break; + } + } + if(flag){ + for(int i = 0; i < n; i++){ + if(i == index){ + cout << temp ; + i++; + } + else cout << num[i]; + } + cout << endl; + return; + } + } + loop{cout << num[i];} + cout << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Mirror_in_the_String.cpp b/B_Mirror_in_the_String.cpp new file mode 100644 index 0000000..f95ff1a --- /dev/null +++ b/B_Mirror_in_the_String.cpp @@ -0,0 +1,51 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + char a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + if(n==1){ + cout << a[0] << a[0] << endl; + return; + } + if(a[0] == a[1]){ + cout << a[0] << a[0] << endl; + return; + } + vector b; + b.push_back(a[0]); + for(int i = 1; i < n; i++){ + if(a[i] <= a[i-1]) b.pb(a[i]); + else break; + } + for(auto &it: b) cout << it; + reverse(b.begin(), b.end()); + for(auto &it: b) cout << it; + cout << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Ordinary_Numbers.cpp b/B_Ordinary_Numbers.cpp new file mode 100644 index 0000000..f2c966c --- /dev/null +++ b/B_Ordinary_Numbers.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +using ll = long long; + +void solve() { + int n; + cin >> n; + int res = 0; + for (ll pw = 1; pw <= n; pw = pw * 10 + 1) { + for (int d = 1; d <= 9; d++) { + if (pw * d <= n) { + res++; + } + } + } + cout << res << endl; +} + +int main() { + int tests; + cin >> tests; + while (tests-- > 0) { + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Permutation_Sort.cpp b/B_Permutation_Sort.cpp new file mode 100644 index 0000000..24f682e --- /dev/null +++ b/B_Permutation_Sort.cpp @@ -0,0 +1,49 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n], b[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + b[i] = a[i]; + } + if(a[0] == n && a[n-1] == 1){ + cout << 3 << endl; + return; + } + sort(a, a+n); + int ct = 0; + if(a[0] != b[0]) ct++; + if(a[n-1] != b[n-1]) ct++; + for(int i = 1; i < n-1; i++){ + if(a[i] != b[i]){ + if(ct==0) ct++; + break; + } + } + cout << ct << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Playing_with_GCD.cpp b/B_Playing_with_GCD.cpp new file mode 100644 index 0000000..a1bb60b --- /dev/null +++ b/B_Playing_with_GCD.cpp @@ -0,0 +1,56 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + int b[n+1]; + b[0] = a[0]; + for(int i = 0; i < n-1; i++){ + b[i+1] = a[i]; + // cout << b[i+1]; + if(b[i+1]%a[i+1] != 0){ + b[i+1] *= a[i+1]/__gcd(a[i+1], b[i+1]); + } + // cout << " now " << b[i+1] << " " << a[i+1] << endl; + } + b[n] = a[n-1]; + + // cout << "the bs" << endl; + // for(int i = 0; i < n+1; i++) cout << b[i] << " "; + // cout << endl; + + for(int i = 0; i < n; i++){ + if(__gcd(b[i], b[i+1]) != a[i]){ + cout << "NO" << endl; + return; + } + } + cout << "YES" << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Prefix_Sum_Addicts.cpp b/B_Prefix_Sum_Addicts.cpp new file mode 100644 index 0000000..434610f --- /dev/null +++ b/B_Prefix_Sum_Addicts.cpp @@ -0,0 +1,77 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n, k; cin >> n >> k; + int a[k]; + for(int i = 0; i < k; i++){ + cin >> a[i]; + } + //when n = k + //the a1 = s1 + //all ai = si-s(i-1) + if(n==k){ + int arr[k]; + arr[0] = a[0]; + for(int i = 1; i < k; i++){ + arr[i] = a[i] - a[i-1]; + } + for(int i = 1; i < k; i++){ + if(arr[i] < arr[i-1]){ + cout << "No" << endl; + return ; + } + } + cout << "Yes" << endl; + return; + } + else{ + //when n < k + //arr[0] gives the an-k+1 th element + //there are in total n elements + //then (n-k) elements all are max equal to arr[0j]th elsement + //and their sum >= a[0] + //else not possible + int arr[k-1]; + for(int i = 1; i < k; i++){ + arr[i-1] = a[i] - a[i-1]; + } + // cout << "the arr array" << endl; + // for(int i = 0; i < k-1; i++) cout << arr[i] << " "; + // cout << endl; + for(int i = 1; i < k-1; i++){ + if(arr[i] < arr[i-1]){ + cout << "No" << endl; + return ; + } + } + if(arr[0]*(n-k+1) < a[0]){ + cout << "No" << endl; + return ; + } + cout << "Yes" << endl; + } +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Roof_Construction.cpp b/B_Roof_Construction.cpp new file mode 100644 index 0000000..4d066ac --- /dev/null +++ b/B_Roof_Construction.cpp @@ -0,0 +1,59 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +// Simple CPP program to find MSB number +// for given POSITIVE n. +#include +using namespace std; + +int setBitNumber(int n) +{ + if (n == 0) + return 0; + + int msb = 0; + n = n / 2; + while (n != 0) { + n = n / 2; + msb++; + } + + return (1 << msb); +} + +//----------------------------------------------------------------------------- +void solve(){ + int n; + cin >> n; + int k = 0; + while((1 << (k + 1)) <= n - 1) ++k; + for(int i = (1 << k) - 1; i >= 0; i--) { + cout << i << ' '; + } + for(int i = (1 << k); i < n; i++) { + cout << i << ' '; + } + cout << '\n'; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Sifid_and_Strange_Subsequences.cpp b/B_Sifid_and_Strange_Subsequences.cpp new file mode 100644 index 0000000..a6d7715 --- /dev/null +++ b/B_Sifid_and_Strange_Subsequences.cpp @@ -0,0 +1,62 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + sort(a, a+n); + int flag = 1, f2 = 1; + loop{ + if(a[i] < 1) flag = 0; + else if(a[i] > 0) f2 =0; + } + if(flag){ + cout << 1 << endl; + return; + } + if(f2){ + cout << n << endl; + return; + } + if(n==1){ + cout << 1 << endl; + return; + } + int mine = a[1] - a[0]; + int index = n; + for(int i = 1; i < n; i++){ + if(a[i] <= 0 && a[i-1] <= 0) mine = min(mine, abs(a[i]-a[i-1])); + if(a[i] > 0){ + if(a[i] > mine) index = i; + else index = i+1; + break; + } + } + cout << index << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_TMT_Document.cpp b/B_TMT_Document.cpp new file mode 100644 index 0000000..7fc0528 --- /dev/null +++ b/B_TMT_Document.cpp @@ -0,0 +1,46 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + string s; cin >> s; + vector t, m; + loop{ + if(s[i]=='T') t.push_back(i); + else m.push_back(i); + } + if(m.size()*2 != t.size()){ + cout << "NO" << endl; + return; + } + for(int i = 0; i < m.size(); i++){ + if(m[i] < t[i] || m[i] > t[i+m.size()]){ + cout << "NO" << endl; + return; + } + } + cout << "YES" << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Tea_with_Tangerines.cpp b/B_Tea_with_Tangerines.cpp new file mode 100644 index 0000000..2ce6d01 --- /dev/null +++ b/B_Tea_with_Tangerines.cpp @@ -0,0 +1,48 @@ +//∑i=1n⌈ai2⋅x−1⌉. + +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//--------------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + sort(a, a+n); + if(a[0] == 1){ + cout << accumulate(a, a+n, 0)-n << endl; + return; + } + int ans = 0; + int div = 2*a[0]-1; + for(int i = 1; i < n; i++){ + if( (a[i] + 2*a[0] - 2) / (2*a[0] - 1) > 1){ + ans += (a[i] + 2*a[0] - 2) / (2*a[0] - 1) - 1; + } + } + cout << ans << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/B_Two_Tables.cpp b/B_Two_Tables.cpp new file mode 100644 index 0000000..72a660d --- /dev/null +++ b/B_Two_Tables.cpp @@ -0,0 +1,53 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + cout << fixed << setprecision(9); + int W, H; + int x1, y1, x2, y2; + int w, h; + cin >> W >> H; + cin >> x1 >> y1 >> x2 >> y2; + cin >> w >> h; + float ans = -1; + //horizontal fit + // cout << "horizontal" << endl; + // cout << x1 << " " << x2 << " " << w << " " << W << endl; + if(w+(x2-x1) <= W){ + int t1 = max(x1, W-x2); + if(t1 >= w) ans = 0; + else ans = min(w-x1, x2-(W-w)); + } + // cout << "vertical" << endl; + if(h+(y2-y1) <= H){ + // cout << y1 << " " << y2 << " " << h << " " << H << endl; + int t2 = max(y1, H-y2); + if(t2 >= h) ans = 0; + else if(ans != -1) ans = min(ans, (float)(min((h-y1), y2-(H-h)))); + else ans = min(h-y1, y2-(H-h)); + } + cout << ans << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_1_Good_Subarrays_Easy_Version.cpp b/C_1_Good_Subarrays_Easy_Version.cpp new file mode 100644 index 0000000..defa3a5 --- /dev/null +++ b/C_1_Good_Subarrays_Easy_Version.cpp @@ -0,0 +1,82 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + + int ct = 0; + map, int> m; + int pre = 0; + int index = 1; + int piche = 0; + for(int i = 0; i < n; i++){ + // cout << i << " " << a[i] << " " << ct << " "; + if(a[i] >= index){ + ct++; + index++; + } + else{ + m[{piche, i-1}] = ct; + ct = 0; + index = 1; + i -= a[i]; + piche = i+1; + } + // cout << ct << endl; + } + // cout << endl; + if(ct != 0) m[{piche, n-1}] = ct; + + // for(auto &it: m) cout << it.first.first << " " << it.first.second << " - > " << it.second << endl; + + //we dont have overlaps + int ans = 0; + for(auto &it: m){ + int x = it.second; + ans += x*(x+1)/2; + } + + //considering the overlaps + vector> v; + for(auto &it: m){ + v.push_back({it.first.first, it.first.second}); + } + int p = v[0].second; + for(int i = 1; i < v.size(); i++){ + int now = v[i].first; + if(now <= p){ + int temp = now - p + 1; + // cout << temp << endl; + ans -= temp*(temp+1)/2; + } + p = v[i].second; + } + cout << ans << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Division_by_Two_and_Permutation.cpp b/C_Division_by_Two_and_Permutation.cpp new file mode 100644 index 0000000..7b38fc0 --- /dev/null +++ b/C_Division_by_Two_and_Permutation.cpp @@ -0,0 +1,59 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + for(int i = 0; i < n; i++){ + while(a[i] > n) a[i] /= 2; + } + sort(a, a+n); + vector vis(n+1, 0); + for(int i = 0; i < n; i++){ + if(vis[a[i]] == 0) vis[a[i]] = 1; + else{ + // cout << a[i] << " visited " << endl; + while(a[i] > 0 && vis[a[i]]){ + a[i] /= 2; + if(vis[a[i]] == 0){ + vis[a[i]] = 1; + break; + } + } + // cout << a[i] << " now visited " << endl; + } + } + for(int i = 1; i < n+1; i++){ + if(!vis[i]){ + cout << "NO" << endl; + return; + } + } + cout << "YES" << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Even_Number_Addicts.cpp b/C_Even_Number_Addicts.cpp new file mode 100644 index 0000000..ec2979b --- /dev/null +++ b/C_Even_Number_Addicts.cpp @@ -0,0 +1,44 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + a[i] %= 4; + } + // for(int i = 0; i < n; i++){ + // cout << a[i] << " "; + // } + // cout << endl; + int zero = 0; + for(int i = 0; i < n; i++){ + if(a[i] == 0) zero++; + } + if(!(zero&1)) cout << "Alice" << endl; + else cout << "Bob" << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Minimize_the_Thickness.cpp b/C_Minimize_the_Thickness.cpp new file mode 100644 index 0000000..43ea0f7 --- /dev/null +++ b/C_Minimize_the_Thickness.cpp @@ -0,0 +1,83 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + int ans = INT_MAX; + int total = accumulate(a, a+n, 0); + vector divisors; + for(int i = 1; i*i <= total; i++){ + if(total%i == 0) divisors.push_back(i); + if(total/i != i) divisors.push_back(i); + } + + if(divisors.size() <= 2){ + cout << n << endl; + return; + } + int div; + + vector v; + + int index = divisors.size(); + int i = 0; + while(index--){ + // cout << div << " div " << endl; + div = divisors[i]; + int maxm = INT_MIN; + int flag = 0; + + int temp = total/div; + int ct = 0; + int len = 0; + for(int i = 0; i < n; i++){ + ct += a[i]; + len++; + if(ct == temp){ + // cout << len << endl; + maxm = max(maxm, len); + ct = 0; + len = 0; + } + if(ct > temp){ + flag = 1; + break; + } + } + if(flag == 0){ + // maxm = max(maxm, len); + ans = min(ans, maxm); + } + + + i++; + } + cout << ans << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Paint_the_Array.cpp b/C_Paint_the_Array.cpp new file mode 100644 index 0000000..8da3057 --- /dev/null +++ b/C_Paint_the_Array.cpp @@ -0,0 +1,68 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + if(n==1){ + cout << a[0] << endl; + return; + } + if(n==2){ + if(a[0] == a[1]){ + cout << 0 << endl; + return; + } + cout << max(a[0], a[1]) << endl; + return; + } + if(n == 3){ + int temp = __gcd(a[0], a[2]); + if(a[1]%temp == 0){ + if(temp/__gcd(temp, a[1]) > 1){ + cout << temp << endl; + return; + } + } + if(a[1]%temp != 0){ + cout << temp << endl; + return; + } + if(a[0]%a[1] != 0 && a[2]%a[1] != 0) cout << a[1] << endl; + else cout << 0 << endl; + return; + } + int h1, h2; + h1 = __gcd(a[0], a[2]); + for(int i = 2; i < n-2; i += 2) h1 = __gcd(h1, a[i]); + h2 = __gcd(a[1], a[3]); + for(int i = 3; i < n-2; i += 2) h1 = __gcd(h2, a[i]); + if(h1==h2) cout << 0 << endl; + else cout << max(h1, h2) << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Pair_Programming.cpp b/C_Pair_Programming.cpp new file mode 100644 index 0000000..9da127f --- /dev/null +++ b/C_Pair_Programming.cpp @@ -0,0 +1,52 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n, x, y; cin >> n >> x >> y; + int a[x+y]; + for(int i = 0; i < x; i++){ + cin >> a[i]; + if(a[i] == 0) n++; + } + for(int i = 0; i < y; i++){ + cin >> a[i+x]; + if(a[i+x] == 0) n++; + } + sort(a, a+x+y) ; + for (int i = 0; i < x+y; i++){ + if(a[i] > n){ + cout << -1 << endl; + return; + } + } + for (int i = 0; i < x+y; i++) + { + /* code */ + cout << a[i] << " "; + } + cout << endl; + +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Paprika_and_Permutation.cpp b/C_Paprika_and_Permutation.cpp new file mode 100644 index 0000000..b383401 --- /dev/null +++ b/C_Paprika_and_Permutation.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +int main(){ + ios::sync_with_stdio(false); + cin.tie(nullptr); + int t; + cin >> t; + while(t>0){ + t--; + int n; + cin >> n; + set st; + for(int i=1;i<=n;i++){st.insert(i);} + vector rem; + for(int i=0;i> v; + if(st.find(v)!=st.end()){st.erase(v);} + else{rem.push_back(v);} + } + sort(rem.begin(),rem.end()); + reverse(rem.begin(),rem.end()); + int pt=0; + bool err=false; + for(auto &nx : rem){ + auto it=st.end(); + it--; + int ctg=(*it); + if(ctg>(nx-1)/2){err=true;break;} + st.erase(it); + } + if(err){cout << "-1\n";} + else{cout << rem.size() << '\n';} + } + return 0; +} \ No newline at end of file diff --git a/C_Sum_of_Cubes.cpp b/C_Sum_of_Cubes.cpp new file mode 100644 index 0000000..6c1f89c --- /dev/null +++ b/C_Sum_of_Cubes.cpp @@ -0,0 +1,47 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +const int N = 1e12; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +set cubes; +void pre(){ + for(int i = 1; i*i*i <= N; i++){ + cubes.insert(i*i*i); + } +} + +void solve(){ + int x; cin >> x; + for(int i = 1; i*i*i <= x; i++){ + int b = x-pow(i, 3); + if(cubes.count(b)) { + cout << "YES\n"; + return; + } + } + cout << "NO\n"; +} +//---------------------------------------------------------------------------- +int32_t main(){ + pre(); + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/C_Yet_Another_Card_Deck.cpp b/C_Yet_Another_Card_Deck.cpp new file mode 100644 index 0000000..2995e9d --- /dev/null +++ b/C_Yet_Another_Card_Deck.cpp @@ -0,0 +1,41 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n, q; cin >> n >> q; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + + int x; + // cout << q << endl; + while(q--){ + cin >> x; + int p = find(a, a+n, x) - a; + cout << p+1 << " "; + rotate(a, a+p, a+p+1); + } + cout << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + solve(); + + return 0; +} \ No newline at end of file diff --git a/D_Co-growing_Sequence.cpp b/D_Co-growing_Sequence.cpp new file mode 100644 index 0000000..423ca9a --- /dev/null +++ b/D_Co-growing_Sequence.cpp @@ -0,0 +1,43 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +int f(int x , int y){ + return x&~y; +} +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + int ans[n]; + ans[0] = 0; + for(int i = 1; i < n; i++){ + ans[i] = f(ans[i - 1] ^ a[i - 1], a[i]); + } + for(int i = 0; i < n; i++) cout << ans[i] << " "; + cout << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/D_Corrupted_Array.cpp b/D_Corrupted_Array.cpp new file mode 100644 index 0000000..46f83db --- /dev/null +++ b/D_Corrupted_Array.cpp @@ -0,0 +1,69 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n+2]; + for(int i = 0; i < n+2; i++){ + cin >> a[i]; + } + sort(a, a+n+2); + int index = -1; + int sum = accumulate(a, a+n+2, 0); + for(int i = 0; i < n+1; i++){ + + int temp = (sum-a[i]); + // cout << i<<" temp " << temp << endl; + if(temp%2 == 0){ + if(temp/2 == a[n+1]){ + index = i; + // cout << index << endl; + break; + } + } + } + if((sum-a[n+1])%2==0){ + if(a[n] == (sum-a[n+1])/2) index = n+1; + } + // cout << index << endl; + // cout << endl; + if(index == -1){ + cout << index << endl; + return; + } + if(index == n+1){ + for(int i = 0; i < n; i++){ + cout << a[i] << " "; + } + cout << endl; + return; + } + for(int i = 0; i < n+1; i++){ + if(i != index)cout << a[i] << " "; + } + cout << endl; + return; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/D_Epic_Transformation.cpp b/D_Epic_Transformation.cpp new file mode 100644 index 0000000..b7a0da0 --- /dev/null +++ b/D_Epic_Transformation.cpp @@ -0,0 +1,64 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + int a[n]; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + map m; + loop{m[a[i]]++;} + priority_queue q; + for(auto &it: m){ + q.push(it.second); + } + if(q.size() == 1){ + cout << q.top() << endl; + // cout << 1 << endl; + return; + } + if(m.size() == 2){ + m[0] = q.top(); q.pop(); + m[1] = q.top(); + cout << abs(m[0]-m[1]) << endl; + // cout << n << " about 2 " << endl; + return; + } + while(q.size() > 1){ + int x = q.top(); + q.pop(); + int y = q.top(); + q.pop(); + // cout << x << " " << y << endl; + if(x-1 > 0) q.push(x-1); + if(y-1 > 0) q.push(y-1); + } + // cout << " the size " << q.size() << " "; + if(q.size() == 0) cout << 0 << endl; + else cout << q.top() << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + // cout << t << " the t " << endl; + solve(); + } + return 0; +} \ No newline at end of file diff --git a/D_Palindromes_Coloring.cpp b/D_Palindromes_Coloring.cpp new file mode 100644 index 0000000..0ccb088 --- /dev/null +++ b/D_Palindromes_Coloring.cpp @@ -0,0 +1,48 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n, k; + cin >> n >> k; + string s; cin >> s; + map m; + for(int i = 0; i < n; i++){ + m[s[i]-'a']++; + } + int pair = 0; + for(auto &it: m){ + pair += it.second/2; + } + int want = n/k; + while(want){ + // cout << want << " " << want/2*k << endl; + if(want/2*k <= pair){ + cout << want << endl; + break; + } + want--; + } +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file diff --git a/E_Arranging_The_Sheep.cpp b/E_Arranging_The_Sheep.cpp new file mode 100644 index 0000000..613b5c6 --- /dev/null +++ b/E_Arranging_The_Sheep.cpp @@ -0,0 +1,55 @@ +#include +#define int long long +#define pb push_back +#define ppb pop_back +#define mp make_pair +#define ff first +#define ss second +#define PI 3.141592653589793238462 +#define set_bits __builtin_popcountll +#define sz(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define loop for(int i = 0; i < n; i++) +using namespace std; + +int countDigit(long long n){ +return floor(log10(n) + 1); +} +//----------------------------------------------------------------------------- +void solve(){ + int n; cin >> n; + string s; + cin >> s; + vector v; + for(int i = 0; i < n; i++){ + if(s[i]=='*') v.push_back(i); + } + if(v.size() == n || v.size() <= 1){ + cout << 0 << endl; + return; + } + int temp = v.size()/2; + // cout << v.size() << " temp " << temp << endl; + int ct = 0, pos = v[temp]; + int minus = 1; + for(int i = temp-1; i >= 0; i--){ + ct += pos-minus-v[i]; + // cout << pos << " " << minus << " " << v[i] << endl; + minus++; + // cout << "ct now " << ct << endl; + } + int add = 1; + for(int i = temp+1; i < v.size(); i++){ + ct += v[i]-(pos+add); + add++; + } + cout << ct << endl; +} +//---------------------------------------------------------------------------- +int32_t main(){ + int t; cin >> t; + while(t--){ + solve(); + } + return 0; +} \ No newline at end of file