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 Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages, making it essential for building data-driven websites. It allows developers to handle database interactions, session 9 min read Longest Increasing Subsequence (LIS) Given an array arr[] of size n, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in which the elements of the subsequence are sorted in increasing order.Examples: Input: arr[] = [3, 10, 2, 1, 20]Output: 3Explanation: The longest increa 14 min read Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo 15+ min read PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi 8 min read Linear Search vs Binary Search Prerequisite: Linear SearchBinary SearchLINEAR SEARCH Assume that item is in an array in random order and we have to find an item. Then the only way to search for a target item is, to begin with, the first position and compare it to the target. If the item is at the same, we will return the position 11 min read Search in a Sorted and Rotated Array Given a sorted and rotated array arr[] of n distinct elements, the task is to find the index of given key in the array. If the key is not present in the array, return -1. Examples: Input: arr[] = [5, 6, 7, 8, 9, 10, 1, 2, 3], key = 3Output: 8Explanation: 3 is present at index 8 in arr[].Input: arr[] 15+ min read Binary Search in Java Binary search is a highly efficient searching algorithm used when the input is sorted. It works by repeatedly dividing the search range in half, reducing the number of comparisons needed compared to a linear search. Here, we are focusing on finding the middle element that acts as a reference frame t 6 min read PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien 5 min read Program for Square Root of Integer Given a positive integer n, find its square root. If n is not a perfect square, then return floor of ân.Examples : Input: n = 4Output: 2Explanation: The square root of 4 is 2.Input: n = 11Output: 3Explanation: The square root of 11 lies in between 3 and 4 so floor of the square root is 3.Table of Co 13 min read Like