binary-search

Deciding mid in binary search

拟墨画扇 提交于 2020-04-30 11:38:50
问题 I was practising a questions on hackerearth : https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/monk-and-special-integer-code-monk/ In this question I had written a binary search code where I had used: int mid=(low+high)/2 My loop got stuck here and so I was getting TLE for some cases. Having realised the problem (that repeatedly low was being chosen) I changed the mid to low +(high-low+1)/2 and with this change whole test cases passed. (Code

Searching triplets in two arrays

情到浓时终转凉″ 提交于 2020-03-25 17:57:58
问题 Given two arrays a and b of size n . I have to choose one element from a and two elements from b such that. a[i] * a[i] = b[j] * b[k] . I am able to find BruteForce in O(n^2 log(n)) traversing both the arrays and binary searching the value. But dont know how to improve it even more. How do I count all these number where elements range from 1 <= a[i] <= 10^6 and 1 <= n <= 10^6 . 回答1: Well, I can think in a O(n²) solution. unordered_multiset S = {} for each element B in b { S = S ∪ B } First of

Searching triplets in two arrays

亡梦爱人 提交于 2020-03-25 17:56:18
问题 Given two arrays a and b of size n . I have to choose one element from a and two elements from b such that. a[i] * a[i] = b[j] * b[k] . I am able to find BruteForce in O(n^2 log(n)) traversing both the arrays and binary searching the value. But dont know how to improve it even more. How do I count all these number where elements range from 1 <= a[i] <= 10^6 and 1 <= n <= 10^6 . 回答1: Well, I can think in a O(n²) solution. unordered_multiset S = {} for each element B in b { S = S ∪ B } First of

Is there a more efficient search factor than midpoint for binary search?

痞子三分冷 提交于 2020-01-24 00:23:10
问题 The naive binary search is a very efficient algorithm: you take the midpoint of your high and low points in a sorted array and adjust your high or low point accordingly. Then you recalculate your endpoint and iterate until you find your target value (or you don't, of course.) Now, quite clearly, if you don't use the midpoint, you introduce some risk to the system. Let's say you shift your search target away from the midpoint and you create two sides - I'll call them a big side and small side.

BinarySearch array of objects by ID

偶尔善良 提交于 2020-01-23 07:07:04
问题 Good day! I have a List of ValueObj: class ValueObj { int ID; float value; } How to get binary search objects by id? (List tempValues) I make ValueComparer class,but dont know am i right? class ValueComparer<ValueObj> { public int Compare(ValueObjx, ValueObjy) { if (x == y) return 0; if (x == null) return -1; if (y == null) return 1; return -1; ///??? } } I need to sort List by ID. Like that?: tempValues.Sort(new ValueComparer()); And how to use BinarySearch? 回答1: The List class in C# has a

BinarySearch array of objects by ID

拜拜、爱过 提交于 2020-01-23 07:05:06
问题 Good day! I have a List of ValueObj: class ValueObj { int ID; float value; } How to get binary search objects by id? (List tempValues) I make ValueComparer class,but dont know am i right? class ValueComparer<ValueObj> { public int Compare(ValueObjx, ValueObjy) { if (x == y) return 0; if (x == null) return -1; if (y == null) return 1; return -1; ///??? } } I need to sort List by ID. Like that?: tempValues.Sort(new ValueComparer()); And how to use BinarySearch? 回答1: The List class in C# has a

binary search found index is incorrect

 ̄綄美尐妖づ 提交于 2020-01-17 13:43:29
问题 I have this code to binary search. public class BinarySearch { private static int location; private static int a = 14; static int[] numbers = new int[]{3, 6, 7, 11, 14, 16, 20, 45, 68, 79}; public static int B_Search(int[] sortedArray, int key) { int lowB = 0; int upB = sortedArray.length; int mid; while (lowB < upB) { mid = (lowB + upB) / 2; if (key < sortedArray[mid]) { upB = mid - 1; } else if (key > sortedArray[mid]) { lowB = mid + 1; } else { return mid; } } return -1; } public static

binary search found index is incorrect

对着背影说爱祢 提交于 2020-01-17 13:42:51
问题 I have this code to binary search. public class BinarySearch { private static int location; private static int a = 14; static int[] numbers = new int[]{3, 6, 7, 11, 14, 16, 20, 45, 68, 79}; public static int B_Search(int[] sortedArray, int key) { int lowB = 0; int upB = sortedArray.length; int mid; while (lowB < upB) { mid = (lowB + upB) / 2; if (key < sortedArray[mid]) { upB = mid - 1; } else if (key > sortedArray[mid]) { lowB = mid + 1; } else { return mid; } } return -1; } public static

binary search found index is incorrect

会有一股神秘感。 提交于 2020-01-17 13:42:24
问题 I have this code to binary search. public class BinarySearch { private static int location; private static int a = 14; static int[] numbers = new int[]{3, 6, 7, 11, 14, 16, 20, 45, 68, 79}; public static int B_Search(int[] sortedArray, int key) { int lowB = 0; int upB = sortedArray.length; int mid; while (lowB < upB) { mid = (lowB + upB) / 2; if (key < sortedArray[mid]) { upB = mid - 1; } else if (key > sortedArray[mid]) { lowB = mid + 1; } else { return mid; } } return -1; } public static

binary search on array of integer in java

非 Y 不嫁゛ 提交于 2020-01-16 18:11:09
问题 I know this is a very famous question and lots of answers provided already, but I am trying to implement binary search algorithm in java of my own. First of all getting following compilation error, why?? This method must return a result of type int Second how this approach differs from this famous solution public static int binarySearch(int test[], int num){ int midLength = test.length/2; int fullLength = test.length; if(num > test[midLength]){ int newArray[] = new int[fullLength - midLength]