Minimum change in given value so that it lies in all given Ranges
Given an array of ranges arr[] of length N and a number D, the task is to find the minimum amount by which the number D should be changed such that D lies in every range of given array. Here a range consists of two integer [start, end] and D is said to be inside of range, if start ? D ? end.
Examples:
Input: N = 3, D = 3
arr[] = {{0, 7}, {2, 14}, {4, 6}}
Output: 1
Explanation:
Here if we increment D by 1 then it will be inside of every range that is start ? D ? end.
Input: N = 2, D = 2
arr[] = {{1, 2}, {3, 2}}
Output: 0
Explanation:
Here D = 2 which is already inside of every range that is start ? D ? end.
Approach:
- Iterate over the array of ranges and to find the maximum value of the start[i] and the minimum value of the end[i] where the final max_start and min_end will show the common range of the given array of ranges.
- Check that D is already inside of the max_start and the min_end computed.
- If D is already inside of the range, then the minimum difference is 0.
- Else find the nearest value to D out of max_start and min_end that is
min(abs(D - max_start), abs(D - min_end))
Below is the code of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumOperation( int n, int d,
int arrays[3][2]){
int cnt = 0;
int first = INT_MIN, end = INT_MAX;
while (n--) {
int arr[2] = { arrays[cnt][0],
arrays[cnt][1] };
sort(arr, arr + 2);
first = max(first, arr[0]);
end = min(end, arr[1]);
cnt++;
}
if (first > end)
cout << "-1" ;
else {
if (d >= first && d <= end) {
cout << "0" ;
}
else
cout << min( abs (first - d),
abs (d - end));
}
}
int main()
{
int n = 3, d = 3;
int arrays[3][2] = {
{ 0, 7 },
{ 2, 14 },
{ 4, 6 }
};
findMinimumOperation(n, d, arrays);
}
|
Java
import java.util.*;
class GFG
{
static void findMinimumOperation( int n, int d,
int arrays[][]){
int cnt = 0 ;
int first = Integer.MIN_VALUE, end = Integer.MAX_VALUE;
while (n > 0 ) {
int arr[] = { arrays[cnt][ 0 ],
arrays[cnt][ 1 ] };
Arrays.sort(arr);
first = Math.max(first, arr[ 0 ]);
end = Math.min(end, arr[ 1 ]);
cnt++;
n--;
}
if (first > end)
System.out.print( "-1" );
else {
if (d >= first && d <= end) {
System.out.print( "0" );
}
else
System.out.print(Math.min(Math.abs(first - d),
Math.abs(d - end)));
}
}
public static void main (String []args)
{
int n = 3 , d = 3 ;
int arrays[][] = {
{ 0 , 7 },
{ 2 , 14 },
{ 4 , 6 }
};
findMinimumOperation(n, d, arrays);
}
}
|
Python3
def findMinimumOperation(n, d,arrays):
cnt = 0
first = - 10 * * 9
end = 10 * * 9
while (n):
arr = [arrays[cnt][ 0 ],arrays[cnt][ 1 ]]
arr = sorted (arr)
first = max (first, arr[ 0 ])
end = min (end, arr[ 1 ])
cnt + = 1
n - = 1
if (first > end):
print ( "-1" ,end = "")
else :
if (d > = first and d < = end):
print ( "0" ,end = "")
else :
print ( min ( abs (first - d), abs (d - end)),end = "")
if __name__ = = '__main__' :
n = 3
d = 3
arrays = [[ 0 , 7 ],
[ 2 , 14 ],
[ 4 , 6 ] ]
findMinimumOperation(n, d, arrays)
|
C#
using System;
class GFG
{
static void findMinimumOperation( int n, int d,
int [,]arrays){
int cnt = 0;
int first = int .MinValue, end = int .MaxValue;
while (n > 0) {
int []arr = { arrays[cnt, 0],
arrays[cnt, 1] };
Array.Sort(arr);
first = Math.Max(first, arr[0]);
end = Math.Min(end, arr[1]);
cnt++;
n--;
}
if (first > end)
Console.Write( "-1" );
else {
if (d >= first && d <= end) {
Console.Write( "0" );
}
else
Console.Write(Math.Min(Math.Abs(first - d),
Math.Abs(d - end)));
}
}
public static void Main(String []args)
{
int n = 3, d = 3;
int [,]arrays = {
{ 0, 7 },
{ 2, 14 },
{ 4, 6 }
};
findMinimumOperation(n, d, arrays);
}
}
|
Javascript
<script>
function findMinimumOperation(n, d, arrays)
{
var cnt = 0;
var first = Number.MIN_VALUE,
end = Number.MAX_VALUE;
while (n > 0)
{
var arr = [arrays[cnt][0], arrays[cnt][1]];
arr.sort((a, b) => a - b);
first = Math.max(first, arr[0]);
end = Math.min(end, arr[1]);
cnt++;
n--;
}
if (first > end)
document.write( "-1" );
else
{
if (d >= first && d <= end)
{
document.write( "0" );
}
else
document.write(Math.min(
Math.abs(first - d),
Math.abs(d - end)));
}
}
var n = 3, d = 3;
var arrays = [ [ 0, 7 ],
[ 2, 14 ],
[ 4, 6 ] ];
findMinimumOperation(n, d, arrays);
</script>
|
Time complexity: O((n^2)*logn) because sort function is being used in while loop
Auxiliary space: O(1)
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...