0% found this document useful (0 votes)
78 views19 pages

Reverse Bits of 43261596

Uploaded by

ticir89187
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views19 pages

Reverse Bits of 43261596

Uploaded by

ticir89187
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Check if a Number is Odd or Even

Problem Statement:
Check if a number is odd or even.

Input:
• An integer n.

Output:
• Return true if the number is odd, otherwise return false.

Constraints:
• -10^9 <= n <= 10^9

Example 1:
Input:
n = 5
Output:
true
Explanation: 5 is odd.

Example 2:
Input:
n = 4
Output:
false
Explanation: 4 is even.

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use n & 1. If the result is 1, the number is odd; otherwise, it’s even.

1
Turn Off the Rightmost Set Bit
Problem Statement:
Given a number n, turn off its rightmost set bit.

Input:
• An integer n.

Output:
• An integer representing the number after turning off its rightmost set bit.

Constraints:
• 1 <= n <= 10^9

Example 1:
Input:
n = 18
Output:
16
Explanation: Binary representation of 18 is 10010. After turning off the
rightmost set bit, it becomes 10000, which is 16.

Example 2:
Input:
n = 7
Output:
6
Explanation: Binary representation of 7 is 111. After turning off the rightmost
set bit, it becomes 110, which is 6.

1
Hints (Scroll Down for Hints):
Click to reveal hints
1. Use n & (n - 1).

2
Check if a Number is a Power of Two
Problem Statement:
Given an integer n, check if it is a power of two.

Input:
• An integer n.

Output:
• Return true if n is a power of two, otherwise return false.

Constraints:
• -2^31 <= n <= 2^31 - 1

Example 1:
Input:
n = 16
Output:
true

Example 2:
Input:
n = 18
Output:
false

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use the property n & (n - 1) == 0 for powers of two.

1
Count the Number of Set Bits
Problem Statement:
Count the number of 1s in the binary representation of a number.

Input:
• An integer n.

Output:
• An integer representing the number of set bits in n.

Constraints:
• 0 <= n <= 2^31 - 1

Example 1:
Input:
n = 5
Output:
2
Explanation: Binary representation of 5 is 101, which has 2 set bits.

Example 2:
Input:
n = 0
Output:
0

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use n & (n - 1) to clear the least significant set bit in each iteration.

1
Reverse the Bits of a Number
Problem Statement:
Reverse the bits of a given 32-bit unsigned integer.

Input:
• A 32-bit unsigned integer n.

Output:
• A 32-bit unsigned integer representing the reversed bits of n.

Constraints:
• 0 <= n <= 2^32 - 1

Example 1:
Input:
n = 43261596
Output:
964176192
Explanation: Binary representation of 43261596 is 00000010100101000001111010011100,
and its reversed binary representation is 00111001011110000010100101000000,
which is 964176192.

Example 2:
Input:
n = 4294967293
Output:
3221225471
Explanation: Binary representation of 4294967293 is 11111111111111111111111111111101,
and its reversed binary representation is 10111111111111111111111111111111,
which is 3221225471.

1
Hints (Scroll Down for Hints):
Click to reveal hints
1. Use bitwise shifts and masks to reverse the bits one by one.

2
Check if a Number is a Palindrome in Binary
Problem Statement:
Check if the binary representation of a number is a palindrome.

Input:
• An integer n.

Output:
• Return true if the binary representation of n is a palindrome, otherwise
return false.

Constraints:
• 1 <= n <= 10^9

Example 1:
Input:
n = 9
Output:
true
Explanation: Binary representation of 9 is 1001, which is a palindrome.

Example 2:
Input:
n = 10
Output:
false
Explanation: Binary representation of 10 is 1010, which is not a palindrome.

Hints (Scroll Down for Hints):


Click to reveal hints
1. Reverse the bits and compare with the original number.

1
Determine the Rightmost Set Bit
Problem Statement:
Find the position of the rightmost set bit in a number.

Input:
• An integer n.

Output:
• An integer representing the position of the rightmost set bit (1-based in-
dex).

Constraints:
• 1 <= n <= 10^9

Example 1:
Input:
n = 18
Output:
2
Explanation: Binary representation of 18 is 10010. The rightmost set bit is
at position 2.

Example 2:
Input:
n = 5
Output:
1
Explanation: Binary representation of 5 is 101. The rightmost set bit is at
position 1.

1
Hints (Scroll Down for Hints):
Click to reveal hints
1. Use n & -n to isolate the rightmost set bit.

