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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:18:20+00:00 2026-05-27T01:18:20+00:00

I have a problem in which a boy has n cards (2 faces one

  • 0

I have a problem in which a boy has n cards (2 faces one black one white <=> 0/1). Initially the cards are all white faced up.
I need to be able to do two operations on the row of n cards.

  • flip(i,j) flips the cards between rows i and j indexes
  • state(i) returns the colour of the card at i index position

What i need to find is a method to keep the sum of these two operations under O(n).
Any ideas of a good data structure to solve this?

  • 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-27T01:18:20+00:00Added an answer on May 27, 2026 at 1:18 am

    You could create a balanced tree where the cards are leaves and have flip indicator at each node.

    To flip the cards, flip the nodes closest to the root that cover the interval. This is O(log n).

    To get the state of a card, go though the root to the corresponding leaf and xor the flip indicators from all nodes in the path. This is also O(log n).

    For example, if you had tree like this:

         A
        / \
       /   \
      B     C
     / \   / \
    1   2 3   4
    

    And wanted to flip the interval [1, 3], you would flip nodes B and 3. To get the state of 2, you would xor values in A, B and 2 and find out that the node is flipped.

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

Sidebar

Related Questions

I have a problem which has been torturing for many hours. I need to
I have a problem which I don't know how to fix. It has to
I have a problem which seems very similar to the one described in http://markmail.org/message/6rlrzkgyx3pspmnf
I have a problem which has an solution that can be solved by iteration,
I have a problem which I cannot seem to be able to resolve: I'm
I have a problem which has been driving me mad for days - I've
I have a problem which has received plenty of attention which, despite much googling,
I have a problem which requires me to parse several log files from a
I have a problem which I think is related to forward declarations, but perhaps
I have a problem which requires a reversable 1:1 mapping of keys to values.

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.