Smallest Pair Sum in an array Last Updated : 16 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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++ // 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) // 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; } 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 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 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 JavaScript <script> // Javascript 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) { let min = Number.MAX_VALUE, secondMin = Number.MAX_VALUE; for (let 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); } let arr = [ 1, 2, 3 ]; let n = arr.length; document.write(smallest_pair(arr, n)); </script> 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 ?> Output3Complexity Analysis: Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Smallest Pair Sum in an array D Devyansh_Ojha Follow Improve Article Tags : Algorithms Competitive Programming C++ Programs C++ DSA +1 More Practice Tags : CPPAlgorithms Similar Reads Sum of minimum elements of all possible sub-arrays of an array Given an array arr[], the task is to find the sum of the minimum elements of every possible sub-array of the array. Examples: Input: arr[] = {1, 3, 2} Output: 15 All possible sub-arrays are {1}, {2}, {3}, {1, 3}, {3, 2} and {1, 3, 2} And, the sum of all the minimum elements is 1 + 2 + 3 + 1 + 2 + 1 9 min read Minimum possible sum of array elements after performing the given operation Given an array arr[] of positive integers and an integer x, the task is to minimize the sum of elements of the array after performing the given operation at most once. In a single operation, any element from the array can be divided by x (if it is divisible by x) and at the same time, any other elem 8 min read Partitioning into two contiguous element subarrays with equal sums Given an array of n positive integers. Find a minimum positive element to be added to one of the indexes in the array such that it can be partitioned into two contiguous sub-arrays of equal sums. Output the minimum element to be added and the position where it is to be added. If multiple positions a 10 min read C++ Program to Find the subarray with least average Given an array arr[] of size n and integer k such that k Examples :Â Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3 Output: Subarray between indexes 3 and 5 The subarray {20, 10, 50} has the least average among all subarrays of size 3. Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2 Output: Subarr 3 min read Minimum count of numbers required from given array to represent S Given an integer S and an array arr[], the task is to find the minimum number of elements whose sum is S, such that any element of the array can be chosen any number of times to get sum S. Examples: Input: arr[] = {25, 10, 5}, S = 30 Output: 2 Explanation: In the given array there are many possible 15+ min read Like