0% found this document useful (0 votes)
5 views

Codechef Assignment Week1

Uploaded by

Ilham Shoyo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Codechef Assignment Week1

Uploaded by

Ilham Shoyo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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>

using namespace std;

int main() {

int t;

cin>>t;

while(t--)

long long int n,x,y,count=0;

cin>>n>>x>>y;

long long int a[n];

for(long long int i=0;i<n;i++)

cin>>a[i];

if(a[i]<=x && (a[i]%y)==0)

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.)

Misha is allowed to chose a sub-sequence (i.e. not necessarily contiguous substring) of


brackets. AFTER chosing a the sub-sequence, she is allowed to re-arrange it (if needed)
such that this chosen sub-sequence (after rearrangement) is balanced. Find the
maximum length of this sub-sequence. (Please note that the resulting sub-sequence ,
after rearrangement MUST be balanced.)
Ans 2.

#include <bits/stdc++.h>

using namespace std;

int main() {

int t;

cin>>t;

while(t--)

long long int n,c1=0,c2=0;

cin>>n;

string s;

cin>>s;

for(long long int i=0;i<n;i++)

if(s[i]=='(')

c1++;

else c2++;

long long int min;

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>

using namespace std;

int main() {

int t;

cin>>t;

while(t--)

long long int n,co=0,ce=0,max=0;

cin>>n;

long long int a[n];

for(long long int i=0;i<n;i++)

cin>>a[i];

if(i%2==0)

ce+=a[i];

else co+=a[i];

max=co>ce?co:ce;

cout<<max<<endl;

return 0;

You might also like