给定一个由n个正整数和整数K组成的数组arr [] 。假设您从位置0开始,并且可以从arr [0]开始的a [i]位置向左或向右移动。任务是打印移动方向,这样您就可以完成N步,而无需通过向右或向左移动来超出[-K,+ K]边界。如果您无法执行步骤,请打印-1 。如果有多个答案,请打印任何一个。
例子:
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
方法:可以按照以下步骤解决上述问题:
- 首先将位置初始化为0。
- 开始遍历所有数组元素,
- 如果a [i] +位置不超过左右边界,则移动将为“右” 。
- 如果位置– a [i]不超过左右边界,则移动将为“左” 。
- 如果在任何阶段这两个条件都失败,则打印-1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 = 120
printSteps(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
= (-$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
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。