Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Binary Search

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

Binary Search Implementations and common errors

We suggest you do not use library functions for binary search as its very likely that you will be explicitly asked to implement the binary search in interviews.

 

 
Implementation of Binary Search
// binary search example in C/C++
/* here Arr is an of integer type, n is size of array 
   and target is element to be found */

int binarySearch(int *Arr, int n, int target) {

	//set stating and ending index
	int start = 0, ending = n-1;

	while(start <= ending) {
		// take mid of the list
		int mid = (start + end) / 2;

		// we found a match
		if(Arr[mid] == target) {
			return mid; 
		}
		// go on right side
		else if(Arr[mid] < target) {
			start = mid + 1;
		}
		// go on left side
		else {
			end = mid - 1;
		}
	}
	// element is not present in list
	return -1;
}
// binary search example in Java
/* here Arr is an of integer type, n is size of array 
   and target is element to be found */

int binarySearch(int Arr[], int n, int target) {

	//set stating and ending index
	int start = 0, ending = n-1;

	while(start <= ending) {
		// take mid of the list
		int mid = (start + end) / 2;

		// we found a match
		if(Arr[mid] == target) {
			return mid; 
		}
		// go on right side
		else if(Arr[mid] < target) {
			start = mid + 1;
		}
		// go on left side
		else {
			end = mid - 1;
		}
	}
	// element is not present in list
	return -1;
}
# binary search example in Python
# here Arr is an of integer type, n is size of array 
#	and target is element to be found
def binarySearch(Arr, n, target) :
	#set stating and ending index
	start, end = 0, n-1

	while start <= end :
		mid = (start + end) / 2
		# we found a match
		if Arr[mid] == target :
			return mid
		# go on right side
		elif Arr[mid] < target :
			start = mid + 1
		# go on left side
		else :
			end = mid - 1;
	# element is not present in list
	return -1


Walkthrough Examples :

COUNTELEMENTS ROTATEDARRAY

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Binary Search Problems

Simple binary search
Search answer
Search step simulation
Problem Score Companies Time Status
Implement Power Function 275
59:44
Simple Queries 300
65:26
Sort modification
Problem Score Companies Time Status
Median of Array 325 87:06
Rotated Sorted Array Search 325 56:53
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
Phone Number *
OTP will be sent to this number for verification
+91 *
+91
Change Number
Phone Number *
OTP will be sent to this number for verification
+91 *
+91
Change Number
Graduation Year *
Graduation Year *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test