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 direction of moves in such a way 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 any one.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print steps such that
// they do not cross the boundary
void printSteps(int a[], int n, int k)
{
  
    // To store the resultant string
    string res = "";
  
    // Initially at zero-th position
    int position = 0;
    int steps = 1;
  
    // Iterate for every i-th move
    for (int i = 0; i < n; i++) {
  
        // Check for right move condition
        if (position + a[i] <= k
            && position + a[i] >= (-k)) {
            position += a[i];
            res += "Right\n";
        }
  
        // Check for left move condition
        else if (position - a[i] >= -k
                 && position - a[i] <= k) {
            position -= a[i];
            res += "Left\n";
        }
  
        // No move is possible
        else {
            cout << -1;
            return;
        }
    }
  
    // Print the steps
    cout << res;
}
  
// Driver code
int main()
{
    int a[] = { 40, 50, 60, 40 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 120;
    printSteps(a, n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to print steps such that
// they do not cross the boundary
static void printSteps(int []a, int n, int k)
{
  
    // To store the resultant string
    String res = "";
  
    // Initially at zero-th position
    int position = 0;
    //int steps = 1;
  
    // Iterate for every i-th move
    for (int i = 0; i < n; i++)
    {
  
        // Check for right move condition
        if (position + a[i] <= k
            && position + a[i] >= (-k)) 
        {
            position += a[i];
            res += "Right\n";
        }
  
        // Check for left move condition
        else if (position - a[i] >= -k
                && position - a[i] <= k) 
        {
            position -= a[i];
            res += "Left\n";
        }
  
        // No move is possible
        else
        {
            System.out.println(-1);
            return;
        }
    }
  
    // Print the steps
    System.out.println(res);
}
  
// Driver code
public static void main (String[] args)
{
  
    int []a = { 40, 50, 60, 40 };
    int n = a.length;
    int k = 120;
    printSteps(a, n, k);
}
}
  
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to print steps such that
# they do not cross the boundary
def printSteps(a, n, k):
  
    # To store the resultant string
    res = ""
  
    # Initially at zero-th position
    position = 0
    steps = 1
  
    # Iterate for every i-th move
    for i in range(n):
  
        # Check for right move condition
        if (position + a[i] <= k and 
            position + a[i] >= -k):
            position += a[i]
            res += "Right\n"
  
        # Check for left move condition
        elif (position-a[i] >= -k and 
              position-a[i] <= k):
            position -= a[i]
            res += "Left\n"
  
        # No move is possible
        else:
            print(-1)
            return
    print(res)
  
# Driver code
a = [40, 50, 60, 40]
n = len(a)
k = 120
printSteps(a, n, k)
  
# This code is contributed by Shrikant13

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to print steps such that
// they do not cross the boundary
static void printSteps(int []a, int n, int k)
{
  
    // To store the resultant string
    String res = "";
  
    // Initially at zero-th position
    int position = 0;
    //int steps = 1;
  
    // Iterate for every i-th move
    for (int i = 0; i < n; i++)
    {
  
        // Check for right move condition
        if (position + a[i] <= k
            && position + a[i] >= (-k)) 
        {
            position += a[i];
            res += "Right\n";
        }
  
        // Check for left move condition
        else if (position - a[i] >= -k
                && position - a[i] <= k) 
        {
            position -= a[i];
            res += "Left\n";
        }
  
        // No move is possible
        else 
        {
            Console.WriteLine(-1);
            return;
        }
    }
  
    // Print the steps
    Console.Write(res);
}
  
// Driver code
static void Main()
{
    int []a = { 40, 50, 60, 40 };
    int n = a.Length;
    int k = 120;
    printSteps(a, n, k);
}
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to print steps such that
// they do not cross the boundary
function printSteps($a, $n, $k)
{
  
    // To store the resultant string
    $res = "";
  
    // Initially at zero-th position
    $position = 0;
    $steps = 1;
  
    // Iterate for every i-th move
    for ($i = 0; $i < $n; $i++) 
    {
  
        // Check for right move condition
        if ($position + $a[$i] <= $k
            && $position + $a[$i] >= (-$k))
            {
            $position += $a[$i];
            $res .= "Right\n";
        }
  
        // Check for left move condition
        else if ($position - $a[$i] >= -$k && 
                 $position - $a[$i] <= $k
        {
            $position -= $a[$i];
            $res .= "Left\n";
        }
  
        // No move is possible
        else 
        {
            echo -1;
            return;
        }
    }
  
    // Print the steps
    echo $res;
}
  
// Driver code
$a = array( 40, 50, 60, 40 );
$n = count($a);
$k = 120;
printSteps($a, $n, $k);
  
// This code is contributed by mits
?>

chevron_right


Output:

Right
Right
Left
Right


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.



Improved By : shrikanth13, Mithun Kumar