Generate Array such that replacing any two element by their difference gives same or greater sum
Last Updated :
16 May, 2022
Given a non-negative integer N, the task is to generate an array such that if any two elements of that array are replaced by their absolute difference then the sum of the current array is either greater or more than the generated array.
Note: If there are multiple possible arrays, then return any of them
Examples:
Input: N = 2
Output: arr[] = [1, 3]
Explanation: In the above example the sum of array is 4.
Select two indices 0 and 1 and replace 1 and 3 by |1-3| = 2, then array is [2, 2].
So now the sum is 4 which is equal to the previous sum.
Input: N = 3
Output: arr[] = [1, 10, 40]
Explanation: Firstly the total sum of array is 51.
Select two indices 0 and 1 and replace them with |arr[0] – arr[1]|.
Then the array would be arr = [9, 9, 40].
So now the total sum is 58 hence the sum is increased.
For another pair of indices 0 and 2
Replace arr[0] and arr[2] by |arr[0] – arr[2]|.
Then the array would be arr = [39, 10, 39].
So now the total sum is 88 hence the sum is increased.
For pair of indices 1 and 2
Replace arr[1] and arr[2] by |arr[1] – arr[2]|.
Then the array would be arr = [1, 30, 30].
So now the total sum is 61 hence the sum is increased.
So the above array is a possible answer
Approach: This problem can be solved based on the following mathematical observation:
If there are two numbers a and b and we want to replace them by |a – b| such that the total sum remains same or increases then:
a + b ≤ |a – b| + |a – b|
=> a + b ≤ 2*|a – b|
Now consider a is the larger element, then
a + b ≤ 2*a – 2*b
=> b + 2*b ≤ 2*a – a
=> 3*b ≤ a
So the greater element must be at least 3 times of the smaller element.
Based on the above observation it can be seen that if for the generated array (say arr[]) any ith element has a value at least 3*arr[i-1] then the array will satisfy the given condition.
Follow the steps mentioned below to implement the above observation:
- Generate an array arr[] of size N and initialize arr[0] = 1.
- Iterate from i = 1 to N-1 in the array:
- Set arr[i] to 3*arr[i-1].
- Return the array generated.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
vector< int > createArray( int N)
{
vector< int > res;
long long int ans = 1;
while (N--) {
res.push_back(ans);
ans *= 3;
}
return res;
}
int main()
{
int N = 3;
vector< int > ans = createArray(N);
for ( int x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static Vector<Integer> createArray( int N)
{
Vector<Integer> res = new Vector<Integer>();
int ans = 1 ;
while (N != 0 ) {
res.add(ans);
ans *= 3 ;
N -= 1 ;
}
return res;
}
public static void main (String[] args) {
int N = 3 ;
Vector<Integer> ans = createArray(N);
ans.forEach((n) -> System.out.print(n));
}
}
|
Python3
def createArray(N):
res = []
ans = 1
while (N):
res.append(ans)
ans * = 3
N - = 1
return res
N = 3
ans = createArray(N)
for x in ans:
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static List< int > createArray( int N)
{
List< int > res = new List< int >();
int ans = 1;
while (N != 0) {
res.Add(ans);
ans *= 3;
N -= 1;
}
return res;
}
public static int Main()
{
int N = 3;
List< int > ans = createArray(N);
for ( int i = 0; i < N; ++i) {
Console.Write(ans[i] + " " );
}
return 0;
}
}
|
Javascript
<script>
function createArray(N)
{
let res = [];
let ans = 1;
while (N--) {
res.push(ans);
ans *= 3;
}
return res;
}
let N = 3;
let ans = createArray(N);
for (let x of ans)
document.write(x, " " );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...