Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Chef has the binary representation S of a number X with him. He can modify the number by applying the following operation exactly once:
Chef wants to minimize the value of X after performing the operation. Help Chef in determining the value of Y which will minimize the value of X after the operation.
For each test case, output on a new line, the value of Y which will minimize the value of X after the operation.
Input:
4
2
10
2
11
3
101
3
110
Output:
2
1
2
1
Test case 1: Since S=10 is the binary representation of 2, the current value of X=2. On choosing Y=2, X becomes 2⊕⌊2/2^2⌋=2. We can show that this is the minimum value of X we can achieve after one operation.
Test case 2: Since S=11 is the binary representation of 3, the current value of X=3. On choosing Y=1, X becomes 3⊕⌊3/2^1⌋=2. We can show that this is the minimum value of X we can achieve after one operation.
Test case 3: Since S=101 is the binary representation of 5, the current value of X=5. On choosing Y=2, X becomes 5⊕⌊5/2^2⌋=4. We can show that this is the minimum value of X we can achieve after one operation.
Test case 4: Since S=110 is the binary representation of 6, the current value of X=6. On choosing Y=1, X becomes 6⊕⌊6/2^1⌋=5. We can show that this is the minimum value of X we can achieve after one operation.
#include<bits/stdc++.h>
int main(){
int t; std::cin >> t;
while(t--){
int n; std::string s;
std::cin >> n >> s;
int res = n;
for(int i = 1; i < n; ++i){
if(s[i] == '1'){
res = std::min(res, i);
break;
}
}
std::cout << res << '\n';
}
}
msd = int(input())
for aj in range(msd):
n = int(input())
S = str(input())
y = n
for i in range(1,n):
if S[i] == '1':
y = i
break
print(y)
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
// Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0) {
int l = Integer.parseInt(br.readLine());
String s = br.readLine();
int i=0, j=0;
while(i<l && s.charAt(i)!='1') i++;
j = i+1;
while(j<l && s.charAt(j)!='1') j++;
System.out.println(j-i);
}
}
}
In our experience, we suggest you solve this Single Operation Part 2 CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Single Operation Part 2 CodeChef Solution
I hope this Single Operation Part 2 CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>