This is a verified interview question from Jpmc. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Switching TV Channels - JPMC OA IIT Roorkee" covers key patterns like Other.
"### Problem There are N channels on your TV. The channels are arranged in such a manner that after the Nth channel, it again starts with the first one, and channels are numbered from 1 to N. More formally, moving from the ith channel brings you to the (i + 1)th channel for 1 ≤ i ≤ N, and moving clockwise from the Nth channel brings you to the 1st channel. The rules for switching TV channels are as follows: * Start from the 1st channel. Count the next K channels clockwise, including the channel you started at. The counting wraps around the circle and may count some channels more than once. * The last channel you counted gets deleted from the TV. Given the number of TV Channels N and an integer K. Print the channel number you are watching when only one channel is left. ### Input The Input consists of a single line: * The line contains two space-separated positive integers N and K. ### Output Print a single number X denoting the channel number you are watching at the end (when only one channel is left). ### Constraints * 1 ≤ K ≤ N ≤ 10^6 ### Example 1: Input: 5 2 Output: 3 ### Explanation: Start at channel 1. Count 2 channels clockwise, which are channels 1 and 2. Channel 2 gets deleted. The next start is channel 3. Count 2 channels clockwise, which are channels 3 and 4. Channel 4 gets deleted. The next start is channel 5. Count 2 channels clockwise, which are channels 5 and 1. Channel 1 gets deleted. The next start is channel 3. Count 2 channels clockwise, which are channels 3 and 5. Channel 5 gets deleted. Only channel 3 is left. So, we will watch channel 3."
Join thousands of developers practicing for Jpmc.