You are viewing a single comment's thread. Return to all comments →
int maxConsectiveOnes(int n){ string binary = bitset<32>(n).to_string();
int maxCount = 0; int currentCount = 0; for(char bit : binary){ if(bit == '1'){ currentCount++; maxCount = max(currentCount, maxCount); } else { currentCount = 0; } } return maxCount;
}
int main() { string n_temp; getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp))); int result = maxConsectiveOnes(n); cout << result; return 0;
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
In C++:
int maxConsectiveOnes(int n){ string binary = bitset<32>(n).to_string();
}
int main() { string n_temp; getline(cin, n_temp);
}