Responsive Ads Here
Showing posts with label codechef. Show all posts
Showing posts with label codechef. Show all posts

Sunday, 28 May 2017

Team Formation For Snackdown

problem id:--https://www.codechef.com/SNCKPA17/problems/TEAMFORM


code:--

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;++i)
{
int num;
cin>>num>>num;
}
string ans[]={"yes","no"};
cout<<ans[n&1]<<'\n';
}
return 0;
}

Saturday, 27 May 2017

Prefix Inversions

problem id:--https://www.codechef.com/LTIME48/problems/PREFINVS


code:--

#include<iostream>
using namespace std;
int main()
{
string str;
cin>>str;
int l=str.length()-1,count=0;
char ch;
while(l>=0 && str[l]=='0')
{
--l;
}
if(l>=0)
do
{
if(str[l]=='0')
++count,ch=str[l];
else
++count,ch=str[l];
while(l>=0 && str[l]==ch)
{
--l;
}
}while(l>=0);
cout<<count<<'\n';
return 0;
}

Nothing in Common

problem id:--https://www.codechef.com/problems/NOTINCOM

code:--

#include<iostream>
#include<unordered_set>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int num;
unordered_set<int> myset;
for(int i=1;i<=n;++i)
{
cin>>num;
myset.insert(num);
}
int count=0;
for(int i=1;i<=m;++i)
{
cin>>num;
if(myset.find(num)!=myset.end())
++count;
}
cout<<count<<'\n';
}
return 0;
}

Friday, 26 May 2017

Bond And Fond (codechef contest)

problem id:--https://www.codechef.com/LOCMAY17/problems/BONDFOND


code:--

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
long long n,num,count=0,m,temp1,temp2,temp=1;
while(t--)
{
count=0;
cin>>n;
if((n&(n-1))==0)
{
cout<<0<<'\n';
continue;
}
num=n;
while(num)
{
++count;
num=num>>1;
}
temp1=n-(temp<<(count-1));
temp2=(temp<<count)-n;
m=temp1<temp2? temp1:temp2;
cout<<((temp1<temp2)? temp1:temp2)<<'\n';
}
return 0;

Wednesday, 24 May 2017

Snake Procession(codechef)

problem id:--https://www.codechef.com/SNCKQL17/problems/SNAKPROC

code:--


#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int l;
cin>>l;
string str;
cin>>str;
int i=0,flag=1;
string ans[]={"Invalid","Valid"};
while(str[i]!='\0')
{
if(str[i]!='.')
{
if(str[i]=='H' && flag==1)
flag=2;
else if(str[i]=='T' && flag==2)
flag=1;
else
{
flag=2;
break;
}
}
++i;
}
cout<<ans[(flag&1)]<<'\n';
}
return 0;
}

Temple Land(codechef)

problem id:-https://www.codechef.com/SNCKQL17/problems/TEMPLELA

code:--

  1. #include<iostream> #include<vector> using namespace std; int main() { int t; cin>>t; while(t--) { int n; cin>>n; int arr[n],flag=1; string ans[]={"no","yes"}; for(int i=0;i<n;++i) cin>>arr[i]; if((n&1)==0) flag=0; else { for(int i=1;i<=n;++i) if(i<=n/2) { if(i!=arr[i-1]) { flag=0; break; } } else { if((n-i+1)!=arr[i-1]) { flag=0; break; } } } cout<<ans[flag]<<'\n'; } return 0; }

Sunday, 14 May 2017

Jon and string (geeksforgeeks problem) using map

problem id:--http://practice.geeksforgeeks.org/problems/jon-and-string/0


code:--


#include<iostream>
#include<map>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
    {
    string str;
    cin>>str;
    int i=0;
    map<char,int> vowels,consonants;
    map<char,int>::iterator vitr,citr;
    while(str[i]!='\0')
        {
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
         ++vowels[str[i]];
        else
         ++consonants[str[i]];
        ++i;
        }
    int flag=1;
    if(str[0]=='a'||str[0]=='e'||str[0]=='i'||str[0]=='o'||str[0]=='u')
     flag=0;
    if(flag)
     {
     citr=consonants.begin();
     cout<<citr->first;
     --citr->second;
     if(citr->second==0)
     consonants.erase(citr);
     }
    while(vowels.size() && consonants.size())
     {
     citr=consonants.begin(),vitr=vowels.begin();
     cout<<vitr->first<<citr->first;
     --citr->second;
     --vitr->second;
     if(citr->second==0)
     consonants.erase(citr);
     if(vitr->second==0)
     vowels.erase(vitr);
}
while(vowels.size())
{
vitr=vowels.begin();
cout<<vitr->first;
--vitr->second;
if(vitr->second==0)
     vowels.erase(vitr);
}
while(consonants.size())
{
citr=consonants.begin();
cout<<citr->first;
--citr->second;
if(citr->second==0)
     consonants.erase(citr);
}
cout<<'\n';
    }

