Minimum sum obtained by choosing N number from given N pairs
Last Updated :
15 Jul, 2025
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
Explanation:
Choose value-A from 1st pair = 7.
Choose value-B from 2nd pair = 50.
Choose value-A from 3rd pair = 30.
Choose value-B from 4th pair = 20.
The minimum sum is 7 + 50 + 30 + 20 = 107.
Input: N = 4, arr[][] = { {10, 20}, {400, 50}, {30, 200}, {30, 20} }
Output: 110
Explanation:
Choose value-A from 1st pair = 10.
Choose value-B from 2nd pair = 50.
Choose value-A from 3rd pair = 30.
Choose value-B from 4th pair = 20.
The minimum sum is 10 + 50 + 30 + 20 = 110.
Approach: This problem can be solved using Greedy Approach. Below are the steps:
- For each pair (A, B) in the given array, store the value of (B - A) with the corresponding index in temporary array(say temp[]). The value (B - A) actually defines how much cost is minimized if A is chosen over B for each element.
- The objective is to minimize the total cost. Hence, sort the array temp[] in decreasing order.
- Pick the first N/2 elements from the array temp[] by choosing A as first N/2 elements will have the maximum sum when A is chosen over B.
- For remaining N/2 elements choose B as the sum of values can be minimized.
Below is the implementation of the above approach:
CPP
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to choose the elements A
// and B such the sum of all elements
// is minimum
int minSum(int arr[][2], int n)
{
// Create an array of pair to
// store Savings and index
pair<int, int> temp[n];
// Traverse the given array of pairs
for (int i = 0; i < 2 * n; i++) {
// Sum minimized when A
// is chosen over B for
// i-th element.
temp[i].first = arr[i][1]
- arr[i][0];
// Store index for the
// future reference.
temp[i].second = i;
}
// Sort savings array in
// non-increasing order.
sort(temp, temp + 2 * n,
greater<pair<int, int> >());
// Storing result
int res = 0;
for (int i = 0; i < 2 * n; i++) {
// First n elements choose
// A and rest choose B
if (i < n)
res += arr[temp[i].second][0];
else
res += arr[temp[i].second][1];
}
// Return the final Sum
return res;
}
// Driver Code
int main()
{
// Given array of pairs
int arr[4][2] = { { 7, 20 },
{ 300, 50 },
{ 30, 200 },
{ 30, 20 } };
// Function Call
cout << minSum(arr, 2);
}
Java
/*package whatever //do not write package name here */
import java.util.*;
public class GFG{
static class Pair<K,V>{
K first;
V second;
Pair(K k, V v){
first = k;
second = v;
}
}
// Function to choose the elements A
// and B such the sum of all elements
// is minimum
static int minSum(int arr[][], int n)
{
// Create an array of pair to
// store Savings and index
Pair<Integer, Integer> temp[] = new Pair[2*n];
// Traverse the given array of pairs
for (int i = 0; i < 2 * n; i++) {
// Sum minimized when A
// is chosen over B for
// i-th element.
temp[i] = new Pair(arr[i][1] - arr[i][0],i);
// Store index for the
// future reference.
}
// Sort savings array in
// non-increasing order.
Arrays.sort(temp,(a,b)->b.first-a.first);
// Storing result
int res = 0;
for (int i = 0; i < 2 * n; i++) {
// First n elements choose
// A and rest choose B
if (i < n)
res += arr[temp[i].second][0];
else
res += arr[temp[i].second][1];
}
// Return the final Sum
return res;
}
public static void main(String[] args) {
// Given array of pairs
int arr[][] = { { 7, 20 }, { 300, 50 }, { 30, 200 }, { 30, 20 } };
// Function Call
System.out.println(minSum(arr, 2));
}
}
// This code is contributed by aadityaburujwale.
Python3
# Python3 program for the above approach
# Function to choose the elements A
# and B such the sum of all elements
# is minimum
def minSum(arr, n):
# Create an array of pair to
# store Savings and index
temp = [None]*(2*n)
# Traverse the given array of pairs
for i in range(2 * n):
# Sum minimized when A
# is chosen over B for
# i-th element.
temp[i] = (arr[i][1] - arr[i][0], i)
# Sort savings array in
# non-increasing order.
temp.sort(reverse=True)
# Storing result
res = 0
for i in range(2 * n):
# First n elements choose
# A and rest choose B
if (i < n):
res += arr[temp[i][1]][0]
else:
res += arr[temp[i][1]][1]
# Return the final Sum
return res
# Driver Code
if __name__ == '__main__':
# Given array of pairs
arr = [[7, 20],
[300, 50],
[30, 200],
[30, 20]]
# Function Call
print(minSum(arr, 2))
C#
using System;
using System.Linq;
class GFG
{
// Function to choose the elements A
// and B such the sum of all elements
// is minimum
public static int MinSum(int[,] arr, int n)
{
// Create an array of Tuple to store savings and index
Tuple<int, int>[] temp = new Tuple<int, int>[2 * n];
// Traverse the given array of pairs
for (int i = 0; i < 2 * n; i++)
{
// Sum minimized when A is chosen over B for i-th element.
temp[i] = new Tuple<int, int>(arr[i, 1] - arr[i, 0], i);
}
// Sort savings array in non-increasing order.
Array.Sort(temp, (x, y) => y.Item1.CompareTo(x.Item1));
// Storing result
int res = 0;
for (int i = 0; i < 2 * n; i++)
{
// First n elements choose A and rest choose B
if (i < n) {
res += arr[temp[i].Item2, 0];
} else {
res += arr[temp[i].Item2, 1];
}
}
// Return the final Sum
return res;
}
public static void Main(string[] args)
{
// Given array of pairs
int[,] arr = { { 7, 20 }, { 300, 50 }, { 30, 200 }, { 30, 20 } };
Console.WriteLine(MinSum(arr, 2));
}
}
// This code is contributed by poojaagarwal2.
JavaScript
// Javascript program for the above approach
// Function to choose the elements A
// and B such the sum of all elements
// is minimum
function minSum(arr, n) {
// Create an array of pair to
// store Savings and index
let temp = new Array(2 * n).fill(null);
// Traverse the given array of pairs
for (let i = 0; i < 2 * n; i++) {
// Sum minimized when A
// is chosen over B for
// i-th element.
temp[i] = [arr[i][1] - arr[i][0], i]
}
// Sort savings array in
// non-increasing order.
temp.sort((a, b) => b[0] - a[0]);
// Storing result
let res = 0
for (let i = 0; i < 2 * n; i++) {
// First n elements choose
// A and rest choose B
if (i < n)
res += arr[temp[i][1]][0]
else
res += arr[temp[i][1]][1]
}
// Return the final Sum
return res
}
// Driver Code
// Given array of pairs
let arr = [[7, 20],
[300, 50],
[30, 200],
[30, 20]]
// Function Call
console.log(minSum(arr, 2))
// This code is contributed by saurabh_jaiswal.
Time Complexity: O(N*log(N))
Auxiliary Space Complexity: O(N)
Similar Reads
Minimum sum by choosing minimum of pairs from array 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 to
4 min read
Minimize the sum of squares of sum of N/2 paired formed by N numbers Given N numbers(N is an even number). Divide the N numbers into N/2 pairs in such a way that the sum of squares of the sum of numbers in pairs is minimal. The task is to print the minimal sum. Examples: Input: a[] = {8, 5, 2, 3} Output: 164 Divide them into two groups of {2, 8} and {3, 5} to give (2
5 min read
Minimum and Maximum number of pairs in m teams of n people There are n people which are to be grouped into exactly m teams such that there is at least one person in each team. All members of a team are friends with each other. Find the minimum and maximum no. of pairs of friends that can be formed by grouping these n people into exactly m teams.Examples: In
6 min read
Minimum total sum from the given two arrays Given two arrays A[] and B[] of N positive integers and a cost C. We can choose any one element from each index of the given arrays i.e., for any index i we can choose only element A[i] or B[i]. The task is to find the minimum total sum of selecting N elements from the given two arrays and if we are
11 min read
Minimum number of pairs required to be selected to cover a given range Given an array arr[] consisting of N pairs and a positive integer K, the task is to select the minimum number of pairs such that it covers the entire range [0, K]. If it is not possible to cover the range [0, K], then print -1. Examples : Input: arr[] = {{0, 3}, {2, 5}, {5, 6}, {6, 8}, {6, 10}}, K =
10 min read
Split array into minimum number of subsets having maximum pair sum at most K Given an array, arr[] of size n, and an integer k, the task is to partition the array into the minimum number of subsets such that the maximum pair sum in each subset is less than or equal to k.Examples:Input: arr[] = [1, 2, 3, 4, 5], k = 5Output: 3Explanation: Subset having maximum pair sum less th
4 min read