Find the smallest contiguous sum pair in an Array

Given an array arr[] containing N distinct integers, the task is to find a contiguous pair such that sum of both elements in pair is minimum.
Examples:
 

Input: arr[] = {1, 2, 3, 4} 
Output: (1, 2) 
Explanation: 
Here, contiguous pairs with their sum are (1, 2) = 3, (2, 3) = 5, (3, 4) = 7 and minimum is 3.
Input: arr[] = {4, 9, -3, 2, 0} 
Output: (-3, 2) 
Explanation: 
Here, contiguous pairs with their sum are (4, 9) = 13, (9, -3) = 6, (-3, 2) = -1, (2, 0) = 2 
 

Approach: 
To solve the problem mentioned above we have to consider all the contiguous pairs and find their sum. The pair having the smallest(minimum) sum is the required answer.
Below is the implementation of above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

//C++ program to find the smallest
// sum contiguous pair
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the smallest sum
// contiguous pair
vector<int> smallestSumpair(int arr[], int n)
{
 
    // Contiguous pair
    vector<int>pair;
 
    // isntialize minimum sum
    // with maximum value
    int min_sum = INT_MAX;
 
    for(int i = 1; i < n; i++)
    {
 
        // Checking for minimum value
        if( min_sum > (arr[i] + arr[i - 1]))
        {
            min_sum = arr[i] + arr[i - 1];
            if (pair.empty())
            {
 
                // Add to pair
                pair.push_back(arr[i - 1]);
                pair.push_back(arr[i]);
            }
            else
            {
 
                // Updating pair
                pair[0] = arr[i - 1];
                pair[1] = arr[i];
            }
        }
    }
    return pair;
}
  
// Driver code
int main()
{
   int arr[] = {4, 9, -3, 2, 0};
   int n = sizeof(arr) / sizeof(arr[0]);
   
   vector<int>pair = smallestSumpair(arr, n);
   cout << pair[0] << " " << pair[1];
}
 
// This code is contributed by chitranayal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the smallest
# sum contiguous pair
 
# importing sys
import sys
 
# Function to find the smallest sum
# contiguous pair
 
 
def smallestSumpair(arr, n):
 
    # Contiguous pair
    pair = []
 
    # isntialize minimum sum
    # with maximum value
    min_sum = sys.maxsize
 
    for i in range(1, n):
 
        # checking for minimum value
        if min_sum > (arr[i] + arr[i-1]):
            min_sum = arr[i] + arr[i-1]
 
            if pair == []:
 
                # Add to pair
                pair.append(arr[i-1])
                pair.append(arr[i])
            else:
 
                # Updating pair
                pair[0] = arr[i-1]
                pair[1] = arr[i]
 
    return pair
 
 
# Driver code
arr = [4, 9, -3, 2, 0]
n = len(arr)
pair = smallestSumpair(arr, n)
print(pair[0], pair[1])

chevron_right


Output

-3 2



Time Complexity: O(n)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : chitranayal

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.