I am solving Help Bob problem but I am unable to find where i am going wrong.
my code
#include <bits/stdc++.h> #define ll long long #define mod 1000000007 #define endl “\n” // for fast input,output file
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); // these 3 lines for fast I/P method
cout.tie(NULL);
ll int n,k;
cin>>n>>k;
ll int arr[n];
for(ll int i=0;i<n;i++) cin>>arr[i];
vector<ll int>temp(n);
temp[0]=arr[0];
for(ll int i=1;i<n;i++) temp[i]=arr[i]^arr[i-1];
ll int cnt=0;
for(ll int i=0;i<n;i++){
if((temp[i]>>(k-1))&1) cnt++;
}
cout<<cnt;
return 0;
This is my accepted solution.Take a look and you’ll find the error in your code.
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6;
long arr[N];
int main() {
int n, k;
cin>>n>>k;
int ele = 0;
for(int i = 0; i < n; i++) {
cin>>arr[i];
}
long x = arr[0], a;
for(int i = 1; i < n; i++) {
a = arr[i];
arr[i] ^= x;
x = a;
}
// for(auto x: arr) cout<<x;
for(int i = 0; i < n; i++) {
if((arr[i] >> (k - 1)) & 1)
ele++;
}
cout<<ele;
i dont think both are different but difference is only that in first i have taken extra array and calculated original array then finding the result but the idea of both solution is same
can you guys figure out what is wrong with first solution