Birthday Gift Again CodeChef Solution

Problem -Birthday Gift Again 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.

Birthday Gift Again CodeChef Solution in C++17

#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t; cin>>t; 
    while(t--) {
        string s; cin>>s;
        ll n = s.size();
        vector<ll>one(n+1), zero(n+1);
        
        one[n]=0; zero[n]=0;
        for(ll i=n-1; i>=0; i--) {
            zero[i] = (s[i] == '0') + zero[i+1];
            one[i] = (s[i] == '1') + one[i+1];
        }
        
        // cout<<"0: "; for(ll i=0; i<zero.size(); i++) cout<<zero[i]<<" ";
        // cout<<"\n1: "; for(ll i=0; i<zero.size(); i++) cout<<one[i]<<" ";
        // cout<<endl;
        
        ll idx = 1, ans=0;
        while(idx*(idx+1) <= n) {
            for(ll i=0; i<=(n-(idx*idx + idx)); i++) {
                ll count_zero = zero[i] - zero[i+idx*idx + idx];
                ll count_one = one[i] - one[i+idx*idx + idx];
                
                // cout<<i<<" "<<i+idx*idx + idx<<endl;
                
                ans += (count_zero == count_one*count_one);
            } idx++;
        } cout<<ans<<endl;
        
    }
}

Birthday Gift Again CodeChef Solution in C++14

#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long int lli;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define REP(i,a,b) for( long long i=a;i<b;i++)
#define REPR(i,a,b) for(long long i=a;i>=b;i--)
#define adb(a) for(auto ii:a) cout << ii << " "; cout << endl
#define adp(a) for(auto ii:a) cout << ii.F << " " << ii.S << endl;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#define endl "\n"
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
#define PI 3.14159265358979323846
const ll MAXN = 1000001;
//const ll p = 1000000007;
#define N 200005
#define INF INTMAX_MAX

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1> {}(p.first);
        auto hash2 = hash<T2> {}(p.second);
        return hash1 ^ hash2;
    }
};
struct cmp
{
    bool operator()(const pll &a, const pll &b)
    {
        if (a.F == b.F) return a.S > b.S;
        return a.F > b.F;
    }
};

ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, 1, -1};

ll power(ll a, ll b, ll m) { ll ans = 1; a = a % m; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; }

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

ll modInverse(ll a, ll m) { ll g = __gcd(a, m); return power(a, m - 2, m); }


int a[20];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output2.txt", "w", stdout);
#endif

    fastIO;
    int test = 1, num = 1;
    cin >> test;
    while (test--)
    {
        string s;
        cin >> s;
        int n = s.size();

        int cnt = 0;
        int ans = 0;
        //map<int,int> mapi;
        int j;
        vector<int>a(n, 0);
        if (s[0] == '1')
            a[0]++;
        for (int i = 1; i < n; i++) {

            a[i] = a[i - 1] + (s[i] == '1');
        }
        // for (int i = 0; i < n; i++)
        //   cout << a[i];
        //int ans=0;
        for (int i = 0; i < n; i++) {
            //previ=a[i];
            for (j = 1; j * j + j + i <= n; j++) {
                int g;
                if (i == 0)
                    g = 0;
                else
                    g = a[i - 1];
                if (a[j * j + j + i - 1] - g == j) {
                    ans++;
                }
            }
            //   cout << ans;
        }
        cout << ans << endl;

    }
    return 0;
}

Birthday Gift Again CodeChef Solution in PYTH 3

a=int(input())
for i in range(a):
    S=input()
    N=len(S)
    B=[]
    x=0
    for j in S:
        if j=="1":
            x+=1 
        B.append(x)
    ans=0
    k=1 
    while (k**2)+k<=N:
        p=(k**2)+k-1 
        x=B[p]
        j=p 
        t=0 
        if p+1==N:
            if x==k:
                ans+=1 
        else:
            while j<=N-1:
                if x==k:
                    ans+=1 
                    j+=1 
                    t+=1 
                    if j!=N:
                        x=B[j]-B[t-1] 
                else:
                    if j+abs(x-k)<=N-1:
                        j+=abs(x-k)
                        t+=abs(x-k)
                        x=B[j]-B[t-1]
                    else:
                        break
        k+=1
    print(ans)

Birthday Gift Again CodeChef Solution in C

#include <stdio.h>
#include <string.h>

int main(){
	int ts;
	scanf("%d",&ts);
	while (ts--)
	{
                char s[100010];
		scanf("%s",s);
		int Ans = 0;
		int ln = strlen(s);
		for(int i = 1; (i*i + i)<= ln;i++){
			int k = i*i+i;
			int front = 0,rear = 0;
			long long one = 0,zero = 0;
			for(int x = 0; x <ln;x++){
				if(front<k-1) {
					if(s[front++] =='1') one++;
				}
				else if(front ==k-1){
					if(s[front++] =='1') one++;
					zero  = k-one;
					if(zero == one*one) {
						Ans++;
					}
				}
				else{
					if(s[rear++] == '1') one--;
					if(s[front++]=='1') one++;
					zero  = k-one;
					if(zero == one*one) {
						Ans++;
					}
				}
			}
		}
		printf("%d\n",Ans);
	}
	return 0;
}

Birthday Gift Again CodeChef Solution in JAVA

import java.io.*;
import java.util.*;

 class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
       // sc.nextLine();
        while(n-->0){
            String s = sc.next();
            int l = s.length();
            int c = 1;
            int ans=0;
            int win = c + (c*c);
            while (win<=l){
                int cnt = count(s.substring(0,win));
                if(cnt==c){
                    ans++;
                }
                int b=-1;

                for (int i = win; i <l; i++) {
                    b++;
                    if(s.charAt(i)=='1'){
                        cnt++;
                    }
                    if(s.charAt(b)=='1')
                        cnt--;
                    if(cnt==c)
                        ans++;

                }
                c++;
                win = c + (c*c);


                

            }
            System.out.println(ans);

        }
    }
    static int count(String s){
        int res =0;
        for (int i = 0; i <s.length() ; i++) {
            if(s.charAt(i)=='1'){
                res++;
            }
        }
        return res;
    }

}

Birthday Gift Again CodeChef Solution in PYPY 3

for _ in range(int(input())):
    data = list(map(int,input()))
    n = len(data)

    # Represents count of ones in data[0:i]
    pre = [0] * (n+1)
    for i in range(n):
        pre[i+1]=pre[i]+data[i]

    total = 0
    for k in range(1,n):
        # window needs k*k ones and k zeroes
        width = k*k + k - 1
        if width>n:
            break

        for l in range(1,n+1 - width):
            r = l + width
            ones = pre[r] - pre[l-1]
#            zeroes = r-(l-1) - ones
#            print(l,r,ones,zeroes)
            if ones == k:
                total += 1


    print(total)
Birthday Gift Again CodeChef Solution Review:

In our experience, we suggest you solve this Birthday Gift Again 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 Birthday Gift Again CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Birthday Gift Again 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 *