Skip to content

Commit be67e8e

Browse files
authored
Merge pull request #94 from dsrao711/issue-91
Completed issue - 91 : Added python code for DSA 450
2 parents 04b2ecf + 38ab30e commit be67e8e

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

DSA 450 GFG/max_and_min_of_array.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Maximum and minimum of an array using minimum number of comparisons - Python
2+
3+
def getMinMax(arr):
4+
5+
n = len(arr)
6+
7+
# If array has even number of elements then initialize the first two elements as minimum and maximum
8+
if(n % 2 == 0):
9+
maximum = max(arr[0], arr[1])
10+
minimum = min(arr[0], arr[1])
11+
12+
# set the starting index for loop
13+
i = 2
14+
15+
# If array has odd number of elements then initialize the first element as minimum and maximum
16+
else:
17+
maximum = minimum = arr[0]
18+
19+
# set the starting index for loop
20+
i = 1
21+
22+
# In the while loop, pick elements in pair and compare the pair with max and min so far
23+
while(i < n - 1):
24+
if arr[i] < arr[i + 1]:
25+
maximum = max(maximum, arr[i + 1])
26+
minimum = min(minimum, arr[i])
27+
else:
28+
maximum = max(maximum, arr[i])
29+
minimum = min(minimum, arr[i + 1])
30+
31+
# Increment the index by 2 as two elements are processed in loop
32+
i += 2
33+
34+
return (maximum, minimum)
35+
36+
# Driver Code
37+
if __name__ =='__main__':
38+
39+
arr = [9, 1, 45, 2, 330, 3]
40+
maximum, minimum = getMinMax(arr)
41+
print("Minimum element is", minimum)
42+
print("Maximum element is", maximum)
43+
44+

DSA 450 GFG/move_all_neg_numbers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
def rearrange (arr , n ):
4+
j = 0
5+
for i in range(0 , n) :
6+
if(arr[i] < 0):
7+
temp = arr[i]
8+
arr[i] = arr[j]
9+
arr[j] = temp
10+
j = j + 1
11+
print(arr)
12+
13+
#Driver code
14+
sequence = [1 , 3, - 6 , 9 , -3 , -1]
15+
length = len(sequence)
16+
print(sequence.sort)
17+
rearrange(sequence , length)

DSA 450 GFG/reverse_an_array.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Write a program to reverse an array
2+
# Given an array (or string), the task is to reverse the array/string.
3+
# Examples :
4+
5+
# Input : arr[] = [1, 2, 3]
6+
# Output : arr[] = [3, 2, 1]
7+
# Input : arr[] = [4, 5, 1, 2]
8+
# Output : arr[] = [2, 1, 5, 4]
9+
10+
11+
12+
13+
# Reverse of an array can be implented in Python using the List slicing
14+
15+
def reverseList(a):
16+
print( a[::-1])
17+
18+
# Driver Code
19+
arr = [4,5,1,2]
20+
print(arr)
21+
print("Reverse : ")
22+
reverseList(arr)

0 commit comments

Comments
 (0)