Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | C ++ program for the above approach ; Print count of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countEvenOdd ( int L , int R ) { int range = R - L + 1 ; int even = ( range / 4 ) * 2 ; if ( ( L & 1 ) && ( range % 4 == 3 ) ) { even ++ ; } else if ( ! ( L & 1 ) && ( range % 4 ) ) { even ++ ; } cout << " Even ▁ = ▁ " << even << " , ▁ Odd ▁ = ▁ " << range - even ; } int main ( ) { int L = 2 , R = 7 ; countEvenOdd ( L , R ) ; return 0 ; }