Untitled

mail@pastecode.io avatar
unknown
java
a year ago
863 B
3
Indexable
Never
import java.util.Scanner;

public class DeleteTVChannels {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt(); // Number of TV channels
        int K = scanner.nextInt(); // Step size for counting and deleting channels
        scanner.close();

        int remainingChannel = findLastRemainingChannel(N, K);
        System.out.println(remainingChannel);
    }

    public static int findLastRemainingChannel(int N, int K) {
        int lastRemaining = 0;

        // The idea is to simulate the process of counting and deleting channels
        for (int i = 2; i <= N; i++) {
            lastRemaining = (lastRemaining + K) % i;
        }

        // Since channel numbers are 1-based, we add 1 to the result to get the correct channel number
        return lastRemaining + 1;
    }
}