Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:39:39+00:00 2026-05-16T01:39:39+00:00

Saw this question recently: Given 2 arrays, the 2nd array containing some of the

  • 0

Saw this question recently:

Given 2 arrays, the 2nd array containing some of the elements of the 1st array, return the minimum window in the 1st array which contains all the elements of the 2nd array.

Eg :
Given A={1,3,5,2,3,1} and B={1,3,2}

Output : 3 , 5 (where 3 and 5 are indices in the array A)

Even though the range 1 to 4 also contains the elements of A, the range 3 to 5 is returned Since it contains since its length is lesser than the previous range ( ( 5 – 3 ) < ( 4 – 1 ) )

I had devised a solution but I am not sure if it works correctly and also not efficient.

Give an Efficient Solution for the problem. Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:39:40+00:00Added an answer on May 16, 2026 at 1:39 am

    A simple solution of iterating through the list.

    1. Have a left and right pointer, initially both at zero
    2. Move the right pointer forwards until [L..R] contains all the elements (or quit if right reaches the end).
    3. Move the left pointer forwards until [L..R] doesn’t contain all the elements. See if [L-1..R] is shorter than the current best.

    This is obviously linear time. You’ll simply need to keep track of how many of each element of B is in the subarray for checking whether the subarray is a potential solution.

    Pseudocode of this algorithm.

    size = bestL = A.length;
    needed = B.length-1;
    found = 0; left=0; right=0;
    counts = {}; //counts is a map of (number, count)
    for(i in B) counts.put(i, 0);
    
    //Increase right bound
    while(right < size) {
        if(!counts.contains(right)) continue;
        amt = count.get(right);
        count.set(right, amt+1);
        if(amt == 0) found++;
        if(found == needed) {
            while(found == needed) {
                //Increase left bound
                if(counts.contains(left)) {
                    amt = count.get(left);
                    count.set(left, amt-1);
                    if(amt == 1) found--;
                }
                left++;
            }
            if(right - left + 2 >= bestL) continue;
            bestL = right - left + 2;
            bestRange = [left-1, right] //inclusive
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does the following expression return in Java? Math.max(Float.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); I saw this question
I saw this question asked about C# I would like an answer for PHP.
I saw this question asking about whether globals are bad . As I thought
I saw this quote in this question : MS support is poor, except when
I just saw this question: Understanding .NET’s SecurityAction parameter for permissions And I have
I saw this same question for VIM and it has been something that I
I saw this quote on the question: What is a good functional language on
I saw this tip in another question and was wondering if someone could explain
I saw this in an answer to another question , in reference to shortcomings
I just saw this mentioned in Stack Overflow question Best WYSIWYG CSS editor and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.