PHP Program to Count 1's in a sorted binary array Last Updated : 23 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0}Output: 2Input: arr[] = {1, 1, 1, 1, 1, 1, 1}Output: 7Input: arr[] = {0, 0, 0, 0, 0, 0, 0}Output: 0A simple solution is to linearly traverse the array. The time complexity of the simple solution is O(n). We can use Binary Search to find count in O(Logn) time. The idea is to look for last occurrence of 1 using Binary Search. Once we find the index last occurrence, we return index + 1 as count.The following is the implementation of above idea. PHP <?php // PHP program to count one's in a // boolean array /* Returns counts of 1's in arr[low..high]. The array is assumed to be sorted in non-increasing order */ function countOnes( $arr, $low, $high) { if ($high >= $low) { // get the middle index $mid = $low + ($high - $low)/2; // check if the element at middle // index is last 1 if ( ($mid == $high or $arr[$mid+1] == 0) and ($arr[$mid] == 1)) return $mid+1; // If element is not last 1, recur for // right side if ($arr[$mid] == 1) return countOnes($arr, ($mid + 1), $high); // else recur for left side return countOnes($arr, $low, ($mid -1)); } return 0; } /* Driver code */ $arr = array(1, 1, 1, 1, 0, 0, 0); $n = count($arr); echo "Count of 1's in given array is " , countOnes($arr, 0, $n-1); // This code is contributed by anuj_67. ?> OutputCount of 1's in given array is 4Complexity Analysis: Time complexity: O(Logn)Space complexity: o(log n) (function call stack) Please refer complete article on Count 1's in a sorted binary array for more details! Comment More infoAdvertise with us Next Article Count 1's in a sorted binary array K kartik Follow Improve Article Tags : PHP Binary Search binary-string Practice Tags : Binary Search Similar Reads Javascript Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0} Output: 2 Input: arr[] = {1, 1, 1, 1, 1, 1, 1} Output: 7 Input: arr[] = {0, 0, 0, 0, 0, 0, 0} Output: 0A simple solution is to traverse the array linearly. The time comp 3 min read Count 1's in a sorted binary array Given a binary array arr[] of size n, which is sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = [1, 1, 0, 0, 0, 0, 0]Output: 2Explanation: Count of the 1's in the given array is 2.Input: arr[] = [1, 1, 1, 1, 1, 1, 1]Output: 7Input: arr[] = [0, 0, 0, 0, 0, 0, 0] 7 min read PHP Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on âPRACTICE 2 min read PHP Program to Count number of binary strings without consecutive 1's Write a PHP program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3Explanation: The 3 strings are 00, 01, 10 Input: N = 3Output: 5Explanation: The 5 strings are 000, 001, 010, 2 min read How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte 3 min read Find the index of first 1 in a sorted array of 0's and 1's Given a sorted array consisting 0's and 1's. The problem is to find the index of first '1' in the sorted array. It could be possible that the array consists of only 0's or only 1's. If 1's are not present in the array then print "-1". Examples : Input : arr[] = {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}Output : 15 min read Like