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

  • SEARCH
  • Home
  • 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 8477799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:33:54+00:00 2026-06-10T18:33:54+00:00

How can i find the minimum interval of an integer array in which all

  • 0

How can i find the minimum interval of an integer array in which all the unique elements of that array
are present .
For example my array is : 1 1 1 2 3 1 1 4 3 3 3 2 1 2 2 4 1
minimum interval is from index 3 to index 7.
I’m looking for an algorithm of O(nlogn) or less (n<=100000)

  • 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-06-10T18:33:55+00:00Added an answer on June 10, 2026 at 6:33 pm

    The strategy is iterate from the end to the start, remembering when you last saw each integer. Eg. somewhere in the middle, you last saw 1 at index 15, 2 at index 20, 3 at index 17. The interval length is the maximum index you last saw something minus your current index.

    To find the maximum index easily, you should use a self-balancing binary search tree (BST), because it has O(log n) insert and removal time, and constant lookup time for the largest index.

    For example, if you have to update the index you last saw a 1, you remove the current last seen index (the 15), and insert the new last seen index.

    By updating the self balancing BST with all the end indices allowed by each integer type, we can pick the largest, and say that we can end there.

    The exact code depends on how the input is defined (eg. whether you know what all the integers are, ie. you know there exists all integers between 1 and 4 in array, then the code is simplified).

    Iteration is O(n), the BST is O(log n). Overall is O(n log n).


    Implementation Details

    Implementation of this takes a little bit of work.

    Initialize:

    • the interval length for each starting index.
    • an array for when you last saw a certain integer. (If you don’t know what possible integers might be in the array, instead of using a normal array, use an associative array (eg. map<> in C++)).
    • a priority queue-like type heap, where the top of the queue is the maximum integer in it. You need to be able to easily remove stuff from it, so use a self-balancing binary search tree

    Now inside the loop (looping index from end of input array to start of input array),

    • You can update your last seen array for this particular index.

      Just check what integer you see, and update the entry in the index last seen array.

    • Using before and after in the last seen array, update the BST (remove old end index, add new index)

    • Update interval length for this starting index, based on largest end index required (from BST).

    • If you see an integer you haven’t seen before, invalidate all interval lengths for starting indices above this index (or just avoid updating interval length until all integers have been seen at least once).


    C++ code implementation

    • Assuming all integers 0-(k-1) are found in input array
    • Disclaimer: untested
    • ignores #include and main function

    Code:

    int n=10,k=3;
    int input[n]=?;
    unsigned int interval[n];
    for (int i=0;i<n;i++) interval[i]=-1; // initialize interval to very large number
    int lastseen[k];
    for (int i=0;i<k;i++) lastseen[i]=-1; // initialize lastseen
    multiset<int> pq;
    
    for (int i=n-1;i>=0;i--) {
      if (lastseen[input[i]] != -1) // if lastseen[] already has index
        pq.erase(pq.find(lastseen[input[i]])); // erase single copy
      lastseen[input[i]]=i; // update last seen
      pq.insert(i); // put last seen index into BST
      if (pq.size()==k) { // if all integers seen (nothing missing)
        // get (maximum of endindex requirements) - current index
        interval[i] = (*pq.rbegin())-i+1;
      }
    }
    // find best answer
    unsigned int minlength=-1;
    int startindex;
    for (int i=0;i<n;i++) {
      if (minlength>interval[i]) { // better answer?
        minlength=interval[i];
        startindex=i;
      }
    }
    // Your answer is [startindex,startindex+minlength)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that Dijkstra's algorithm can find the minimum distance between two nodes
In python to find the index of the minimum value of the array I
I was wondering how can I find minimum and maximum values from a dataset,
Find minimum window width in string x that contains all characters of another string
In all the examples that I can find that use a JTextArea , the
I can find ample evidence that MongoDBs are always kept in the order given
Is there a place (website) where i can find information on which VM is
I can find the JS and HTML code in response by alert() . But
I can find the tool command in my filesystem under: /Applications/Xcode.app/Contents/Developer/usr/bin/otool If I specify
Where can find resources about best practices for SharePoint programming? I am talking about

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.