Open In App

Remove the Kth element from a given range in circular order

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer K and a range [L, R] from that the elements are placed on a circle in clockwise order such that ith and (i+1)th elements are adjacent for all L ≤ i ≤ R, and L and R are also adjacent. In each move, find the integer that is at the Kth place from the smallest one in clockwise order and remove it from the range. The task is to remove integers from the range until only one integer remains and print whether it’s Even or Odd.

Examples:

Input: L = 1, R = 10, K = 4
Output: Even
Explanation: Range = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
After first step, 4 will be deleted then 5, Then 6, 7, 8, 9 and 10
Now left numbers are 1, 2, 3 so from 1 -> 2 -> 3 -> 1, 1 will deleted.
Now left numbers are 2, 3so from 2 -> 3 -> 2 -> 3, 3 will deleted.
Now, 2 is left whose parity is Even.

Input: L = 2, R = 5, K = 3
Output: Odd

Approach: To solve the problem, follow the below idea:

The problem can be solved using the following observations:

Case 1: K = 1

  • In each move, we remove the smallest element in clockwise order, which is the element at position L.
  • Since R will be the last number, so if R is odd, the answer will be odd else the answer will be even.

Case 2: K = 2

  • In each move, we remove the second smallest element in clockwise order, which is the element at position (L+1).
  • Since, we are always removing the (L+1)th element, the last element will be L, so if L is odd, the answer will be odd else the answer will be even.

Case 3: K > 2:

  • First, we remove all elements from position [L + K, R] from [1, N] and for remaining [L, L+K-1], it first removes element from Position L then L+2 then L+4….. so on, so if L is odd then all odd numbers will be removed and even numbers remains and last remaining integer will definitely by even, and similarly if L is even then all even numbers will be removed and odd numbers remains, and last remaining integer will definitely by odd.

Below is the implementation of the approach:

C++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find parity of last
// one remaining integer
void findEvenOrOdd(int L, int R, int K)
{
    // If K = 1
    if (K == 1)
    {
        if (R & 1)
            cout << "ODD" << endl;
        else
            cout << "EVEN" << endl;
    }
 
    // If K = 2
    else if (K == 2)
    {
        if (L & 1)
            cout << "ODD" << endl;
        else
            cout << "EVEN" << endl;
    }
 
    // If K >= 3
    else
    {
        if (L & 1)
            cout << "EVEN" << endl;
        else
            cout << "ODD" << endl;
    }
}
 
// Driver Code
int main()
{
    int L = 1, R = 10, K = 4;
    findEvenOrOdd(L, R, K);
 
    return 0;
}


Java




public class Main {
 
    // Function to find parity of the last one remaining integer
    static void findEvenOrOdd(int L, int R, int K) {
        // If K = 1
        if (K == 1) {
            if ((R & 1) == 1)
                System.out.println("ODD");
            else
                System.out.println("EVEN");
        }
 
        // If K = 2
        else if (K == 2) {
            if ((L & 1) == 1)
                System.out.println("ODD");
            else
                System.out.println("EVEN");
        }
 
        // If K >= 3
        else {
            if ((L & 1) == 1)
                System.out.println("EVEN");
            else
                System.out.println("ODD");
        }
    }
 
    // Driver Code
    public static void main(String[] args) {
        int L = 1, R = 10, K = 4;
        findEvenOrOdd(L, R, K);
    }
}
 
// This code is contributed by shivamgupta310570


Python3




# Function to find parity of last
# one remaining integer
def find_even_or_odd(L, R, K):
    # If K = 1
    if K == 1:
        if R & 1:
            print("ODD")
        else:
            print("EVEN")
 
    # If K = 2
    elif K == 2:
        if L & 1:
            print("ODD")
        else:
            print("EVEN")
 
    # If K >= 3
    else:
        if L & 1:
            print("EVEN")
        else:
            print("ODD")
 
# Driver Code
if __name__ == "__main__":
    L, R, K = 1, 10, 4
    find_even_or_odd(L, R, K)
     
# This code is contributed by akshitaguprzj3


C#




// C# code to implement the above approach
using System;
 
class GFG
{
    // Function to find parity of last
    // one remaining integer
    static void FindEvenOrOdd(int L, int R, int K)
    {
        // If K = 1
        if (K == 1)
        {
            if ((R & 1) != 0)
                Console.WriteLine("ODD");
            else
                Console.WriteLine("EVEN");
        }
 
        // If K = 2
        else if (K == 2)
        {
            if ((L & 1) != 0)
                Console.WriteLine("ODD");
            else
                Console.WriteLine("EVEN");
        }
 
        // If K >= 3
        else
        {
            if ((L & 1) != 0)
                Console.WriteLine("EVEN");
            else
                Console.WriteLine("ODD");
        }
    }
 
    // Driver Code
    static void Main()
    {
        int L = 1, R = 10, K = 4;
        FindEvenOrOdd(L, R, K);
    }
}


Javascript




// Function to find parity of last
// one remaining integer
function findEvenOrOdd(L, R, K) {
    // If K = 1
    if (K === 1) {
        if (R & 1)
            console.log("ODD");
        else
            console.log("EVEN");
    }
 
    // If K = 2
    else if (K === 2) {
        if (L & 1)
            console.log("ODD");
        else
            console.log("EVEN");
    }
 
    // If K >= 3
    else {
        if (L & 1)
            console.log("EVEN");
        else
            console.log("ODD");
    }
}
 
// Driver Code
(function main() {
    const L = 1, R = 10, K = 4;
    findEvenOrOdd(L, R, K);
})();


Output

EVEN

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads