Minimum cost to reach a point N from 0 with two different operations allowed

Given integers N, P and Q where N denotes the destination position. The task is to move from position 0 to position N with minimum cost possible and print the calculated cost. All valid movements are:

  1. From position X you can go to position X + 1 with a cost of P
  2. Or, you can go to the position 2 * X with a cost of Q

Examples:

Input: N = 1, P = 3, Q = 4
Output: 3
Move from position 0 to 1st position with cost = 3.

Input: N = 9, P = 5, Q = 1
Output: 13
Move from position 0 to 1st position with cost = 5,
then 1st to 2nd with cost = 1,
then 2nd to 4th with cost = 1,
then 4th to 8th with cost = 1,
finally 8th to 9th with cost = 5.
Total cost = 5 + 1 + 1 + 1 + 5 = 13.

Approach: Instead of going from beginning to destination we can start moving from the destination to initial position and keep track of the cost of jumps.



Below is the implementation of above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation of above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return minimum
// cost to reach destination
int minCost(int N, int P, int Q)
{
    // Initialize cost to 0
    int cost = 0;
  
    // going backwards until we
    // reach initial position
    while (N > 0) {
  
        if (N & 1) {
            cost += P;
            N--;
        }
        else {
            int temp = N / 2;
  
            // if 2*X jump is
            // better than X+1
            if (temp * P > Q)
                cost += Q;
            // if X+1 jump is better
            else
                cost += P * temp;
  
            N /= 2;
        }
    }
  
    // return cost
    return cost;
}
  
// Driver program
int main()
{
    int N = 9, P = 5, Q = 1;
  
    cout << minCost(N, P, Q);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
  
class GFG{
// Function to return minimum
// cost to reach destination
static int minCost(int N, int P, int Q)
{
    // Initialize cost to 0
    int cost = 0;
  
    // going backwards until we
    // reach initial position
    while (N > 0) {
  
        if ((N & 1)>0) {
            cost += P;
            N--;
        }
        else {
            int temp = N / 2;
  
            // if 2*X jump is
            // better than X+1
            if (temp * P > Q)
                cost += Q;
            // if X+1 jump is better
            else
                cost += P * temp;
  
            N /= 2;
        }
    }
  
    // return cost
    return cost;
}
  
// Driver program
public static void main(String[] args)
{
    int N = 9, P = 5, Q = 1;
  
    System.out.println(minCost(N, P, Q));
}
}
// This code is contributed by mits
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of above approach 
  
  
# Function to return minimum 
# cost to reach destination
  
def minCost(N,P,Q): 
    # Initialize cost to 0 
    cost = 0 
    
    # going backwards until we 
    # reach initial position 
    while (N > 0):  
        if (N & 1): 
            cost += P
            N-=1
        else:
            temp = N // 2
    
            # if 2*X jump is 
            # better than X+1 
            if (temp * P > Q):
                cost +=
            # if X+1 jump is better 
            else:
                cost += P * temp 
            N //= 2 
    return cost
  
    
# Driver program 
N = 9
P = 5
Q = 1
print(minCost(N, P, Q))
#this code is improved by sahilshelangia 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
  
class GFG
{
// Function to return minimum
// cost to reach destination
static int minCost(int N, int P, int Q)
{
    // Initialize cost to 0
    int cost = 0;
  
    // going backwards until we
    // reach initial position
    while (N > 0) 
    {
  
        if ((N & 1) > 0) 
        {
            cost += P;
            N--;
        }
        else
        {
            int temp = N / 2;
  
            // if 2*X jump is
            // better than X+1
            if (temp * P > Q)
                cost += Q;
                  
            // if X+1 jump is better
            else
                cost += P * temp;
  
            N /= 2;
        }
    }
  
    // return cost
    return cost;
}
  
// Driver Code
static void Main()
{
    int N = 9, P = 5, Q = 1;
  
    System.Console.WriteLine(minCost(N, P, Q));
}
}
  
// This code is contributed by mits
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function to return minimum
// cost to reach destination
function minCost($N, $P, $Q)
{
    // Initialize cost to 0
    $cost = 0;
  
    // going backwards until we
    // reach initial position
    while ($N > 0)
    {
  
        if ($N & 1)
        {
            $cost += $P;
            $N--;
        }
        else
        {
            $temp = $N / 2;
  
            // if 2*X jump is
            // better than X+1
            if ($temp * $P > $Q)
                $cost += $Q;
                  
            // if X+1 jump is better
            else
                $cost += $P * $temp;
  
            $N /= 2;
        }
    }
  
    // return cost
    return $cost;
}
  
// Driver Code
$N = 9; $P = 5; $Q = 1;
  
echo minCost($N, $P, $Q);
  
// This code is contributed 
// by Akanksha Rai
?>
chevron_right

Output:
13

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :