Smallest Pair Sum in an array
Given an array of distinct integers arr[], the task is to find a pair which has the minimum sum and print the sum.
Examples:
Input: arr[] = {1, 2, 3}
Output: 3
The pair (1, 2) will have the minimum sum pair i.e. 1 + 2 = 3
Input: arr[] = {3, 5, 6, 2}
Output: 5
Approach:
- Find the minimum element from the array and store it in min.
- Find the second minimum element from the array and store it in secondMin.
- Print min + secondMin.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallest_pair( int a[], int n)
{
int min = INT_MAX, secondMin = INT_MAX;
for ( int j = 0; j < n; j++) {
if (a[j] < min) {
secondMin = min;
min = a[j];
}
else if ((a[j] < secondMin) && a[j] != min)
secondMin = a[j];
}
return (secondMin + min);
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << smallest_pair(arr, n);
return 0;
}
|
Java
import java .io.*;
class GFG
{
static int smallest_pair( int [] a, int n)
{
int min = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE;
for ( int j = 0 ; j < n; j++)
{
if (a[j] < min)
{
secondMin = min;
min = a[j];
}
else if ((a[j] < secondMin) && a[j] != min)
secondMin = a[j];
}
return (secondMin + min);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
int n = arr.length;
System.out.println(smallest_pair(arr, n));
}
}
|
Python3
import sys
def smallest_pair(a, n) :
min = sys.maxsize
secondMin = sys.maxsize
for j in range (n) :
if (a[j] < min ) :
secondMin = min
min = a[j]
elif ((a[j] < secondMin) and
a[j] ! = min ) :
secondMin = a[j]
return (secondMin + min )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 ]
n = len (arr)
print (smallest_pair(arr, n))
|
C#
using System;
class GFG
{
static int smallest_pair( int [] a, int n)
{
int min = int .MaxValue, secondMin = int .MaxValue;
for ( int j = 0; j < n; j++)
{
if (a[j] < min)
{
secondMin = min;
min = a[j];
}
else if ((a[j] < secondMin) && a[j] != min)
secondMin = a[j];
}
return (secondMin + min);
}
public static void Main()
{
int [] arr = { 1, 2, 3 };
int n = arr.Length;
Console.Write(smallest_pair(arr, n));
}
}
|
PHP
<?php
function smallest_pair( $a , $n )
{
$min = PHP_INT_MAX;
$secondMin = PHP_INT_MAX;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $a [ $j ] < $min )
{
$secondMin = $min ;
$min = $a [ $j ];
}
else if (( $a [ $j ] < $secondMin ) &&
$a [ $j ] != $min )
$secondMin = $a [ $j ];
}
return ( $secondMin + $min );
}
$arr = array ( 1, 2, 3 );
$n = sizeof( $arr );
echo smallest_pair( $arr , $n );
?>
|
Javascript
<script>
function smallest_pair(a, n)
{
let min = Number.MAX_VALUE,
secondMin = Number.MAX_VALUE;
for (let j = 0; j < n; j++)
{
if (a[j] < min)
{
secondMin = min;
min = a[j];
}
else if ((a[j] < secondMin) &&
a[j] != min)
secondMin = a[j];
}
return (secondMin + min);
}
let arr = [ 1, 2, 3 ];
let n = arr.length;
document.write(smallest_pair(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Last Updated :
12 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...