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 = 3Input: 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++
// C++ program to print the sum of the minimum pair #include <bits/stdc++.h> using namespace std; // Function to return the sum of // the minimum pair from the array int smallest_pair(int a[], int n) { int min = INT_MAX, secondMin = INT_MAX; for (int j = 0; j < n; j++) { // If found new minimum if (a[j] < min) { // Minimum now becomes second minimum secondMin = min; // Update minimum min = a[j]; } // If current element is > min and < secondMin else if ((a[j] < secondMin) && a[j] != min) // Update secondMin secondMin = a[j]; } // Return the sum of the minimum pair return (secondMin + min); } // Driver code int main() { int arr[] = { 1, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); cout << smallest_pair(arr, n); return 0; } |
chevron_right
filter_none
Java
// Java program to print the sum // of the minimum pair import java .io.*; class GFG { // Function to return the sum of // the minimum pair from the array 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 found new minimum if (a[j] < min) { // Minimum now becomes second minimum secondMin = min; // Update minimum min = a[j]; } // If current element is > min and < secondMin else if ((a[j] < secondMin) && a[j] != min) // Update secondMin secondMin = a[j]; } // Return the sum of the minimum pair return (secondMin + min); } // Driver code public static void main(String[] args) { int[] arr = { 1, 2, 3 }; int n = arr.length; System.out.println(smallest_pair(arr, n)); } } // This code is contributed // by inder_verma |
chevron_right
filter_none
Python3
# Python3 program to print the # sum of the minimum pair import sys # Function to return the sum of # the minimum pair from the array def smallest_pair(a, n) : min = sys.maxsize secondMin = sys.maxsize for j in range(n) : # If found new minimum if (a[j] < min) : # Minimum now becomes # second minimum secondMin = min # Update minimum min = a[j] # If current element is > min # and < secondMin elif ((a[j] < secondMin) and a[j] != min) : # Update secondMin secondMin = a[j] # Return the sum of the minimum pair return (secondMin + min) # Driver code if __name__ == "__main__" : arr = [ 1, 2, 3 ] n = len(arr) print(smallest_pair(arr, n)) # This code is contributed by Ryuga |
chevron_right
filter_none
C#
// C# program to print the sum // of the minimum pair using System; class GFG { // Function to return the sum of // the minimum pair from the array static int smallest_pair(int[] a, int n) { int min = int.MaxValue, secondMin = int.MaxValue; for (int j = 0; j < n; j++) { // If found new minimum if (a[j] < min) { // Minimum now becomes second minimum secondMin = min; // Update minimum min = a[j]; } // If current element is > min and < secondMin else if ((a[j] < secondMin) && a[j] != min) // Update secondMin secondMin = a[j]; } // Return the sum of the minimum pair return (secondMin + min); } // Driver code public static void Main() { int[] arr = { 1, 2, 3 }; int n = arr.Length; Console.Write(smallest_pair(arr, n)); } } // This code is contributed // by Akanksha Rai |
chevron_right
filter_none
PHP
<?php // PHP program to print the sum // of the minimum pair // Function to return the sum of // the minimum pair from the array function smallest_pair($a, $n) { $min = PHP_INT_MAX; $secondMin = PHP_INT_MAX; for ($j = 0; $j < $n; $j++) { // If found new minimum if ($a[$j] < $min) { // Minimum now becomes // second minimum $secondMin = $min; // Update minimum $min = $a[$j]; } // If current element is > min // and < secondMin else if (($a[$j] < $secondMin) && $a[$j] != $min) // Update secondMin $secondMin = $a[$j]; } // Return the sum of the minimum pair return ($secondMin + $min); } // Driver code $arr = array( 1, 2, 3 ); $n = sizeof($arr); echo smallest_pair($arr, $n); // This code is contributed by ajit ?> |
chevron_right
filter_none
Output:
3
Recommended Posts:
- Smallest Difference pair of values between two unsorted Arrays
- Find the smallest and second smallest elements in an array
- Maximum sum of smallest and second smallest in an array
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- XOR of Sum of every possible pair of an array
- Maximum AND value of a pair in an array
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Closest product pair in an array
- Print pair with maximum AND value in an array
- Find the largest pair sum in an unsorted array
- Find pair with greatest product in array
- Given a sorted and rotated array, find if there is a pair with a given sum
- Check if product of every pair exists in an array
- Find a pair with maximum product in array of Integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



