0% found this document useful (0 votes)
7 views3 pages

Binary Search - 1

Uploaded by

Utkarsh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Binary Search - 1

Uploaded by

Utkarsh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignments

Binary Search - 1
Q1. Given a sorted array of n elements and a target ‘x’. Find the last occurrence of ‘x’ in the array. If ‘x’

does not exist return -1.

Input 1: arr[] = {1,2,3,3,4,4,4,5} , x = 4

Output 1: 6

Q2. Given a sorted binary array, efficiently count the total number of 1’s in it.

Input 1 : a = [0,0,0,0,1,1]

Output 1: 2

Q3. Given a matrix having 0-1 only where each row is sorted in increasing order, find the row with the maximum
number of 1’s.

Input matrix : 0 1 1 1

0 0 1 1

1 1 1 1 // this row has maximum 1s

0 0 0 0

Output: 2

Q4. Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n]

inclusive in sorted order.

There is only one repeated number in nums, return this repeated number.

Input 1: arr[] = {1,2,3,3,4}

Output 1: 3

Input 2: arr[] = {1,2,2,3,4,5}

Output 2: 2

Q5. Given a number ‘n’. Predict whether ‘n’ is a valid perfect square or not.

Input 1: n = 36

Output 1: yes

Input 2: n = 45

Output 2: no

Q6. You have n coins and you want to build a staircase with these coins. The staircase consists of k

rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.

Given the integer n, return the number of complete rows of the staircase you will build.

Java
C++ &+ DSA
Example 1:

Input: n = 5

Output: 2

Explanation: Because the 3rd row is incomplete, we return 2.

Example 2:

Input: n = 8

Output: 3

Explanation: Because the 4th row is incomplete, we return 3.

Note:- Please try to invest time doing the assignments which are necessary to build a strong foundation. Do not
directly Copy Paste using Google or ChatGPT. Please use your brain.

Java
C++ &+ DSA
DSA

You might also like