Question: There is an array A , consisting of N integers, and two integers, L and R . Your task is to find the shortest fragment

There is an array A, consisting of N integers, and two integers, L and R. Your task is to find the shortest fragment of consecutive elements of A that contains every integer from L to R inclusive.
Write a function:
class Solution { public int solution(int[] A, int L. int R): )
that, given A, L and R, returns the length of the shortest fragment of A that contains all numbers from L to R. If no such fragment exists, the function should return -1.
Examples:
1. Given A [2,1,4,3,2,1,1,4), L =2 and R =4, the function should return 3. The shortest fragment containing the numbers 2,3 and 4 starts at the third element of A: [2,1,4,3,2,1,1,4]
2. Given A (109,1,1,1,1,1,10-11, L-1011 and R-109, the function should return 7. The fragment is the whole array.
3. Given A =[1,3,5,7), L=3 and R =5, the function should return -1. Array A does not contain the number 4.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000);
1LSR1,000,000,000;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!