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:Â
Â
- From position X you can go to position X + 1 with a cost of P
- 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.Â
Â
- If N is odd then the only valid move that could lead us here is N-1 to N with a cost of P.
- If N is even then we calculate cost of going from N to N/2 position with both the moves and take the minimum of them.
- When N equals 0, we return our total calculated cost.
Below is the implementation of above approach:
Â
C++
// CPP implementation of above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to return minimum// cost to reach destinationint 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 programint main(){Â Â Â Â int N = 9, P = 5, Q = 1;Â
    cout << minCost(N, P, Q);Â
    return 0;} |
Java
// Java implementation of above approachÂ
class GFG{// Function to return minimum// cost to reach destinationstatic 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 programpublic 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 |
Python3
# 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 += Q             # if X+1 jump is better             else:                cost += P * temp             N //= 2    return costÂ
   # Driver program N = 9P = 5Q = 1print(minCost(N, P, Q))#this code is improved by sahilshelangia |
C#
// C# implementation of above approachÂ
class GFG{// Function to return minimum// cost to reach destinationstatic 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 Codestatic void Main(){Â Â Â Â int N = 9, P = 5, Q = 1;Â
    System.Console.WriteLine(minCost(N, P, Q));}}Â
// This code is contributed by mits |
PHP
<?php// PHP implementation of above approachÂ
// Function to return minimum// cost to reach destinationfunction 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?> |
Javascript
<script>//Javascript implementation of above approachÂ
// Function to return minimum// cost to reach destinationfunction minCost( N, P, Q){Â Â Â Â // Initialize cost to 0Â Â Â Â var cost = 0;Â
    // going backwards until we    // reach initial position    while (N > 0) {Â
        if (N & 1) {            cost += P;            N--;        }        else {            var temp =parseInt( 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 = parseInt(N / 2);        }    }Â
    // return cost    return cost;}var N = 9, P = 5, Q = 1;Â
document.write( minCost(N, P, Q));Â
//This code is contributed by SoumikMondal</script> |
13
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



