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++
// 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; } |
Java
// 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 |
Python3
# 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 = 120printSteps(a, n, k) # This code is contributed by Shrikant13 |
C#
// 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 |
PHP
<?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 ?> |
Right Right Left Right
Recommended Posts:
- Count possible moves in the given direction in a grid
- Print a given matrix in spiral form using direction tracking method
- Swap Alternate Boundary Pairs
- Find the direction from given string
- Find if there exists a direction for ranges such that no two range intersect
- Number of comparisons in each direction for m queries in linear search
- Count number of 1s in the array after N moves
- Collect maximum points in an array with k moves
- Minimum moves to reach from i to j in a cyclic string
- Array element moved by k using single moves
- How to evenly put N objects into N places with adjacent moves
- Find the index of the left pointer after possible moves in the array
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Total position where king can reach on a chessboard in exactly M moves
- Minimum number of moves to make all elements equal
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.


