We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You need to keep track of the current set of consecutive ones that you are reading, as well as the maximum number ever found. Hence the comparison between max and sum.
In your example for 1111101011:
sum = 0, max = 0;
Reading from right to left you get:
sum = 2, max = 0; // two consecutive 1s
is sum > max? Yes it is, set max to sum and reset sum.
sum = 1, max = 2;
Is sum > max? No. So max remains as 2.
sum = 5, max = 2;
Is sum > max? Yes, set max to sum and reset sum.
Max is now 5 which is the largest amount of consective 1s found.
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
You need to keep track of the current set of consecutive ones that you are reading, as well as the maximum number ever found. Hence the comparison between max and sum. In your example for 1111101011: sum = 0, max = 0; Reading from right to left you get:
sum = 2, max = 0; // two consecutive 1s is sum > max? Yes it is, set max to sum and reset sum.
sum = 1, max = 2; Is sum > max? No. So max remains as 2.
sum = 5, max = 2; Is sum > max? Yes, set max to sum and reset sum.
Max is now 5 which is the largest amount of consective 1s found.