Mystical Numbers CodeChef Solution

Problem -Mystical Numbers 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.

Mystical Numbers CodeChef Solution in C++17

#include <bits/stdc++.h>
#define ll long long int
#define fastio ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;
#define rep(i,a,b) for(i=a;i<=b;i++)
#define maxHeap priority_queue<ll>
#define minHeap priority_queue<ll,vector<ll>,greater<ll>>
#define all(x) (x).begin(),(x).end()
const ll MOD=1e9+7;
const double PI=3.141592653589793238;
const ll INF=1e18;
ll highestset(ll x)
{
    ll ans=0,i;
    for(i=31;i>=0;i--)
    {
        if((x&(1<<i)))
            return i;
    }
    return -1;
}
void solve()
{
    ll n,i,q,l,r,x,j;
    cin>>n;
    ll a[n+1];
    ll prefix[n+1][32]={0};
    vector<ll> nonzero(n+1,0);
    rep(i,0,n)
    {
        rep(j,0,31)
            prefix[i][j]=0;
    }
    rep(i,1,n)
    {
        cin>>a[i];
        nonzero[i]+=nonzero[i-1];
        if(a[i]!=0)
            nonzero[i]++;
        for(j=31;j>=0;j--)
            prefix[i][j]+=prefix[i-1][j];
        for(j=31;j>=0;j--)
        {
            if((a[i]&(1<<j)))
            {
                prefix[i][j]++;
                break;
            }
        }
    }
    cin>>q;
    while(q--)
    {
        cin>>l>>r>>x;
        ll nums=r-l+1;
        if(x==0)
            cout<<nonzero[r]-nonzero[l-1]<<'\n';
        else
        {
            ll temp=prefix[r][highestset(x)]-prefix[l-1][highestset(x)];
            // cout<<highestset(x)<<' '<<mp[r][highestset(x)]<<" "<<mp[l-1][highestset(x)]<<'\n';
            cout<<nums-temp<<"\n";
        }
    }
}
int main() {
    // fastio
    ll t;
    cin>>t;
    while(t--)
    {
        solve();
        // cout<<"\n";
    }
	return 0;
}

Mystical Numbers CodeChef Solution in C++14

#include <iostream>
#include<cmath>
using ll=long long;
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	
	while(t--)
	{
	    ll n;
	    cin>>n;
	    
	    ll A[n];
	    int v[32][n+1];
	    
	    for(ll i=0;i<n;i++)
	    cin>>A[i];
	    
	    for(int i=0;i<32;i++)
	    for(int j=0;j<=n;j++)
	    v[i][j]=0;
	    
	    for(int i=0;i<n;i++)
        {
            if(A[i]>0)
            {int x=log2(A[i])+1;
            v[x][i+1]=1;}
            else v[0][i+1]=1;
        }	    
	 for(int i=1;i<=n;i++)
	 {
	     for(int j=0;j<32;j++)
	     {
	         v[j][i]+=v[j][i-1];
	     }
	 }
	    ll Q;
	    cin>>Q;
	    
	    while(Q--)
	    {
	        ll L,R,X;
	        cin>>L>>R>>X;
	        int msb=0;
            if(X>0)
	        msb=log2(X)+1;
	        
	         int total=R-L+1;
	         int added=v[msb][R]-v[msb][L-1];
	            
	        cout<<total-added<<endl;

	    }
	    
	}
	return 0;
}

Mystical Numbers CodeChef Solution in PYTH 3

# cook your dish here
import math
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    cd = {}
    for i in range(32): cd[i]=0
    counts = [cd]
    for j in range(n):
        newc = counts[j].copy()
        if a[j]!=0: digs = int(math.log2(a[j]))+1
        #print(a[j],digs)
        if a[j]==0: digs = 0
        newc[digs]+=1
        counts.append(newc)
    #print(counts)
    q = int(input())
    for _ in range(q):
        (l,r,x) = map(int,input().split())
        if x!=0:xd = int(math.log2(x))+1
        if x==0:xd = 0
        print((r-l+1)-(counts[r][xd]-counts[l-1][xd]))

Mystical Numbers CodeChef Solution in C

#include <stdio.h>
#include<math.h>

int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int N;
        scanf("%d",&N);
        int ar[33][N+1];
        for(int i=0;i<33;i++)
         {
             for(int j=0;j<=N;j++)
             {
                 ar[i][j]=0;
             }
         }
         int t;
         for(int index=1;index<=N;index++)
         {
             scanf("%d",&t);
             for(int index1=0;index1<33;index1++)
             {
                 ar[index1][index]=ar[index1][index-1];
             }
             int n=32;
             if(t!=0)n=(int)(log(t)/log(2));
             ar[n][index]++;
             
         }
         int Q;
         scanf("%d",&Q);
         while(Q--)
         {
             int L,R,X,count,result,power=32;
             scanf("%d %d %d",&L,&R,&X);
             if(X!=0)power=(int)(log(X)/log(2));
             count=ar[power][R]-ar[power][L-1];
             result=(R-L+1)-count;
             printf("%d\n",result);
             
         }
         printf("\n");
    }

	return 0;
}

