• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

November 14, 2014 by Dhaval Dave

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1
Example : 00001111
Output : 4Method 1 : in O(n) we can do it . with sequential search.

Method 2 : Binary search.

Yes binary search is useful, as data is sorted.
Just we need to change its conditions for 0 and 1.
lets have a look at Function firstOccurance.

firstOccurrance(int[] sortedBins) {
    int lower = 0;
    int upper = sortedBins.length - 1;
    while (lower <= upper) {
        int middle = 1 + (upper - lower) / 2;
        if (sortedBins[middle] == 0) {
            lower = middle + 1;
        } else {
            upper = middle - 1;
        } 
        if (sortedBins[lower] == 1) {
            return lower;
        }
    }
    return -1;
}

Similar Articles

Filed Under: Adobe Interview Questions, Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array, Binary Search

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

C++ OOPs Part2

Test Cases for Round Function

TicTacToe Game As Asked in Flipkart

The greedy coins game Dynamic Programming

C Program for TAIL command of UNIX

Find two non repeating elements in an array of repeating elements

Generate next palindrome number

Given a sorted array and a number x, find the pair in array whose sum is closest to x

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Printing Longest Common Subsequence

Practo Hiring Experience

Find Percentage of Words matching in Two Strings

VMWare Openings

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Flipkart SDET Interview Experience

Longest Increasing Subsequence

Common Ancestor in a Binary Tree or Binary Search Tree

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Adobe Interview Questions 8 month Exp

The Magic HackerEarth Nirvana solutions Hiring Challenge

Calculate price of parking from parking start end time prices

Print vertical sum of all the axis in the given binary tree

Knight Tour Problem (Graph – Breadth First Search)

Edit Distance ( Dynamic Programming )

Find the smallest window in a string containing all characters of another string

How Radix sort works

FizzBuzz Solution C C++

Given a float number convert it into the string WITHOUT using any inbuilt Function

Find position of the only set bit

Copyright © 2026 · Genesis Framework · WordPress · Log in