Generate an Array from given Array according to given conditions
Last Updated :
24 Jul, 2023
Given an array a[] and two integers l and r, the task is to create another array b[] which satisfies the following conditions:
- l < b[i] < r
- b[i]-a[i] < b[i+1] – a[i+1], for every i less than n-1 (where n is the size of the array a).
- b[i] < b[i+1], for every i less than n-1 (where n is the size of the array a).
Note: Among all such possible arrays of b, generate the smallest lexicographical array. If no such vector is possible print -1.
Examples:
Input: a[] = {1, 2, 1, 3}, l = 1, r = 10
Output: b[] = {1, 3, 3, 6}
Explanation:
- Array b is non-decreasing and every element in array b is between the given range of l and r.
- Checking the condition 2
- for i=0, 1-1 < 3-2
- for i=1, 3-2 < 3-1
- for i=3, 3-1 < 6-3
- And b is the smallest possible lexicographical array.
Input: a[] = {1, 2, 1, 2}, l = 1, r = 10
Output: b[] = {1, 3, 3, 5}
Explanation:
- Array b is non-decreasing and every element in array b is between the given range of l and r.
- Checking the condition 2
- for i=0, 1-1 < 3-2
- for i=1, 3-2 < 3-1
- for i=3, 3-1 < 5-2
- And b is the smallest possible lexicographical array.
Approach: To solve the problem follow the below idea:
The first thing we can notice here is to make the resulting array (b[]) lexicographically smallest the first element should always be equal to l as there is no other condition being applied to the first value. Now we can iterate through the second element of the vector a. Now calculate the previous difference of elements between (b[i-1]-a[i-1]), and then add a[i]+1 to it such that it is strictly following the second condition and then just at last check whether the current element has not crossed the range of r if not so add the element to the resulting array else print -1 and return.
Steps that were to follow to implement the above approach:
- Initialize a resultant vector let’s say b[]
- Insert the first element to the resultant vector equal to l
- Now iterate through the second element of the given array and then calculate the last difference and then add a[i]+1 this will be the current element
- check that the current element has not crossed r, in that case, print -1 and return else insert the current element to the resulting array
- print the resulting the array
Below is the code to implement the above steps:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector< int > generateArray(vector< int >& a, int & l, int & r)
{
vector< int > b;
b.push_back(l);
for ( int i = 1; i < a.size(); i++) {
int lastDifference = b[i - 1] - a[i - 1];
int currElement = lastDifference + a[i] + 1;
if (currElement > r or currElement < l) {
b.push_back(-1);
return b;
}
b.push_back(currElement);
}
return b;
}
int main()
{
vector< int > a = { 1, 2, 1, 3 };
int l = 1;
int r = 10;
vector< int > b = generateArray(a, l, r);
for ( auto & it : b)
cout << it << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
class GFG {
static void arrayCreation( int a[], int l, int r)
{
ArrayList<Integer> b = new ArrayList<Integer>();
b.add(l);
for ( int i = 1 ; i < a.length; i++) {
int lastDifference = b.get(i - 1 ) - a[i - 1 ];
int currElement = lastDifference + a[i] + 1 ;
if (currElement > r || currElement < l) {
System.out.print( "-1" );
return ;
}
b.add(currElement);
}
for ( int i = 0 ; i < b.size(); i++) {
System.out.print(b.get(i) + " " );
}
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 1 , 3 };
int l = 1 ;
int r = 10 ;
arrayCreation(a, l, r);
}
}
|
Python3
def generateArray(a, l, r):
b = [l]
for i in range ( 1 , len (a)):
lastDifference = b[i - 1 ] - a[i - 1 ]
currElement = lastDifference + a[i] + 1
if currElement > r or currElement < l:
b.append( - 1 )
return b
b.append(currElement)
return b
a = [ 1 , 2 , 1 , 3 ]
l = 1
r = 10
b = generateArray(a, l, r)
for i in b:
print (i, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void ArrayCreation( int [] a, int l, int r)
{
List< int > b = new List< int >();
b.Add(l);
for ( int i = 1; i < a.Length; i++)
{
int lastDifference = b[i - 1] - a[i - 1];
int currElement = lastDifference + a[i] + 1;
if (currElement > r || currElement < l)
{
Console.Write( "-1" );
return ;
}
b.Add(currElement);
}
foreach ( int num in b)
{
Console.Write(num + " " );
}
}
static void Main( string [] args)
{
int [] a = { 1, 2, 1, 3 };
int l = 1;
int r = 10;
ArrayCreation(a, l, r);
}
}
|
Javascript
function generateArray(a, l, r) {
let b = [l];
for (let i = 1; i < a.length; i++) {
let lastDifference = b[i - 1] - a[i - 1];
let currElement = lastDifference + a[i] + 1;
if (currElement > r || currElement < l) {
b.push(-1);
return b;
}
b.push(currElement);
}
return b;
}
let a = [1, 2, 1, 3];
let l = 1;
let r = 10;
let b = generateArray(a, l, r);
for (let i = 0; i < b.length; i++) {
console.log(b[i] + " " );
}
|
Time Complexity: O(n)
Auxiliary Space: O(n), since n extra space has been taken.
Share your thoughts in the comments
Please Login to comment...