Single Operation Part 2 CodeChef Solution

Problem – Single Operation Part 2 CodeChef Solution

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.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two lines of inputs:
    • The first line contains the length of the binary string S.
    • The second line contains the binary string S.

Output Format

For each test case, output on a new line, the value of Y which will minimize the value of X after the operation.

Constraints

  • 1≤T≤5⋅10^4
  • 1≤∣S∣≤10^5
  • The sum of |S| over all test cases won’t exceed 5⋅10^5.
  • S contains the characters 0 and 1 only.

Sample 1:

Input:
4
2
10
2
11
3
101
3
110
Output:
2
1
2
1

Explanation:

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.

Single Operation Part 2 CodeChef Solution in C++17

#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';
    }
}

Single Operation Part 2 CodeChef Solution in Pyth 3

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)
        

    
    

Single Operation Part 2 CodeChef Solution in Java

/* 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);
		}
		
		
		
	}
}
Single Operation Part 2 CodeChef Solution Review:

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

Find on CodeChef

Conclusion:

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

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *