Check whether bits are in alternate pattern in the given range | Set-2
Given a non-negative number
and two values
and
. The problem is to check whether or not N has an alternate pattern in its binary representation in the range L to R.
Here, alternate pattern means that the set and unset bits occur in alternate order. The bits are numbered from right to left, i.e., the least significant bit is considered to be at first position.
Examples:
Input : N = 18, L = 1, R = 3 Output : Yes (18)10 = (10010)2 The bits in the range 1 to 3 in the binary representation of 18 are in alternate order. Input : N = 22, L = 2, R = 4 Output : No (22)10 = (10110)2 The bits in the range 2 to 4 in the binary representation of 22 are not in alternate order.
Simple Approach: It has been discussed in this post which has a worst case time complexity of O(log2n).
Efficient Approach: Following are the steps:
- Declare two variables num and left_shift.
- Check if rth bit is set or not in n. Refer this post. If set then, assign num = n and left_shift = r, Else set the (r+1)th bit in n and assign it to num. Refer this post. Also assign left_shift = r + 1.
- Perform num = num & ((1 << left_shift) – 1).
- Perform num = num >> (l – 1).
- Finally check whether bits are in alternate pattern in num or not. Refer this post.
The entire idea of the above approach is to create a number num in which bits are in same pattern as in the given range of n and then check whether bits are in alternate pattern in num or not.
C++
// C++ implementation to check whether bits are in // alternate pattern in the given range #include <bits/stdc++.h> using namespace std; // function to check whether rightmost // kth bit is set or not in 'n' bool isKthBitSet(unsigned int n, unsigned int k) { if ((n >> (k - 1)) & 1) return true; return false; } // function to set the rightmost kth bit in 'n' unsigned int setKthBit(unsigned int n, unsigned int k) { // kth bit of n is being set by this operation return ((1 << (k - 1)) | n); } // function to check if all the bits are set or not // in the binary representation of 'n' bool allBitsAreSet(unsigned int n) { // if true, then all bits are set if (((n + 1) & n) == 0) return true; // else all bits are not set return false; } // function to check if a number // has bits in alternate pattern bool bitsAreInAltOrder(unsigned int n) { unsigned int num = n ^ (n >> 1); // to check if all bits are set // in 'num' return allBitsAreSet(num); } // function to check whether bits are in // alternate pattern in the given range bool bitsAreInAltPatrnInGivenRange(unsigned int n, unsigned int l, unsigned int r) { unsigned int num, left_shift; // preparing a number 'num' and 'left_shift' // which can be further used for the check // of alternate pattern in the given range if (isKthBitSet(n, r)) { num = n; left_shift = r; } else { num = setKthBit(n, (r + 1)); left_shift = r + 1; } // unset all the bits which are left to the // rth bit of (r+1)th bit num = num & ((1 << left_shift) - 1); // right shift 'num' by (l-1) bits num = num >> (l - 1); return bitsAreInAltOrder(num); } // Driver program to test above int main() { unsigned int n = 18; unsigned int l = 1, r = 3; if (bitsAreInAltPatrnInGivenRange(n, l, r)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation to check whether bits are in // alternate pattern in the given range class GFG { // function to check whether rightmost // kth bit is set or not in 'n' static boolean isKthBitSet(int n, int k) { if ((n >> (k - 1)) == 1) return true; return false; } // function to set the rightmost kth bit in 'n' static int setKthBit(int n, int k) { // kth bit of n is being set by this operation return ((1 << (k - 1)) | n); } // function to check if all the bits are set or not // in the binary representation of 'n' static boolean allBitsAreSet(int n) { // if true, then all bits are set if (((n + 1) & n) == 0) return true; // else all bits are not set return false; } // function to check if a number // has bits in alternate pattern static boolean bitsAreInAltOrder(int n) { int num = n ^ (n >> 1); // to check if all bits are set // in 'num' return allBitsAreSet(num); } // function to check whether bits are in // alternate pattern in the given range static boolean bitsAreInAltPatrnInGivenRange(int n, int l, int r) { int num, left_shift; // preparing a number 'num' and 'left_shift' // which can be further used for the check // of alternate pattern in the given range if (isKthBitSet(n, r)) { num = n; left_shift = r; } else { num = setKthBit(n, (r + 1)); left_shift = r + 1; } // unset all the bits which are left to the // rth bit of (r+1)th bit num = num & ((1 << left_shift) - 1); // right shift 'num' by (l-1) bits num = num >> (l - 1); return bitsAreInAltOrder(num); } // Driver code public static void main(String[] args) { int n = 18; int l = 1, r = 3; if (bitsAreInAltPatrnInGivenRange(n, l, r)) System.out.println("Yes"); else System.out.println("No"); } } // This code has been contributed by 29AjayKumar |
Python3
# Python 3 implementation to check # whether bits are in alternate pattern # in the given range # function to check whether rightmost # kth bit is set or not in 'n' def isKthBitSet(n, k): if((n >> (k - 1)) & 1): return True return False # function to set the rightmost kth bit in 'n' def setKthBit(n, k): # kth bit of n is being set # by this operation return ((1 << (k - 1)) | n) # function to check if all the bits are set or not # in the binary representation of 'n' def allBitsAreSet(n): # if true, then all bits are set if (((n + 1) & n) == 0): return True # else all bits are not set return False # function to check if a number # has bits in alternate pattern def bitsAreInAltOrder(n): num = n ^ (n >> 1) # to check if all bits are set # in 'num' return allBitsAreSet(num) # function to check whether bits are in # alternate pattern in the given range def bitsAreInAltPatrnInGivenRange(n, l, r): # preparing a number 'num' and 'left_shift' # which can be further used for the check # of alternate pattern in the given range if (isKthBitSet(n, r)): num = n left_shift = r else: num = setKthBit(n, (r + 1)) left_shift = r + 1 # unset all the bits which are left to the # rth bit of (r+1)th bit num = num & ((1 << left_shift) - 1) # right shift 'num' by (l-1) bits num = num >> (l - 1) return bitsAreInAltOrder(num) # Driver Code if __name__ == '__main__': n = 18 l = 1 r = 3 if (bitsAreInAltPatrnInGivenRange(n, l, r)): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation to check whether bits are in // alternate pattern in the given range using System; class GFG { // function to check whether rightmost // kth bit is set or not in 'n' static bool isKthBitSet(int n, int k) { if ((n >> (k - 1)) == 1) return true; return false; } // function to set the rightmost kth bit in 'n' static int setKthBit(int n, int k) { // kth bit of n is being set by this operation return ((1 << (k - 1)) | n); } // function to check if all the bits are set or not // in the binary representation of 'n' static bool allBitsAreSet(int n) { // if true, then all bits are set if (((n + 1) & n) == 0) return true; // else all bits are not set return false; } // function to check if a number // has bits in alternate pattern static bool bitsAreInAltOrder(int n) { int num = n ^ (n >> 1); // to check if all bits are set // in 'num' return allBitsAreSet(num); } // function to check whether bits are in // alternate pattern in the given range static bool bitsAreInAltPatrnInGivenRange(int n, int l, int r) { int num, left_shift; // preparing a number 'num' and 'left_shift' // which can be further used for the check // of alternate pattern in the given range if (isKthBitSet(n, r)) { num = n; left_shift = r; } else { num = setKthBit(n, (r + 1)); left_shift = r + 1; } // unset all the bits which are left to the // rth bit of (r+1)th bit num = num & ((1 << left_shift) - 1); // right shift 'num' by (l-1) bits num = num >> (l - 1); return bitsAreInAltOrder(num); } // Driver code public static void Main() { int n = 18; int l = 1, r = 3; if (bitsAreInAltPatrnInGivenRange(n, l, r)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } /* This code contributed by PrinciRaj1992 */ |
Yes
Time Complexity: O(1).
Recommended Posts:
- Check whether bits are in alternate pattern in the given range
- Print numbers in the range 1 to n having bits in alternate pattern
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether all the bits are set in the given range
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check if bits in range L to R of two numbers are complement of each other or not
- Alternate bits of two numbers to create a new number
- Check if bits of a number has count of consecutive set bits in increasing order
- Count set bits in a range
- Set all the bits in given range of a number
- Set bits in N equals to M in the given range.
- Copy set bits in a range
- Unset bits in the given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



