Given a point on x axis, (N,0), you are standing at origin and you can only move on x-axis. Lets say you are at (x,0), at each step you can either move one step forward ie (x+1,0) or one step backwards (x-1,0) or make a jump to double your current position (2*x,0). One step movement costs you A while a jump costs you B. If your initial position is (0,0) , find the minimum cost to reach (N,0) .
Input: N = 7, A = 1, B = 100
Explaination: Move forward to (N, 0) one
unit at each step.
Input: N = 8, A = 2, B = 4
Explaination: The optimal movement is to
1, 2, 3, 4, 8.
You do not need to read input or print anything. Your task is to complete the function optimalWalk() which takes N, A and B as input parameters and returns the minimum cost to go to (N, 0) from (0, 0).
Expected Time Complexity: O(N) Expected Auxiliary Space: O(1)