GeeksforGeeks App
Open App
Browser
Continue

# Generate Array such that replacing any two element by their difference gives same or greater sum

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

 `// C++ program for the above approach` `#include ``using` `namespace` `std;``#define ll long long int` `// Function to generate the array``vector<``int``> createArray(``int` `N)``{``    ``vector<``int``> res;` `    ``// Initializing the arr[0] with 1``    ``long` `long` `int` `ans = 1;``    ``while` `(N--) {``        ``res.push_back(ans);` `        ``// For every number we are multiplying``        ``// the previous number with 3``        ``ans *= 3;``    ``}``    ``return` `res;``}` `// Driver code``int` `main()``{``    ``int` `N = 3;` `    ``// Function call``    ``vector<``int``> ans = createArray(N);``    ``for` `(``int` `x : ans)``        ``cout << x << ``" "``;``    ``return` `0;``}`

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `  ``// Function to generate the array``  ``static` `Vector createArray(``int` `N)``  ``{``    ``Vector res = ``new` `Vector();` `    ``// Initializing the arr[0] with 1``    ``int`  `ans = ``1``;``    ``while` `(N != ``0``) {``      ``res.add(ans);` `      ``// For every number we are multiplying``      ``// the previous number with 3``      ``ans *= ``3``;``      ``N -= ``1``;``    ``}``    ``return` `res;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main (String[] args) {   ``    ``int` `N = ``3``;` `    ``// Function call``    ``Vector ans = createArray(N);``    ``ans.forEach((n) -> System.out.print(n));``  ``}``}` `// This code is contributed by hrithikgarg03188`

## Python3

 `# Python program for above approach` `# Function to generate the array``def` `createArray(N):``    ``res ``=` `[]` `    ``# Initializing the arr[0] with 1``    ``ans ``=` `1``    ``while` `(N):``        ``res.append(ans)` `        ``# For every number we are multiplying``        ``# the previous number with 3``        ``ans ``*``=` `3``        ``N ``-``=` `1` `    ``return` `res` `# Driver code``N ``=` `3` `# Function call``ans ``=` `createArray(N)``for` `x ``in` `ans:``    ``print``(x, end ``=` `" "``)` `# This code is contributed by shinjanpatra`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;``class` `GFG {` `  ``// Function to generate the array``  ``static` `List<``int``> createArray(``int` `N)``  ``{``    ``List<``int``> res = ``new` `List<``int``>();` `    ``// Initializing the arr[0] with 1``    ``int` `ans = 1;``    ``while` `(N != 0) {``      ``res.Add(ans);` `      ``// For every number we are multiplying``      ``// the previous number with 3``      ``ans *= 3;``      ``N -= 1;``    ``}``    ``return` `res;``  ``}` `  ``// Driver code``  ``public` `static` `int` `Main()``  ``{``    ``int` `N = 3;` `    ``// Function call``    ``List<``int``> ans = createArray(N);``    ``for` `(``int` `i = 0; i < N; ++i) {``      ``Console.Write(ans[i] + ``" "``);``    ``}``    ``return` `0;``  ``}``}` `// This code is contributed by Taranpreet`

## Javascript

 ``

Output

`1 3 9 `

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up