Open In App

Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1

Given two positive integers N and K, and an integer X ( initially 1 ), the task is to find the minimum value of X that can be obtained after performing one of the following operations N numbers of times:

Examples:



Input: N = 4, K = 3, X = 1
Output: 10
Explanation: 
Following operations are performed:
Operation 1: Double the value of X from 1 to 2.
Operation 2: Double the value of X from 2 to 4.
Operation 3: Add the value K(= 3) to X modifies the value from 4 to 7.
Operation 4: Add the value K(= 3) to X modifies the value from 7 to 10.
After performing the above operations N(= 4) number of times, the value of X is 10, which is minimum among all possible combinations of operations performed.

Input: N = 7, K = 4, X = 1
Output: 24



Approach: The given problem can be solved by using the Greedy Approach. The idea is to perform one of the given operations repeatedly until performing the second operation locally maximize the value of X. Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum value
// of X after increment X by K or twice
// value of X in each of N operations
int minPossibleValue(int N, int K, int X)
{
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // If the value of X is less
        // than equal to K
        if (X <= K) {
            X = X * 2;
        }
 
        // Otherwise
        else {
            X = X + K;
        }
    }
 
    // Return the minimum value of X
    return X;
}
 
// Driver Code
int main()
{
    int N = 7, K = 4, X = 1;
    cout << minPossibleValue(N, K, X);
 
    return 0;
}




// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the minimum value
// of X after increment X by K or twice
// value of X in each of N operations
public static int minPossibleValue(int N, int K,
                                   int X)
{
     
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // If the value of X is less
        // than equal to K
        if (X <= K)
        {
            X = X * 2;
        }
 
        // Otherwise
        else
        {
            X = X + K;
        }
    }
 
    // Return the minimum value of X
    return X;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 7, K = 4, X = 1;
     
    System.out.println(minPossibleValue(N, K, X));
}
}
 
// This code is contributed by Potta Lokesh




# Python program for the above approach
 
# Function to find the minimum value
# of X after increment X by K or twice
# value of X in each of N operations
def minPossibleValue(N, K, X):
     
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
       
        # If the value of X is less
        # than equal to K
        if (X <= K):
            X = X * 2;
 
        # Otherwise
        else :
            X = X + K;
 
    # Return the minimum value of X
    return X;
 
# Driver Code
N = 7;
K = 4;
X = 1;
 
print(minPossibleValue(N, K, X));
 
# This code is contributed by _saurabh_jaiswal




// C# program for the above approach
using System;
 
class GFG {
 
    // Function to find the minimum value
    // of X after increment X by K or twice
    // value of X in each of N operations
    static int minPossibleValue(int N, int K, int X)
    {
 
        // Iterate over the range [1, N]
        for (int i = 1; i <= N; i++) {
 
            // If the value of X is less
            // than equal to K
            if (X <= K) {
                X = X * 2;
            }
 
            // Otherwise
            else {
                X = X + K;
            }
        }
 
        // Return the minimum value of X
        return X;
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 7, K = 4, X = 1;
 
        Console.WriteLine(minPossibleValue(N, K, X));
    }
}
 
// This code is contributed by subham348.




<script>
 
// JavaScript program for the above approach
 
// Function to find the minimum value
// of X after increment X by K or twice
// value of X in each of N operations
function minPossibleValue(N, K, X)
{
     
    // Iterate over the range [1, N]
    for(let i = 1; i <= N; i++)
    {
        // If the value of X is less
        // than equal to K
        if (X <= K)
        {
            X = X * 2;
        }
 
        // Otherwise
        else
        {
            X = X + K;
        }
    }
 
    // Return the minimum value of X
    return X;
}
 
// Driver Code
let N = 7, K = 4, X = 1;
 
document.write(minPossibleValue(N, K, X));
 
// This code is contributed by Potta Lokesh
 
</script>

Output: 
24

 

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


Article Tags :