Open In App

Ishaan’s position for internship selection

Problem Statement :-

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++ code for the above approach:
#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;
}
 
// Driver code
int main()
{
 
    int n = 30, k = 3;
 
    // Function call
    cout<<"Ishan must stand at position number -";
    cout<< getCandidate(n, k)<< endl;
}




#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;
}




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));
    }
}




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# Implementation:
using System;
 
public class GFG {
    // Function to get candidate
    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;
    }
 
    // driver code
    public static void Main(string[] args)
    {
        int n = 30, k = 3;
 
        Console.Write(
            "Ishan must stand at position number - ");
 
        // function call
        Console.WriteLine(GetCandidate(n, k));
    }
}
// This code is contributed by Sakshi




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)


Article Tags :
DSA