# Generate an N-length array with sum equal to twice the sum of its absolute difference with same-indexed elements of given array

• Last Updated : 09 Feb, 2022

Given an array arr[] of size N, the task is to construct an array brr[] of size N that satisfies the following conditions:

• In every pair of consecutive elements in the array brr[], one element must be divisible by the other, i.e. brr[i] must be divisible by brr[i + 1] or vice-versa.
• Every ith element in the array brr[] must satisfy brr[i] >= arr[i] / 2.
• The sum of elements of the array arr[] must be greater than or equal to 2 * Î£abs(arr[i] – brr[i]).

Examples:

Input: arr[] = { 11, 5, 7, 3, 2 }
Output: 8 4 4 2 2
Explanation:
abs(11 – 8) + abs(5 – 4) + abs(7 – 4) + abs(3 – 2) + abs(2 – 2) = 8
arr[0] + arr[1] + … + arr[4] = 28
2 * 8 <= 28 and for every ith element brr[i] >= arr[i] / 2.
Therefore, one of the possible values of brr[] are 8 4 4 2 2.

Input: arr[] = { 11, 7, 5 }
Output: { 8, 4, 4 }

Approach: The idea is based on the following observation:

If brr[i] is the nearest power of 2 and is smaller than or equal to arr[i], then brr[i] must be greater than or equal to arr[i] / 2 and also the sum of elements of the array, arr[] must be greater than or equal to 2 * Î£abs(arr[i] – brr[i]).

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to construct an array// with given conditionsvoid constructArray(int arr[], int N){    int brr[N] = { 0 };     // Traverse the array arr[]    for (int i = 0; i < N; i++) {         int K = log(arr[i]) / log(2);         // Stores closest power of 2        // less than or equal to arr[i]        int R = pow(2, K);         // Stores R into brr[i]        brr[i] = R;    }     // Print array elements    for (int i = 0; i < N; i++) {        cout << brr[i] << " ";    }} // Driver Codeint main(){     // Given array    int arr[] = { 11, 5, 7, 3, 2 };     // Size of the array    int N = sizeof(arr) / sizeof(arr[0]);     // Function Call    constructArray(arr, N);     return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{ // Function to construct an array// with given conditionsstatic void constructArray(int arr[], int N){    int brr[] = new int[N];         // Traverse the array arr[]    for(int i = 0; i < N; i++)    {        int K = (int)(Math.log(arr[i]) /                      Math.log(2));                 // Stores closest power of 2        // less than or equal to arr[i]        int R = (int)Math.pow(2, K);                 // Stores R into brr[i]        brr[i] = R;    }         // Print array elements    for(int i = 0; i < N; i++)    {        System.out.print(brr[i] + " ");    }} // Driver Codepublic static void main(String[] args){         // Given array    int arr[] = { 11, 5, 7, 3, 2 };     // Size of the array    int N = arr.length;     // Function Call    constructArray(arr, N);}} // This code is contributed by 29AjayKumar

## Python3

 # Python3 program for the above approachfrom math import log # Function to construct an array# with given conditionsdef constructArray(arr, N):    brr = [0]*N     # Traverse the array arr[]    for i in range(N):        K = int(log(arr[i])/log(2))         # Stores closest power of 2        # less than or equal to arr[i]        R = pow(2, K)         # Stores R into brr[i]        brr[i] = R     # Print array elements    for i in range(N):        print(brr[i], end = " ") # Driver Codeif __name__ == '__main__':     # Given array    arr = [11, 5, 7, 3, 2]     # Size of the array    N = len(arr)     # Function Call    constructArray(arr, N) # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approach   using System;         class GFG{         // Function to construct an array   // with given conditions   static void constructArray(int []arr, int N)   {       int[] brr = new int[N];       Array.Clear(brr, 0, brr.Length);            // Traverse the array arr[]       for(int i = 0; i < N; i++)    {           int K = (int)(Math.Log(arr[i]) /                      Math.Log(2));                                  // Stores closest power of 2           // less than or equal to arr[i]           int R = (int)Math.Pow(2, K);                    // Stores R into brr[i]           brr[i] = R;       }             // Print array elements       for(int i = 0; i < N; i++)    {        Console.Write(brr[i] + " ");       }   }         // Driver Code   public static void Main()   {            // Given array       int []arr = { 11, 5, 7, 3, 2 };             // Size of the array       int N = arr.Length;             // Function Call       constructArray(arr, N);   }   } // This code is contributed by SURENDRA_GANGWAR

## Javascript



Output:

8 4 4 2 2

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

My Personal Notes arrow_drop_up