Given an array arr[] where each element represents the length of a sub-track, and an integer k representing the maximum distance a car can travel on any sub-track.
- You may add petrol to increase the car's maximum travel distance.
- Each unit of petrol increases this maximum distance by 1 kilometer for all sub-tracks.
Determine the minimum units of petrol required so that the car can travel through every sub-track. If the car can already cover all sub-tracks with its initial capacity, return -1.
Examples:
Input: arr[] = [2, 5, 4, 5, 2], k = 7
Output: -1
Explanation: Since the car can already traverse all the given sub-tracks, no extra petrol is required. Hence, the answer is -1.
Input: arr[] = [1, 6, 3, 5, 2], k = 4
Output: 2
Explanation: After adding 2 units of petrol, the car's maximum travel distance becomes 6 km, allowing it to traverse all the given sub-tracks. Hence, the answer is 2.
Table of Content
[Naive Approach] Sorting the Array - O(n log n) Time and O(1) Auxiliary Space
The idea is to sort the array and obtain the maximum sub-track length from the last position of the sorted array. If the maximum length is greater than
k, the required petrol is the difference between them. Otherwise, no extra petrol is needed.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int required(vector<int> &arr, int k)
{
// Sort the array
sort(arr.begin(), arr.end());
// Maximum sub-track length
int maxElement = arr.back();
// No extra petrol required
if (k >= maxElement)
return -1;
// Return the required petrol
return maxElement - k;
}
int main()
{
vector<int> arr = {1, 6, 3, 5, 2};
int k = 4;
cout << required(arr, k);
return 0;
}
import java.util.Arrays;
public class GFG {
public static int required(int[] arr, int k)
{
// Sort the array
Arrays.sort(arr);
// Maximum sub-track length
int maxElement = arr[arr.length - 1];
// No extra petrol required
if (k >= maxElement)
return -1;
// Return the required petrol
return maxElement - k;
}
public static void main(String[] args)
{
int[] arr = { 1, 6, 3, 5, 2 };
int k = 4;
System.out.println(required(arr, k));
}
}
def required(arr, k):
# Sort the array
arr.sort()
# Maximum sub-track length
maxElement = arr[-1]
# No extra petrol required
if k >= maxElement:
return -1
# Return the required petrol
return maxElement - k
if __name__ == "__main__":
arr = [1, 6, 3, 5, 2]
k = 4
print(required(arr, k))
using System;
public class GFG {
public static int required(int[] arr, int k)
{
// Sort the array
Array.Sort(arr);
// Maximum sub-track length
int maxElement = arr[arr.Length - 1];
// No extra petrol required
if (k >= maxElement)
return -1;
// Return the required petrol
return maxElement - k;
}
public static void Main()
{
int[] arr = { 1, 6, 3, 5, 2 };
int k = 4;
Console.WriteLine(required(arr, k));
}
}
function required(arr, k)
{
// Sort the array
arr.sort((a, b) => a - b);
// Maximum sub-track length
let maxElement = arr[arr.length - 1];
// No extra petrol required
if (k >= maxElement)
return -1;
// Return the required petrol
return maxElement - k;
}
// Driver code
let arr = [ 1, 6, 3, 5, 2 ];
let k = 4;
console.log(required(arr, k));
Output
2
[Expected Approach] Linear Scan for Maximum Element - O(n) Time and O(1) Auxiliary Space
The idea is to traverse the array once and find the maximum sub-track length. The car must be able to travel through the longest sub-track, so if the maximum element exceeds
k, the required petrol ismaxElement - k; otherwise, return-1.
Let us understand with an example:
Input: arr[] = [1, 6, 3, 5, 2], k = 4
- Initialize maxElement = -1.
- Traverse the array and update maxElement whenever a larger element is found. After the traversal, maxElement = 6.
- Compare k with maxElement. Since k = 4 is less than 6, the car cannot cover the longest sub-track.
- Compute the additional petrol required as maxElement - k = 6 - 4 = 2.
- Return 2 as the minimum extra petrol required.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int required(vector<int> &arr, int k)
{
int n = arr.size();
int maxElement = -1;
// Iterating over the array to find the maximum element
for (int i = 0; i < n; i++)
{
if (arr[i] > maxElement)
maxElement = arr[i];
}
// If k is greater than or equal to the maximum element, return -1
if (k >= maxElement)
return -1;
// Return the difference between the maximum element and k
return (maxElement - k);
}
int main()
{
vector<int> arr = {1, 6, 3, 5, 2};
int k = 4;
cout << required(arr, k);
return 0;
}
import java.util.Arrays;
public class GFG {
public static int required(int[] arr, int k)
{
int n = arr.length;
int maxElement
= -1;
// Iterating over the array to find the maximum
// element
for (int i = 0; i < n; i++) {
if (arr[i] > maxElement)
maxElement = arr[i];
}
// If k is greater than or equal to the maximum
// element, return -1
if (k >= maxElement)
return -1;
// Return the difference between the maximum element
// and k
return (maxElement - k);
}
public static void main(String[] args)
{
int[] arr = { 1, 6, 3, 5, 2 };
int k = 4;
System.out.println(required(arr, k));
}
}
def required(arr, k):
n = len(arr)
maxElement = -1
# Iterating over the array to find the maximum element
for i in range(n):
if arr[i] > maxElement:
maxElement = arr[i]
# If k is greater than or equal to the maximum element, return -1
if k >= maxElement:
return -1
# Return the difference between the maximum element and k
return (maxElement - k)
if __name__ == "__main__":
arr = [1, 6, 3, 5, 2]
k = 4
print(required(arr, k))
using System;
public class GFG {
public static int required(int[] arr, int k)
{
int n = arr.Length;
int maxElement
= -1;
// Iterating over the array to find the maximum
// element
for (int i = 0; i < n; i++) {
if (arr[i] > maxElement)
maxElement = arr[i];
}
// If k is greater than or equal to the maximum
// element, return -1
if (k >= maxElement)
return -1;
// Return the difference between the maximum element
// and k
return (maxElement - k);
}
public static void Main()
{
int[] arr = { 1, 6, 3, 5, 2 };
int k = 4;
Console.WriteLine(required(arr, k));
}
}
function required(arr, k)
{
let n = arr.length;
let maxElement
= -1;
// Iterating over the array to find the maximum element
for (let i = 0; i < n; i++) {
if (arr[i] > maxElement) {
maxElement = arr[i];
}
}
// If k is greater than or equal to the maximum element,
// return -1
if (k >= maxElement) {
return -1;
}
// Return the difference between the maximum element and
// k
return (maxElement - k);
}
// Driver code
let arr = [ 1, 6, 3, 5, 2 ];
let k = 4;
console.log(required(arr, k));
Output
2