Maximum value after merging all elements in the array
Given an array a of size N. The task is to merge all elements in the array and find the maximum possible value. One can merge two elements in the array as explained below.
If i and j are two indexes of the array(i ≠j). Merging jth element into ith element makes a[i] as a[i] – a[j] and remove a[j] from the array.
Examples:
Input : a[] = {2 1 2 1} (n == 4)
Output : 4
Merge 3rd element into 2nd element then the array becomes {2, -1, 1}
Merge 3rd element into 2nd element then the array becomes {2, -2}
Merge 2nd element into 1st element then the array becomes {4}
Input: a[] = {1, 3, 5, -2, -6}
Output: 17
Merge 4th element into 3rd element then the array becomes {1, 3, -7, -6}
Merge 2nd element into 3rd element then the array becomes {1, -10, -6}
Merge 2nd element into 1st element then the array becomes {11, -6}
Merge 2nd element into 1st element then the array becomes {17}
Approach:
- If the array contains both positive and negative elements, then add absolute value all elements of the array
- If the array contains the only positive elements. Then subtract the least element from the summation of all other elements
- If the array contains the only negative elements. First, replace all elements with their absolute values. Then subtract the least element from the summation of all other elements
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Max_sum( int a[], int n)
{
int pos = 0, neg = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] > 0)
pos = 1;
else if (a[i] < 0)
neg = 1;
if (pos == 1 and neg == 1)
break ;
}
int sum = 0;
if (pos==1 and neg==1)
{
for ( int i=0; i < n ; i++)
sum += abs (a[i]);
}
else if (pos == 1)
{
int mini = a[0];
sum = a[0];
for ( int i=1; i < n; i++)
{
mini = min(mini, a[i]);
sum += a[i];
}
sum -= 2*mini;
}
else if (neg == 1)
{
for ( int i = 0; i < n; i++)
a[i] = abs (a[i]);
int mini = a[0];
sum = a[0];
for ( int i=1; i < n; i++)
{
mini = min(mini, a[i]);
sum += a[i];
}
sum -= 2*mini;
}
return sum;
}
int main()
{
int a[] = {1, 3, 5, -2, -6};
int n = sizeof (a) / sizeof (a[0]);
cout << Max_sum(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int Max_sum( int a[], int n)
{
int pos = 0 , neg = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] > 0 )
pos = 1 ;
else if (a[i] < 0 )
neg = 1 ;
if ((pos == 1 ) && (neg == 1 ))
break ;
}
int sum = 0 ;
if ((pos == 1 ) && (neg == 1 ))
{
for ( int i = 0 ; i < n ; i++)
sum += Math.abs(a[i]);
}
else if (pos == 1 )
{
int mini = a[ 0 ];
sum = a[ 0 ];
for ( int i = 1 ; i < n; i++)
{
mini = Math.min(mini, a[i]);
sum += a[i];
}
sum -= 2 *mini;
}
else if (neg == 1 )
{
for ( int i = 0 ; i < n; i++)
a[i] = Math.abs(a[i]);
int mini = a[ 0 ];
sum = a[ 0 ];
for ( int i = 1 ; i < n; i++)
{
mini = Math.min(mini, a[i]);
sum += a[i];
}
sum -= 2 *mini;
}
return sum;
}
public static void main (String[] args)
{
int []a = { 1 , 3 , 5 , - 2 , - 6 };
int n = a.length;
System.out.println (Max_sum(a, n));
}
}
|
Python3
def Max_sum(a, n):
pos = 0
neg = 0
for i in range (n):
if (a[i] > 0 ):
pos = 1
elif (a[i] < 0 ):
neg = 1
if (pos = = 1 and neg = = 1 ):
break
sum = 0
if (pos = = 1 and neg = = 1 ):
for i in range (n):
sum + = abs (a[i])
elif (pos = = 1 ):
mini = a[ 0 ]
sum = a[ 0 ]
for i in range ( 1 ,n, 1 ):
mini = min (mini, a[i])
sum + = a[i]
sum - = 2 * mini
elif (neg = = 1 ):
for i in range (n):
a[i] = abs (a[i])
mini = a[ 0 ]
sum = a[ 0 ]
for i in range ( 1 ,n):
mini = min (mini, a[i])
sum + = a[i]
sum - = 2 * mini
return sum
if __name__ = = '__main__' :
a = [ 1 , 3 , 5 , - 2 , - 6 ]
n = len (a)
print (Max_sum(a, n))
|
C#
using System;
class GFG
{
static int Max_sum( int [] a, int n)
{
int pos = 0, neg = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] > 0)
pos = 1;
else if (a[i] < 0)
neg = 1;
if ((pos == 1) && (neg == 1))
break ;
}
int sum = 0;
if ((pos == 1) && (neg == 1))
{
for ( int i = 0; i < n; i++)
sum += Math.Abs(a[i]);
}
else if (pos == 1)
{
int mini = a[0];
sum = a[0];
for ( int i = 1; i < n; i++)
{
mini = Math.Min(mini, a[i]);
sum += a[i];
}
sum -= 2 * mini;
}
else if (neg == 1)
{
for ( int i = 0; i < n; i++)
a[i] = Math.Abs(a[i]);
int mini = a[0];
sum = a[0];
for ( int i = 1; i < n; i++)
{
mini = Math.Min(mini, a[i]);
sum += a[i];
}
sum -= 2 * mini;
}
return sum;
}
public static void Main(String[] args)
{
int [] a = { 1, 3, 5, -2, -6 };
int n = a.Length;
Console.WriteLine(Max_sum(a, n));
}
}
|
Javascript
<script>
function Max_sum(a, n)
{
let pos = 0, neg = 0;
for (let i = 0; i < n; i++)
{
if (a[i] > 0)
pos = 1;
else if (a[i] < 0)
neg = 1;
if (pos == 1 && neg == 1)
break ;
}
let sum = 0;
if (pos==1 && neg==1)
{
for (let i=0; i < n ; i++)
sum += Math.abs(a[i]);
}
else if (pos == 1)
{
let mini = a[0];
sum = a[0];
for (let i=1; i < n; i++)
{
mini = Math.min(mini, a[i]);
sum += a[i];
}
sum -= 2*mini;
}
else if (neg == 1)
{
for (let i = 0; i < n; i++)
a[i] = Math.abs(a[i]);
let mini = a[0];
sum = a[0];
for (let i=1; i < n; i++)
{
mini = Math.min(mini, a[i]);
sum += a[i];
}
sum -= 2*mini;
}
return sum;
}
let a = [1, 3, 5, -2, -6];
let n = a.length;
document.write(Max_sum(a, n));
</script>
|
Output:
17
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...