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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:27:03+00:00 2026-05-24T10:27:03+00:00

Found the following inteview q on the web: You have an array of 0s

  • 0

Found the following inteview q on the web:

You have an array of
0s and 1s and you want to output all the intervals (i, j) where the
number of 0s and numbers of 1s are equal. Example

    pos = 0 1 2 3 4 5 6 7 8
          0 1 0 0 1 1 1 1 0

One interval is (0, 1) because there the number
of 0 and 1 are equal. There are many other intervals, find all of them
in linear time.

I think there is no linear time algo, as there may be n^2 such intervals.
Am I right? How can I prove that there are n^2 such ?

  • 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-24T10:27:04+00:00Added an answer on May 24, 2026 at 10:27 am

    This is the fastest way I can think of to do this, and it is linear to the number of intervals there are.

    Let L be your original list of numbers and A be a hash of empty arrays where initially A[0] = [0]

    sum = 0
    for i in 0..n
      if L[i] == 0:
        sum--
        A[sum].push(i)
      elif L[i] == 1:
        sum++
        A[sum].push(i)
    

    Now A is essentially an x y graph of the sum of the sequence (x is the index of the list, y is the sum). Every time there are two x values x1 and x2 to an y value, you have an interval (x1, x2] where the number of 0s and 1s is equal.

    There are m(m-1)/2 (arithmetic sum from 1 to m – 1) intervals where the sum is 0 in every array M in A where m = M.length

    Using your example to calculate A by hand we use this chart

    L           #   0  1  0  1  0  0  1  1  1  1  0
    A keys      0  -1  0 -1  0 -1 -2 -1  0  1  2  1
    L index    -1   0  1  2  3  4  5  6  7  8  9  10
    

    (I’ve added a # to represent the start of the list with an key of -1. Also removed all the numbers that are not 0 or 1 since they’re just distractions) A will look like this:

    [-2]->[5]
    [-1]->[0, 2, 4, 6]
    [0]->[-1, 1, 3, 7]
    [1]->[8, 10]
    [2]->[9]
    

    For any M = [a1, a2, a3, …], (ai + 1, aj) where j > i will be an interval with the same number of 0s as 1s. For example, in [-1]->[0, 2, 4, 6], the intervals are (1, 2), (1, 4), (1, 6), (3, 4), (3, 6), (5, 6).

    Building the array A is O(n), but printing these intervals from A must be done in linear time to the number of intervals. In fact, that could be your proof that it is not quite possible to do this in linear time to n because it’s possible to have more intervals than n and you need at least the number of interval iterations to print them all.

    Unless of course you consider building A is enough to find all the intervals (since it’s obvious from A what the intervals are), then it is linear to n 😛

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found following code to get all action from controller name. Type t
For a moment i found following example: $robots = Robots::find(array(limit => 100)); Is it
I have found following working example of CGI with bash. If I change first
On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
I found the following lines in a makefile tutorial, but I have some problem
After searching lot on the internet we have found following code to get only
i have found following code on online for suffix tree #include <stdio.h> #define E
I have Found following Code Sample on MSDN when i was searching for RSACypthyServiceProvider.I
I have found following function calls in several frameworks which appear to me as
I have found following code in mysql documentation but wondering what does it mean

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.