Answers for "how to find the maximum number of 1s in a binary"

C
1

how to find the maximum number of 1s in a binary

/**
* findMaxConsecutiveOnes - A function that returns the max No. of 
*							consecutive ones in a string
* @length: the length of the calculated 1s
* @maxLength: maximum length i=of the calculated 1s
* MAX: Returns the largest of 2 values
*/

#define MAX(a,b) (a > b) ? a : b

int findMaxConsecutiveOnes(int* nums, int numsSize){
        int length = 0;
        int maxLength = 0;
        
        for(int i = 0; i < numsSize; i++){
                if(nums[i] == 1){
                        length++;
                        maxLength = MAX(length,maxLength);
                }
                else{
                        length = 0;
                }
        }
        return(maxLength);
}
Posted by: Guest on August-18-2021

Code answers related to "how to find the maximum number of 1s in a binary"

Code answers related to "C"

Browse Popular Code Answers by Language