Open In App

Parity of final value after repeated circular removal of Kth Integer

Improve
Improve
Like Article
Like
Save
Share
Report

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]→[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 <bits/stdc++.h>
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




<script>
    // JavaScript code to implement the approach
 
    // Function to find the parity of the last element
    const parity = (N, K) => {
        if ((K == 1 && (N % 2 != 0)) || K == 2)
            return 1;
        return 0;
    }
 
    // Driver code
    let N = 5, K = 1;
 
    // Function call
    if (parity(N, K))
        document.write("Odd");
    else
        document.write("Even");
 
    // This code is contributed by rakeshsahni
 
</script>


PHP




<?php
  // Function to find the parity of the last element
  function parity($N, $K)
  {
    if (($K == 1 && ($N % 2 != 0)) || $K == 2)
      return 1;
    return 0;
  }
 
  // Driver code
  $N = 5;
  $K = 1;
 
  // Function call
  if (parity($N, $K)!=0)
    echo "Odd";
  else
    echo "Even";
 
// This code is contributed by rohit768.
?>


Output

Odd

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



Last Updated : 26 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads