Skip to content

Commit 9cbb455

Browse files
Add binary search implementation to find largest integer index
This commit introduces the `get_index` function, which uses binary search to identify the index of the largest integer in an array. It includes updates to the `ArrayReader` class and adds several unit tests to validate edge cases and standard scenarios. This ensures robustness and correctness of the implementation.
1 parent 1693208 commit 9cbb455

File tree

1 file changed

+96
-5
lines changed

1 file changed

+96
-5
lines changed

Binary Search/Standard Search/find_the_index_of_the_larger_integer.py

+96-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,35 @@
1919

2020

2121
def get_index(reader: "ArrayReader") -> int:
22-
pass
22+
# Get the length of the array from the reader
23+
n = reader.length()
24+
25+
# Initialize the left and right pointers for binary search
26+
left, right = 0, n - 1
27+
28+
# Perform binary search to find the index
29+
while left < right:
30+
# Calculate the middle index
31+
mid = (left + right) // 2
32+
33+
# Determine if the current subarray has an even number of elements
34+
if (right - left + 1) % 2 == 0:
35+
# Compare the two halves of the subarray
36+
compare = reader.compareSub(left, mid, mid + 1, right)
37+
else:
38+
# Compare the two halves including the middle element in both halves
39+
compare = reader.compareSub(left, mid, mid, right)
40+
41+
# If the left half is smaller, the element is in the right half
42+
if compare < 0:
43+
left = mid + 1
44+
continue
45+
46+
# If the left half is larger or equal, the element is in the left half
47+
right = mid
48+
49+
# Return the index of the element
50+
return left
2351

2452

2553
class ArrayReader:
@@ -35,10 +63,10 @@ def compareSub(self, l: int, r: int, x: int, y: int) -> int:
3563
"""
3664
Compares the sum of arr[l..r] with the sum of arr[x..y].
3765
38-
:param l: Start index of the first range
39-
:param r: End index of the first range
40-
:param x: Start index of the second range
41-
:param y: End index of the second range
66+
:param l: The start index of the first range
67+
:param r: The end index of the first range
68+
:param x: The start index of the second range
69+
:param y: The end index of the second range
4270
:return:
4371
- 1 if sum(arr[l..r]) > sum(arr[x..y])
4472
- 0 if sum(arr[l..r]) == sum(arr[x..y])
@@ -61,3 +89,66 @@ def length(self) -> int:
6189
:return: Length of the array
6290
"""
6391
return len(self.array)
92+
93+
94+
class TestGetIndex(unittest.TestCase):
95+
def test_single_element(self):
96+
array = [10]
97+
reader = ArrayReader(array)
98+
self.assertEqual(get_index(reader), 0)
99+
100+
def test_two_elements_first_large(self):
101+
array = [20, 10]
102+
reader = ArrayReader(array)
103+
self.assertEqual(get_index(reader), 0)
104+
105+
def test_two_elements_second_large(self):
106+
array = [10, 20]
107+
reader = ArrayReader(array)
108+
self.assertEqual(get_index(reader), 1)
109+
110+
def test_large_array_uniform_with_one_large(self):
111+
array = [
112+
46,
113+
46,
114+
46,
115+
46,
116+
46,
117+
46,
118+
46,
119+
46,
120+
46,
121+
46,
122+
46,
123+
46,
124+
46,
125+
46,
126+
46,
127+
46,
128+
46,
129+
46,
130+
46,
131+
46,
132+
57,
133+
46,
134+
46,
135+
46,
136+
46,
137+
]
138+
reader = ArrayReader(array)
139+
self.assertEqual(get_index(reader), 20)
140+
141+
def test_large_array_middle_large(self):
142+
array = [1, 1, 50, 1, 1]
143+
reader = ArrayReader(array)
144+
self.assertEqual(get_index(reader), 2)
145+
146+
def test_large_array_end_large(self):
147+
array = [1, 1, 1, 1, 50]
148+
reader = ArrayReader(array)
149+
self.assertEqual(get_index(reader), 4)
150+
151+
def test_large_array_start_large(self):
152+
array = [50, 1, 1, 1, 1]
153+
reader = ArrayReader(array)
154+
self.assertEqual(get_index(reader), 0)

0 commit comments

Comments
 (0)