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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:01:19+00:00 2026-05-29T23:01:19+00:00

I have been working on the Interview Street challenge Changing Bits in my spare

  • 0

I have been working on the Interview Street challenge Changing Bits in my spare time for a little over a week now and just spinning my wheels at this point so I am hoping someone might be able to give me a pointer or hint in the right direction.

The basics of the challenge is taking two bit strings A & B and running a number of queries manipulating the two bit strings.

Let A and B be two N bit numbers. You are given initial values for A and B, and you should write a program which processes three kinds of queries:

  • set_a idx x: Set A[idx] to x, where 0 <= idx < N, where A[idx] is
    idx’th least significant bit of A.
  • set_b idx x: Set B[idx] to x, where 0 <= idx < N.
  • get_c idx: Print C[idx], where C=A+B, and 0<=idx

Where the bit numbers are length 1 to 100,000 and the program can have between 1 to 500,000 queries of any combination of set_a, set_b, or get_c.

To minimize looping I am using C as a running total. When a bit in A or B changes, the changed bit is also added or subtracted, from C. Further minimizing looping when adding and subtracting is done from the changed bit to left hand while there is still a carry bit.

private static void add(final boolean[] the_array, final int the_index)
{
    for(int iter = the_array.length - the_index - 1; iter >= 0; iter--)
    {
        if(the_array[iter])
        {
            the_array[iter] = false;
        }
        else if(!the_array[iter])
        {
            the_array[iter] = true;
            return ;
        }
    }
}

private static void subtract(final boolean[] the_array, final int the_index)
{
    for(int iter = the_array.length - the_index - 1; iter >= 0; iter--)
    {
        if(the_array[iter])
        {
            the_array[iter] = false;
            return ;
        }
        else if(!the_array[iter])
        {
            the_array[iter] = true;
        }
    }
}

Overall the program runs pretty well completing the worst edge case of bit length of 100,000 and 500,000 queries runs in about 120ms but it is still not fast enough for the purposes of the challenge. Due to the size of the bit length quickly exceeds the primitive integer (long as well) values in Java so most of the API is not applicable for this. As I am not making much progress increasing the overall run time magnitude is starting to suggest there might be an algorithm or data structure that is better suited for this than just an array. Any 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-29T23:01:22+00:00Added an answer on May 29, 2026 at 11:01 pm

    Hmm, off the top of my head:
    Don’t keep C around / updated, just calculate the bit you’re asked for on the fly. Remember that when you add the corresponding bit positions of A and B, you only need to look down the adjacent less-significant bits until you find a point where they’re both zero – anything beyond that can’t carry bits past it. e.g.

    A: 001101001010111
    B: 010110100010110
         ^ asked for C there
       1000111^ only need to add bits from here
         ^ until you get your answer.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a Windows Mobile app for a little while now
I have been working on a project for some time now, and I decided
Have been working on a Tower Defense game for iOS for some time now.
I have been working with android for a little while now and feel pretty
I have been working on this problem for 2 days now and it's an
I have been working in C# for quite some time but come around this
I have been working with ASP.NET for a few years and am now working
For quiet sometime now I have been working on improving my algo skills because
I have been working on an application in Xcode for a while now and
I have been working in Python for a few months now, and it has

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.