Code Lab

Count the ones

EasyArrays3 min

An array holds only zeros and ones, and it is sorted. All zeros come first, then all ones. Write a function countOnes(bits) that returns how many ones the array has.

Examples

countOnes([0, 0, 1, 1, 1])

Returns 3

countOnes([0, 0, 0])

Returns 0

countOnes([1, 1])

Returns 2

Asked at Sprinklr · Source

Loading the editor...
  1. InputcountOnes([ 0, 0, 1, 1, 1 ])
    Expected3
  2. InputcountOnes([ 0, 0, 0 ])
    Expected0
  3. InputcountOnes([ 1, 1 ])
    Expected2

Hidden tests run on Submit.