Question
Cities and Teleporters distance between two cities ij is | A-A, I. The energy lost while traveling from one city to another is equal to
Cities and Teleporters
distance between two cities ij is | A-A, I. The energy lost while traveling from one city to another is equal to the distance between those two cities. Additionally, In every city i (1iN-1) there is a teleporter that can teleport you from the ith city to (i+1)th city without using any energy Your initial energy is X and you can use the teleporters at most K times. Find the farthest city you can reach if you use these teleporters optimally.
// Driver Code Ends
Note: A
Example 1:
Input:
N = 4, X = 2, K = 2
A[] = {0,1,2,3}
Output:
4
Explanation:
You can use teleporters of city 1 and 2 and then travel from the 3rd city to the 3rd city to the 4th city.
18
};
//write your code here
}
Example 2:
// Driver Code Ends
Input:
N = 3, X = 2, K = 1
A[] = {0, 2, 10)
Output:
3
Explanation:
You can travel to the 2nd city and then use the teleporter there to reach the 3rd city.
Your Task: You don't need to read input or print anything. Your task is to complete the function solve () which takes an integer N, initial energy X, number of times you can use the teleporters K and location array A as input parameter and returns the farthest city you can reach if you use teleporters optimally.
Constraints:
1 N,K105
1 A, X 10
NOTE: SOLVE THIS IN 20 MINUTES ONLY.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started