Mystical Numbers 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
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int tc = Integer.parseInt(br.readLine());
        while(tc-->0){
            int n =Integer.parseInt(br.readLine());
            // int[] arr = new int[n];
            StringTokenizer st = new StringTokenizer(br.readLine());
            int dp[][] = new int[n+1][33];
            
            for(int i=1;i<=n;i++){
                int temp = Integer.parseInt(st.nextToken());
                for(int j=0;j<33;j++){
                    dp[i][j] = dp[i-1][j];
                }
                int pwr = 32;
                if(temp!=0) pwr = (int)(Math.log(temp)/Math.log(2));
                dp[i][pwr] ++;
                
            }
            int q = Integer.parseInt(br.readLine());
            for(int i =0;i<q;i++){
                st = new StringTokenizer(br.readLine());
                int si = Integer.parseInt(st.nextToken());
                int ei = Integer.parseInt(st.nextToken());
                int x = Integer.parseInt(st.nextToken());
                int cnt =0;
                int pwr = 32;
                if(x!=0)pwr = (int)(Math.log(x)/Math.log(2));
                cnt = dp[ei][pwr]-dp[si-1][pwr];
                
                
                int ans =(ei-si+1)-cnt;
                
                System.out.println(ans);
            }
        }
  }
}

Mystical Numbers CodeChef Solution in PYPY 3

import math 
def highest_bit(n):
    return int(math.log(n,2))
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    hset_bit=[[0]*(33) for i in range(n+1)]
    notz=[0]*(n+1)
    for i,num in enumerate(l):
        # zeroes[i+1]=(num==0)+zeroes[i]
        if num!=0:
            f=highest_bit(num)
            hset_bit[i+1][f]+=1
            notz[i+1]=1+notz[i]
        else:notz[i+1]=notz[i]
        for j in range(33):hset_bit[i+1][j]+=hset_bit[i][j]
    # for i in hset_bit:print(i)
    for q in range(int(input())):
        l,r,x=map(int,input().split())
        if x==0:
            print(notz[r]-notz[l-1])
            continue
        hh=highest_bit(x)
        print((r-l+1)-(hset_bit[r][hh]-hset_bit[l-1][hh]))
    # print(notz)

Mystical Numbers CodeChef Solution in PYTH

def hi(n):
	if n == 0:
		r = 0
	else:
		r = 1
		v = 2
		while n >= v:
			r += 1
			v = v*2
		# endwhile
	# endif
	return r
# end fun
t = int(raw_input())
for i in range(t):
	N = int(raw_input())
	st = raw_input().split()
	A = []
	for x in st:
		A.append(int(x))
	# endfor x
	B = [[0 for x in range(32)] for y in range(N+1)]
	for p in range(1,N+1):
		B[p] = list(B[p-1])
		n = A[p-1]
		r = hi(n)
		B[p][r] += 1
	# endfor p
	Q = int(raw_input())
	for k in range(Q):
		st = raw_input().split()
		L = int(st[0])
		R = int(st[1])
		X = int(st[2])
		v = hi(X)
		n = B[R][v]-B[L-1][v]
		r = R+1-L-n
		print r
	# endfor k
# endfor i

Mystical Numbers CodeChef Solution in C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using System.Numerics;

public class Test
{
	public static void Main()
	{
	    int A = GetInt();
	    while(A-- != 0){
	        int N = GetInt();
            int[,] a = new int[33,N+1];
            for(int i=0;i<33;i++)
             {
                 for(int j=0;j<=N;j++)
                 {
                     a[i,j]=0;
                }
            }
         int[] t = GetIntArray();;
         for(int k=1;k<=N;k++)
         {
             for(int l=0;l<33;l++)
             {
                 a[l,k]=a[l,k-1];
             }
             int n=32;
             if(t[k-1]!=0) n=(int)(Math.Log(t[k-1])/Math.Log(2));
             a[n,k]++;
         }
         int Q = GetInt();
         while(Q-- != 0)
         {
             int[] b = GetIntArray();
             int L = b[0],R = b[1],X = b[2],count,result,power=32;
             if(X!=0)power=(int)(Math.Log(X)/Math.Log(2));
             count=a[power,R]-a[power,L-1];
             result=(R-L+1)-count;
             Console.WriteLine(result);
         }
	    }
	    
		// your code goes here
	}
	
	public static void Writex(string s) => (Console.WriteLine(s));
	public static int GetInt() => int.Parse(Console.ReadLine());
    public static long GetLong() => long.Parse(Console.ReadLine());
    public static string GetString() => (Console.ReadLine());

    public static string[] GetStringArray() => Console.ReadLine().Trim().Split(' ');
    public static int[] GetIntArray() => Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
    public static long[] GetLongArray() =>  Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray();
}

Mystical Numbers CodeChef Solution in GO

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		m := readNum(reader)
		Q := make([][]int, m)
		for i := 0; i < m; i++ {
			Q[i] = readNNums(reader, 3)
		}
		res := solve(A, Q)
		for _, cur := range res {
			buf.WriteString(fmt.Sprintf("%d\n", cur))
		}
	}
	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

const H = 32

func solve(A []int, Q [][]int) []int {
	n := len(A)

	cnt := make([][]int, n)
	for i := 0; i < n; i++ {
		cnt[i] = make([]int, H+1)
		if i > 0 {
			copy(cnt[i], cnt[i-1])
		}
		h := H
		for h > 0 && (A[i]>>uint(h-1))&1 == 0 {
			h--
		}
		cnt[i][h]++
	}

	ans := make([]int, len(Q))
	for i, cur := range Q {
		l, r, x := cur[0], cur[1], cur[2]
		l--
		r--

		b := H
		for b > 0 && (x>>uint(b-1))&1 == 0 {
			b--
		}

		a := cnt[r][b]
		if l > 0 {
			a -= cnt[l-1][b]
		}
		ans[i] = r - l + 1 - a
	}

	return ans
}
Mystical Numbers CodeChef Solution Review:

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

Find on CodeChef

Conclusion:

I hope this Mystical Numbers 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 *