Print direction of moves such that you stay within the [-k, +k] boundary
Given an array, arr[] of N positive integers and an integer K. It is given that you start at position 0 and you can move to left or right by a[i] positions starting from arr[0]. The task is to print the direction of moves so that you can complete N steps without exceeding the [-K, +K] boundary by moving right or left. In case you cannot perform steps, print -1. In case of multiple answers, print anyone.
Examples:
Input: arr[] = {40, 50, 60, 40}, K = 120
Output:
Right
Right
Left
Right
Explanation :
Since N = 4 (Number of elements in the array),
we need to make 4 moves from arr[0] such that
value does not go out of [-120, 120]
Move 1: Position = 0 + 40 = 40
Move 2: Position = 40 + 50 = 90
Move 3: Position = 90 – 60 = 30
Move 4: Position = 30 + 50 = 80
Input: arr[] = {40, 50, 60, 40}, K = 20
Output: -1
Approach: The following steps can be followed to solve the above problem:
- Initialize position to 0 in the beginning.
- Start traversing all the array elements,
- If a[i] + position does not exceed the left and the right boundary, then the move will be “Right”.
- If position – a[i] does not exceed the left and the right boundary, then the move will be “Left”.
- If at any stage both the condition fail then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printSteps( int a[], int n, int k)
{
string res = "" ;
int position = 0;
int steps = 1;
for ( int i = 0; i < n; i++) {
if (position + a[i] <= k
&& position + a[i] >= (-k)) {
position += a[i];
res += "Right\n" ;
}
else if (position - a[i] >= -k
&& position - a[i] <= k) {
position -= a[i];
res += "Left\n" ;
}
else {
cout << -1;
return ;
}
}
cout << res;
}
int main()
{
int a[] = { 40, 50, 60, 40 };
int n = sizeof (a) / sizeof (a[0]);
int k = 120;
printSteps(a, n, k);
return 0;
}
|
Java
class GFG
{
static void printSteps( int []a, int n, int k)
{
String res = "" ;
int position = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (position + a[i] <= k
&& position + a[i] >= (-k))
{
position += a[i];
res += "Right\n" ;
}
else if (position - a[i] >= -k
&& position - a[i] <= k)
{
position -= a[i];
res += "Left\n" ;
}
else
{
System.out.println(- 1 );
return ;
}
}
System.out.println(res);
}
public static void main (String[] args)
{
int []a = { 40 , 50 , 60 , 40 };
int n = a.length;
int k = 120 ;
printSteps(a, n, k);
}
}
|
Python3
def printSteps(a, n, k):
res = ""
position = 0
steps = 1
for i in range (n):
if (position + a[i] < = k and
position + a[i] > = - k):
position + = a[i]
res + = "Right\n"
elif (position - a[i] > = - k and
position - a[i] < = k):
position - = a[i]
res + = "Left\n"
else :
print ( - 1 )
return
print (res)
a = [ 40 , 50 , 60 , 40 ]
n = len (a)
k = 120
printSteps(a, n, k)
|
C#
using System;
class GFG
{
static void printSteps( int []a, int n, int k)
{
String res = "" ;
int position = 0;
for ( int i = 0; i < n; i++)
{
if (position + a[i] <= k
&& position + a[i] >= (-k))
{
position += a[i];
res += "Right\n" ;
}
else if (position - a[i] >= -k
&& position - a[i] <= k)
{
position -= a[i];
res += "Left\n" ;
}
else
{
Console.WriteLine(-1);
return ;
}
}
Console.Write(res);
}
static void Main()
{
int []a = { 40, 50, 60, 40 };
int n = a.Length;
int k = 120;
printSteps(a, n, k);
}
}
|
PHP
<?php
function printSteps( $a , $n , $k )
{
$res = "" ;
$position = 0;
$steps = 1;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $position + $a [ $i ] <= $k
&& $position + $a [ $i ] >= (- $k ))
{
$position += $a [ $i ];
$res .= "Right\n" ;
}
else if ( $position - $a [ $i ] >= - $k &&
$position - $a [ $i ] <= $k )
{
$position -= $a [ $i ];
$res .= "Left\n" ;
}
else
{
echo -1;
return ;
}
}
echo $res ;
}
$a = array ( 40, 50, 60, 40 );
$n = count ( $a );
$k = 120;
printSteps( $a , $n , $k );
?>
|
Javascript
<script>
function printSteps(a , n , k) {
var res = "" ;
var position = 0;
for (i = 0; i < n; i++) {
if (position + a[i] <= k && position + a[i] >= (-k)) {
position += a[i];
res += "Right<br/>" ;
}
else if (position - a[i] >= -k && position - a[i] <= k) {
position -= a[i];
res += "Left<br/>" ;
}
else {
document.write(-1);
return ;
}
}
document.write(res);
}
var a = [ 40, 50, 60, 40 ];
var n = a.length;
var k = 120;
printSteps(a, n, k);
</script>
|
Output:
Right
Right
Left
Right
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of elements in the array.
Auxiliary Space: O(N), as we are using extra space for the string res. Where N is the number of elements in the array.
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...