Remove the Kth element from a given range in circular order
Last Updated :
15 Feb, 2024
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++
#include <bits/stdc++.h>
using namespace std;
void findEvenOrOdd( int L, int R, int K)
{
if (K == 1)
{
if (R & 1)
cout << "ODD" << endl;
else
cout << "EVEN" << endl;
}
else if (K == 2)
{
if (L & 1)
cout << "ODD" << endl;
else
cout << "EVEN" << endl;
}
else
{
if (L & 1)
cout << "EVEN" << endl;
else
cout << "ODD" << endl;
}
}
int main()
{
int L = 1, R = 10, K = 4;
findEvenOrOdd(L, R, K);
return 0;
}
|
Java
public class Main {
static void findEvenOrOdd( int L, int R, int K) {
if (K == 1 ) {
if ((R & 1 ) == 1 )
System.out.println( "ODD" );
else
System.out.println( "EVEN" );
}
else if (K == 2 ) {
if ((L & 1 ) == 1 )
System.out.println( "ODD" );
else
System.out.println( "EVEN" );
}
else {
if ((L & 1 ) == 1 )
System.out.println( "EVEN" );
else
System.out.println( "ODD" );
}
}
public static void main(String[] args) {
int L = 1 , R = 10 , K = 4 ;
findEvenOrOdd(L, R, K);
}
}
|
Python3
def find_even_or_odd(L, R, K):
if K = = 1 :
if R & 1 :
print ( "ODD" )
else :
print ( "EVEN" )
elif K = = 2 :
if L & 1 :
print ( "ODD" )
else :
print ( "EVEN" )
else :
if L & 1 :
print ( "EVEN" )
else :
print ( "ODD" )
if __name__ = = "__main__" :
L, R, K = 1 , 10 , 4
find_even_or_odd(L, R, K)
|
C#
using System;
class GFG
{
static void FindEvenOrOdd( int L, int R, int K)
{
if (K == 1)
{
if ((R & 1) != 0)
Console.WriteLine( "ODD" );
else
Console.WriteLine( "EVEN" );
}
else if (K == 2)
{
if ((L & 1) != 0)
Console.WriteLine( "ODD" );
else
Console.WriteLine( "EVEN" );
}
else
{
if ((L & 1) != 0)
Console.WriteLine( "EVEN" );
else
Console.WriteLine( "ODD" );
}
}
static void Main()
{
int L = 1, R = 10, K = 4;
FindEvenOrOdd(L, R, K);
}
}
|
Javascript
function findEvenOrOdd(L, R, K) {
if (K === 1) {
if (R & 1)
console.log( "ODD" );
else
console.log( "EVEN" );
}
else if (K === 2) {
if (L & 1)
console.log( "ODD" );
else
console.log( "EVEN" );
}
else {
if (L & 1)
console.log( "EVEN" );
else
console.log( "ODD" );
}
}
( function main() {
const L = 1, R = 10, K = 4;
findEvenOrOdd(L, R, K);
})();
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...