Given an array of pairs where each pair represents a range, the task is to find the sum of the minimum difference between the consecutive elements of an array where the array is filled in the below manner:
- Each element of an array lies in the range given at its corresponding index in the range array.
- Final sum of difference of consecutive elements in the array formed is minimum.
Examples:
Input: range[] = {{2, 4}, {3, 6}, {1, 5}, {1, 3}, {2, 7}}
Output: 0
The result is 0 because the array {3, 3, 3, 3, 3} is choosen
then the sum of difference of consecutive element will be
{ |3-3| + |3-3| + |3-3| + |3-3| } = 0 which the minimum.Input: range[] = {{1, 3}, {2, 5}, {6, 8}, {1, 2}, {2, 3}}
Output: 7
The result is 7 because if the array {3, 3, 6, 2, 2} is choosen
then the sum of difference of consecutive element will be
{ |3-3| + |6-3| + |2-6| + |2-2| }= 7 which is the minimum.
Approach: A greedy approach has been followed to solve the above problem. Initially we have to fill the array in such a way that the sum of the difference obtained is minimum. The greedy approach is as follows:
- If the range of the previous index intersects the range of current index then, in this case, the minimum difference will be 0 and store the highest value and the lowest value of intersecting range.
- If the lowest value of the range of the previous index is greater then the highest value for the current index than in this case the least possible sum is the difference in the lowest value of the previous range and the highest value stored of the current range.
- If the highest range of the previous index is lower than the lowest value of current range then the minimum sum is the difference in the lowest value stored for the current index and the highest range of the previous index.
Below is the implementation of the above approach:
C++
// C++ program for finding the minimum sum of // difference between consecutive elements #include <bits/stdc++.h> using namespace std; // function to find minimum sum of // difference of consecutive element int solve(pair<int, int> v[], int n) { // ul to store upper limit // ll to store lower limit int ans, ul, ll; // storethe lower range in ll // and upper range in ul ll = v[0].first; ul = v[0].second; // inititalize the answer with 0 ans = 0; // iterate for all ranges for (int i = 1; i < n; i++) { // case 1, in this case the difference will be 0 if ((v[i].first <= ul && v[i].first >= ll) || (v[i].second >= ll && v[i].second <= ul)) { // change upper limit and lower limit if (v[i].first > ll) { ll = v[i].first; } if (v[i].second < ul) { ul = v[i].second; } } // case 2 else if (v[i].first > ul) { // store the difference ans += abs(ul - v[i].first); ul = v[i].first; ll = v[i].first; } // case 3 else if (v[i].second < ll) { // store the difference ans += abs(ll - v[i].second); ul = v[i].second; ll = v[i].second; } } return ans; } // Driver code int main() { // array of range pair<int, int> v[] = { { 1, 3 }, { 2, 5 }, { 6, 8 }, { 1, 2 }, { 2, 3 } }; int n = sizeof(v) / sizeof(v[0]); cout << solve(v, n) << endl; return 0; } |
Java
// Java program for finding the // minimum sum of difference // between consecutive elements import java.io.*; class GFG { // function to find minimum // sum of difference of // consecutive element static int solve(int[][] v, int n) { // ul to store upper limit // ll to store lower limit int ans, ul, ll; int first = 0; int second = 1; // storethe lower range // in ll and upper range // in ul ll = v[0][first]; ul = v[0][second]; // inititalize the // answer with 0 ans = 0; // iterate for all ranges for (int i = 1; i < n; i++) { // case 1, in this case // the difference will be 0 if ((v[i][first] <= ul && v[i][first] >= ll) || (v[i][second] >= ll && v[i][second] <= ul)) { // change upper limit // and lower limit if (v[i][first] > ll) { ll = v[i][first]; } if (v[i][second] < ul) { ul = v[i][second]; } } // case 2 else if (v[i][first] > ul) { // store the difference ans += Math.abs(ul - v[i][first]); ul = v[i][first]; ll = v[i][first]; } // case 3 else if (v[i][second] < ll) { // store the difference ans += Math.abs(ll - v[i][second]); ul = v[i][second]; ll = v[i][second]; } } return ans; } // Driver code public static void main(String []args) { // array of range int[][] v = {{ 1, 3 }, { 2, 5 }, { 6, 8 }, { 1, 2 }, { 2, 3 }}; int n = 5; System.out.println(solve(v, n)); } } // This code is contributed // by chandan_jnu |
Python
# Python program for finding # the minimum sum of difference # between consecutive elements class pair: first = 0 second = 0 def __init__(self, a, b): self.first = a self.second = b # function to find minimum # sum of difference of # consecutive element def solve(v, n): # ul to store upper limit # ll to store lower limit ans = 0; ul = 0; ll = 0; # storethe lower range # in ll and upper range # in ul ll = v[0].first ul = v[0].second # inititalize the # answer with 0 ans = 0 # iterate for all ranges for i in range(1, n): # case 1, in this case # the difference will be 0 if (v[i].first <= ul and v[i].first >= ll) or \ (v[i].second >= ll and v[i].second <= ul): # change upper limit # and lower limit if v[i].first > ll: ll = v[i].first if v[i].second < ul: ul = v[i].second; # case 2 elif v[i].first > ul: # store the difference ans += abs(ul - v[i].first) ul = v[i].first ll = v[i].first # case 3 elif v[i].second < ll: # store the difference ans += abs(ll - v[i].second); ul = v[i].second; ll = v[i].second; return ans # Driver code # array of range v = [pair(1, 3), pair(2, 5), pair(6, 8), pair(1, 2), pair(2, 3) ] n = len(v) print(solve(v, n)) # This code is contributed # by Harshit Saini |
C#
// C# program for finding the // minimum sum of difference // between consecutive elements using System; class GFG { // function to find minimum // sum of difference of // consecutive element static int solve(int[,] v, int n) { // ul to store upper limit // ll to store lower limit int ans, ul, ll; int first = 0; int second = 1; // storethe lower range // in ll and upper range // in ul ll = v[0, first]; ul = v[0, second]; // inititalize the // answer with 0 ans = 0; // iterate for all ranges for (int i = 1; i < n; i++) { // case 1, in this case // the difference will be 0 if ((v[i, first] <= ul && v[i, first] >= ll) || (v[i, second] >= ll && v[i, second] <= ul)) { // change upper limit // and lower limit if (v[i, first] > ll) { ll = v[i, first]; } if (v[i, second] < ul) { ul = v[i, second]; } } // case 2 else if (v[i, first] > ul) { // store the difference ans += Math.Abs(ul - v[i, first]); ul = v[i, first]; ll = v[i, first]; } // case 3 else if (v[i, second] < ll) { // store the difference ans += Math.Abs(ll - v[i, second]); ul = v[i, second]; ll = v[i, second]; } } return ans; } // Driver code static void Main() { // array of range int[,] v = new int[5,2]{ { 1, 3 }, { 2, 5 }, { 6, 8 }, { 1, 2 }, { 2, 3 } }; int n = 5; Console.WriteLine(solve(v, n)); } } // This code is contributed // by chandan_jnu |
PHP
<?php // PHP program for finding the // minimum sum of difference // between consecutive elements // function to find minimum // sum of difference of // consecutive element function solve($v, $n) { // ul to store upper limit // ll to store lower limit $ans; $ul; $ll; $first = 0; $second = 1; // storethe lower range // in ll and upper range // in ul $ll = $v[0][$first]; $ul = $v[0][$second]; // inititalize the // answer with 0 $ans = 0; // iterate for all ranges for ($i = 1; $i <$n; $i++) { // case 1, in this case // the difference will be 0 if (($v[$i][$first] <= $ul and $v[$i][$first] >= $ll) or ($v[$i][$second] >= $ll and $v[$i][$second] <= $ul)) { // change upper limit // and lower limit if ($v[$i][$first] > $ll) { $ll = $v[$i][$first]; } if ($v[$i][$second] < $ul) { $ul = $v[$i][$second]; } } // case 2 else if ($v[$i][$first] > $ul) { // store the difference $ans += abs($ul - $v[$i][$first]); $ul = $v[$i][$first]; $ll = $v[$i][$first]; } // case 3 else if ($v[$i][$second] < $ll) { // store the difference $ans += abs($ll - $v[$i][$second]); $ul = $v[$i][$second]; $ll = $v[$i][$second]; } } return $ans; } // Driver code // array of range $v = array(array( 1, 3 ), array( 2, 5 ), array( 6, 8 ), array( 1, 2 ), array( 2, 3 )); $n = 5; echo(solve($v, $n)); // This code is contributed // by chandan_jnu ?> |
7
Time Complexity: O(N)
Auxiliary Space: O(1)
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:
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Find elements of array using XOR of consecutive elements
- Minimum array insertions required to make consecutive difference <= K
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Minimum number of consecutive sequences that can be formed in an array
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximize sum of consecutive differences in a circular array
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Minimize the difference between minimum and maximum elements
- Maximize the maximum among minimum of K consecutive sub-arrays
- Minimum bit flips such that every K consecutive bits contain at least one set bit
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.

