Number of pairs with maximum sum
Last Updated :
25 Jul, 2022
Given an array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.
Example :
Input : arr[] = {1, 1, 1, 2, 2, 2}
Output : 3
Explanation: The maximum possible pair
sum where i<j is 4, which is given
by 3 pairs, so the answer is 3
the pairs are (2, 2), (2, 2) and (2, 2)
Input : arr[] = {1, 4, 3, 3, 5, 1}
Output : 1
Explanation: The pair 4, 5 yields the
maximum sum i.e, 9 which is given by 1 pair only
Method 1 (Naive): Traverse a loop i from 0 to n, i.e length of the array and another loop j from i+1 to n to find all possible pairs with i<j. Find the pair with the maximum possible sum, again traverse for all pairs and keep the count of the number of pairs which gives the pair sum equal to maximum
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int a[], int n)
{
int maxSum = INT_MIN;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
maxSum = max(maxSum, a[i] + a[j]);
int c = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
if (a[i] + a[j] == maxSum)
c++;
return c;
}
int main()
{
int array[] = { 1, 1, 1, 2, 2, 2 };
int n = sizeof (array) / sizeof (array[0]);
cout << sum(array, n);
return 0;
}
|
Java
class GFG {
static int sum( int a[], int n)
{
int maxSum = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
maxSum = Math.max(maxSum, a[i] +
a[j]);
int c = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
if (a[i] + a[j] == maxSum)
c++;
return c;
}
public static void main(String[] args)
{
int array[] = { 1 , 1 , 1 , 2 , 2 , 2 };
int n = array.length;
System.out.println(sum(array, n));
}
}
|
Python3
def _sum( a, n):
maxSum = - 9999999
for i in range (n):
for j in range (n):
maxSum = max (maxSum, a[i] + a[j])
c = 0
for i in range (n):
for j in range (i + 1 , n):
if a[i] + a[j] = = maxSum:
c + = 1
return c
array = [ 1 , 1 , 1 , 2 , 2 , 2 ]
n = len (array)
print (_sum(array, n))
|
C#
using System;
class GFG {
static int sum( int []a, int n)
{
int maxSum = int .MinValue;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
maxSum = Math.Max(maxSum,
a[i] + a[j]);
int c = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
if (a[i] + a[j] == maxSum)
c++;
return c;
}
public static void Main()
{
int []array = { 1, 1, 1, 2, 2, 2 };
int n = array.Length;
Console.WriteLine(sum(array, n));
}
}
|
PHP
<?php
function sum( $a , $n )
{
$maxSum = PHP_INT_MIN;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
$maxSum = max( $maxSum , $a [ $i ] + $a [ $j ]);
$c = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
if ( $a [ $i ] + $a [ $j ] == $maxSum )
$c ++;
return $c ;
}
$array = array (1, 1, 1, 2, 2, 2);
$n = count ( $array );
echo sum( $array , $n );
?>
|
Javascript
<script>
function sum(a, n)
{
let maxSum = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
maxSum = Math.max(maxSum,
a[i] + a[j]);
let c = 0;
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
if (a[i] + a[j] == maxSum)
c++;
return c;
}
let array = [ 1, 1, 1, 2, 2, 2 ];
let n = array.length;
document.write(sum(array, n));
</script>
|
Output :
3
Time complexity: O(n2)
Auxiliary Space: O(1)
Method 2 (Efficient):
If we take a closer look, we can notice following facts.
- Maximum element is always part of solution
- If maximum element appears more than once, then result is maxCount * (maxCount – 1)/2. We basically need to choose 2 elements from maxCount (maxCountC2).
- If maximum element appears once, then result is equal to count of second maximum element. We can form a pair with every second max and max
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int a[], int n)
{
int maxVal = a[0], maxCount = 1;
int secondMax = INT_MIN, secondMaxCount;
for ( int i = 1; i < n; i++) {
if (a[i] == maxVal)
maxCount++;
else if (a[i] > maxVal) {
secondMax = maxVal;
secondMaxCount = maxCount;
maxVal = a[i];
maxCount = 1;
}
else if (a[i] == secondMax) {
secondMax = a[i];
secondMaxCount++;
}
else if (a[i] > secondMax) {
secondMax = a[i];
secondMaxCount = 1;
}
}
if (maxCount > 1)
return maxCount * (maxCount - 1) / 2;
return secondMaxCount;
}
int main()
{
int array[] = { 1, 1, 1, 2, 2, 2, 3 };
int n = sizeof (array) / sizeof (array[0]);
cout << sum(array, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sum( int a[], int n)
{
int maxVal = a[ 0 ], maxCount = 1 ;
int secondMax = Integer.MIN_VALUE,
secondMaxCount = 0 ;
for ( int i = 1 ; i < n; i++) {
if (a[i] == maxVal)
maxCount++;
else if (a[i] > maxVal) {
secondMax = maxVal;
secondMaxCount = maxCount;
maxVal = a[i];
maxCount = 1 ;
}
else if (a[i] == secondMax) {
secondMax = a[i];
secondMaxCount++;
}
else if (a[i] > secondMax) {
secondMax = a[i];
secondMaxCount = 1 ;
}
}
if (maxCount > 1 )
return maxCount * (maxCount - 1 ) / 2 ;
return secondMaxCount;
}
public static void main(String[] args)
{
int array[] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 };
int n = array.length;
System.out.println(sum(array, n));
}
}
|
Python3
import sys
def sum (a, n):
maxVal = a[ 0 ]; maxCount = 1
secondMax = sys.maxsize
for i in range ( 1 , n) :
if (a[i] = = maxVal) :
maxCount + = 1
elif (a[i] > maxVal) :
secondMax = maxVal
secondMaxCount = maxCount
maxVal = a[i]
maxCount = 1
elif (a[i] = = secondMax) :
secondMax = a[i]
secondMaxCount + = 1
elif (a[i] > secondMax) :
secondMax = a[i]
secondMaxCount = 1
if (maxCount > 1 ):
return maxCount * (maxCount - 1 ) / 2
return secondMaxCount
array = [ 1 , 1 , 1 , 2 , 2 , 2 , 3 ]
n = len (array)
print ( sum (array, n))
|
C#
using System;
class GFG {
static int sum( int []a, int n)
{
int maxVal = a[0], maxCount = 1;
int secondMax = int .MinValue;
int secondMaxCount = 0;
for ( int i = 1; i < n; i++)
{
if (a[i] == maxVal)
maxCount++;
else if (a[i] > maxVal)
{
secondMax = maxVal;
secondMaxCount = maxCount;
maxVal = a[i];
maxCount = 1;
}
else if (a[i] == secondMax)
{
secondMax = a[i];
secondMaxCount++;
}
else if (a[i] > secondMax)
{
secondMax = a[i];
secondMaxCount = 1;
}
}
if (maxCount > 1)
return maxCount *
(maxCount - 1) / 2;
return secondMaxCount;
}
public static void Main()
{
int []array = { 1, 1, 1, 2,
2, 2, 3 };
int n = array.Length;
Console.WriteLine(sum(array, n));
}
}
|
PHP
<?php
function sum( $a , $n )
{
$maxVal = $a [0]; $maxCount = 1;
$secondMax = PHP_INT_MIN;
$secondMaxCount ;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] == $maxVal )
$maxCount ++;
else if ( $a [ $i ] > $maxVal )
{
$secondMax = $maxVal ;
$secondMaxCount = $maxCount ;
$maxVal = $a [ $i ];
$maxCount = 1;
}
else if ( $a [ $i ] == $secondMax )
{
$secondMax = $a [ $i ];
$secondMaxCount ++;
}
else if ( $a [ $i ] > $secondMax )
{
$secondMax = $a [ $i ];
$secondMaxCount = 1;
}
}
if ( $maxCount > 1)
return $maxCount *
( $maxCount - 1) / 2;
return $secondMaxCount ;
}
$array = array (1, 1, 1, 2,
2, 2, 3 );
$n = count ( $array );
echo sum( $array , $n );
?>
|
Javascript
<script>
function sum(a, n)
{
let maxVal = a[0], maxCount = 1;
let secondMax = Number.MIN_VALUE;
let secondMaxCount = 0;
for (let i = 1; i < n; i++)
{
if (a[i] == maxVal)
maxCount++;
else if (a[i] > maxVal)
{
secondMax = maxVal;
secondMaxCount = maxCount;
maxVal = a[i];
maxCount = 1;
}
else if (a[i] == secondMax)
{
secondMax = a[i];
secondMaxCount++;
}
else if (a[i] > secondMax)
{
secondMax = a[i];
secondMaxCount = 1;
}
}
if (maxCount > 1)
return maxCount * parseInt((maxCount - 1) / 2, 10);
return secondMaxCount;
}
let array = [ 1, 1, 1, 2, 2, 2, 3 ];
let n = array.length;
document.write(sum(array, n));
</script>
|
Output :
3
Time complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...