Ishaan’s position for internship selection
Last Updated :
09 Sep, 2023
Problem Statement :-
- Ishaan wants to intern at GeeksForGeeks but for that, he has to go through a test. There are n candidates applying for the internship including Ishaan and only one is to be selected. Since he wants to qualify he asks you to help him. The test is as follows.
The candidates are asked to stand in a line at positions 1 to n and given a number k. Now, every kth candidate remains and the rest are eliminated. This is repeated until the number of candidates is less than k.
Out of the remaining candidates, the one standing at the smallest position is selected. You need to tell Ishaan at the position he must stand to get selected.
Examples:
Input: n = 30, k = 3
Output: 27
Explanation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
3 6 9 12 15 18 21 24 27 30
9 18 27
27
Input: n = 18, k = 3
Output: 9
Explanation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 183 6 9 12 15 189 18 (less than k)9
Approach: To solve the problem follow the below idea:
Initialize i with 1 and iterate it i*k times and when i*k becomes greater than n means you have elements less than k, immediately break the loop and return i.
Below is the implementation of the above approach:
C++14
#include <iostream>
using namespace std;
int getCandidate( int n, int k)
{
int i = 1;
for (i = 1; i< n; i = i * k) {
if (i * k> n) {
break ;
}
}
return i;
}
int main()
{
int n = 30, k = 3;
cout<< "Ishan must stand at position number -" ;
cout<< getCandidate(n, k)<< endl;
}
|
C
#include <stdio.h>
int getCandidate( int n, int k) {
int i;
for (i = 1; i < n; i = i * k) {
if (i * k > n) {
break ;
}
}
return i;
}
int main() {
int n = 30, k = 3;
printf ( "Ishan must stand at position number - %d\n" , getCandidate(n, k));
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int getCandidate( int n, int k) {
int i = 1 ;
for (i = 1 ; i < n; i = i * k) {
if (i * k > n) {
break ;
}
}
return i;
}
public static void main(String[] args) {
int n = 30 , k = 3 ;
System.out.print( "Ishan must stand at position number - " );
System.out.println(getCandidate(n, k));
}
}
|
Python3
def getCandidate(n, k):
i = 1
while i < n:
if i * k > n:
break
i = i * k
return i
n = 30
k = 3
print ( "Ishan must stand at position number - " )
print (getCandidate(n, k))
|
C#
using System;
public class GFG {
public static int GetCandidate( int n, int k)
{
int i = 1;
for (i = 1; i < n; i = i * k) {
if (i * k > n) {
break ;
}
}
return i;
}
public static void Main( string [] args)
{
int n = 30, k = 3;
Console.Write(
"Ishan must stand at position number - " );
Console.WriteLine(GetCandidate(n, k));
}
}
|
Javascript
class Main
{
static getCandidate(n, k)
{
var i = 1;
for (i = 1; i < this .n; i = i * this .k)
{
if (i * this .k > this .n)
{
break ;
}
}
return i;
}
static main(args)
{
var n = 30;
var k = 3;
console.log( "Ishan must stand at position number - " );
console.log(Main.getCandidate(n, k));
}
}
Main.main([]);
|
Output
Ishan must stand at position number -27
Time complexity: O(log n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...