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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:30:44+00:00 2026-05-26T04:30:44+00:00

I came across this problem during an interview forum., Given an int array which

  • 0

I came across this problem during an interview forum.,

Given an int array which might contain duplicates, find the largest subset of it which form a sequence.
Eg. {1,6,10,4,7,9,5}
then ans is 4,5,6,7
Sorting is an obvious solution. Can this be done in O(n) time.

My take on the problem is that this cannot be done O(n) time & the reason is that if we could do this in O(n) time we could do sorting in O(n) time also ( without knowing the upper bound).
As a random array can contain all the elements in sequence but in random order.

Does this sound a plausible explanation ? your thoughts.

  • 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-26T04:30:44+00:00Added an answer on May 26, 2026 at 4:30 am

    I believe it can be solved in O(n) if you assume you have enough memory to allocate an uninitialized array of a size equal to the largest value, and that allocation can be done in constant time. The trick is to use a lazy array, which gives you the ability to create a set of items in linear time with a membership test in constant time.

    Phase 1: Go through each item and add it to the lazy array.

    Phase 2: Go through each undeleted item, and delete all contiguous items.

    In phase 2, you determine the range and remember it if it is the largest so far. Items can be deleted in constant time using a doubly-linked list.

    Here is some incredibly kludgy code that demonstrates the idea:

    int main(int argc,char **argv)
    {
      static const int n = 8;
      int values[n] = {1,6,10,4,7,9,5,5};
      int index[n];
      int lists[n];
      int prev[n];
      int next_existing[n]; // 
      int prev_existing[n];
      int index_size = 0;
      int n_lists = 0;
    
      // Find largest value
      int max_value = 0;
      for (int i=0; i!=n; ++i) {
        int v=values[i];
        if (v>max_value) max_value=v;
      }
    
      // Allocate a lazy array
      int *lazy = (int *)malloc((max_value+1)*sizeof(int));
    
      // Set items in the lazy array and build the lists of indices for
      // items with a particular value.
      for (int i=0; i!=n; ++i) {
        next_existing[i] = i+1;
        prev_existing[i] = i-1;
        int v = values[i];
        int l = lazy[v];
        if (l>=0 && l<index_size && index[l]==v) {
          // already there, add it to the list
          prev[n_lists] = lists[l];
          lists[l] = n_lists++;
        }
        else {
          // not there -- create a new list
          l = index_size;
          lazy[v] = l;
          index[l] = v;
          ++index_size;
          prev[n_lists] = -1;
          lists[l] = n_lists++;
        }
      }
      // Go through each contiguous range of values and delete them, determining
      // what the range is.
      int max_count = 0;
      int max_begin = -1;
      int max_end = -1;
      int i = 0;
      while (i<n) {
        // Start by searching backwards for a value that isn't in the lazy array
        int dir = -1;
        int v_mid = values[i];
        int v = v_mid;
        int begin = -1;
        for (;;) {
          int l = lazy[v];
          if (l<0 || l>=index_size || index[l]!=v) {
            // Value not in the lazy array
            if (dir==1) {
              // Hit the end
              if (v-begin>max_count) {
                max_count = v-begin;
                max_begin = begin;
                max_end = v;
              }
              break;
            }
            // Hit the beginning
            begin = v+1;
            dir = 1;
            v = v_mid+1;
          }
          else {
            // Remove all the items with value v
            int k = lists[l];
            while (k>=0) {
              if (k!=i) {
                next_existing[prev_existing[l]] = next_existing[l];
                prev_existing[next_existing[l]] = prev_existing[l];
              }
              k = prev[k];
            }
    
            v += dir;
          }
        }
        // Go to the next existing item
        i = next_existing[i];
      }
    
      // Print the largest range
      for (int i=max_begin; i!=max_end; ++i) {
        if (i!=max_begin) fprintf(stderr,",");
        fprintf(stderr,"%d",i);
      }
      fprintf(stderr,"\n");
    
      free(lazy);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
I came across this problem while preparing for an interview and curious to know
i came across this weird problem and don't seem to find a solution for
For fun I'm learning about graph theory and I came across this problem. Given
I am new to creating Java web applications and came across this problem when
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
I am watching a video about [LINQ][1] and came across a problem. In this
I came across this problem when referring a piece of JavaScript code: <a href="javascript:void(0);"><!--
I just came across this weird problem that is happening in IE. In the
I've came across on this problem, I have a sever running apache and php.

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.