Code Drive Division 3 Solutions

Codechef Code Drive Division 3 Solutions





Code Drive Division 3


Game between friends 


Nitin and Sobhagya were playing a game with coins. If Sobhagya has more coins then he is winning, otherwise Nitin is winning. Note that this means if both Nitin and Sobhagya have the same number of coins, then Nitin is winning.

Initially Nitin has A coins while Sobhagya has B coins. Then Ritik came and gave his C coins to the player who is not winning currently, after which Satyarth came and repeated the same process (gave his D coins to the player who is not winning currently).

Find the final winner of the game.

Input Format

  • The first line of the input contains an integer T - the number of test cases. The test cases then follow.
  • The only line of each test case contains four space-separated integers ABC, and D.

Output Format

For each test case, output on a single line N if Nitin is the final winner of the game, or S if Sobhagya is the final winner of the game.

Constraints

  • 1T1000
  • 0A,B,C,D106

Sample Input 1 

3
2 3 4 5
3 3 3 3
2 3 1 2

Sample Output 1 

S
N
S

Explanation

  • Test case 1:
    • Initially, Nitin has 2 coins and Sobhagya has 3 coins, so Sobhagya is winning.
    • Then, Ritik gives his 4 coins to Nitin. Now Nitin has 6 coins and Sobhagya has 3 coins, so Nitin is winning.
    • Then, Satyarth gives his 5 coins to Sobhagya. Finally Nitin has 6 coins and Sobhagya has 8 coins, so Sobhagya is the final winner.
  • Test case 2:
    • Initially, Nitin has 3 coins and Sobhagya has 3 coins, so Nitin is winning.
    • Then, Ritik gives his 3 coins to Sobhagya. Now Nitin has 3 coins and Sobhagya has 6 coins, so Sobhagya is winning.
    • Then, Satyarth gives his 3 coins to Nitin. Finally Nitin has 6 coins and Sobhagya has 6 coins, so Nitin is the final winner.

Solution in C++ :

#include <iostream>
using namespace std;

int main() {
int t,a,b,c,d;
cin>>t;
while(t--){
    cin>>a>>b>>c>>d;
    if(a>=b) b+=c;
    else a+=c;
    if(a>=b) b+=d;
    else a+=d;
    
   if(a>=b) cout<<"N"<<endl;
   else cout<<"S"<<endl;
    
}
return 0;
}



Favourite String of Chef


A string S is called Chef's favourite if every substring chef in S must have a substring code before it.

You are given a string S of size N that contains both code and chef as a substring. Please determine if S is Chef's favourite.

Note: A substring is a continuous part of string which can be obtained by deleting some (may be none) character's from the beginning and some (may be none) character's from the end.

Input Format

  • The first line of the input contains an integer T - the number of test cases. The test cases then follow.
  • The first line of each test contains an integer N - the size of the string.
  • The second line of each test contains a string S.

Output Format

For each test case, output on a single line AC if S is Chef's favourite, or WA otherwise.

Constraints

  • 1T10
  • 1N104
  • |S|=N
  • S consists only of lowercase English characters
  • Both code and chef appear as a substring at least once in S

Sample Input 1 

4
8
codechef
8
chefcode
14
sxycodeghychef
21
sxychefvsvcodehghchef

Sample Output 1 

AC
WA
AC
WA

Explanation

  • Test case 1: Substring code is present before chef.
  • Test case 2: Substring code is not present before chef.
  • Test case 3: Substring code is present before chef.
  • Test case 4: There is no code substring present before the first occurrence of chef.

Solution in Python :

for _ in range(int(input())):
   n=int(input())
   #l=list(map(int,input().split()))
   s=input()
   if s.find("code")<s.find("chef"):
      print("AC")
   else : print("WA")




Count Number of Peaks 


You are given an integer N where N10. Consider any array A with length N where each element can either be 01, or 2, we define f(A) as the number of extrema in A. You need to find the sum of f(A) over all possible arrays A.

Note:

  • In an array A, we consider Ai as an extremum if it is strictly greater than both of its neighbors (i.e. Ai>Ai1 and Ai>Ai+1), or if it is strictly smaller than both of its neighbors (i.e. Ai<Ai1 and Ai<Ai+1). Note that first and last elements are not counted as extremum.
  • Extrema is the plural of extremum.

Input Format

  • The first line of the input contains an integer T - the number of test cases. The test cases then follow.
  • The only line of each test case contains one integer N.

Output Format

For each test case, output on a single the sum of f(A) over all possible arrays A.

Constraints

  • 1T10
  • 1N10

Sample Input 1 

3
1
3
5

Sample Output 1 

0
10
270

Explanation

  • Test case 1:
    • A=[0]. Then f(A)=0.
    • A=[1]. Then f(A)=0.
    • A=[2]. Then f(A)=0.

Therefore the answer is 0.

  • Test case 2: There are 10 arrays A with f(A)=1:
    • A=[0,1,0].
    • A=[0,2,0].
    • A=[0,2,1].
    • A=[1,2,0].
    • A=[1,2,1].
    • A=[2,1,2].
    • A=[2,0,2].
    • A=[2,0,1].
    • A=[1,0,2].
    • A=[1,0,1].

The remaining A's has f(A)=0. Therefore the answer is 110=10.


Solution in Python :

for _ in range(int(input())):
   n=int(input())
   s=0
   if(n==3): print(10)
   elif(n>3):
       s=pow(3,(n-3))*8*(n-2)
       s+=s//4
       print(s)
   else:
       print(0)

OR

for _ in range(int(input())):
   n=int(input())
   s=0
   if(n==3): print(10)
   elif(n>3):
       print(pow(3,(n-3))*10*(n-2))
   else:
       print(0)