 Open in App
Not now

# Parity of final value after repeated circular removal of Kth Integer

• Last Updated : 26 Sep, 2022

Given an integer N, denoting the size of a circle where first N integers are placed in clockwise order such that j and (j+1) are adjacent, and 1 and N are also adjacent. Given an integer K (K < N), the task is to find if the last remaining value is odd or even when in each turn the Kth element from the start (i.e., 1) is removed and it is performed till only one element remains,

Examples:

Input: N = 5, K = 1
Output: Odd
Explanation: Here K = 1 it means first position element is removed from 5 integer circular array i.e. [ 1, 2, 3, 4, 5]→[2, 3, 4, 5] and repeat this step until only one integer remains i.e.[ 2, 3, 4, 5]→[ 3, 4, 5]→[ 4, 5]→. Hence last integer is odd.

Input: N = 3, K = 3
Output: Even

Approach: The problem can be solved based on the following observation:

Observations:

• If K = 1: All numbers from 1 to N-1 are deleted and only N remains
• If K = 2: All numbers from 2 to N are deleted and only 1 remains.
• If K > 2: All numbers from K to N are deleted. Remaining numbers are from 1 to K-1. So in each turn  when elements are deleted it follows pattern like 1, 3, 5, . . . because the size of the list decreases by 1 after each iteration and total numbers till the next odd number also decreases by 1. So all the odd elements get deleted first and then the even values. So the last remaining value is always an even number.

Follow the steps mentioned below to implement the observation:

• Check the value of K and N.
• Based on their values, decide which of the above cases is applicable.
• Return the parity of the element thus obtained.

Below is the implementation of the above approach:

## C++

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;`   `// Function to find the parity of the last element` `int` `parity(``int` `N, ``int` `K)` `{` `    ``if` `((K == 1 && (N % 2 != 0)) || K == 2)` `        ``return` `1;` `    ``return` `0;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `N = 5, K = 1;`   `    ``// Function call` `    ``if` `(parity(N, K))` `        ``cout << ``"Odd"``;` `    ``else` `        ``cout << ``"Even"``;` `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach`   `import` `java.util.*;`   `class` `GFG {`   `    ``// Function to find the parity` `    ``// of the last element` `    ``public` `static` `int` `parity(``int` `N, ``int` `K)` `    ``{` `        ``if` `(K == ``2` `|| (N % ``2` `!= ``0` `&& K == ``1``))` `            ``return` `1``;` `        ``return` `0``;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``5``, K = ``1``;`   `        ``// Function call` `        ``if` `(parity(N, K) == ``1``)` `            ``System.out.println(``"Odd"``);` `        ``else` `            ``System.out.println(``"Even"``);` `    ``}` `}`

## Python

 `# Python code to implement the approach`   `# Function to find the parity of the last element` `def` `parity(N, K):` `    ``if` `(K ``=``=` `1` `and` `(N ``%` `2` `!``=` `0``)) ``or` `K ``=``=` `2``:` `        ``return` `1` `    ``return` `0`     `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``N ``=` `5` `    ``K ``=` `1`   `    ``# Function call` `    ``if` `parity(N, K) ``=``=` `1``:` `        ``print``(``"Odd"``)` `    ``else``:` `        ``print``(``"Even"``)`

## C#

 `// C# code to implement the approach` `using` `System;`   `class` `GFG {`   `    ``// Function to find the parity` `    ``// of the last element` `    ``public` `static` `int` `parity(``int` `N, ``int` `K)` `    ``{` `        ``if` `(K == 2 || (N % 2 != 0 && K == 1))` `            ``return` `1;` `        ``return` `0;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``int` `N = 5, K = 1;`   `        ``// Function call` `        ``if` `(parity(N, K) == 1)` `            ``Console.WriteLine(``"Odd"``);` `        ``else` `            ``Console.WriteLine(``"Even"``);` `    ``}` `}`   `// This code is contributed by AnkThon`

## Javascript

 ``

## PHP

 ``

Output

`Odd`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles