Skip to content

Instantly share code, notes, and snippets.

@vinitshahdeo
Last active October 15, 2018 16:49
Show Gist options
  • Save vinitshahdeo/815ff68bb1fd58841bbe2a8425f4f141 to your computer and use it in GitHub Desktop.
Save vinitshahdeo/815ff68bb1fd58841bbe2a8425f4f141 to your computer and use it in GitHub Desktop.
CodeChef
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include<bits/stdc++.h>
using namespace std;
bool check(int n)
{
int prev=0;
while(n)
{
if((n&1) && prev)
return false;
prev=n&1;
n>>=1;
}
return true;
}
int main()
{
int n,q;
cin>>n>>q;
int a[n];
bool b[n]={false};
for(int i=0;i<n;i++)
{
cin>>a[i];
if(!check(a[i]))
b[i]=true;
}
while(q--)
{
int l,r;
cin>>l>>r;
int count=0;
for(int i=l-1;i<r;i++)
{
if(b[i])
count++;
}
cout<<count<<endl;
}
}
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,k,tot=0;
string s1,s2;
cin>>s1>>s2;
string n1=s1;
string n2=s2;
i=s1.size();
j=s2.size();
tot=i+j;
for(i=0;i<s1.size();i++)
{
for(j=0;j<s2.size();j++)
{
if(s1[i]==s2[j])
{
tot=tot-2;
s2[j]='*';
break;
}
}
}
if(tot == 2 ||tot== 4 || tot==7 ||tot==9 )
cout<<"Enemy";
else if( tot == 3 ||tot==5 || tot==14 ||tot==16 || tot==18 )
cout<<"Friendship";
else if( tot == 8 || tot==12 || tot==17)
cout<<"Affection";
else if( tot == 10)
cout<<"Love";
else if( tot == 6 || tot==11 || tot==15)
cout<<"Marriage";
else
cout<<"Sister";
return 0;
}
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l=s.size();
int dp[l+1];
memset(dp,0,sizeof(dp));
for(int i=l-1;i>=0;i--)
{
if((s[i]-'0')%2==0)
dp[i]=dp[i+1]+1;
else
dp[i]=dp[i+1];
}
for(int i=0;i<l;i++)
cout<<dp[i]<<" ";
cout<<endl;
}
}
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
map<string,int> m;
int flag=0;
while(n--)
{
string s;
int x;
cin>>s>>x;
if(m.find(s)!=m.end())
{
if(m[s]<x)
{
m[s]=x;
flag=1;
}
}
else
{
if(x>=k)
m[s]=x;
else
flag=1;
}
}
if(!flag)
cout<<"Sorry Winit!";
else
{
map<string,int> ::iterator it;
for(it=m.begin();it!=m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
}
}
return 0;
}
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
int t,flag;
cin>>t;
while(t--)
{
string s,s1;
cin>>s;
flag=0;
for(int i=0;i<s.length();i++)
{
if(!(s[i]=='1'||s[i]=='0'||s[i]=='8'))
{
flag=1;
break;
}
}
if(flag==0)
{
string s1=s;
reverse(s.begin(),s.end());
if(s1==s)
cout<<"YES!! Please have a RED BULL"<<endl;
else
cout<<"SORRY! Better Luck Next time"<<endl;
}
else
cout<<"SORRY! Better Luck Next time"<<endl;
}
}
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include <bits/stdc++.h>
using namespace std;
string myfunc(string s)
{
stack<char> stk;
for(int i=0;i<s.size();i++)
{
if(stk.empty())
stk.push(s[i]);
else
{
if(stk.top()==s[i])
continue;
else
stk.push(s[i]);
}
}
string ans="";
while(!stk.empty())
{
ans+=stk.top();
stk.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
cout<<myfunc(s)<<endl;
}
return 0;
}
/*
Author - Vinit Shahdeo
Copyright - CodeChef VIT Chapter
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int a[n];
vector<int> v;
for(int i=0;i<n;i++)
{
cin>>a[i];
v.push_back(__builtin_popcount(a[i]));
}
sort(v.rbegin(),v.rend());
int sum=0;
for(int i=0;i<k;i++)
{
sum+=v[i];
}
cout<<sum<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment