Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4398 |

@sanjay05 | 3762 |

@mantu_singh | 3654 |

@gfgaccount | 3601 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 899 |

@akhileshkumar562002 | 862 |

@kanewilliamson123 | 854 |

@pradeeppatidar1999 | 852 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@smitabose7826 | 784 |

@wallflowe101 | 778 |

Complete Leaderboard |

Given an integer **N** and an integer **D**, **rotate the binary representation** of the integer **N by D **digits to the **left **as well as **right **and print the **results **in **decimal values** after each of the rotation.

**Note**: Integer N is stored using **16 bits**. i.e. 12 will be stored as 0000.....001100.

**Example 1:**

**Input:**
N = 28, D = 2
**Output:
**112
7
**Explanation**: 28 in Binary is:
000...011100
Rotating left by 2 positions, it becomes
00...1110000 = 112 (in decimal).
Rotating right by 2 positions, it becomes
000...000111 = 7 (in decimal).

â€‹**Example 2:**

**Input**:
N = 29, D = 2
**Output:**
116
16391
**Explanation**: 29 in Binary is:
000...011101
Rotating left by 2 positions, it becomes
00...1110100 = 116 (in decimal).
Rotating right by 2 positions, it becomes
010...000111 = 16391 (in decimal).

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **rotate() **which takes the integer N and integer D as inputs and returns an array of size 2 where arr[0] = Decimal value after left rotation and arr[1] = Decimal value after right rotation.

**Expected Time Complexity: **O(1).

**Expected Auxiliary Space: **O(1).

**Constraints:**

1 <= N < 2^{16}

1 <= D <= 10^{5}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Rotate Bits

...