Sort by

recency

|

2801 Discussions

|

  • + 0 comments

    Python : took time to fix a silly miss in step if binnum[i] == '1' .. missed quotes

    bin_num = ''
    temp_num = n
    while temp_num > 0:
        bin_num = str(temp_num % 2) + bin_num
        temp_num //= 2
    
    count = 0
    max_count = 0
    i=0
    for i in range(len(bin_num)):
        if bin_num[i] == '1':
            count += 1
            if count > max_count:
                max_count = count
        else:
            count = 0
    
    print(max(max_count, count))
    
  • + 1 comment

    if name == 'main':

    n = int(input().strip())
    binary = bin(n)[2:]
    max_consecutive = max(len(ones) for ones in binary.split('0'))
    print(max_consecutive)
    
  • + 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;
    

    }

  • + 0 comments
    #include <bits/stdc++.h>
    
    using namespace std;
    
    string ltrim(const string &);
    string rtrim(const string &);
    
    
    
    int main()
    {
        int bin[32];
        int concount[10];
        int n;
        int j = 0;
        int count =0;
        cin >> n;
        int x=0;
        
        while (n>0){
        
            int i = n%2;
            n = n/2;
            bin[j] = i;
            j++;
        }
        
        for (int i = j-1; i>=0; i--){
            if(bin[i] == 1){
                count += 1;
            }else if (bin[i] == 0 && count > 0){
                
                concount[x] = count;
                x++;
                count = 0;
            }
        }  
        // If there were trailing 1s
        if (count > 0) {
            concount[x] = count; // Store the last count of consecutive 1s
            x++;
        }
        int maxvalue = concount[0];
        // Output the counts of consecutive 1s
    
        for (int i = 0; i < x; i++) { // Use x to determine how many counts to print     
        if (concount[i] > maxvalue) {
            maxvalue = concount[i]; // Update maxValue if a larger value is found
            }
            
        }
        cout << maxvalue;
        cout << endl;
    
        return 0;
    }
    
  • + 0 comments

    i dont know how i got this: python

    if __name__ == '__main__':
        n = int(input().strip())
        if n == 0:
            print("0")
        else:
            a=[]
            while n>0:
                b=n % 2
                a.append(b)
                n= n//2
            a.reverse()
            cout=1
            c=[]
            for i in range(1, len(a)):
                if a[i]==a[i-1] and a[i]==1:
                    cout+=1
                    if i == len(a)-1:
                        c.append(cout)
                else:
                    if a[i]== 1 or a[i-1]:
                        c.append(cout)
                        cout=1
            print(max(c))