Open In App

Reconstructing the Array with Maximum Possible Sum by Given Operations

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Consider an array A[] of length N. Suppose, you can create obtain B[] from A[] using below steps:

  • Iterate N/2 times on A and follow below the step below:
    • Insert (Ai + Ai+(N/2)) and |Ai – Ai+(N/2)| into B[]
  • After that rearrange B[] in any random order.

You are given B[], the task is to reconstructing any A[] if exists such that B[] can be obtained using above operations. If it is, then sum of A[] must be maximum possible else output -1.

Examples:

Input: N = 6, B[] = {4, 2, 8, 2, 10, 4}
Output: A[] = {7, 5, 3, 3, 3, 1}
Explanation: Considered that A[] is {7, 5, 3, 3, 3, 1} and B[] empty. Then (N=6)/2 = 3 iterations will be as follows:

  • First Iteration (i = 1): (A1 + A1+(6/2)) and |A1 – A1+(6/2)| and (A1 + A1+(6/2)) and |A1 – A1+(6/2)| are 7+3 = 10 and |7-3| = 4 respectively. Updated B[] = {10, 4}
  • Second Iteration (i = 2): (A2 + A2+(6/2)) and |A2 – A2+(6/2)| and (A2 + A2+(6/2)) and |A2 – A2+(6/2)| are 5+3 = 8 and âˆ£5−3∣= 2 respectively. Updated B[] = {10, 4, 8, 2}
  • Third Iteration (i = 3): (A3 + A3+(6/2)) and |A3 – A3+(6/2)| and (A3 + A3+(6/2)) and |A3 – A3+(6/2)| are 3+1 = 4 and âˆ£3−1∣ = 2 respectively. Updated B[] = {10, 4, 8, 2, 4, 2}
    It can be verified that obtained updated B[] = {10, 4, 8, 2, 4, 2} is an arrangement of input B[] = {4, 2, 8, 2, 10, 4}. Thus, B[] can be achieved by A[] using given operation. It can also verified that the sum of A[] among all possible such arrays is maximum possible, which is 22 (7+5+3+3+3+1).

Input: N = 2, B[] = {2, 3}
Output: -1
Explanation: It can be verified that no such A[] exists, by which we can obtain B[] using given operation.

Approach:

Firstly, we will check if the sum of B[] is even or not, the reason for checking is this we can confirm that whether such an array A[] really exist or not.

After this break the B[] in to arrays one with all even number and one with odd number. Next the two elements of A[] can be computed by basic math and we need max sum of A[]. Therefore, what we would do is this:

Greatest(EVEN) + Smallest(EVEN) and Greatest(EVEN) – Greatest(EVEN)
Same we will do with the subarray containing odd elements.

Step-by-step approach:

  • Initialize two arrays EV[] for even numbers and OD[] for odd numbers.
  • Iterate over the B[] and add even numbers to EV[] and odd numbers to OD[].
  • Check if the size of both EV[] and OD[] is even. If not, output -1.
  • Sort both EV[] and OD[].
  • Initialize an array let say A[] of size N to store the result.
  • Use two pointers p and q to iterate over EV[] and OD[] from the smallest to the largest and from the largest to the smallest, respectively.
  • Calculate the sum X and the difference Y for each pair of elements from EV[] and OD[].
  • Store the average of the sum in the first half of array A[] and the average of the difference in the second half of array A[].
  • Continue this process until all elements are processed.
  • Print the elements of array A[].

Below is the implementation of the above approach:

C++




