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++
// 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] + " ");
}
Time Complexity: O(n)
Auxiliary Space: O(n), since n extra space has been taken.
Similar Reads
Generate an array B[] from the given array A[] which satisfies the given conditions Given an array A[] of N integers such that A[0] + A[1] + A[2] + ... A[N - 1] = 0. The task is to generate an array B[] such that B[i] is either ?A[i] / 2? or ?A[i] / 2? for all valid i and B[0] + B[1] + B[2] + ... + B[N - 1] = 0. Examples: Input: A[] = {1, 2, -5, 3, -1} Output: 0 1 -2 1 0Input: A[]
6 min read
Find the array B[] from A[] according to the given conditions Given an array X[] of length N, having all elements less than or equal to M, the task is to print an array let's say Y[] of the same length following the given conditions: Prefix GCD till index i in Y[] must be equal to ith element of X[] for all i (0 <= i <= N-1)The output array must be lexog
8 min read
Generate an array of size N according to the given rules Given a number N, the task is to create an array arr[] of size N, where the value of the element at every index i is filled according to the following rules: arr[i] = ((i - 1) - k), where k is the index of arr[i - 1] that has appeared second most recently. This rule is applied when arr[i - 1] is pre
10 min read
Create Y[] by given Array X[] following given condition Given an array X[] of length N, Where (1 ? Xi ? N). Create an array Y[], where the element at Y[i] should divide an element present at Y[X[i]-1]. and (X[i]-1) is the maximum such position in which the element is divisible by Y[i]. Note: If there are multiple possible arrays Y[]. Then print any one o
4 min read
Restore the original array from another array generated by given operations Given an array b. The array b is obtained initially by array a, by doing the following operations. Choose a fixed point x from array a.Cyclically rotate the array a to the left exactly x times.Fixed point x is that point where (a[i] = i ). These operations are performed on the array a k times, deter
8 min read