Array Partition I in Python


We are given the array, let's say arr[] of 2n integers. We have to make the pair group of the integer elements like(a1,b1), (a2,b2)....(an,bn) which makes the sum of min(ai,bi) for all elements in the array as large as possible. The task is to find the maximum sum of pairs For example arr[] = [1, 2, 3, 4] and output is 4 the maximum sum of pairs is 4. The all possible are 

  • (1, 2) and (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4.

  • (1, 4) and (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3.

  • (1, 3) and (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3.

Maximum possible sum is  4.

Array in Python is a container that can store multiple values of a single type which can be accessed using the reference to an index value stored in an array. It is important to note that python doesn’t provide the inbuilt for an array, instead it provides implementation for the List.In array the indexing starts from 0. For example, arr[0] is the first element , arr[1] is the second element and so on till n -1, the arr[n-1]is the last element. Array stores the element in contiguous memory allocation and arr in arr[] is a pointer which stores the memory of the first element. It is easy to access all the elements of an array.

In this tutorial. In the first approach we will sort the array then traverse the array through a for loop with increment of 2 then add the array elements to res and then return the res. In the second approach sort the array and declare the list res then traverse the array with increment of 2 in for loop then append the a[i] and a[i+1] then return the sum of minimum in array res. In the third approach, sort the array and declare the variable count and start the loop while count till half of the array then add the min of a[n] and a[n+1] to sum then increment the count with 1 at last return the sum.

Example

Input − arr[] = { 7, 8, 6, 9}

Output − The maximum sum is: 9

Explanation  We are given the array [7, 8, 6, 9] with 2n numbers of integers. We have to

make the pairs of integers like(a1,b1), (a2,b2)....(an,bn) which makes the sum on min(ai,bi) for all elements in the array as large as possible. The task is to find the maximum sum of pairs. In this approach we will sort the array and initialize the array with 0 then traverse the array through FOR loop with increment of 2 and then add the a[i] to variable res and at last return the res. The maximum sum is: 9.

Input − arr[] = { 4,9,4,5,9,7}

Output − The maximum subarray sum is: 18

Explanation  We are given the array [ 4,9,4,5,9,7] with 2n numbers of integers. We have to make the pairs of integers like(a1,b1), (a2,b2)....(an,bn) which makes the sum on min(ai,bi) for all elements in the array as large as possible. The task is to find the maximum sum of pairs. In this approach we will sort the array then initialize the list res then traverse the array through FOR loop then append the arr[i] and arr[i+1] and return the sum of minimum in res array.

Approach used in the below program is as follows

  • First Approach

    • Input the array elements

    • Print the array.

    • Then call the arrayPairSum() by passing the array as an argument to print the maximum sum.

    • Inside the arrayPairSum().

    • Initialize the variable the res with 0.

    • Sort the array.

    • Then start the loop FOR from 0 till less than len(a)-1 with increment of 2.

    • Then add the a[i] to res.

    • Then return res.

  • Second Approach

    • Input the array elements.

    • Print the array.

    • Then call the arrayPairSum() by passing the array as an argument to print the maximum sum.

    • Inside the arrayPairSum().

    • Sort the array.

    • Initialize the list res.

    • Start the loop FOR from 0 till less than len(a) with increment with 2.

    • Append the pair of a[i] and a[i+1] to res.

    • Then return the sum of min(r) from the res list.

  • Third Approach

    • Input the array elements.

    • Print the array.

    • Then call the arrayPairSum() by passing the array as an argument to print the maximum sum.

    • Inside the arrayPairSum().

    • Sort the array.

    • Then initialize the variable l with len(a).

    • Initialize the variable sum, n and count with 0.

    • Then start the loop WHILE count

    • Then add the min of a[n] and a[n+1].

    • Then increment the n with 2.

    • Then increment the count with 1.

    • At last return the sum.

Example 1

class Solution(object): def arrayPairSum(self, a): res = 0 a = sorted(a) for i in range(0,len(a)-1,2): res += a[i] return res ob1 = Solution() arr = [90,23,76,21,38] print("The array is:",arr) print("The maximum sum is:",ob1.arrayPairSum(arr))

Output

If we run the above code it will generate the following output −

The array is: 90,23,76,21,38
The maximum sum is: 59

Example 2

class Solution(object): def arrayPairSum(self, a): a = sorted(a) res = [] for i in range(0, len(a), 2): res.append((a[i], a[i+1])) return sum([min(r) for r in res]) ob1 = Solution() arr = [17,18,16,19] print("The array is:",arr) print("The maximum sum is:",ob1.arrayPairSum(arr))

Output

If we run the above code it will generate the following output −

The array is: 17,18,16,19
The maximum sum is: 34

Example 3

class Solution(object): def arrayPairSum(self, a): a = sorted(a) l = len(a) sum = 0 n = 0 count = 0 while count < l/2: sum += min(a[n], a[n + 1]) n += 2 count += 1 return sum ob1 = Solution() arr = [41,93,43,56,79,87] print("The array is:",arr) print("The maximum sum is:",ob1.arrayPairSum(arr))

Output

If we run the above code it will generate the following output −

The array is: 41 93 43 56 79 87
The maximum sum is: 184

Updated on: 03-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements