Minimum K to make H zero
Last Updated :
14 Aug, 2023
Given integer H and array A[] of size N. A[] is a sorted array representing the start of the operation, start subtracting 1 from H for every second from A[i] to A[i] + K, and the task for this problem is to find minimum K to make H zero.
Note: For every second you can subtract 1 from H only once
Examples:
Input: H = 5, A[] = {1, 5}
Output: 3
Explanation: For K = 3 , subtraction is done in seconds [1, 2, 3, 5, 6, 7]
- for A[0] = 1, subtraction will be done in {1, 2, 3}
- for A[1] = 5, subtraction will be done in {5, 6, 7}
Input: H = 10, A[] = {2, 4, 10};
Output: 4
Explanation: For K = 4, subtraction is done in seconds [2, 3, 4, 5, 6, 7, 10, 11, 12, 13]
- for A[0] = 2, subtraction will be done in {2, 3, 4, 5}
- for A[1] = 4, subtraction will be done in {4, 5, 6, 7}
- for A[2] = 10, subtraction will be done in {10, 11, 12, 13}
in A[0] and A[1], 4 and 5 are common while performing operations subtract 1 from H only once for time 4 and 5 (read note of this problem)
Approach: To solve the problem follow the below idea:
Binary Search can be used to solve this problem. Monotonic function is for different K value of H is monotonic. We have monotonic function FFFFTTTTT for different K. problem require us to find minimum K value for which function is true for the first time.
Below are the steps for the above approach:
- Create a test function that takes parameters as mid, A[], N, and H.
- Create a CNT = 0 variable to store the maximum number that can be subtracted from H.
- Iterate from 0 to N – 1 and if the difference of consecutive elements is less than equal to mid then add the difference of consecutive elements to CNT else add mid to CNT.
- Finally, add mid to CNT and return CNT >= H.
- Create low = 0 and high = 1e9 and mid variables.
- Run while loop until high – low > 1
- set mid = (low + high) / 2.
- if the test function for this mid is true set high = mid.
- else set low = mid + 1.
- Finally, if the test function for low is true return low else return high.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool test( int mid, int A[], int N, int H)
{
int cnt = 0;
for ( int i = 0; i < N - 1; i++) {
if (A[i + 1] - A[i] <= mid)
cnt += A[i + 1] - A[i];
else
cnt += mid;
}
cnt += mid;
return cnt >= H;
};
int minimizeOperations( int A[], int N, int H)
{
int low = 1, high = 1e9, mid;
while (high - low > 1) {
mid = (low + high) / 2;
if (test(mid, A, N, H)) {
high = mid;
}
else {
low = mid + 1;
}
}
if (test(low, A, N, H))
return low;
else
return high;
}
int32_t main()
{
int N = 2, H = 5;
int A[] = { 1, 5 };
cout << minimizeOperations(A, N, H) << endl;
int N1 = 3, H1 = 10;
int A1[] = { 2, 4, 10 };
cout << minimizeOperations(A1, N1, H1) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean test( int mid, int [] A, int N, int H)
{
int cnt = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
if (A[i + 1 ] - A[i] <= mid)
cnt += A[i + 1 ] - A[i];
else
cnt += mid;
}
cnt += mid;
return cnt >= H;
}
static int minimizeOperations( int [] A, int N, int H)
{
int low = 1 , high = ( int )1e9, mid;
while (high - low > 1 ) {
mid = (low + high) / 2 ;
if (test(mid, A, N, H)) {
high = mid;
}
else {
low = mid + 1 ;
}
}
if (test(low, A, N, H))
return low;
else
return high;
}
public static void main (String[] args)
{
int N = 2 , H = 5 ;
int [] A = { 1 , 5 };
System.out.println(minimizeOperations(A, N, H));
int N1 = 3 , H1 = 10 ;
int [] A1 = { 2 , 4 , 10 };
System.out.println(minimizeOperations(A1, N1, H1));
}
}
|
Python3
def test(mid, A, N, H):
cnt = 0
for i in range (N - 1 ):
if A[i + 1 ] - A[i] < = mid:
cnt + = A[i + 1 ] - A[i]
else :
cnt + = mid
cnt + = mid
return cnt > = H
def minimizeOperations(A, N, H):
low = 1
high = 1e9
while high - low > 1 :
mid = (low + high) / / 2
if test(mid, A, N, H):
high = mid
else :
low = mid + 1
if test(low, A, N, H):
return int (low)
else :
return int (high)
if __name__ = = "__main__" :
N = 2
H = 5
A = [ 1 , 5 ]
print (minimizeOperations(A, N, H))
N1 = 3
H1 = 10
A1 = [ 2 , 4 , 10 ]
print (minimizeOperations(A1, N1, H1))
|
C#
using System;
class GFG {
static bool test( int mid, int [] A, int N, int H)
{
int cnt = 0;
for ( int i = 0; i < N - 1; i++) {
if (A[i + 1] - A[i] <= mid)
cnt += A[i + 1] - A[i];
else
cnt += mid;
}
cnt += mid;
return cnt >= H;
}
static int minimizeOperations( int [] A, int N, int H)
{
int low = 1, high = ( int )1e9, mid;
while (high - low > 1) {
mid = (low + high) / 2;
if (test(mid, A, N, H)) {
high = mid;
}
else {
low = mid + 1;
}
}
if (test(low, A, N, H))
return low;
else
return high;
}
static public void Main()
{
int N = 2, H = 5;
int [] A = { 1, 5 };
Console.WriteLine(minimizeOperations(A, N, H));
int N1 = 3, H1 = 10;
int [] A1 = { 2, 4, 10 };
Console.WriteLine(minimizeOperations(A1, N1, H1));
}
}
|
Javascript
function test(mid, A, N, H)
{
let cnt = 0;
for (let i = 0; i < N - 1; i++) {
if (A[i + 1] - A[i] <= mid)
cnt += A[i + 1] - A[i];
else
cnt += mid;
}
cnt += mid;
return cnt >= H;
}
function minimizeOperations( A, N, H)
{
let low = 1, high = 1e9, mid;
while (high - low > 1) {
mid = Math.floor((low + high) / 2);
if (test(mid, A, N, H)) {
high = mid;
}
else {
low = mid + 1;
}
}
if (test(low, A, N, H))
return low;
else
return high;
}
let N = 2, H = 5;
let A = [ 1, 5 ];
console.log(minimizeOperations(A, N, H)+ "<br>" );
let N1 = 3, H1 = 10;
let A1 = [ 2, 4, 10 ];
console.log(minimizeOperations(A1, N1, H1)+ "<br>" );
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Related Articles:
Share your thoughts in the comments
Please Login to comment...