Given an integer N, the task is to print the count of all the non-negative integers K less than or equal to N, such that bitwise XOR of K and K+1 equals bitwise XOR of K+2 and K+3.
Examples:
Input: N = 3
Output: 2
Explanation:
The numbers satisfying the conditions are:
- K = 0, the bitwise XOR of 0 and 1 is equal to 1 and the bitwise xor of 2 and 3 is also equal to 1.
- K = 2, the bitwise XOR of 2 and 3 is equal to 1 and the bitwise xor of 4 and 5 is also equal to 1.
Therefore, there are 2 numbers satisfying the condition.
Input: 4
Output: 3
Naive Approach: The simplest approach is to iterate over the range [0, N] and check if the current number satisfies the condition or not. If it satisfies, increment the count by 1. After checking all the numbers, print the value of the count.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by the observation that all the even numbers in the range [0, N] satisfy the given condition.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to count all the integers // less than N satisfying the given // condition int countXor( int N)
{ // Store the count of even
// numbers less than N+1
int cnt = N / 2 + 1;
// Return the count
return cnt;
} // Driver Code int main()
{ // Given Input
int N = 4;
// Function Call
cout << countXor(N);
return 0;
} |
// Java Program for the above approach import java.io.*;
class GFG
{ // Function to count all the integers
// less than N satisfying the given
// condition
static int countXor( int N)
{
// Store the count of even
// numbers less than N+1
int cnt = ( int ) N / 2 + 1 ;
// Return the count
return cnt;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 4 ;
// Function Call
System.out.println(countXor(N));
}
} // This code is contributed by Potta Lokesh |
# Python 3 program for the above approach # Function to count all the integers # less than N satisfying the given # condition def countXor(N):
# Store the count of even
# numbers less than N+1
cnt = N / / 2 + 1
# Return the count
return cnt
# Driver Code if __name__ = = '__main__' :
# Given Input
N = 4
# Function Call
print (countXor(N))
# This code is contributed by SUTENDRA_GANGWAR.
|
// C# program for the above approach using System;
class GFG{
// Function to count all the integers // less than N satisfying the given // condition static int countXor( int N)
{ // Store the count of even
// numbers less than N+1
int cnt = ( int )N / 2 + 1;
// Return the count
return cnt;
} // Driver code static void Main()
{ // Given Input
int N = 4;
// Function Call
Console.WriteLine(countXor(N));
} } // This code is contributed by abhinavjain194 |
<script> // JavaScript program for the above approach
// Function to count all the integers
// less than N satisfying the given
// condition
function countXor(N) {
// Store the count of even
// numbers less than N+1
let cnt = Math.floor(N / 2) + 1;
// Return the count
return cnt;
}
// Driver Code
// Given Input
let N = 4;
// Function Call
document.write(countXor(N));
// This code is contributed by Potta Lokesh
</script> |
3
Time Complexity: O(1)
Auxiliary Space: O(1)