Skip to content

Commit 2807660

Browse files
committed
🍪 Added First Code chef contest - CDRV21C
1 parent c7a7951 commit 2807660

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed

Diff for: codechef/CDRV21C/CHEFFAV.java

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package codechef.CDRV21C;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* A string S is called Chef's favourite if every substring chef in S must have a substring code before it.
7+
*
8+
* 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.
9+
*
10+
* 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.
11+
*
12+
* Input Format
13+
* The first line of the input contains an integer T - the number of test cases. The test cases then follow.
14+
* The first line of each test contains an integer N - the size of the string.
15+
* The second line of each test contains a string S.
16+
* Output Format
17+
* For each test case, output on a single line AC if S is Chef's favourite, or WA otherwise.
18+
*
19+
* Constraints
20+
* 1≤T≤10
21+
* 1≤N≤10^4
22+
* |S|=N
23+
* S consists only of lowercase English characters
24+
* Both code and chef appear as a substring at least once in S
25+
* Sample Input 1
26+
* 4
27+
* 8
28+
* codechef
29+
* 8
30+
* chefcode
31+
* 14
32+
* sxycodeghychef
33+
* 21
34+
* sxychefvsvcodehghchef
35+
* Sample Output 1
36+
* AC
37+
* WA
38+
* AC
39+
* WA
40+
* Explanation
41+
* Test case 1: Substring code is present before chef.
42+
* Test case 2: Substring code is not present before chef.
43+
* Test case 3: Substring code is present before chef.
44+
* Test case 4: There is no code substring present before the first occurrence of chef.
45+
*/
46+
public class CHEFFAV {
47+
public static void main (String[] args) throws java.lang.Exception
48+
{
49+
// your code goes here
50+
Scanner sc = new Scanner(System.in);
51+
int t = sc.nextInt();
52+
while (t-- > 0) {
53+
int num = sc.nextInt();
54+
String s = sc.next();
55+
56+
int codeIndex = s.indexOf("code");
57+
int chefIndex = s.indexOf("chef");
58+
59+
if(codeIndex < chefIndex){
60+
System.out.println("AC");
61+
}else{
62+
System.out.println("WA");
63+
}
64+
65+
}
66+
}
67+
}

Diff for: codechef/CDRV21C/FRGAME.java

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package codechef.CDRV21C;
2+
3+
import java.util.*;
4+
import java.lang.*;
5+
6+
/* Name of the class has to be "Main" only if the class is public. */
7+
8+
/**
9+
* @docs https://www.codechef.com/CDRV21C/problems/FRGAME
10+
*
11+
* 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.
12+
*
13+
* 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).
14+
*
15+
* Find the final winner of the game.
16+
*
17+
* Input Format
18+
* The first line of the input contains an integer T - the number of test cases. The test cases then follow.
19+
* The only line of each test case contains four space-separated integers A, B, C, and D.
20+
* Output Format
21+
* 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.
22+
*
23+
* Constraints
24+
* 1≤T≤1000
25+
* 0≤A,B,C,D≤10^6
26+
* Sample Input 1
27+
* 3
28+
* 2 3 4 5
29+
* 3 3 3 3
30+
* 2 3 1 2
31+
* Sample Output 1
32+
* S
33+
* N
34+
* S
35+
* Explanation
36+
* Test case 1:
37+
* Initially, Nitin has 2 coins and Sobhagya has 3 coins, so Sobhagya is winning.
38+
* Then, Ritik gives his 4 coins to Nitin. Now Nitin has 6 coins and Sobhagya has 3 coins, so Nitin is winning.
39+
* Then, Satyarth gives his 5 coins to Sobhagya. Finally Nitin has 6 coins and Sobhagya has 8 coins, so Sobhagya is the final winner.
40+
* Test case 2:
41+
* Initially, Nitin has 3 coins and Sobhagya has 3 coins, so Nitin is winning.
42+
* Then, Ritik gives his 3 coins to Sobhagya. Now Nitin has 3 coins and Sobhagya has 6 coins, so Sobhagya is winning.
43+
* Then, Satyarth gives his 3 coins to Nitin. Finally Nitin has 6 coins and Sobhagya has 6 coins, so Nitin is the final winner.
44+
*/
45+
class FRGAME
46+
{
47+
public static void main (String[] args) throws java.lang.Exception
48+
{
49+
// your code goes here
50+
Scanner sc = new Scanner(System.in);
51+
int t = sc.nextInt();
52+
while (t-- > 0) {
53+
char currentWin = 'N';
54+
55+
int[] arr = new int[4];
56+
57+
for(int i=0;i<arr.length;i++){
58+
arr[i] = sc.nextInt();
59+
}
60+
61+
if(arr[0] < arr[1]){
62+
arr[0] = arr[0] + arr[2];
63+
}else{
64+
arr[1] += arr[2];
65+
}
66+
67+
if(arr[0] < arr[1]){
68+
arr[0] = arr[0] + arr[3];
69+
}else{
70+
arr[1] += arr[3];
71+
}
72+
73+
if(arr[0] < arr[1]){
74+
System.out.println('S');
75+
}else{
76+
System.out.println('N');
77+
}
78+
}
79+
}
80+
}

