Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Self-Destructing Strings CodeChef Solution

Problem -Self-Destructing Strings CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Self-Destructing Strings CodeChef Solution in C++17

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
void solve(){
    string str;
    cin >> str;
    ll one=0,zero=0;
    for(int i=0;i<str.length();i++){
        if(str[i]=='0') zero++;
        else one++;
    }
    if(str.length()%2==1 or zero==0 or one==0){
        cout<<-1<<endl;
        return;
    }
    cout<<abs(one-zero)/2<<endl;
}
int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   // Write your code here 
   ll t;
   cin >> t;
   while(t--){
    solve();
   }
   return 0;
}

Self-Destructing Strings CodeChef Solution in C++14

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
void solve(string str){
   
    ll one = 0, zero = 0;
    for(int i = 0;i < str.length(); i++){
        if(str[i] == '0') zero++;
        else one++;
    }
    if(str.length() % 2 == 1 or zero == 0 or one == 0){
        cout << -1 << endl;
        return;
    }
    cout << abs(one - zero) / 2 << endl;
}
int main(){

   ll t;
   cin >> t;
   while(t--){
        string str;
        cin >> str;
        solve(str);
   }
   return 0;
}

Self-Destructing Strings CodeChef Solution in PYTH 3

# cook your dish here
for _ in range(int(input())):
    s = input()
    if len(s) % 2 == 1 or s.count('0') == 0 or s.count('0') == len(s):
        print(-1)
    
        
        
    else:
        # abs(0s-1s) = abs(0s-(n-0s))
        print(abs(2*s.count('0')-len(s))//2)

Self-Destructing Strings CodeChef Solution in C

#include <stdio.h>
#include <string.h>
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		char str[1000000];
		scanf("%s",str);
		int len=strlen(str),res,count0=0,count1=0,flag=0,diff;
	
		for(int i=0;i<len;i++){
			if(str[i]=='0'){
				count0++;
			}
			if(str[i]=='1'){
				count1++;
			}
		}
			if(len%2!=0 || count0==0 || count1==0){
			res=-1;
			flag=1;
		}
		diff=count0-count1;
		if(diff<0){
			diff=-(diff);
		}
		if(flag==0){
			res=diff/2;
		}
		printf("%d\n",res);
	}
}

Self-Destructing Strings 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) {
		Scanner scnScanner=new Scanner(System.in);
		int cases=scnScanner.nextInt();
		while(cases!=0) {
			String string=scnScanner.next();
			System.out.println(count(string));
			cases--;
		}

	}
	public static int count(String str) {
		if(str.length()%2!=0) {
			return -1;
		} 
		int one=0,zero=0;
		for(int i=0;i<str.length();i++) {
			if(str.charAt(i)=='0') {
				zero++;
			}else {
				one++;
			}
		}
		if(zero==one) {
			return 0;
		}
		if(zero==0||one==0) {
			return -1;
		}
		else {
			int diff=Math.abs(zero-one);
			return diff/2;
		}
	}
}

Self-Destructing Strings CodeChef Solution in PYPY 3

T = int(input())

for _ in range(T):
    S = input()
    zeros = S.count('0')
    ones = len(S) - zeros
    
    if len(S) % 2 == 1 or zeros == len(S) or ones == len(S):
        print(-1)
    else:
        print(abs(zeros-ones) // 2)
        
                
                    

Self-Destructing Strings CodeChef Solution in PYTH

t = int(raw_input())
for i in range(t):
	st = raw_input().strip()
	sz = len(st)
	if sz%2 == 1:
		r = -1
	else:
		cnt = 0
		for x in st:
			if x == '0':
				cnt += 1
			# endif
		# endfor x
		c1 = sz-cnt
		r = abs(c1-cnt)/2
		if (c1 == 0) or (cnt == 0):
			r = -1
		# endif
	# endif
	print r
# endfor i

Self-Destructing Strings CodeChef Solution in C#

using System;
using System.Text;

public class Test
{
	public static void Main()
	{
		SelfDestructingStrings.Run();
	}
	
	public class SelfDestructingStrings
    {
        public static void Run()
        {
            var T = Convert.ToInt32(Console.ReadLine().Trim());
            var resBuilder = new StringBuilder();
            for (int t = 0; t < T; t++)
            {                
                var S = Console.ReadLine();

                var res = -1;
                if (S.Length % 2 == 0)
                {
                    var oneCount = 0;
                    var zeroCount = 0;
                    foreach (var s in S)
                    {
                        if (s == '0')
                            zeroCount++;
                        else
                            oneCount++;
                    }

                    if (zeroCount == 0 || oneCount == 0)
                        res = -1;
                    else
                        res = Math.Abs(zeroCount - oneCount) / 2;
                }
                resBuilder.AppendLine(res + "");
            }
            Console.WriteLine(resBuilder.ToString().Trim());
        }     
    }   
}

Self-Destructing Strings CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func maxInt(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

func minInt(x int, y int) int {
	if x > y {
		return y
	}
	return x
}

func absInt(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func getMoves(s *string) int {
	if len(*s)%2 == 1 {
		return -1
	}
	numZeros := 0
	for _, c := range *s {
		if c == '0' {
			numZeros++
		}
	}
	numOnes := len(*s) - numZeros
	if numZeros == 0 || numOnes == 0 {
		return -1
	}
	return absInt(numZeros-numOnes) / 2
}

func main() {
	defer writer.Flush()
	var testCases int
	for scanf("%d\n", &testCases); testCases > 0; testCases-- {
		var s string
		scanf("%s\n", &s)
		printf("%d\n", getMoves(&s))
	}
}
Self-Destructing Strings CodeChef Solution Review:

In our experience, we suggest you solve this Self-Destructing Strings 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 Self-Destructing Strings CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Self-Destructing Strings 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 Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

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