Find all possible original Arrays using given Difference Array and range of array elements
Given an array arr[] of size N that contains the differences between the adjacents elements of an array. The task is to generate all possible arrays such that all the elements of the array lie in the range [L, R].
Examples:
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
Approach: The solution is based on the concept of recursion. Use recursion in the range of L and R and try all possible cases. If no such sequence is found print -1;
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool flag = false ;
void find( int L, int R, int starting, vector< int >& arr,
vector< int >& ans)
{
if (starting == ans.size() - 1) {
flag = true ;
for ( auto elements : ans) {
cout << elements << " " ;
}
cout << "\n" ;
return ;
}
for ( int i = L; i <= R; ++i) {
if (starting == -1) {
ans[starting + 1] = i;
find(L, R, starting + 1, arr, ans);
}
else {
if (i - ans[starting] == arr[starting]) {
ans[starting + 1] = i;
find(L, R, starting + 1, arr, ans);
}
}
}
}
int main()
{
vector< int > arr{ 1, -3, 4 };
int L = 1;
int R = 6;
vector< int > ans(arr.size() + 1);
find(L, R, -1, arr, ans);
if (!flag)
cout << (-1);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean flag = false ;
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 ;
}
for ( int i = L; i <= R; ++i) {
if (starting == - 1 ) {
ans[starting + 1 ] = i;
find(L, R, starting + 1 ,
arr, ans);
}
else {
if (i - ans[starting]
== arr[starting]) {
ans[starting + 1 ] = i;
find(L, R, starting + 1 ,
arr, ans);
}
}
}
}
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
flag = False
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
for i in range (L, R + 1 ):
if (starting = = - 1 ):
ans[starting + 1 ] = i
find(L, R, starting + 1 , arr, ans)
else :
if (i - ans[starting] = = arr[starting]):
ans[starting + 1 ] = i
find(L, R, starting + 1 , arr, ans)
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 )
|
C#
using System;
class GFG
{
static bool flag = false ;
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 ;
}
for ( int i = L; i <= R; ++i)
{
if (starting == -1)
{
ans[starting + 1] = i;
find(L, R, starting + 1,
arr, ans);
}
else
{
if (i - ans[starting]
== arr[starting])
{
ans[starting + 1] = i;
find(L, R, starting + 1,
arr, ans);
}
}
}
}
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);
}
}
|
Javascript
<script>
let flag = false ;
function find(L, R, starting,
arr, ans) {
if (starting == ans.length - 1) {
flag = true ;
for (let elements of ans) {
document.write(elements
+ " " );
}
document.write( '<br>' )
return ;
}
for (let i = L; i <= R; ++i) {
if (starting == -1) {
ans[starting + 1] = i;
find(L, R, starting + 1,
arr, ans);
}
else {
if (i - ans[starting]
== arr[starting]) {
ans[starting + 1] = i;
find(L, R, starting + 1,
arr, ans);
}
}
}
}
let arr = [1, -3, 4];
let L = 1;
let R = 6;
let ans = new Array(arr.length + 1).fill(0)
find(L, R, -1, arr, ans);
if (!flag)
document.write(-1);
</script>
|
Time Complexity: O((L-R)*N)
Auxiliary Space: O(N)
Last Updated :
21 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...