• 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

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Generate largest number arranging a no. of given non negative integer numbers

LeetCode: Binary Tree Maximum Path Sum

LeetCode : Word Search

VMWare SDEII Interview

Coin Collection Dynamic Programming

Print Power Set of a Set

K’th Largest Element in BST when modification to BST is not allowed

Templates in C++

Count Possible Decodings of a given Digit Sequence

Reversal of LinkedList

Handle duplicates in Binary Search Tree

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

Connect n ropes with minimum cost

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

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

25 horses 5 tracks Find 3 fastest puzzle

Python Dictionaries

Edit Distance ( Dynamic Programming )

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

FizzBuzz Solution C C++

Convert number to words java

Minimum insertions to form a palindrome

Introduction To Number Theory ( Part 1 )

Leetcode: Merge Intervals

simple sql injection

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Python List

Doubly linked list

Cisco Hiring Event 21st – 22nd Feb 2015

Copyright © 2026 · Genesis Framework · WordPress · Log in