Minimum sum by choosing minimum of pairs from array
Last Updated :
18 Sep, 2023
Given an array A[] of n-elements. We need to select two adjacent elements and delete the larger of them and store smaller of them to another array say B[]. We need to perform this operation till array A[] contains only single element. Finally, we have to construct the array B[] in such a way that total sum of its element is minimum. Print the total sum of array B[]
Examples:
Input : A[] = {3, 4}
Output : 3
Input : A[] = {2, 4, 1, 3}
Output : 3
There is an easy trick to solve this question and that is always choose the smallest element of array A[] and its adjacent, delete the adjacent element and copy smallest one to array B[]. Again for next iteration we have same smallest element and any random adjacent element which is to be deleted. After n-1 operations all of elements of A[] got deleted except the smallest one and at the same time array B[] contains "n-1" elements and all are equal to smallest element of array A[].
Thus total sum of array B[] is equal to smallest * (n-1).
Implementation: Rearrange positive and negative numbers using inbuilt sort function
C++
// CPP program to minimize the cost
// of array minimization
#include <bits/stdc++.h>
using namespace std;
// Returns minimum possible sum in
// array B[]
int minSum(int A[], int n)
{
int min_val = *min_element(A, A+n);
return (min_val * (n-1));
}
// driver function
int main()
{
int A[] = { 3, 6, 2, 8, 7, 5};
int n = sizeof(A)/ sizeof (A[0]);
cout << minSum(A, n);
return 0;
}
Java
// Java program to minimize the
// cost of array minimization
import java.util.Arrays;
public class GFG {
// Returns minimum possible
// sum in array B[]
static int minSum(int[] A, int n) {
int min_val = Arrays.stream(A).min().getAsInt();
return (min_val * (n - 1));
}
// Driver Code
static public void main(String[] args) {
int[] A = {3, 6, 2, 8, 7, 5};
int n = A.length;
System.out.println((minSum(A, n)));
}
}
// This code is contributed by Rajput-Ji
Python
# Python code for minimum cost of
# array minimization
# Function definition for minCost
def minSum(A):
# find the minimum element of A[]
min_val = min(A);
# return the answer
return min_val * (len(A)-1)
# driver code
A = [7, 2, 3, 4, 5, 6]
print (minSum(A))
C#
// C# program to minimize the
// cost of array minimization
using System;
using System.Linq;
public class GFG
{
// Returns minimum possible
// sum in array B[]
static int minSum(int []A, int n)
{
int min_val = A.Min();
return (min_val * (n - 1));
}
// Driver Code
static public void Main()
{
int []A = {3, 6, 2, 8, 7, 5};
int n = A.Length;
Console.WriteLine(minSum(A, n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to minimize the
// cost of array minimization
// Returns minimum possible
// sum in array B[]
function minSum($A, $n)
{
$min_val = min($A);
return ($min_val * ($n - 1));
}
// Driver Code
$A = array(3, 6, 2, 8, 7, 5);
$n = count($A);
echo minSum($A, $n);
// This code is contributed by vt_m.
?>
JavaScript
<script>
// JavaScript program to minimize the cost
// of array minimization
// Returns minimum possible sum in
// array B[]
function minSum(A, n)
{
let min_val = Math.min(...A);
return (min_val * (n-1));
}
// driver function
let A = [3, 6, 2, 8, 7, 5];
let n = A.length;
document.write(minSum(A, n));
// This code is contributed by Mayank Tyagi
</script>
Time Complexity : O(n) in finding the smallest element of the array.
Auxiliary Space: O(1)
Similar Reads
Maximum sum of minimums of pairs in an array Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.Examples: Input: arr[] = {1, 5, 3, 2} Output: 4 (1, 5) and (3, 2) -> 1 + 2 = 3 (1,
4 min read
Minimum sum obtained by choosing N number from given N pairs Given an array arr[] of N pairs of integers (A, B) where N is even, the task is to find the minimum sum of choosing N elements such that value A and B from all the pairs are chosen exactly (N/2) times.Examples: Input: N = 4, arr[][] = { {7, 20}, {300, 50}, {30, 200}, {30, 20} } Output: 107 Explanati
7 min read
Maximum and minimum sum of Bitwise XOR of pairs from an array Given an array arr[] of size N, the task is to find the maximum and minimum sum of Bitwise XOR of all pairs from an array by splitting the array into N / 2 pairs. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6 10Explanation:Bitwise XOR of the all possible pair splits are as follows:(1, 2), (3, 4) â
11 min read
Maximize value of given expression by choosing pair from Array Given an array A[] of length N, choose any two elements x and y from the array, the task is to find the maximum value of the expression (x * y + x - y). Examples: Input: A[] = {5, 2, 3} Output: 17Explanation: There are six possible pairs:For pairs {2, 3} and {3, 2}, answer = 2 ? 3 + max(2?3, 3?2) =
5 min read
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read