Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions allalgorithms/numeric/countOnes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: UTF-8 -*-
#
# count ones in a array
# The All ▲lgorithms library for python
#
# Contributed by: Natan Lucena
# Github: @NatanLucena
#


def countOnes(arr,low,high):

if high>=low:

mid = low + (high-low)/2

if ((mid == high or arr[mid+1]==0) and (arr[mid]==1)):
return mid+1

if arr[mid]==1:
return countOnes(arr, (mid+1), high)

return countOnes(arr, low, mid-1)

return 0