Skip to content

Commit 8afa77f

Browse files
author
Amogh Singhal
authored
Create min_max_array_oneLoop.py
1 parent af3c6b9 commit 8afa77f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

min_max_array_oneLoop.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def maxMinIndex(arr):
2+
3+
max_index = 0
4+
min_index = 0
5+
6+
# using two counters in single for loop
7+
for i, j in zip(range(1, len(arr)), range(1, len(arr))):
8+
if arr[max_index] < arr[i]:
9+
max_index = i
10+
if arr[min_index] > arr[i]:
11+
min_index = i
12+
13+
return max_index, min_index
14+
15+
arr = [4,5,6,7,8,1,2,11,12,13,3,9,10]
16+
res = maxMinIndex(arr)
17+
print("maximum element is", arr[res[0]],"at index:", res[0])
18+
print("minimum element is", arr[res[1]],"at index:", res[1])

0 commit comments

Comments
 (0)