使用给定的差异数组和数组元素范围查找所有可能的原始数组
给定一个大小为N的数组arr[] ,其中包含数组相邻元素之间的差异。任务是生成所有可能的数组,使数组的所有元素都位于[L, R]范围内。
例子:
Input: arr[] = {1, -3, 4}, L = 1, R = 6
Output: {3, 4, 1, 5}, {4, 5, 2, 6}
Explanation: These two are the only possible sequences having all the elements in given range.
Input: arr[] = {4}, L = 2, R = 5
Output: -1
Explanation: No such sequence is possible
方法:解决方案是基于递归的概念。在L 和 R的范围内使用递归并尝试所有可能的情况。如果没有找到这样的序列,则打印 -1;
下面是上述方法的实现。
C++
// C++ program to implement the approach
#include
using namespace std;
// Global variable to note
// if any sequence found or not
bool flag = false;
// Function to find a sequence
void find(int L, int R, int starting, vector& arr,
vector& ans)
{
if (starting == ans.size() - 1) {
flag = true;
for (auto elements : ans) {
cout << elements << " ";
}
cout << "\n";
return;
}
// Loop to form the sequence
for (int i = L; i <= R; ++i) {
if (starting == -1) {
ans[starting + 1] = i;
// Recursive call
find(L, R, starting + 1, arr, ans);
}
else {
// Check the previous
if (i - ans[starting] == arr[starting]) {
ans[starting + 1] = i;
// Recursive call
find(L, R, starting + 1, arr, ans);
}
}
}
}
// Driver code
int main()
{
vector arr{ 1, -3, 4 };
int L = 1;
int R = 6;
vector ans(arr.size() + 1);
find(L, R, -1, arr, ans);
if (!flag)
cout << (-1);
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program to implement the approach
import java.io.*;
class GFG {
// Global variable to note
// if any sequence found or not
static boolean flag = false;
// Function to find a sequence
static void find(int L, int R, int starting,
int arr[], int ans[])
{
if (starting == ans.length - 1) {
flag = true;
for (int elements : ans) {
System.out.print(elements
+ " ");
}
System.out.println();
return;
}
// Loop to form the sequence
for (int i = L; i <= R; ++i) {
if (starting == -1) {
ans[starting + 1] = i;
// Recursive call
find(L, R, starting + 1,
arr, ans);
}
else {
// Check the previous
if (i - ans[starting]
== arr[starting]) {
ans[starting + 1] = i;
// Recursive call
find(L, R, starting + 1,
arr, ans);
}
}
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, -3, 4 };
int L = 1;
int R = 6;
int ans[] = new int[arr.length + 1];
find(L, R, -1, arr, ans);
if (!flag)
System.out.println(-1);
}
}
Python3
# Python 3 program to implement the approach
# Global variable to note
# if any sequence found or not
flag = False
# Function to find a sequence
def find(L, R, starting, arr, ans):
if (starting == len(ans) - 1):
global flag
flag = True
for elements in ans:
print(elements, end=" ")
print()
return
# Loop to form the sequence
for i in range(L, R + 1):
if (starting == -1):
ans[starting + 1] = i
# Recursive call
find(L, R, starting + 1, arr, ans)
else:
# Check the previous
if (i - ans[starting] == arr[starting]):
ans[starting + 1] = i
# Recursive call
find(L, R, starting + 1, arr, ans)
# Driver code
if __name__ == "__main__":
arr = [1, -3, 4]
L = 1
R = 6
ans = [0] * (len(arr) + 1)
find(L, R, -1, arr, ans)
if (not flag):
print(-1)
# This code is contributed by ukasp.
C#
// C# program to implement the approach
using System;
class GFG
{
// Global variable to note
// if any sequence found or not
static bool flag = false;
// Function to find a sequence
static void find(int L, int R, int starting,
int[] arr, int[] ans)
{
if (starting == ans.Length - 1)
{
flag = true;
foreach (int elements in ans)
{
Console.Write(elements + " ");
}
Console.WriteLine();
return;
}
// Loop to form the sequence
for (int i = L; i <= R; ++i)
{
if (starting == -1)
{
ans[starting + 1] = i;
// Recursive call
find(L, R, starting + 1,
arr, ans);
}
else
{
// Check the previous
if (i - ans[starting]
== arr[starting])
{
ans[starting + 1] = i;
// Recursive call
find(L, R, starting + 1,
arr, ans);
}
}
}
}
// Driver code
public static void Main()
{
int[] arr = { 1, -3, 4 };
int L = 1;
int R = 6;
int[] ans = new int[arr.Length + 1];
find(L, R, -1, arr, ans);
if (!flag)
Console.Write(-1);
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
输出
3 4 1 5
4 5 2 6
时间复杂度: O((LR)*N)
辅助空间: O(N)