Farthest index that can be reached from the Kth index of given array by given operations
Given an array arr[] consisting of N integers and three integers X, Y, and K, the task is to find the farthest index that can be reached by the following operations:
- If arr[i] ? arr[i + 1]: Move from index i to i + 1.
- If arr[i] < arr[i+1]: Either decrement X by 1 if the value of X > 0 or decrement Y by (arr[i + 1] – arr[i]) if the value of Y > (arr[i+1] – arr[i]).
Examples:
Input: arr[] = {4, 2, 7, 6, 9, 14, 12}, X = 1, Y = 5, K = 0
Output: 4
Explanation:
Initially, K = 0.
arr[0] > arr[1]: Therefore, move to index 1.
arr[1] < arr[2]: Decrement X by 1 and move to index 2. Now X = 0.
arr[2] > arr[3]: Move to index 3.
arr[3] < arr[4]: Decrement Y by 3 and move to index 4. Now Y = 2
arr[4] < arr[5]: Neither X > 0 nor Y > 5. Hence, it is not possible to move to the next index.
Therefore, the maximum index that can be reached is 4.
Input: arr[] = {14, 3, 19, 3}, X = 17, Y = 0, K = 1
Output: 3
Approach: The idea is to use X for the maximum difference between indexes and Y for the remaining difference. Follow the steps below to solve this problem:
- Declare a priority queue.
- Traverse the given array arr[] and perform the following operations:
- If the current element (arr[i]) is greater than the next element (arr[i + 1]), then move to the next index.
- Otherwise, push the difference of (arr[i + 1] – arr[i]) into the priority queue.
- If the size of the priority queue is greater than Y, then decrement X by the top element of the priority queue and pop that element.
- If X is less than 0, the farthest index that can be reached is i.
- After completing the above steps, if the value of X is at least 0, then the farthest index that can be reached is (N – 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void farthestHill( int arr[], int X,
int Y, int N, int K)
{
int i, diff;
priority_queue< int > pq;
for (i = K; i < N - 1; i++) {
if (arr[i] >= arr[i + 1])
continue ;
diff = arr[i + 1] - arr[i];
pq.push(diff);
if (pq.size() > Y) {
X -= pq.top();
pq.pop();
}
if (X < 0) {
cout << i;
return ;
}
}
cout << N - 1;
}
int main()
{
int arr[] = { 4, 2, 7, 6, 9, 14, 12 };
int X = 5, Y = 1;
int K = 0;
int N = sizeof (arr) / sizeof (arr[0]);
farthestHill(arr, X, Y, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void farthestHill( int arr[], int X,
int Y, int N, int K)
{
int i, diff;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for (i = K; i < N - 1 ; i++)
{
if (arr[i] >= arr[i + 1 ])
continue ;
diff = arr[i + 1 ] - arr[i];
pq.add(diff);
if (pq.size() > Y)
{
X -= pq.peek();
pq.poll();
}
if (X < 0 )
{
System.out.print(i);
return ;
}
}
System.out.print(N - 1 );
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 7 , 6 , 9 , 14 , 12 };
int X = 5 , Y = 1 ;
int K = 0 ;
int N = arr.length;
farthestHill(arr, X, Y, N, K);
}
}
|
Python3
def farthestHill(arr, X, Y, N, K):
pq = []
for i in range (K, N - 1 , 1 ):
if (arr[i] > = arr[i + 1 ]):
continue
diff = arr[i + 1 ] - arr[i]
pq.append(diff)
if ( len (pq) > Y):
X - = pq[ - 1 ]
pq[ - 1 ]
if (X < 0 ):
print (i)
return
print (N - 1 )
arr = [ 4 , 2 , 7 , 6 , 9 , 14 , 12 ]
X = 5
Y = 1
K = 0
N = len (arr)
farthestHill(arr, X, Y, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void farthestHill( int [] arr, int X,
int Y, int N, int K)
{
int i, diff;
List< int > pq = new List< int >();
for (i = K; i < N - 1; i++)
{
if (arr[i] >= arr[i + 1])
continue ;
diff = arr[i + 1] - arr[i];
pq.Add(diff);
pq.Sort();
pq.Reverse();
if (pq.Count > Y)
{
X -= pq[0];
pq.RemoveAt(0);
}
if (X < 0)
{
Console.Write(i);
return ;
}
}
Console.Write(N - 1);
}
public static void Main(String[] args)
{
int [] arr = { 4, 2, 7, 6, 9, 14, 12 };
int X = 5, Y = 1;
int K = 0;
int N = arr.Length;
farthestHill(arr, X, Y, N, K);
}
}
|
Javascript
<script>
function farthestHill(arr, X, Y, N, K)
{
var i, diff;
var pq = [];
for (i = K; i < N - 1; i++)
{
if (arr[i] >= arr[i + 1])
continue ;
diff = arr[i + 1] - arr[i];
pq.push(diff);
pq.sort();
pq = pq.reverse();
if (pq.length > Y)
{
X -= pq[0];
pq = pq.slice(1);
}
if (X < 0)
{
document.write(i);
return ;
}
}
document.write(N - 1);
}
var arr = [4, 2, 7, 6, 9, 14, 12];
var X = 5, Y = 1;
var K = 0;
var N = arr.length;
farthestHill(arr, X, Y, N, K);
</script>
|
Time Complexity: O(N*log(E)), where E is the maximum number of elements in the priority queue.
Auxiliary Space: O(E)
Last Updated :
07 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...