Given two integers n and d, perform both left and right circular rotations of the 16-bit binary representation of n by d positions.
Return an array containing the decimal values obtained after the left rotation and the right rotation, in the format: [leftRotation, rightRotation].
Note:
- Treat n as a 16-bit unsigned integer. For example, the binary representation of 12 is 0000000000001100.
- The rotations are circular, i.e., bits shifted out from one end are inserted back at the other end.
Examples:
Input: n = 28, d = 2
Output: [112, 7]
Explanation: The 16-bit binary representation of 28 is: 0000000000011100.
After a left circular rotation by 2 positions, it becomes: 0000000001110000, which is 112 in decimal.
After a right circular rotation by 2 positions, it becomes: 0000000000000111, which is 7 in decimal.Input: n = 29, d = 2
Output: [116, 16391]
Explanation: The 16-bit binary representation of 29 is: 0000000000011101.
After a left circular rotation by 2 positions, it becomes: 0000000001110100,
which is 116 in decimal.
After a right circular rotation by 2 positions, it becomes: 0100000000000111,
which is 16391 in decimal.Input: n = 11, d = 10
Output: [11264, 704]
Explanation: The 16-bit binary representation of 11 is: 0000000000001011.
After a left circular rotation by 10 positions, it becomes: 0010110000000000,
which is 11264 in decimal.
After a right circular rotation by 10 positions, it becomes: 0000001011000000,
which is 704 in decimal.
Table of Content
Bit Manipulation - O(1) Time and O(1) Space
For 16-bit number, rotate bits left and right using bit shifts. Left rotation moves bits left and brings overflow to right. Right rotation moves bits right and brings overflow to left. Mask with 0xFFFF to keep 16 bits.
- Reduce d modulo 16
- Left rotation = ((n << d) | (n >> (16 - d))) & 0xFFFF
- Right rotation = ((n >> d) | (n << (16 - d))) & 0xFFFF
- Return both results
#include <iostream>
#include <vector>
using namespace std;
vector<int> rotate(int n, int d) {
// Since the number is stored in 16 bits,
// rotating by 17 is same as rotating by 1, etc.
d %= 16;
// For left rotation:
// Move bits to the left and bring the overflowed bits
// back to the right side.
int left = ((n << d) | (n >> (16 - d))) & 0xFFFF;
// For right rotation:
// Move bits to the right and bring the overflowed bits
// back to the left side.
int right = ((n >> d) | (n << (16 - d))) & 0xFFFF;
// Return {left rotation, right rotation}
return {left, right};
}
int main() {
int n = 28, d = 2;
vector<int> ans = rotate(n, d);
cout << ans[0] << " " << ans[1] << endl;
return 0;
}
import java.util.ArrayList;
class GFG {
static ArrayList<Integer> rotate(int n, int d) {
// Since the number is stored in 16 bits,
// rotating by 17 is same as rotating by 1, etc.
d %= 16;
// For left rotation:
// Move bits to the left and bring the overflowed bits
// back to the right side.
int left = ((n << d) | (n >> (16 - d))) & 0xFFFF;
// For right rotation:
// Move bits to the right and bring the overflowed bits
// back to the left side.
int right = ((n >> d) | (n << (16 - d))) & 0xFFFF;
// Store {left rotation, right rotation}
ArrayList<Integer> ans = new ArrayList<>();
ans.add(left);
ans.add(right);
return ans;
}
public static void main(String[] args) {
int n = 28, d = 2;
ArrayList<Integer> ans = rotate(n, d);
System.out.println(ans.get(0) + " " + ans.get(1));
}
}
def rotate(n, d):
# Since the number is stored in 16 bits,
# rotating by 17 is same as rotating by 1, etc.
d %= 16
# For left rotation:
# Move bits to the left and bring the overflowed bits
# back to the right side.
left = ((n << d) | (n >> (16 - d))) & 0xFFFF
# For right rotation:
# Move bits to the right and bring the overflowed bits
# back to the left side.
right = ((n >> d) | (n << (16 - d))) & 0xFFFF
# Return [left rotation, right rotation]
return [left, right]
# Driver code
if __name__ == "__main__":
n, d = 28, 2
ans = rotate(n, d)
print(ans[0], ans[1])
using System; using System.Collections.Generic;
class GFG {
static List<int> rotate(int n, int d) {
// Since the number is stored in 16 bits,
// rotating by 17 is same as rotating by 1, etc.
d %= 16;
// For left rotation:
// Move bits to the left and bring the overflowed bits
// back to the right side.
int left = ((n << d) | (n >> (16 - d))) & 0xFFFF;
// For right rotation:
// Move bits to the right and bring the overflowed bits
// back to the left side.
int right = ((n >> d) | (n << (16 - d))) & 0xFFFF;
// Return {left rotation, right rotation}
return new List<int>{left, right};
}
static void Main(string[] args) {
int n = 28, d = 2;
List<int> ans = rotate(n, d);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
function rotate(n, d) {
// Since the number is stored in 16 bits,
// rotating by 17 is same as rotating by 1, etc.
d %= 16;
// For left rotation:
// Move bits to the left and bring the overflowed bits
// back to the right side.
let left = ((n << d) | (n >> (16 - d))) & 0xFFFF;
// For right rotation:
// Move bits to the right and bring the overflowed bits
// back to the left side.
let right = ((n >> d) | (n << (16 - d))) & 0xFFFF;
// Return [left rotation, right rotation]
return [left, right];
}
// Driver code
const n = 28, d = 2;
const ans = rotate(n, d);
console.log(ans[0], ans[1]);
Output
112 7
String Rotation - O(1) Time and O(1) Space
Convert the 16-bit number to binary string. Left rotation moves first d characters to end. Right rotation moves last d characters to front. Convert back to decimal.
- Reduce d modulo 16
- Convert n to 16-bit binary string
- Left rotation = binary.substr(d) + binary.substr(0, d)
- Right rotation = binary.substr(16-d) + binary.substr(0, 16-d)
- Convert both to decimal and return
#include <iostream>
#include <vector>
#include <bitset>
#include <string>
using namespace std;
vector<int> rotate(int n, int d) {
// Rotation repeats after every 16 shifts
d %= 16;
// Convert the number into a 16-bit binary string
bitset<16> bits(n);
string binary = bits.to_string();
// Left rotation:
// Take the first d characters and move them to the end
string leftBinary =
binary.substr(d) + binary.substr(0, d);
// Right rotation:
// Take the last d characters and move them to the front
string rightBinary =
binary.substr(16 - d) + binary.substr(0, 16 - d);
// Convert rotated binary strings back to decimal
int left = bitset<16>(leftBinary).to_ulong();
int right = bitset<16>(rightBinary).to_ulong();
// Return {left rotation, right rotation}
return {left, right};
}
int main() {
int n = 28, d = 2;
vector<int> ans = rotate(n, d);
cout << ans[0] << " " << ans[1] << endl;
return 0;
}
import java.util.ArrayList;
class GFG {
static ArrayList<Integer> rotate(int n, int d) {
// Rotation repeats after every 16 shifts
d %= 16;
// Convert the number into a 16-bit binary string
String binary = String.format("%16s", Integer.toBinaryString(n))
.replace(' ', '0');
// Left rotation:
// Take the first d characters and move them to the end
String leftBinary = binary.substring(d) + binary.substring(0, d);
// Right rotation:
// Take the last d characters and move them to the front
String rightBinary = binary.substring(16 - d) + binary.substring(0, 16 - d);
// Convert rotated binary strings back to decimal
int left = Integer.parseInt(leftBinary, 2);
int right = Integer.parseInt(rightBinary, 2);
// Store {left rotation, right rotation} in ArrayList
ArrayList<Integer> ans = new ArrayList<>();
ans.add(left);
ans.add(right);
return ans;
}
public static void main(String[] args) {
int n = 28, d = 2;
ArrayList<Integer> ans = rotate(n, d);
System.out.println(ans.get(0) + " " + ans.get(1));
}
}
def rotate(n, d):
# Rotation repeats after every 16 shifts
d %= 16
# Convert the number into a 16-bit binary string
binary = format(n, '016b')
# Left rotation:
# Take the first d characters and move them to the end
leftBinary = binary[d:] + binary[:d]
# Right rotation:
# Take the last d characters and move them to the front
rightBinary = binary[16 - d:] + binary[:16 - d]
# Convert rotated binary strings back to decimal
left = int(leftBinary, 2)
right = int(rightBinary, 2)
# Return [left rotation, right rotation]
return [left, right]
if __name__ == "__main__":
n, d = 28, 2
ans = rotate(n, d)
print(ans[0], ans[1])
using System;
using System.Collections.Generic;
class GFG {
static List<int> rotate(int n, int d) {
// Rotation repeats after every 16 shifts
d %= 16;
// Convert the number into a 16-bit binary string
string binary = Convert.ToString(n, 2).PadLeft(16, '0');
// Left rotation:
// Take the first d characters and move them to the end
string leftBinary = binary.Substring(d) + binary.Substring(0, d);
// Right rotation:
// Take the last d characters and move them to the front
string rightBinary = binary.Substring(16 - d) + binary.Substring(0, 16 - d);
// Convert rotated binary strings back to decimal
int left = Convert.ToInt32(leftBinary, 2);
int right = Convert.ToInt32(rightBinary, 2);
// Return {left rotation, right rotation}
return new List<int>{left, right};
}
static void Main(string[] args) {
int n = 28, d = 2;
List<int> ans = rotate(n, d);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
function rotate(n, d) {
// Rotation repeats after every 16 shifts
d %= 16;
// Convert the number into a 16-bit binary string
let binary = n.toString(2).padStart(16, '0');
// Left rotation:
// Take the first d characters and move them to the end
let leftBinary = binary.substring(d) + binary.substring(0, d);
// Right rotation:
// Take the last d characters and move them to the front
let rightBinary = binary.substring(16 - d) + binary.substring(0, 16 - d);
// Convert rotated binary strings back to decimal
let left = parseInt(leftBinary, 2);
let right = parseInt(rightBinary, 2);
// Return [left rotation, right rotation]
return [left, right];
}
// Driver code
const n = 28, d = 2;
const ans = rotate(n, d);
console.log(ans[0], ans[1]);
Output
112 7