PHP Program to Count 1's in a sorted binary array Last Updated : 23 Jul, 2024 Comments Improve Suggest changes 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 PHP Program to Count 1's in a sorted binary array 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 Maximum consecutive oneâs (or zeros) in a binary array Given an array arr[] consisting of only 0's and 1's, the task is to find the count of a maximum number of consecutive 1's or 0's present in the array.Examples : Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}Output: 4Explanation: The maximum number of consecutive 1's in the array is 4 from index 7 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 Count of 1's after flipping the bits at multiples from 1 to N Given N sized binary array A[] containing all 0's initially. The task is to find the final count of 1's after flipping the bits at multiples from 1 to N. Examples: Input: A[] = [0, 0, 0, 0]Output: 2Explanation: Flipping bits at multiples of 1 - [1, 1, 1, 1]Flipping bits at multiples of 2 - [1, 0, 1, 9 min read Count of elements that are binary searchable in the given array Given an array arr[] consisting of N integers, the task is to find the maximum count of integers that are binary searchable in the given array. Examples: Input: arr[] = {1, 3, 2}Output: 2Explanation: arr[0], arr[1] can be found. Input: arr[] = {3, 2, 1, 10, 23, 22, 21}Output: 3Explanation: arr[1], a 13 min read Count of Unique elements in a very large sorted Array Given a sorted array arr[] of size N, the task is to find the number of unique elements in this array. Note: The array is very large, and unique numbers are significantly less. i.e., (unique elements <<size of the array). Examples: Input: arr[] = {1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 5, 5, 7, 7, 8 11 min read Like