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

Solutions on MaxInterview for how to find the maximum number of 1s in a binary by the best coders in the world

showing results for - "how to find the maximum number of 1s in a binary"
Levy
20 Jul 2016
1/**
2* findMaxConsecutiveOnes - A function that returns the max No. of 
3*							consecutive ones in a string
4* @length: the length of the calculated 1s
5* @maxLength: maximum length i=of the calculated 1s
6* MAX: Returns the largest of 2 values
7*/
8
9#define MAX(a,b) (a > b) ? a : b
10
11int findMaxConsecutiveOnes(int* nums, int numsSize){
12        int length = 0;
13        int maxLength = 0;
14        
15        for(int i = 0; i < numsSize; i++){
16                if(nums[i] == 1){
17                        length++;
18                        maxLength = MAX(length,maxLength);
19                }
20                else{
21                        length = 0;
22                }
23        }
24        return(maxLength);
25}
similar questions