Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CC_KSUB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t; cin>>t;
while(t--){
int n, k, g=0, kc=0; cin>>n>>k;
int arr[n];
for(int i=0; i<n; i++) cin>>arr[i], g=__gcd(g, arr[i]);
int tmpgcd=arr[0];
for(int i=1; i<n; i++){
if(tmpgcd==g){
kc++; tmpgcd=arr[i];
}
tmpgcd=__gcd(tmpgcd, arr[i]);
}
if(tmpgcd==g) kc++;
if(kc>=k) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
29 changes: 29 additions & 0 deletions appledivision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;
#define int long long

int res = INT_MAX, sum=0;
int cnt=0;
void fun(vector<int>&arr, int i, int s)
{
res=min(abs(sum-2*s), res);
if(i<0)
return;
fun(arr, i-1, s);
fun(arr, i-1, s+arr[i]);
}

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
vector<int> arr(n);

for(int i=0; i<n; i++) cin>>arr[i], sum+=arr[i];

fun(arr, n-1, 0);

cout<<res;
return 0;
}
27 changes: 27 additions & 0 deletions cses_NumberSpiral.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q; cin>>q;
while(q--){
int r, c; cin>>r>>c;
if(r==c){
cout<<(r-1)*(r-1)+r<<"\n";
continue;
}
int mx=max(r, c)-1;
mx*=mx;
if((max(r, c)&1 and max(r, c)==c) or (max(r, c)%2==0 and max(r, c)==r)){
cout<<mx+2*max(r,c)-min(r, c)<<"\n";
}
else{
cout<<mx+min(r,c)<<"\n";
}
}
return 0;
}
13 changes: 13 additions & 0 deletions cses_TwoKnights.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
for(int i=1; i<=n; i++){
cout<<(i*i*(i*i-1))/2-4*(i-1)*(i-2)<<"\n";
}
return 0;
}
19 changes: 19 additions & 0 deletions cses_bitstrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bits/stdc++.h>
using namespace std;
#define P 1000000007
int pow(int a, int n){
if(n==0) return 1;
if(n==1) return a;
long long tmp = pow(a, n/2)%P;
if(n&1) return (((tmp*tmp)%P)*a)%P;
return (tmp*tmp)%P;
}

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
cout<<pow(2, n);
return 0;
}
58 changes: 58 additions & 0 deletions cses_chessboardandqueens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bits/stdc++.h>
using namespace std;

#define int long long

bool canplace(int r, int c, vector<vector<char>> &arr){

if(arr[r][c]!='.') return false;

for(int i=r; i>=0; i--)
if(arr[i][c]=='P') return false;

for(int i=c; i>=0; i--)
if(arr[r][i]=='P') return false;

for(int i=r, j=c; i>=0 && j>=0; i--, j--)
if(arr[i][j]=='P') return false;

for(int i=r, j=c; i>=0 && j<8; i--, j++)
if(arr[i][j]=='P') return false;
//cout<<r<<" "<<c<<"}";
return true;
}

void fun(vector<vector<char>> &grid, int r, int c, int&res, int cnt)
{
if(cnt==8) res++;
if(cnt>8 or r>7 or c>7) return;
for(int i=r; i<8; i++){
for(int j=c; j<8; j++){
cout<<i<<" "<<j<<"}";
if(canplace(i, j, grid)){
char tmp='P';
swap(grid[i][j], tmp);
fun(grid, i+1, j+1, res, cnt+1);
swap(grid[i][j], tmp);
}
fun(grid, i+1, j+1, res, cnt);
}
}
}

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<vector<char>> grid(8,vector<char>(8));
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
cin>>grid[i][j];


int res=0;
fun(grid, 0, 0, res, 0);
cout<<res;

return 0;
}
24 changes: 24 additions & 0 deletions cses_graycode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;

vector<string> fun(int n){
if(n<1) return {};
if(n==1) return {"0", "1"};
vector<string> res=fun(n-1), res2=res;
for(int i=0; i<res.size(); i++) res[i]= '0'+res[i];
for(int i=0; i<res2.size(); i++) res2[i]= '1'+res2[i];
for(int i=res2.size()-1; i>=0; i--) res.push_back(res2[i]);
return res;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
vector<string> res = fun(n);
for(auto it : res){
cout << it <<"\n";
}
return 0;
}
19 changes: 19 additions & 0 deletions cses_increasing_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bits/stdc++.h>
using namespace std;

template <typename T> T mmax(T a, T b){
return a>b?a:b;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, sum=0, prev_mx; cin>>n>>prev_mx;
for(int i=0; i<n-1; i++){
long long curr; cin>>curr;
sum += mmax(prev_mx-curr, (long long int)0);
prev_mx = mmax(curr, prev_mx);
}
cout<<sum;
return 0;
}
29 changes: 29 additions & 0 deletions cses_palindromereorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s; cin>>s;
unordered_map<char, int> mp;
for(auto it : s) mp[it]++;
int oc=0, cnt=0;
char c;
for(auto it : mp) if(it.second &1) oc++, c=it.first, cnt=it.second;
if(oc>1 or (s.size()%2==1 and oc!=1)) cout<<"NO SOLUTION";
else{
deque<char> q;
while(cnt-->0) q.push_front(c);
mp[c]=0;
for(auto it : mp){
while(mp[it.first]){
q.push_back(it.first);
q.push_front(it.first);
mp[it.first]-=2;
}
}
while(q.size()){ cout<<q.front(); q.pop_front();}
}
return 0;
}
19 changes: 19 additions & 0 deletions cses_permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
if(n==1){
cout<<1; return 0;
}
if(n<4){
cout<<"NO SOLUTION";
return 0;
}
for(int i=2; i<=n; i+=2) cout<<i<<" ";
for(int i=1; i<=n; i+=2) cout<<i<<" ";
return 0;
}
12 changes: 12 additions & 0 deletions cses_trailingzeros.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, ans=0; cin>>n;
for(long long i=5; n/i>0; i*=5) ans+=n/i;
cout<<ans;
return 0;
}
30 changes: 30 additions & 0 deletions cses_twosets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
if(((n*(n+1))/2) &1)
{
cout<<"NO";
return 0;
}
cout<<"YES\n";
int ans=(n*(n+1))/4;
vector<int> s1, s2;
for(int i=n; i>0; i--){
if(i<=ans){
s1.push_back(i);
ans-=i;
}
else s2.push_back(i);
}
cout<<s1.size()<<"\n";
for(auto it : s1) cout<<it<<" ";

cout<<"\n"<<s2.size()<<"\n";
for(auto it : s2) cout<<it<<" ";
return 0;
}
11 changes: 11 additions & 0 deletions leetcode_greycode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
for(int i=0; i<(1<<n); i++) cout<< (i^(i>>1)) <<" ";
return 0;
}