kunal-kushwaha / DSA-Bootcamp-Java

This repository consists of the code samples, assignments, and notes for the Java data structures & algorithms + interview preparation bootcamp of WeMakeDevs.
https://www.youtube.com/playlist?list=PL9gnSGHSqcnr_DxHsP7AW9ftq0AtAyYqJ
17.25k stars 11.08k forks source link

Wrong Output in Ceiling.java (BINARY SEARCH) #1076

Open mr-mods-yg opened 1 year ago

mr-mods-yg commented 1 year ago

Suppose we have an array : int[] nums = {3,3,6,6,7,8,8,9}; and the target is 6 ---------------s----m------e According to previous code, it will return index 3, but it is wrong in this case. ans should be index 2 as this is first smallest element greator than or equal to target. image I can fix this code if u assign me to fix it.

Sahilkumar19 commented 1 year ago

Suppose we have an array : int[] nums = {3,3,6,6,7,8,8,9}; ---------------s----m------e According to previous code, it will return index 3, but it is wrong in this case. ans should be index 2 as this is first smallest element greator than or equal to target. image I can fix this code if u assign me to fix it.

what is target here?

mr-mods-yg commented 1 year ago

Suppose we have an array : int[] nums = {3,3,6,6,7,8,8,9}; ---------------s----m------e According to previous code, it will return index 3, but it is wrong in this case. ans should be index 2 as this is first smallest element greator than or equal to target. image I can fix this code if u assign me to fix it.

what is target here?

target is 6 , I forget to write it there 📟

Sahilkumar19 commented 1 year ago

How does it matter whether we are getting index 2 or 3?

mr-mods-yg commented 1 year ago

How does it matter whether we are getting index 2 or 3?

It is a sorted array, the ceiling number is the least int number greater than or equal to the given number. so the least int number greater than or equal to is ofcourse 6 but in sorted array if u check from left to right as going in sorted way .The ceiling num is at index 2.

Sahilkumar19 commented 1 year ago

How does it matter whether we are getting index 2 or 3?

It is a sorted array, the ceiling number is the least int number greater than or equal to the given number. so the least int number greater than or equal to is ofcourse 6 but in sorted array if u check from left to right as going in sorted way .The ceiling num is at index 2.

ok ,thanks