0% found this document useful (0 votes)
10 views

Java Binary Search

The document contains a set of assignment questions related to binary search and algorithms in Java. It includes problems such as finding the last occurrence of an element in a sorted array, counting 1's in a binary array, and determining if a number is a perfect square. Additional tasks involve searching in a matrix and handling rotated sorted arrays.

Uploaded by

sibas8848
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)
10 views

Java Binary Search

The document contains a set of assignment questions related to binary search and algorithms in Java. It includes problems such as finding the last occurrence of an element in a sorted array, counting 1's in a binary array, and determining if a number is a perfect square. Additional tasks involve searching in a matrix and handling rotated sorted arrays.

Uploaded by

sibas8848
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/ 4

JAVA

BinarySearch

Assignment Questions
A ssignment Questions
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.

Example 1:
Input: n = 5

Output: 2

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

Java + DSA
Assignment Questions
Example 2:
Input: n = 8

Output: 3

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

Q7. Write a program to apply binary search in array sorted in decreasing order.

Q8. You have a sorted array of infinite numbers, how would you search an element in the array?

Q9. You are given an m x n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order.
The first integer of each row is greater than the last integer of the previous row.
Given an integer target , return true if target is in matrix or false otherwise.

You must write a solution in O(log(m * n)) time complexity.

Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3

Output: true

Example 2:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13

Output: false

Q10. There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).

Before being passed to your function, nums is rotated at an unknown pivot index k ( 0 <= k< nums.length )
such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1],
nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot
index 5 and become [4,5,6,6,7,0,1,2,4,4] . Given the array nums after the rotation and an integer target ,
return true if target is in nums , or false if it is not in nums .
You must decrease the overall operation steps as much as possible.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0

Output: true

Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3

Output: false

Java + DSA
THANK

YOU !

You might also like