2
Find the Missing Number in an Array
Problem Statement:
Given an array of size n containing numbers from 0 to n with one missing, find
the missing number.

Input:
• An integer array arr of size n.

Output:
• An integer representing the missing number.

Constraints:
• 1 <= n <= 10^5
• 0 <= arr[i] <= n

Example 1:
Input:
arr = [3, 0, 1]
Output:
2

Example 2:
Input:
arr = [0, 1]
Output:
2

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use XOR to cancel out all numbers except the missing one.

1
Find the Position of the Most Significant Set Bit
Problem Statement:
Find the position of the highest set bit in a number.

Input:
• An integer n.

Output:
• An integer representing the position of the most significant set bit (1-based
index).

Constraints:
• 1 <= n <= 10^9

Example 1:
Input:
n = 18
Output:
5
Explanation: Binary representation of 18 is 10010. The most significant set
bit is at position 5.

Example 2:
Input:
n = 5
Output:
3
Explanation: Binary representation of 5 is 101. The most significant set bit
is at position 3.

1
Hints (Scroll Down for Hints):
Click to reveal hints
1. Use bitwise shifts to count the position.

2
Find the Two Non-Repeating Elements
Problem Statement:
Given an array where every element appears twice except two, find the two
unique elements.

Input:
• An integer array arr of size n.

Output:
• Two integers representing the two unique elements.

Constraints:
• 2 <= n <= 10^5
• -10^9 <= arr[i] <= 10^9

Example 1:
Input:
arr = [4, 1, 2, 1, 2, 5]
Output:
[4, 5]

Example 2:
Input:
arr = [2, 3, 7, 9, 7, 3]
Output:
[2, 9]

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use XOR to find the XOR of the two unique numbers.
2. Use the rightmost set bit to separate them.

1
Find the XOR of All Numbers in a Range
Problem Statement:
Given two integers l and r, find the XOR of all numbers in the range [l, r].

Input:
• Two integers l and r.

Output:
• An integer representing the XOR of all numbers in the range.

Constraints:
• 0 <= l <= r <= 10^9

Example 1:
Input:
l = 2, r = 4
Output:
5
Explanation: 2 ^ 3 ^ 4 = 5.

Example 2:
Input:
l = 0, r = 3
Output:
0
Explanation: 0 ^ 1 ^ 2 ^ 3 = 0.

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use the XOR properties and precompute XOR from 0 to n.

1
Generate All Subsets of a Set
Problem Statement:
Given a set of size n, generate all its subsets.

Input:
• An integer array arr of size n.

Output:
• A list of all subsets of the set.

Constraints:
• 1 <= n <= 20

Example 1:
Input:
arr = [1, 2, 3]
Output:
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

Example 2:
Input:
arr = [0]
Output:
[[], [0]]

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use the binary representation of numbers from 0 to 2^n - 1 to represent
subsets.

1
Find the Only Non-Repeating Element
Problem Statement:
You are given an array of integers where every element appears twice except
for one element, which appears only once. Your task is to find and return the
element that appears only once.

Input:
1. An integer n (size of the array, where n is always odd and n >= 1).
2. An array of integers of size n, where all elements except one appear exactly
twice.

Output:
• A single integer, the element that appears only once.

Constraints:
1. 1 <= n <= 10^5
2. -10^9 <= arr[i] <= 10^9
3. The array will always have exactly one non-repeating element.

Example 1:
Input:
n = 5
arr = [4, 1, 2, 1, 2]
Output:
4

Example 2:
Input:
n = 7
arr = [2, 3, 5, 4, 5, 3, 2]
Output:
4

1
Hints (Scroll Down for Hints):
Click to reveal hints
1. Use the XOR property: x ^ x = 0 and x ^ 0 = x.
2. XOR all elements of the array. The result will be the non-repeating ele-
ment because all duplicate elements cancel out.

2
Swap Two Numbers Without Using a Temporary
Variable
Problem Statement:
Swap two integers a and b using bitwise XOR.

Input:
• Two integers a and b.

Output:
• The swapped values of a and b.

Constraints:
• -10^9 <= a, b <= 10^9

Example 1:
Input:
a = 3, b = 5
Output:
a = 5, b = 3

Example 2:
Input:
a = -2, b = 7
Output:
a = 7, b = -2

Hints (Scroll Down for Hints):


Click to reveal hints
1. Use a = a ^ b; b = a ^ b; a = a ^ b;.

You might also like