• + 0 comments

    In C++:

    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;
    

    }