#K7806. Remaining Element Elimination

    ID: 35002 Type: Default 1000ms 256MiB

Remaining Element Elimination

Remaining Element Elimination

This problem is a variation of the famous Josephus problem. You are given an array of n integers and a step value k. Starting from the beginning of the array, repeatedly remove the k-th element (in a circular fashion) until only one element remains. The task is to determine the last remaining element.

Mathematically, this process can be defined as follows: Let \( A = [a_1, a_2, \dots, a_n] \). Remove the element at index \( (k-1) \mod n \), then re-index the remaining elements, and repeat until only one element is left. Output that element.

inputFormat

Input is provided via standard input (stdin). The first line contains two integers \( n \) and \( k \), where \( n \) is the length of the array and \( k \) is the step index for removal. The second line contains \( n \) space-separated integers, representing the elements of the array.

outputFormat

Output the single remaining element after repeatedly removing every \( k \)-th element. The output should be printed to standard output (stdout).

## sample
7 3
1 2 3 4 5 6 7
4

</p>