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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:21:55+00:00 2026-05-13T20:21:55+00:00

There is an array of M binary numbers and each of them is in

  • 0

There is an array of M binary numbers and each of them is in state ‘0’ or ‘1’. You can perform several steps in changing the state of the numbers and in each step you are allowed to change the state of exactly N sequential numbers. Given the numbers M, N and the array with the members of course, you are about to calculate the minimal number of steps needed to turn all the binary numbers into one state – 1 or 0.


If M = 6 and N = 3 and the array is 1 0 0 1 0 0, then the solution will be 2.
Explanation: We can flip them so they all be 1 in two steps: we flipped from index 2 to 4 and we transform the array to 111000, and then flip the last three (N) 0s to 1.

  • 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-13T20:21:56+00:00Added an answer on May 13, 2026 at 8:21 pm

    If I’ve understood the question correctly, a little bit of thought will convince you that even dynamic programming is not necessary — the solution is entirely trivial.

    This is the question as I understand it: you are given an array a[1]..a[M] of 0s and 1s, and you are allowed operations of the form Sk, where Sk means to flip the N elements a[k],a[k+1],…a[k+N-1]. This is only defined for 1≤k≤M-N+1, clearly.

    By performing a sequence of these operations Sk, you want to reach either the state of all 0s, or all 1s. We’ll solve for both separately, and take the smaller number. So suppose we want to make them all 0 (the other case, all 1s, is symmetric).

    The crucial idea is that you would never want to perform any operation Sk more than once (doing it twice is equivalent to not doing it at all), and that the order of operations does not matter. So the question is only to determine which subset of the operations you perform, and this is easy to determine. Look at a[1]. If it’s 0, then you know you won’t perform S1. Else, you know you must perform S1 (as this is the only operation that will flip a[1]), so perform it — this will toggle all bits from a[1] to a[N]. Now look at a[2] (after this operation). Depending on whether it is 1 or 0, you know whether you will perform S2 or not. And so on — you can determine how many operations (and which) to perform, in linear time.

    Edit: Replaced pseudo-code with C++ code, since there’s a C++ tag. Sorry for the ugly code; when in “contest mode” I revert to contest habits. 🙂

    #include <iostream>
    using namespace std;
    const int INF = 20000000;
    #define FOR(i,n) for(int i=0,_n=n; i<_n; ++i)
    
    int flips(int a[], int M, int N, int want) {
      int s[M]; FOR(i,M) s[i] = 0;
      int sum=0, ans=0;
      FOR(i,M) {
        s[i] = (a[i]+sum)%2 != want;
        sum += s[i] - (i>=N-1?s[i-N+1]:0);
        ans += s[i];
        if(i>M-N and s[i]!=0) return INF;
      }
      return ans;
    }
    
    int main() {
      int M = 6, N = 3;
      int a[] = {1, 0, 0, 1, 0, 0};
      printf("Need %d flips to 0 and and %d flips to 1\n",
             flips(a, M, N, 0), flips(a, M, N, 1));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any speed/space/general performance gains in using array based implementation of a binary
I have multiple binary trees stored as an array. In each slot is either
I want to convert decimal numbers to binary numbers. I want to store them
Consider there are three array list each of equal length and having positive, negative
There is an array of user states stored in the session. This works: <?php
Scenario: If there is an array of integers and I want to get array
I am making a page in which there will be array of images. I
What is the appropriate function that shows how many are there in an array?
I have a really tough situation in here. There is an array of these
I'm trying to write a snake game. It's pretty simple, there's an array of

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.