Codechef Assignment Week1
Codechef Assignment Week1
Q1.
Given an array A of N integers and two integers X and Y, find the number of integers in the array
that are both less than or equal to X and divisible by Y.
Ans 1.
#include <iostream>
int main() {
int t;
cin>>t;
while(t--)
cin>>n>>x>>y;
cin>>a[i];
count++;
cout<<count<<endl;
return 0;
}
Q2.
Misha was playing with Balanced Brackets alone. Mykhailo also wanted to play the
game with her, so he comes up and asks her a question.
"You sure play lot of balanced brackets, but do you know how to make balanced
brackets?"
He tells Misha that, he will give her a string consisting of only '(' and ')', which is not
necessarily balanced (A string of '(' and ')' is balanced if for every '(' we can find a
matching ')' later and vice versa.)
#include <bits/stdc++.h>
int main() {
int t;
cin>>t;
while(t--)
cin>>n;
string s;
cin>>s;
if(s[i]=='(')
c1++;
else c2++;
min=c1<c2?c1:c2;
cout<<2*min<<endl;
return 0;
}
Q3.
You are given an array of N non-negative integers: A1, A2, ..., AN. An alternating
subsequence is a subsequence in which the indices of any two consecutive elements
differ by exactly two in the original array. That is, if Ai1, Ai2, ..., Aik is some subsequence,
then for it to be an alternating subsequence, (i2 - i1 = 2), (i3 - i2 = 2), and so on should all
hold true. Among all alternating subsequences, find the one which has maximum sum of
elements, and output that sum.
Ans 3.
#include <iostream>
int main() {
int t;
cin>>t;
while(t--)
cin>>n;
cin>>a[i];
if(i%2==0)
ce+=a[i];
else co+=a[i];
max=co>ce?co:ce;
cout<<max<<endl;
return 0;