Ishaan's Internship | Practice | GeeksforGeeks

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 are less than k.
Out of the remaining candidates, the one standing at smallest position is selected. You need to tell Ishaan at position he must stand to get selected.
Example 1:
Input 
n = 30
k = 3

Output 
27
Explaination 
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
Example 2:
Input 
n = 18
k = 3
Output  
9
Explaination 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
3 6 9 12 15 18
9 18 (less than k)
9
Your Task :
You don't need to read input or print anything. Your task is to Complete the function getCandidate() which takes two integer n and k as input parameters, and returns the answer.
Expected Time Complexity: O(logkn)
Expected Auxiliary Space: O(1)
Constraints 
1 <= n <= 105
2 <= k <= 10
0
dishasshah10225 days ago
int getCandidate(int n, int k){   int cnt=0;   while(n>=k){       n=n/k;       cnt++;   }   int ans=pow(k,cnt);   return ans;//complte the function here}
0
kumarashishranjan2 months ago
int getCandidate(int n, int k){   int val = k;   while(val<=n)       val*=k;   return (val/k);}
0
nitianguddu8412083 months ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;int getCandidate(int n, int k);
int main() {
   int t;   cin >> t;   while(t--){    int n, k;    cin >> n >> k;    cout << getCandidate(n, k) << endl;   }return 0;}// } Driver Code Ends
int getCandidate(int n, int k){   //complte the function here   int i=1;   int ans=1;   while(ans<=n)   {       ans=pow(k,i);       i++;   }   return pow(k,i-2);}
0
jasmeen kaur9 months ago
jasmeen kaur
DRY  RUN OF EXAMPLE NO : 1
   30>=3   1*3=3   n=10
   10>=3   3*3=9   10/3=3
   3>=3   9*3=27   n=1
int getCandidate(int n, int k){   int ans=1;   while(n>=k){       ans = ans*k;       n/=k;   }      return ans;   }
+2
Kryxzl10 months ago
Kryxzl
Correct Answer.✔️Execution Time:0.03
int getCandidate(int n, int k){    int i = 1;    for(; pow(k, i) <= n; i++);        if(pow(k, i) == n)        return n;            return pow(k, i - 1);}
0
Ashman Jagdev11 months ago
Ashman Jagdev
https://uploads.disquscdn.c...
0
SAKSHAM GARG11 months ago
SAKSHAM GARG
C++
https://uploads.disquscdn.c...
0
Nikunj Bisht1 year ago
Nikunj Bisht
java 0.8
 int getCandidate(int n, int k){
        int counter = n;
   counter-=counter%k;int i = 1;while(Math.pow(k,i) <= n){
    i++;
}return (int)Math.pow(k,i-1);        //complete the function here    }
0
Deyyala Srinivas Kumar1 year ago
Deyyala Srinivas Kumar
int getCandidate(int n, int k){    //complte the function here    int count=0;    int p=pow(k,count);    while(p<=n)    {        count++;        p=pow(k,count);    }    return pow(k,count-1);}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.