Open In App

Generate an Array from given Array according to given conditions

Last Updated : 24 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Function to generate the result
// array - b[].
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++) {
 
        // Calculating the last difference
        int lastDifference = b[i - 1] - a[i - 1];
 
        // Calculating potential current
        // element
        int currElement = lastDifference + a[i] + 1;
 
        if (currElement > r or currElement < l) {
            b.push_back(-1);
            return b;
        }
 
        b.push_back(currElement);
    }
 
    return b;
}
 
// Driver's code
int main()
{
 
    vector<int> a = { 1, 2, 1, 3 };
    int l = 1;
    int r = 10;
 
    // Function call.
    vector<int> b = generateArray(a, l, r);
 
    for (auto& it : b)
        cout << it << " ";
 
    return 0;
}


Java




// Java code to implement the above approach.
import java.io.*;
import java.util.ArrayList;
 
class GFG {
 
    // function to generate the result array - b[].
    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++) {
 
            // calculating the last difference
            int lastDifference = b.get(i - 1) - a[i - 1];
            // calculating potential current element
            int currElement = lastDifference + a[i] + 1;
 
            if (currElement > r || currElement < l) {
                System.out.print("-1");
                return;
            }
 
            b.add(currElement);
        }
 
        // printing the resultant array
        for (int i = 0; i < b.size(); i++) {
 
            System.out.print(b.get(i) + " ");
        }
    }
    // driver code
    public static void main(String[] args)
    {
        int a[] = { 1, 2, 1, 3 };
        int l = 1;
        int r = 10;
 
        arrayCreation(a, l, r);
    }
}


Python3




# Python code for the above approach
 
# Function to generate the result
# array - b[].
def generateArray(a, l, r):
    b = [l]
     
    for i in range(1, len(a)):
         
        # Calculating the last difference
        lastDifference = b[i - 1] - a[i - 1]
 
        # Calculating potential current
        # element
        currElement = lastDifference + a[i] + 1
 
        if currElement > r or currElement < l:
            b.append(-1)
            return b
 
        b.append(currElement)
 
    return b
 
 
# Driver's code
 
a = [1, 2, 1, 3]
l = 1
r = 10
 
# Function call.
b = generateArray(a, l, r)
 
for i in b:
    print(i, end=" ")
 
# This code is contributed by prasad264


C#




// C# code to implement the above approach.
using System;
using System.Collections.Generic;
 
class GFG
{
    // function to generate the result array - b[]
    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++)
        {
            // calculating the last difference
            int lastDifference = b[i - 1] - a[i - 1];
            // calculating potential current element
            int currElement = lastDifference + a[i] + 1;
 
            if (currElement > r || currElement < l)
            {
                Console.Write("-1");
                return;
            }
 
            b.Add(currElement);
        }
 
        // printing the resultant array
        foreach (int num in b)
        {
            Console.Write(num + " ");
        }
    }
 
    // driver code
    static void Main(string[] args)
    {
        int[] a = { 1, 2, 1, 3 };
        int l = 1;
        int r = 10;
 
        ArrayCreation(a, l, r);
    }
}
 
// This code is contributed by Vaibhav Nandan


Javascript




// JavaScript code for the above approach
 
// Function to generate the result array - b[]
function generateArray(a, l, r) {
    let b = [l];
     
    for (let i = 1; i < a.length; i++) {
        // Calculating the last difference
        let lastDifference = b[i - 1] - a[i - 1];
     
        // Calculating potential current element
        let currElement = lastDifference + a[i] + 1;
     
        if (currElement > r || currElement < l) {
            b.push(-1);
            return b;
        }
     
        b.push(currElement);
    }
     
    return b;
}
 
// Driver's code
let a = [1, 2, 1, 3];
let l = 1;
let r = 10;
 
// Function call
let b = generateArray(a, l, r);
 
for (let i = 0; i < b.length; i++) {
    console.log(b[i] + " ");
}


Output

1 3 3 6 



Time Complexity: O(n)
Auxiliary Space: O(n), since n extra space has been taken.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads