Codechefs starters 106->div 3 : B-> Playing with OR

Statement:  Playing with OR click the link and also click the link what u get after clicking on Playing with OR link.that will direct u to the problem statement.

Explanation:  
look if we have any odd number in any sub-array of length k.then it is obvious that the OR of all elements of this sub-array will also be odd.

Example: 5->101 ,7->111 look 5,7 has 1 as set bit on the
 0'th bit,so if u do OR with any other number like 4->100 or 3->011 the 0'th bit of the resultant number
 will also be 1. because  0|1 and 1|1 is 1 always so,we can eliminate 1 from the 0'th bit.So,when we do OR
 in the sub-array of length K ,then one odd number will be enough to give us odd OR value .

So,we just need to check if there is any odd number in the all sub-array of length K in array a.

Code:


#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define db double
#define p_b push_back
#define pp endl



int main() {
    ll t; cin>>t;
    while(t--){
        ll n,k,c=0,cnt=0,res=0,d=0,p; cin>>n>>k;
        ll a[n];
        ll i,f1,j;

        for(i=0;i<n;i++) {
            cin>>a[i];

         
           
        }
        for(i=0;i<=n-k;i++){
            for(j=i;j<i+k;j++){
                if(a[j]%2){
                    cnt++;
                    break;
                }
            }
        }
        
        cout<<cnt<<pp;
    }


    

}🍌


Comments

Popular Posts