Sum of minimum difference between consecutive elements of an array
Last Updated :
26 Oct, 2021
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 chosen
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 chosen
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 than the highest value for the current index then in this case the least possible sum is the difference in the lowest value of the previous range and the highest value stored in 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++
#include <bits/stdc++.h>
using namespace std;
int solve(pair< int , int > v[], int n)
{
int ans, ul, ll;
ll = v[0].first;
ul = v[0].second;
ans = 0;
for ( int i = 1; i < n; i++) {
if ((v[i].first <= ul && v[i].first >= ll) ||
(v[i].second >= ll && v[i].second <= ul)) {
if (v[i].first > ll) {
ll = v[i].first;
}
if (v[i].second < ul) {
ul = v[i].second;
}
}
else if (v[i].first > ul) {
ans += abs (ul - v[i].first);
ul = v[i].first;
ll = v[i].first;
}
else if (v[i].second < ll) {
ans += abs (ll - v[i].second);
ul = v[i].second;
ll = v[i].second;
}
}
return ans;
}
int main()
{
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
import java.io.*;
class GFG
{
static int solve( int [][] v, int n)
{
int ans, ul, ll;
int first = 0 ;
int second = 1 ;
ll = v[ 0 ][first];
ul = v[ 0 ][second];
ans = 0 ;
for ( int i = 1 ; i < n; i++)
{
if ((v[i][first] <= ul &&
v[i][first] >= ll) ||
(v[i][second] >= ll &&
v[i][second] <= ul))
{
if (v[i][first] > ll)
{
ll = v[i][first];
}
if (v[i][second] < ul)
{
ul = v[i][second];
}
}
else if (v[i][first] > ul)
{
ans += Math.abs(ul - v[i][first]);
ul = v[i][first];
ll = v[i][first];
}
else if (v[i][second] < ll)
{
ans += Math.abs(ll - v[i][second]);
ul = v[i][second];
ll = v[i][second];
}
}
return ans;
}
public static void main(String []args)
{
int [][] v = {{ 1 , 3 }, { 2 , 5 },
{ 6 , 8 }, { 1 , 2 },
{ 2 , 3 }};
int n = 5 ;
System.out.println(solve(v, n));
}
}
|
Python
class pair:
first = 0
second = 0
def __init__( self , a, b):
self .first = a
self .second = b
def solve(v, n):
ans = 0 ; ul = 0 ; ll = 0 ;
ll = v[ 0 ].first
ul = v[ 0 ].second
ans = 0
for i in range ( 1 , n):
if (v[i].first < = ul and
v[i].first > = ll) or \
(v[i].second > = ll and
v[i].second < = ul):
if v[i].first > ll:
ll = v[i].first
if v[i].second < ul:
ul = v[i].second;
elif v[i].first > ul:
ans + = abs (ul - v[i].first)
ul = v[i].first
ll = v[i].first
elif v[i].second < ll:
ans + = abs (ll - v[i].second);
ul = v[i].second;
ll = v[i].second;
return ans
v = [pair( 1 , 3 ), pair( 2 , 5 ),
pair( 6 , 8 ), pair( 1 , 2 ),
pair( 2 , 3 ) ]
n = len (v)
print (solve(v, n))
|
C#
using System;
class GFG
{
static int solve( int [,] v, int n)
{
int ans, ul, ll;
int first = 0;
int second = 1;
ll = v[0, first];
ul = v[0, second];
ans = 0;
for ( int i = 1; i < n; i++)
{
if ((v[i, first] <= ul &&
v[i, first] >= ll) ||
(v[i, second] >= ll &&
v[i, second] <= ul))
{
if (v[i, first] > ll)
{
ll = v[i, first];
}
if (v[i, second] < ul)
{
ul = v[i, second];
}
}
else if (v[i, first] > ul)
{
ans += Math.Abs(ul - v[i, first]);
ul = v[i, first];
ll = v[i, first];
}
else if (v[i, second] < ll)
{
ans += Math.Abs(ll - v[i, second]);
ul = v[i, second];
ll = v[i, second];
}
}
return ans;
}
static void Main()
{
int [,] v = new int [5,2]{ { 1, 3 }, { 2, 5 },
{ 6, 8 }, { 1, 2 },
{ 2, 3 } };
int n = 5;
Console.WriteLine(solve(v, n));
}
}
|
PHP
<?php
function solve( $v , $n )
{
$ans ; $ul ; $ll ;
$first = 0;
$second = 1;
$ll = $v [0][ $first ];
$ul = $v [0][ $second ];
$ans = 0;
for ( $i = 1; $i < $n ; $i ++)
{
if (( $v [ $i ][ $first ] <= $ul and
$v [ $i ][ $first ] >= $ll ) or
( $v [ $i ][ $second ] >= $ll and
$v [ $i ][ $second ] <= $ul ))
{
if ( $v [ $i ][ $first ] > $ll )
{
$ll = $v [ $i ][ $first ];
}
if ( $v [ $i ][ $second ] < $ul )
{
$ul = $v [ $i ][ $second ];
}
}
else if ( $v [ $i ][ $first ] > $ul )
{
$ans += abs ( $ul - $v [ $i ][ $first ]);
$ul = $v [ $i ][ $first ];
$ll = $v [ $i ][ $first ];
}
else if ( $v [ $i ][ $second ] < $ll )
{
$ans += abs ( $ll - $v [ $i ][ $second ]);
$ul = $v [ $i ][ $second ];
$ll = $v [ $i ][ $second ];
}
}
return $ans ;
}
$v = array ( array ( 1, 3 ),
array ( 2, 5 ),
array ( 6, 8 ),
array ( 1, 2 ),
array ( 2, 3 ));
$n = 5;
echo (solve( $v , $n ));
?>
|
Javascript
<script>
function solve(v, n)
{
let ans, ul, ll;
let first = 0;
let second = 1;
ll = v[0][first];
ul = v[0][second];
ans = 0;
for (let i = 1; i < n; i++)
{
if ((v[i][first] <= ul &&
v[i][first] >= ll) ||
(v[i][second] >= ll &&
v[i][second] <= ul))
{
if (v[i][first] > ll)
{
ll = v[i][first];
}
if (v[i][second] < ul)
{
ul = v[i][second];
}
}
else if (v[i][first] > ul)
{
ans += Math.abs(ul - v[i][first]);
ul = v[i][first];
ll = v[i][first];
}
else if (v[i][second] < ll)
{
ans += Math.abs(ll - v[i][second]);
ul = v[i][second];
ll = v[i][second];
}
}
return ans;
}
let v = [[ 1, 3 ], [ 2, 5 ],
[ 6, 8 ], [ 1, 2 ],
[ 2, 3 ]];
let n = 5;
document.write(solve(v, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...