Diff for: codechef/CDRV21C/KLXOR.java

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package codechef.CDRV21C;
2+
3+
import java.util.*;
4+
5+
/**
6+
* @docs https://www.codechef.com/CDRV21C/problems/KLXOR
7+
* You may have solved a lot of problems related to XOR and we are sure that you liked them, so here is one more problem for you.
8+
*
9+
* You are given a binary string S of length N. Let string T be the result of taking the XOR of all substrings of size K of S. You need to find the number of 1 bits in T.
10+
*
11+
* Note:
12+
*
13+
* 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.
14+
* XOR of many similar-sized strings is defined here.
15+
* Input Format
16+
* The first line of the input contains an integer T - the number of test cases. The test cases then follow.
17+
* The first line of each test case contains two integers N and K.
18+
* The second line of each test case contains a binary string S of size N.
19+
* Output Format
20+
* For each test case, output on a single line number of 1 bits in the XOR of all substrings of size K of S.
21+
*
22+
* Constraints
23+
* 1≤T≤10
24+
* 1≤K≤N≤2⋅105
25+
* |S|=N
26+
* S only contains characters 0 and 1.
27+
* Sample Input 1
28+
* 4
29+
* 4 1
30+
* 1010
31+
* 4 2
32+
* 1001
33+
* 4 3
34+
* 1111
35+
* 4 4
36+
* 0110
37+
* Sample Output 1
38+
* 0
39+
* 2
40+
* 0
41+
* 2
42+
* Explanation
43+
* Test case 1: All 1-sized substrings of S are:
44+
* 0
45+
* 1
46+
* 1
47+
* 0
48+
* The XOR of these strings is 0, therefore the number of 1 bits is 0.
49+
*
50+
* Test case 2: All 2-sized substrings of S are:
51+
* 10
52+
* 00
53+
* 01
54+
* The XOR of these strings is 11, therefore the number of 1 bits is 2.
55+
*
56+
* Test case 3: All 3-sized substrings of S are:
57+
* 111
58+
* 111
59+
* The XOR of these strings is 000, therefore the number of 1 bits is 0.
60+
*
61+
* Test case 4: All 4-sized substrings of S are:
62+
* 0110
63+
* The XOR of these strings is 0110, therefore the number of 1 bits is 2.
64+
*
65+
*
66+
*/
67+
public class KLXOR {
68+
69+
public static void main (String[] args) throws java.lang.Exception
70+
{
71+
// your code goes here
72+
Scanner sc = new Scanner(System.in);
73+
int t = sc.nextInt();
74+
while (t-- > 0) {
75+
int length = sc.nextInt();
76+
int k = sc.nextInt();
77+
String s = sc.next();
78+
79+
int count = 0;
80+
int fxor = s.charAt(0) - '0';
81+
int[] mas = new int[length];
82+
mas[0] = fxor;
83+
for (int i = 1; i < length; i++) {
84+
fxor ^= (s.charAt(i) - '0');
85+
mas[i] = fxor;
86+
}
87+
88+
for (int i = 0; i < k; i++) {
89+
int xor = fxor;
90+
for (int j = length-k+1+i; j < length; j++) {
91+
xor ^= (s.charAt(j) - '0');
92+
}
93+
if(i>0){
94+
xor ^= mas[i-1];
95+
}
96+
// for (int j = 0; j <i; j++) {
97+
// xor ^= (s.charAt(j) - '0');
98+
// }
99+
if (xor == 1) count++;
100+
}
101+
102+
System.out.println(count);
103+
104+
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)