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++
//C++ program to find the smallest// sum contiguous pair#include<bits/stdc++.h>using namespace std; // Function to find the smallest sum// contiguous pairvector<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 codeint 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 |
Python3
# Python program to find the smallest# sum contiguous pair# importing sysimport sys# Function to find the smallest sum# contiguous pairdef 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 codearr = [4, 9, -3, 2, 0]n = len(arr)pair = smallestSumpair(arr, n)print(pair[0], pair[1]) |
-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.
Recommended Posts:
- Program to find whether a no is power of two
- Program to find parity
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- A Product Array Puzzle
- Find Union and Intersection of two unsorted arrays
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
- Shuffle a given array using Fisher–Yates shuffle Algorithm
- Find the largest multiple of 2, 3 and 5
- Program to find amount of water in a given glass
- Print all possible combinations of r elements in a given array of size n
- Find the maximum distance covered using n bikes
- Find if two rectangles overlap
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
- Find the smallest number whose digits multiply to a given number n
- Find next greater number with same set of digits
- Find n'th number in a number system with only 3 and 4
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

