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 6219151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:43:47+00:00 2026-05-24T07:43:47+00:00

Given a sequence such as S = {1,8,2,1,4,1,2,9,1,8,4}, I need to find the minimal-length

  • 0

Given a sequence such as S = {1,8,2,1,4,1,2,9,1,8,4}, I need to find the minimal-length subsequence that contains all element of S (no duplicates, order does not matter). How do find this subsequence in an efficient way?

Note: There are 5 distinct elements in S: {1,2,4,8,9}. The minimum-length subsequence must contain all these 5 elements.

  • 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-24T07:43:48+00:00Added an answer on May 24, 2026 at 7:43 am

    Algorithm:

    First, determine the quantity of different elements in the array – this can be easily done in linear time. Let there be k different elements.

    Allocate an array cur of size 10^5, each showing how much of each element is used in current subsequence (see later).

    Hold a cnt variable showing how many different elements are there currently in the considered sequence. Now, take two indexes, begin and end and iterate them through the array the following way:

    1. initialize cnt and begin as 0, end as -1 (to get 0 after first increment). Then while possible perform follows:
    2. If cnt != k:

      2.1. increment end. If end already is the end of array, then break. If cur[array[end]] is zero, increment cnt. Increment cur[array[end]].

      Else:

      2.2 {

      Try to increment the begin iterator: while cur[array[begin]] > 1, decrement it, and increment the begin (cur[array[begin]] > 1 means that we have another such element in our current subsequence). After all, compare the [begin, end] interval with current answer and store it if it is better.

      }

    After the further process becomes impossible, you got the answer. The complexity is O(n) – just passing two interators through the array.

    Implementation in C++:

        #include <iostream>
    
    using namespace std;
    
    const int MAXSIZE = 10000;
    
    int arr[ MAXSIZE ];
    int cur[ MAXSIZE ];
    
    int main ()
    {
       int n; // the size of array
       // read n and the array
    
       cin >> n;
       for( int i = 0; i < n; ++i )
          cin >> arr[ i ];
    
       int k = 0;
       for( int i = 0; i < n; ++i )
       {
          if( cur[ arr[ i ] ] == 0 )
             ++k;
          ++cur[ arr[ i ] ];
       }
    
       // now k is the number of distinct elements
    
       memset( cur, 0, sizeof( cur )); // we need this array anew
       int begin = 0, end = -1; // to make it 0 after first increment
       int best = -1; // best answer currently found
       int ansbegin, ansend; // interval of the best answer currently found
       int cnt = 0; // distinct elements in current subsequence
    
       while(1)
       {
          if( cnt < k )
          {
             ++end;
             if( end == n )
                break;
             if( cur[ arr[ end ]] == 0 )
                ++cnt; // this elements wasn't present in current subsequence;
             ++cur[ arr[ end ]];
             continue;
          }
          // if we're here it means that [begin, end] interval contains all distinct elements
          // try to shrink it from behind
          while( cur[ arr[ begin ]] > 1 ) // we have another such element later in the subsequence
          {
             --cur[ arr[ begin ]];
             ++begin;
          }
          // now, compare [begin, end] with the best answer found yet
          if( best == -1 || end - begin < best )
          {
             best = end - begin;
             ansbegin = begin;
             ansend = end;
          }
          // now increment the begin iterator to make cur < k and begin increasing the end iterator again
          --cur[ arr[ begin]];
          ++begin;
          --cnt;
       }
    
       // output the [ansbegin, ansend] interval as it's the answer to the problem
    
       cout << ansbegin << ' ' << ansend << endl;
       for( int i = ansbegin; i <= ansend; ++i )
          cout << arr[ i ] << ' ';
       cout << endl;
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a sequence of data (it may have duplicates), a fixed-sized moving window, move
Given an input sequence, what is the best way to find the longest (not
I'm looking for an algorithm that given two permutations of a sequence (e.g. [2,
Given a package, how can I automatically find all its sub-packages?
Good day. I can handle a ComplexType such as: <xsd:element name=Prerequisite> <xsd:complexType> <xsd:sequence> <xsd:element
Equilibrium index of a sequence is an index such that the sum of elements
Given the following question, Given an array of integers A of length n, find
i need such a query where child and parent field are given with a
Matcher.find finds the next subsequence, starting at a given index, which is compliant with
I would like to know if anyone sees a pattern in the sequence given

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.