Path traversed using exactly M coins in K jumps
Last Updated :
20 Oct, 2021
Given three integers N, K and M representing the Number of boxes (aligned horizontally from 1 to N), total numbers of allowed jumps and total available coins respectively, the task is to print the path that can be traversed within [1, N] by using exactly M coins in exactly K jumps. If a jump is made from position X to position Y then abs(X – Y) coins are used. If it is not possible to use M coins in K jumps, then print -1.
Examples:
Input : N = 5, K = 4, M = 12
Output : 5 1 4 5
Explanation :
First jump: Box 1 -> Box 5. Hence, |1-5| = 4 coins used.
Second Jump: Box 5 -> Box 1 Hence, |5-1| = 4 coins used.
Third Jump: Box 1 -> Box 4 using 3 coins.
Fourth Jump: Box 4 -> Box 5 using 1 coin.
Hence, exactly 12 coins used in 4 jumps.
Input : N = 4, K = 3, M = 10
Output : -1
Approach:
We can observe that the answer will be -1 for the following two cases:
- K > N-1 or
- K * (N-1) < M.
The problem can be solved using Greedy Approach following the steps given below:
Repeat the below operation until K become zero.
- Find the minimum of N-1 and M – K – 1.
- Based on the above minimum value, move towards the left or right based on the availability reduce K.
- Repeat the above steps until K becomes 0.
Below is implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print_path( int N, int jump, int coin)
{
if (jump > coin
|| jump * (N - 1) < coin) {
cout << "-1" << endl;
}
else {
int pos = 1;
while (jump > 0) {
int tmp = min(N - 1,
coin - (jump - 1));
if (pos + tmp <= N) {
pos += tmp;
}
else {
pos -= tmp;
}
cout << pos << " " ;
coin -= tmp;
jump -= 1;
}
}
}
int main()
{
int N = 5, K = 4, M = 12;
print_path(N, K, M);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void print_path( int N, int jump,
int coin)
{
if (jump > coin || jump * (N - 1 ) < coin)
{
System.out.println( "-1" );
}
else
{
int pos = 1 ;
while (jump > 0 )
{
int tmp = Math.min(N - 1 ,
coin - (jump - 1 ));
if (pos + tmp <= N)
{
pos += tmp;
}
else
{
pos -= tmp;
}
System.out.print(pos + " " );;
coin -= tmp;
jump -= 1 ;
}
}
}
public static void main (String[] args)
{
int N = 5 , K = 4 , M = 12 ;
print_path(N, K, M);
}
}
|
Python3
def print_path(N, jump, coin):
if (jump > coin or
jump * (N - 1 ) < coin):
print ( "-1" )
else :
pos = 1 ;
while (jump > 0 ):
tmp = min (N - 1 ,
coin - (jump - 1 ));
if (pos + tmp < = N):
pos + = tmp;
else :
pos - = tmp;
print (pos, end = " " )
coin - = tmp;
jump - = 1 ;
N = 5
K = 4
M = 12
print_path(N, K, M);
|
C#
using System;
class GFG{
static void print_path( int N, int jump,
int coin)
{
if (jump > coin || jump * (N - 1) < coin)
{
Console.WriteLine( "-1" );
}
else
{
int pos = 1;
while (jump > 0)
{
int tmp = Math.Min(N - 1,
coin - (jump - 1));
if (pos + tmp <= N)
{
pos += tmp;
}
else
{
pos -= tmp;
}
Console.Write(pos + " " );
coin -= tmp;
jump -= 1;
}
}
}
public static void Main(String[] args)
{
int N = 5, K = 4, M = 12;
print_path(N, K, M);
}
}
|
Javascript
<script>
function print_path(N, jump, coin)
{
if (jump > coin || jump * (N - 1) < coin)
{
document.write( "-1" );
}
else
{
let pos = 1;
while (jump > 0)
{
let tmp = Math.min(N - 1,
coin - (jump - 1));
if (pos + tmp <= N)
{
pos += tmp;
}
else
{
pos -= tmp;
}
document.write(pos + " " );;
coin -= tmp;
jump -= 1;
}
}
}
let N = 5, K = 4, M = 12;
print_path(N, K, M);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...