Chef in Heaven Codechef Solution

Hello Programmers In this post, you will know how to solve the Chef in Heaven Codechef Solution.

Chef in Heaven Codechef Solution
Chef in Heaven Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Ezoicreport this adProblem

When Chef was born, his parents took him to the famous monk Doctor Strange to know whether he will land himself in heaven after his life or not. According to Strange, Chef will live for LL years in total. If he wants to go to heaven, he must spend at least 50%50% of his life years doing good deeds. He also shows them his future using a string SS of length LL where Si=0Si=0 means the ii-th year will be counted as bad as per the rule books of heaven and Si=1Si=1 means the ii-th year will be counted as good.

Also, Strange can use his special powers to make Chef end his life earlier than that planned by god, i.e, he can choose some L′L′ (1≤L′≤L1≤L′≤L) and make him live for only L′L′ years. Strange wants Chef to succeed, so if there is any choice of L′L′ that allows Chef to go to heaven, he will do so.

Tell whether Chef can go to heaven.

Input

  • The first line contains an integer TT, the number of test cases. Then the test cases follow.
  • Each test case contains two lines of input.
  • The first line contains a single integer LL.
  • The second line contains a string SS of length LL, consisting of symbols 0 and 1.

Output

For each test case, output the answer in a single line: “YES” if Chef can go to heaven and “NO” if not (without quotes).

You may print each character of each string in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).

Constraints

  • 1≤L≤1051≤L≤105
  • The sum of LL over all tests does not exceed 106106

Subtasks

Subtask #1 (100 points): original constraints

Sample Input 1 

3
2
10
3
001
4
0100

Sample Output 1 

YES
NO
YES

Explanation

Test case 1: If Chef lives for the complete 22 years, he will have a total of 11 good year which is 1∗1002=50%1∗1002=50% of his life, and hence he will go to heaven.

Test case 2: There’s no way Chef can go to heaven.

Test case 3: If Chef lives for 22 years, he will have a total of 11 good year which is 1∗1002=50%1∗1002=50% of his life, and hence he will go to heaven.

Chef in Heaven CodeChef Solution in JAVA

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 in=new Scanner(System.in);
		int T=in.nextInt();
		int L,good=0,bad=0,flag=0;
		char c;
		String s;
		while(T>0)
		{
		    T--;
		    L=in.nextInt();
		    s=in.next();
		    for(int y=0;y<s.length();y++)
		    {
		        c=s.charAt(y);
		        if(c=='1')
		        {
		            good++;
		        }
		        else
		        {
		            bad++;
		        }
		        if(good>=bad)
		        {
		            System.out.println("YES");
		            flag=100;
		            break;
		        }
		    }
		    if(flag!=100)
		    {
		        System.out.println("NO");
		    }
		    flag=0;good=0;bad=0;L=0;
		}
	}
}
Ezoicreport this ad

Chef in Heaven CodeChef Solution in CPP

#include <iostream>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t--){
	    int l;
	    cin>>l;
	    string s;
	    cin>>s;
	    int g=0;
	    int zero=0,one=0;
	    for(int i=0;i<l;i++){
	        if(s[i]=='0'){
	            zero++;
	        }else{
	            one++;
	        }
	            if(one>=zero){
	                g=1;
	                cout<<"YES\n";
	                break;
	            }
	    }
	    if(g==1){
	    }else{
	        cout<<"NO"<<endl;
	    }
	}
	return 0;
}

Chef in Heaven CodeChef Solution in Python

for _ in range(int(input())):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    ans=[]
    for i in range(k-1,n):
        ans.append(l[i]-l[i-(k-1)])
    print(min(ans))

Disclaimer: The above Problem (Chef in Heaven) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Chef and Laddus For Children Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad