19
19
20
20
21
21
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
23
51
24
52
25
53
class ArrayReader :
@@ -35,10 +63,10 @@ def compareSub(self, l: int, r: int, x: int, y: int) -> int:
35
63
"""
36
64
Compares the sum of arr[l..r] with the sum of arr[x..y].
37
65
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
42
70
:return:
43
71
- 1 if sum(arr[l..r]) > sum(arr[x..y])
44
72
- 0 if sum(arr[l..r]) == sum(arr[x..y])
@@ -61,3 +89,66 @@ def length(self) -> int:
61
89
:return: Length of the array
62
90
"""
63
91
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