Skip to content

Commit

Permalink
Added CSES solved problems
Browse files Browse the repository at this point in the history
  • Loading branch information
SambhavJain74 authored Sep 24, 2023
1 parent e4a0b99 commit 26c6588
Show file tree
Hide file tree
Showing 24 changed files with 934 additions and 0 deletions.
26 changes: 26 additions & 0 deletions CSES/1_introductory_problems/10_trailing_zeros.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

int solve()
{
int n,ans=0;
cin>>n;
while(n>0)
{
ans+=n/=5;
}
return ans;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout<<solve()<<"\n";
return 0;
}
32 changes: 32 additions & 0 deletions CSES/1_introductory_problems/11_coin_piles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<bits/stdc++.h>
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

bool solve()
{
int a,b;
cin>>a>>b;
int t=2*(min(a,b))-max(a,b);
if(t<0||t%3)
{
return false;
}
return true;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
solve()?yes:no;
}
return 0;
}
53 changes: 53 additions & 0 deletions CSES/1_introductory_problems/12_palindrome_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

string solve()
{
string s;
int n,odd_count=0;
vector<int>vec(26,0);
cin>>s;
n=s.size();
for(int i=0;i<n;i++)
{
vec[s[i]-'A']++;
}
for(int i=0;i<26;i++)
{
if(vec[i]&1)
{
odd_count++;
s[n/2]=(char)(i+'A');
vec[i]--;
}
}
if(odd_count>1)
{
return "NO SOLUTION";
}
int j=0;
for(int i=0;i<26;i++)
{
while(vec[i])
{
s[j]=(char)(i+'A');
s[n-j-1]=(char)(i+'A');;
vec[i]-=2;
j++;
}
}
return s;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout<<solve()<<"\n";
return 0;
}
37 changes: 37 additions & 0 deletions CSES/1_introductory_problems/13_gray_code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

void solve()
{
int n,range,div,pos;
cin>>n;
range=pow(2,n);
string s(n,'0');
cout<<s<<"\n";
for(int i=1;i<range;i++)
{
div=1;
pos=n-1;
while(i%(div*2)==0)
{
div*=2;
pos--;
}
s[pos]=(s[pos]=='0')?'1':'0';
cout<<s<<"\n";
}
return;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
30 changes: 30 additions & 0 deletions CSES/1_introductory_problems/14_tower_of_hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

void solve(int n,int a,int b,int c)
{
if(n==1)
{
cout<<a<<" "<<c<<"\n";
return;
}
solve(n-1,a,c,b);
cout<<a<<" "<<c<<"\n";
solve(n-1,b,a,c);
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
cout<<pow(2,n)-1<<"\n";
solve(n,1,2,3);
return 0;
}
35 changes: 35 additions & 0 deletions CSES/1_introductory_problems/1_weird_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

void solve()
{
int n;
cin>>n;
while(n!=1)
{
cout<<n<<" ";
if(n&1)
{
n=n*3+1;
}
else
{
n/=2;
}
}
cout<<n;
return;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
35 changes: 35 additions & 0 deletions CSES/1_introductory_problems/2_missing_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

int solve()
{
int n,temp;
cin>>n;
vector<bool>vec(n,false);
for(int i=0;i<n-1;i++)
{
cin>>temp;
vec[temp-1]=true;
}
for(int i=0;i<n;i++)
{
if(!vec[i])
{
return i+1;
}
}
return -1;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout<<solve()<<"\n";
return 0;
}
36 changes: 36 additions & 0 deletions CSES/1_introductory_problems/3_repetitions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

int solve()
{
int n,curr,ans=0;
string s;
cin>>s;
n=s.size();
int i=0;
while(i<n)
{
curr=1;
while(i<n-1&&s[i]==s[i+1])
{
curr++;
i++;
}
i++;
ans=max(ans,curr);
}
return ans;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout<<solve();
return 0;
}
35 changes: 35 additions & 0 deletions CSES/1_introductory_problems/4_increasing_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

int solve()
{
int n,curr,prev,ans=0;
cin>>n;
cin>>prev;
for(int i=1;i<n;i++)
{
cin>>curr;
if(curr<prev)
{
ans+=prev-curr;
}
else
{
prev=curr;
}
}
return ans;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout<<solve();
return 0;
}
40 changes: 40 additions & 0 deletions CSES/1_introductory_problems/5_permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define endl "\n"
#define int long long
using ll=long long;
using namespace std;

void solve()
{
int n;
cin>>n;
if(n==1)
{
cout<<1;
return;
}
if(n<=3)
{
cout<<"NO SOLUTION";
return;
}
for(int i=2;i<=n;i+=2)
{
cout<<i<<" ";
}
for(int i=1;i<=n;i+=2)
{
cout<<i<<" ";
}
return;
}

int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
Loading

0 comments on commit 26c6588

Please sign in to comment.