Posts

Showing posts from June, 2022

Java Code to Partition an Array and return the maximized Sum of Pairs

Image
  Given an integer array  nums  of  2n  integers, group these integers into  n  pairs  (a 1 , b 1 ), (a 2 , b 2 ), ..., (a n , b n )  such that the sum of  min(a i , b i )  for all  i  is  maximized . Return  the maximized sum This is a leetcode problem , which we will try to solve today.  On analyzing the problem we can understand that we need to split or partition the array into pairs of 2, and then take the minimum value from a pair, such that the sum of these minimum values is maximum.  Ideally, the value can be maximum only if we are taking bigger numbers, but we have to take minimum values from a pair of Integers. So, to work around this problem, we will sort the array first. This way, when we make pairs, we can ensure that the minimum value of a pair can come out to be a bigger number. We will then take the sum and return the same.  /**   * Group an array of 2n integers , into n pairs of two such that sum of min(ai , bi) for all i is maximized   * @param nums   * @retur