#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
// Function to output A[], if it exists
void FindA(int N, vector<int>& B) {
    vector<int> A(N);
    vector<int> EV; // Vector to store even numbers
    vector<int> OD; // Vector to store odd numbers
 
    for (int i = 0; i < N; i++) {
        if (B[i] % 2 == 0) {
            EV.push_back(B[i]); // Add even numbers to EV
        } else {
            OD.push_back(B[i]); // Add odd numbers to OD
        }
    }
 
    // Check if the number of even and odd numbers are even
    if (EV.size() % 2 != 0 || OD.size() % 2 != 0) {
        cout << "-1" << endl;
    } else {
        sort(EV.begin(), EV.end()); // Sort the array of even numbers
        sort(OD.begin(), OD.end()); // Sort the array of odd numbers
        int n = EV.size();
        int p = 0;
        int q = n - 1;
        int j = 0;
 
        while (p < q) {
            int a = EV[q]; // Get the greatest even number
            int b = EV[p]; // Get the smallest even number
            int X = a + b;  // Sum of the greatest and smallest even numbers
            int Y = a - b;  // Difference of the greatest and smallest even numbers
 
            A[j] = X / 2;          // Store the average of the sum in the first half of array A
            A[j + (N / 2)] = Y / 2; // Store the average of the difference in the
                                    // second half of array
 
            j++;
            q--;
            p++;
        }
 
        n = OD.size();
        p = 0;
        q = n - 1;
 
        while (p < q) {
            int a = OD[q]; // Get the greatest odd number
            int b = OD[p]; // Get the smallest odd number
            int X = a + b;  // Sum of the greatest and smallest odd numbers
            int Y = a - b;  // Difference of the greatest and smallest odd numbers
 
            A[j] = X / 2;          // Store the average of the sum in the first half of array A
            A[j + (N / 2)] = Y / 2; // Store the average of the difference
                                    // in the second half of array
 
            j++;
            q--;
            p++;
        }
 
        for (int i : A) {
            cout << i << " ";
        }
 
        cout << endl;
    }
}
 
// Driver Code
int main() {
    // Input
    int N = 6;
    vector<int> B = {4, 2, 8, 2, 10, 4};
 
    // Function call
    FindA(N, B);
 
    return 0;
}


Java




// Java code to implement the approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Driver Class
class Main {
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // Input
        int N = 6;
        int[] B = { 4, 2, 8, 2, 10, 4 };
 