return 0;
}

Saturday, 13 May 2017

Factorial of large number (even 100!) using array

code:--

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[500]={0};
a[0]=1;
int l=1;
int carry=0,num;
for(int i=2;i<=n;++i)
{
for(int j=0;j<l;++j)
{
num=a[j]*i+carry;
a[j]=num%10;
carry=num/10;
}
while(carry)
a[l]=carry%10,++l,carry/=10;
}
for(int i=l-1;i>=0;--i)
cout<<a[i];
cout<<'\n';
}
return 0;
}

Friday, 12 May 2017

Chef and Sub Array (codechef)

problem id :- https://www.codechef.com/MAY17/problems/CHEFSUBA

code:-

#include<iostream>
using namespace std;
int main()
{
int n,k,p;
cin>>n>>k>>p;
int a[n];
for(int i=0;i<n;++i)
cin>>a[i];
string str;
cin>>str;
int j=0,i=0,max_val=0,val_j;
while(str[i])
{
if(str[i]=='!')
++j;
else
{
if(j!=0 && val_j==j )
{
cout<<max_val<<'\n';
continue;
}
val_j=j;
int count=0,m=0,z;
for(z=0;z<k;++z)
{
if(a[(n-j+z)%n]==1)
{
++count;
m=max(m,count);
}
else
count=0;
}
while(z<n)
{
if(a[(n-j+z)%n]==1 )
{
++count;
m=max(m,count%k);
}
else
{
count=0;
}
++z;
}
max_val=m;
cout<<m<<'\n';
}
++i;
}
return 0;
}

Wednesday, 10 May 2017

CPTTRN2 - Character Patterns (Act 2)

problem id:-http://www.spoj.com/problems/CPTTRN2/

code:-

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(i==0||i==n-1||j==0||j==m-1)
cout<<"*";
else
cout<<".";
}
cout<<'\n';
}
cout<<'\n';
}
return 0;
}

CPTTRN1 - Character Patterns (Act 1)

problem id:-http://www.spoj.com/problems/CPTTRN1/

code:--

#include <iostream>
using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
string str=".*";
int k=1;
for(int i=0;i<n;++i)
{
k=(i&1);
for(int j=0;j<m;++j)
{
k=k^1;
cout<<str[k];
}
cout<<'\n';
}
cout<<'\n';
}

return 0;
}

STRHH - Half of the half

problem id:-http://www.spoj.com/problems/STRHH/

code:-

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
int i=str.length()/2,j=0;
while(j<i)
{
cout<<str[j];
j+=2;
}
cout<<'\n';
}

return 0;
}

TEST - Life, the Universe, and Everything

problem id:-http://www.spoj.com/problems/TEST/

code:-

#include <iostream>
using namespace std;

int main() 
{
int n;
while(1)
{
cin>>n;
if(n==42)
break;
cout<<n<<'\n';
}
return 0;
}

Tuesday, 9 May 2017

codechef contest problem :Median of adjacent maximum numbers

problem id:-https://www.codechef.com/MAY17/problems/MXMEDIAN

code:--
//it works good for small test case but not for large test case
// wait till we upload the optimized code
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int num;
int max=0;
for(int i=0;i<n;++i)
{
cin>>num;
if(max<num)
max=num;
}
cout<<n-max<<'\n';
}
return 0;
}

Courses in an university

problem id:-https://www.codechef.com/MAY17/problems/UNICOURS

code:-

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int num;
int max=0;
for(int i=0;i<n;++i)
{
cin>>num;
if(max<num)
max=num;
}
cout<<n-max<<'\n';
}
return 0;
}

Chef and his daily routine

problem id:-https://www.codechef.com/MAY17/problems/CHEFROUT
code:-

#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
int i=0;
int min=0,sec,flag=0;
string s[]={"yes","no"};
while(str[i]!='\0')
{
switch(str[i])
{
case 'C':
sec=1;
break;

case 'E':
sec=2;
break;

case 'S':
sec=3;
}
if(sec-min<0)
{
flag=1;
break;
}
min=sec;
++i;
}
cout<<s[flag]<<'\n';
}
return 0;
}