0

The question was "Max Continuous Series of 1s"

You are given an array of 1s and 0s. And you are given an integer M, which signifies number of flips allowed.

Find the position of zeros which when flipped will produce maximum continuous series of 1s.

I used sliding window approach to solve the answer, but there is something I noticed that if I write start=i;end=j; its wrong, but start=i,end=j; is right.

What's the difference between the execution of them?

vector<int> Solution::maxone(vector<int> &A, int B) {
    int n=A.size();
    int start=0,end=0,count=0,i,j;
    
    for(i=0,j=0;j<n;){
        if(B>=0 && !A[j++])
       
        B--;
        
        if(B<0 && !A[i++])
        
        B++;
        
        if(end-start<j-i)
        start=i,end=j;    // Here I get wrong ans if I write start=i;end=j;
    }
    
    vector<int> v;
    while(start<end)
    v.push_back(start++);
    
    return v;
    
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Because you don't use brackets `{ }` in the `if`. So `start=i,end=j;` is a single line and part of the `if` but `start=i;end=j;` is 2 lines and only the first one belongs to the `if`. – 001 Jun 30 '21 at 17:34
  • @mocha_nirvana No, "_we_" never do. Why wouldn't you use brackets? – Ted Lyngmo Jun 30 '21 at 17:38
  • Personally, I _always_ use brackets - even for "one liners". – 001 Jun 30 '21 at 17:40
  • 3
    Let me correct that for you: *Ok so to execute such a line after `if` and we* **always** *use brackets we* **never** *use a comma.* – Eljay Jun 30 '21 at 17:41
  • 1
    the comma operator is a weird beast, often uses are accidentally, and often wrong. It has some use-cases, but this isnt one. In some sense `start=i,end=j;` is only correct by luck. – 463035818_is_not_an_ai Jun 30 '21 at 17:43
  • Got it :) Thanks! I am new and still learning. I will keep this in mind. – mocha_nirvana Jun 30 '21 at 17:44
  • 3
    "to execute such a line after if and not use brackets we use a comma" - Would a great way to leave a landmine that could trip up future developers (including yourself) and then introduce subtle bugs. I would never let this pass code review and get checked in to any real codebase. – TheUndeadFish Jun 30 '21 at 17:44
  • 2
    Generalization: Don't surprise people. Surprised people make bugs. – user4581301 Jun 30 '21 at 17:52
  • 4
    please dont change the question after you got answers to ask for something else. The question you now put in the title already has been asked I believe and the answers you got adress a slightly different question – 463035818_is_not_an_ai Jun 30 '21 at 17:53
  • for more on comma operator see eg [here](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work) or [here](https://stackoverflow.com/questions/5602112/when-to-overload-the-comma-operator) or [here](https://stackoverflow.com/search?q=%5Bc%2B%2B%5D+comma+operator) – 463035818_is_not_an_ai Jun 30 '21 at 17:54

3 Answers3

7

In case of start=i;end=j; you get

if(end-start<j-i)
  start=i;
end = j;

because ; make end=j; an another statement

start=i,end=j; is a one statement, because comma is a Comma operator

Solution: use if() {}

slsy
  • 1,257
  • 8
  • 21
2

start = i, end = j; is a single statement. start = i; end = j; is two statements. An if controls the next statement. So:

if (false)
    start = i, end = j;

doesn’t modify start or end.

if (false)
    start = i; end = j;

modifies end because end = j is the second statement after the if, so the if does not affect it.

if (false) {
    start = i; end = j;
}

doesn’t modify start or end because both assignments are inside the compound statement delimited by the curly braces. The compound statement is the single statement that the if controls.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

Firstly, the problem is simple if you are not using curly brackets {} due without curly brackets so only one statement (not line) is considered under if as true statement so as soon as you put ; it will complete the one statement.

if()
    <true statement>;

if(){
   <true statement>;
   <true statement>;
}

Secondly, if you format your code properly then you will easily able to get the bug.

Finally, when you keep , instead of ; then it will be considered as single statement

HandsomeCoder
  • 726
  • 5
  • 8
  • 1
    Actually, an `if` always controls exactly one statement. When you write `if () { … }` the curly braces designate a compound statement, which is a single statement that encloses zero or more statements. – Pete Becker Jun 30 '21 at 18:44