        // Function_call
        FindA(N, B);
    }
 
    // method to output A[], If it exists
    public static void FindA(int N, int[] B)
    {
        int[] A = new int[N];
        ArrayList<Integer> EV
            = new ArrayList<Integer>(); // Array to store
                                        // even numbers
        ArrayList<Integer> OD
            = new ArrayList<Integer>(); // Array to store
                                        // odd numbers
        for (int i = 0; i < N; i++) {
            if (B[i] % 2 == 0) {
                EV.add(B[i]); // Add even numbers to EV
            }
            else {
                OD.add(B[i]); // Add odd numbers to OD
            }
        }
 
        // Check if the number of even and odd numbers are
        // even
        if ((EV.size()) % 2 != 0 || (OD.size()) % 2 != 0) {
            System.out.println("-1");
        }
        else {
            Collections.sort(
                EV); // Sort the array of even numbers
            Collections.sort(
                OD); // Sort the array of odd numbers
            int n = EV.size();
            int p = 0;
            int q = n - 1;
            int j = 0;
            while (p < q) {
                int a = EV.get(
                    q); // Get the greatest even number
                int b = EV.get(
                    p); // Get the smallest even number
                int X = (a + b); // Sum of the greatest and
                                 // smallest even numbers
                int Y
                    = (a - b); // Difference of the greatest
                               // and smallest even numbers
                A[j]
                    = X / 2; // Store the average of the sum
                             // in the first half of array A
                A[j + (N / 2)]
                    = Y / 2; // Store the average of the
                             // difference in the second
                             // half of array A
                j++;
                q--;
                p++;
            }
            n = OD.size();
            p = 0;
            q = n - 1;
            while (p < q) {
                int a = OD.get(
                    q); // Get the greatest odd number
                int b = OD.get(
                    p); // Get the smallest odd number
                int X = (a + b); // Sum of the greatest and
                                 // smallest odd numbers
                int Y
                    = (a - b); // Difference of the greatest
                               // and smallest odd numbers
                A[j]
                    = X / 2; // Store the average of the sum
                             // in the first half of array A
                A[j + (N / 2)]
                    = Y / 2; // Store the average of the
                             // difference in the second
                             // half of array A
                j++;
                q--;
                p++;
            }
            for (int i : A) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}


Python3




def GFG(N, B):
    A = [0] * N
    EV = []
    OD = []
 
    # Separate even and odd numbers
    for i in range(N):
        if B[i] % 2 == 0:
            EV.append(B[i])
        else:
            OD.append(B[i])
 
    # Check if the number of even and odd numbers is even
    if len(EV) % 2 != 0 or len(OD) % 2 != 0:
        print("-1")
    else:
        EV.sort()  # Sort the array of even numbers
        OD.sort()  # Sort the array of odd numbers
 
        n = len(EV)
        p = 0
        q = n - 1
        j = 0
 
        # Process even numbers
        while p < q:
            a = EV[q]
            b = EV[p]
            X = a + b
            Y = a - b
            A[j] = X // 2  # Store the average of the sum in the first half of array A
            A[j + N // 2] = Y // 2  # Store the average of the difference in the second half of array A
            j += 1
            q -= 1
            p += 1
 
        n = len(OD)
        p = 0
        q = n - 1
 
        # Process odd numbers
        while p < q:
            a = OD[q]
            b = OD[p]
            X = a + b
            Y = a - b
            A[j] = X // 2
            A[j + N // 2] = Y // 2
            j += 1
            q -= 1
            p += 1
 
        # Print the result
        print(" ".join(map(str, A)))
 
 
# Driver Code
def main():
    # Input
    N = 6
    B = [4, 2, 8, 2, 10, 4]
    # Function call
    GFG(N, B)
 
 
if __name__ == "__main__":
    main()


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    // Function to output A[], if it exists
    static void FindA(int N, List<int> B)
    {
        List<int> A = new List<int>(new int[N]);
        List<int> EV = new List<int>(); // List to store even numbers
        List<int> OD = new List<int>(); // List to store odd numbers
 
        // Separate even and odd numbers
        foreach (int num in B)
        {
            if (num % 2 == 0)
            {
                EV.Add(num); // Add even numbers to EV
            }
            else
            {
                OD.Add(num); // Add odd numbers to OD
            }
        }
 
        // Check if the number of even and odd numbers are even
        if (EV.Count % 2 != 0 || OD.Count % 2 != 0)
        {
            Console.WriteLine("-1");
        }
        else
        {
            EV.Sort(); // Sort the list of even numbers
            OD.Sort(); // Sort the list of odd numbers
            int n = EV.Count;
            int p = 0;
            int q = n - 1;
            int j = 0;
 
            // Merge even numbers
            while (p < q)
            {
                int a = EV[q]; // Get the greatest even number
                int b = EV[p]; // Get the smallest even number
                int X = a + b;  // Sum of the greatest and smallest even numbers
                int Y = a - b;  // Difference of the greatest and smallest even numbers
 
                A[j] = X / 2;          // Store the average of the sum in the first half of array A
                A[j + (N / 2)] = Y / 2; // Store the average of the difference in the second half of array
 
                j++;
                q--;
                p++;
            }
 
            n = OD.Count;
            p = 0;
            q = n - 1;
 
            // Merge odd numbers
            while (p < q)
            {
                int a = OD[q]; // Get the greatest odd number
                int b = OD[p]; // Get the smallest odd number
                int X = a + b;  // Sum of the greatest and smallest odd numbers
                int Y = a - b;  // Difference of the greatest and smallest odd numbers
 
                A[j] = X / 2;          // Store the average of the sum in the first half of array A
                A[j + (N / 2)] = Y / 2; // Store the average of the difference in the second half of array
 
                j++;
                q--;
                p++;
            }
 
            // Output the resulting array A
            foreach (int i in A)
            {
                Console.Write(i + " ");
            }
 
            Console.WriteLine();
        }
    }
 
    // Driver Code
    static void Main()
    {
        // Input
        int N = 6;
        List<int> B = new List<int> { 4, 2, 8, 2, 10, 4 };
 
        // Function call
        FindA(N, B);
    }
}


Javascript




function GFG(N, B) {
    let A = new Array(N);
    let EV = [];
    let OD = [];
    // Separate even and odd numbers
    for (let i = 0; i < N; i++) {
        if (B[i] % 2 === 0) {
            EV.push(B[i]);
        } else {
            OD.push(B[i]);
        }
    }
    // Check if the number of the even and odd numbers are even
    if (EV.length % 2 !== 0 || OD.length % 2 !== 0) {
        console.log("-1");
    } else {
        EV.sort((a, b) => a - b); // Sort the array of the even numbers
        OD.sort((a, b) => a - b); // Sort the array of odd numbers
        let n = EV.length;
        let p = 0;
        let q = n - 1;
        let j = 0;
        // Process even numbers
        while (p < q) {
            let a = EV[q];
            let b = EV[p];
            let X = a + b;
            let Y = a - b;
            A[j] = X / 2; // Store the average of the sum in the first half of array A
            A[j + N / 2] = Y / 2; // Store the average of the difference in the second half of array A
            j++;
            q--;
            p++;
        }
        n = OD.length;
        p = 0;
        q = n - 1;
        // Process odd numbers
        while (p < q) {
            let a = OD[q];
            let b = OD[p];
            let X = a + b;
            let Y = a - b;
            A[j] = X / 2;
            A[j + N / 2] = Y / 2;
            j++;
            q--;
            p++;
        }
        // Print the result
        console.log(A.join(" "));
    }
}
// Driver Code
function main() {
    // Input
    let N = 6;
    let B = [4, 2, 8, 2, 10, 4];
    // Function call
    GFG(N, B);
}
main();


Output

6 5 4 4 3 0 

Time Complexity: O(NLogN), As sorting is performed.
Auxiliary Space: O(N), As ArrayLists EV and OD of length N are used.



Similar Reads

Reconstructing Segment Tree
We are given 2*N - 1 integers. We need to check whether it is possible to construct a Range Minimum Query segment tree for an array of N distinct integers from these integers. If so, we must output the segment tree array. N is given to be a power of 2.An RMQ segment tree is a binary tree where each node is equal to the minimum value of its children
11 min read
Maximum sum possible from given Matrix by performing given operations
Given a matrix arr[][] of dimensions 2 * N, the task is to maximize the sum possible by selecting at most one element from each column such that no two consecutive elements are chosen from the same row. Examples: Input: arr[][] = {{1, 50, 21, 5}, {2, 10, 10, 5}}Output: 67Explanation: Elements arr[1][0]( = 2), arr[0][1]( = 50), arr[0][2]( = 10) and
11 min read
Maximum possible Array sum after performing given operations
Given array arr[] of positive integers, an integer Q, and arrays X[] and Y[] of size Q. For each element in arrays X[] and Y[], we can perform the below operations: For each query from array X[] and Y[], select at most X[i] elements from array arr[] and replace all the selected elements with integer Y[i].After performing Q operations, the task is t
9 min read
Maximum Possible Product in Array after performing given Operations
Given an array with size N. You are allowed to perform two types of operations on the given array as described below: Choose some position i and j, such that (i is not equals to j), replace the value of a[j] with a[i]*a[j] and remove the number from the ith cell.Choose some position i and remove the number from the ith cell (This operation can be p
13 min read
Maximum score possible after performing given operations on an Array
Given an array A of size N, the task is to find the maximum score possible of this array. The score of an array is calculated by performing the following operations on the array N times: If the operation is odd-numbered, the score is incremented by the sum of all elements of the current array. If the operation is even-numbered, the score is decreme
29 min read
Check if its possible to make sum of the array odd with given Operations
Given an array arr[], the task is to check if it is possible to make the sum of array odd such that any two indices i and j can be chosen and arr[i] can be set equal to arr[j] given that i != j. Examples: Input: arr[] = { 5, 4, 4, 5, 1, 3 } Output: Yes Explanation: The sum of the array is 22. Put arr[0] = arr[1] where i=0 and j=1. The sum of this n
7 min read
Maximum possible sum after M operations on N cards
Given an array arr[] of size N which represents the initial number on each card and given a two dimensional array B[][] of size M where M represents the number of operations that need to be performed. At each operation, choose at most B[j][0] cards (possibly zero) and replace the integer written on each chosen card with B[j][1]. The task is to find
8 min read
Maximum number of multiplication by 3 or division by 2 operations possible on an array
Given an array arr[] consisting of N positive integers, the task is to find the maximum number of times each array element can either be multiplied by M or divided by K. Note: At least one element needs to be divided by M and K respectively in each operation. Examples: Input: arr[] = {5, 2, 4}, M = 3, K = 2Output: 3Explanation:One possible way to p
6 min read
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
Given an array A[] consisting of N integers, the task is to find the maximum subset-sum possible if the sum of all the elements is negated if the first element of the array is included. An empty subset(having sum 0) can also be considered. Examples: Input: N = 5, A[] = {1, 10, 4, -6, 3} Output: 17 Explanation: On excluding A[0], subset with maximum
7 min read
Maximum XOR value of maximum and second maximum element among all possible subarrays
Given an array arr[] of N distinct positive integers, let's denote max(i, j) and secondMax(i, j) as the maximum and the second maximum element of the subarray arr[i...j]. The task is to find the maximum value of max(i, j) XOR secondMax(i, j) for all possible values of i and j. Note that the size of the subarray must be at least two.Examples: Input:
5 min read