0% found this document useful (0 votes)
6 views3 pages

Find Maximum and Minimum in Array

The document contains a series of Python functions that perform various operations on arrays, including finding maximum and minimum values, reversing the array, summing elements, checking if sorted, counting even and odd numbers, and more. Each function is accompanied by example usage demonstrating its functionality. The document serves as a practical guide for common array manipulations in Python.

Uploaded by

ssstutuorial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Find Maximum and Minimum in Array

The document contains a series of Python functions that perform various operations on arrays, including finding maximum and minimum values, reversing the array, summing elements, checking if sorted, counting even and odd numbers, and more. Each function is accompanied by example usage demonstrating its functionality. The document serves as a practical guide for common array manipulations in Python.

Uploaded by

ssstutuorial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Find Maximum and Minimum in Array


def find_max_min(arr):
return max(arr), min(arr)

arr = [4, 2, 7, 1, 9]
print("Max and Min:", find_max_min(arr))

2. Reverse the Array


def reverse_array(arr):
return arr[::-1]

arr = [1, 2, 3, 4, 5]
print("Reversed:", reverse_array(arr))

3. Sum of All Elements


def array_sum(arr):
return sum(arr)

arr = [2, 4, 6]
print("Sum:", array_sum(arr))

4. Check if Array is Sorted


def is_sorted(arr):
return arr == sorted(arr)

arr = [1, 2, 3, 4, 5]
print("Sorted:", is_sorted(arr))

5. Count Even and Odd Numbers


def count_even_odd(arr):
even = sum(1 for x in arr if x % 2 == 0)
odd = len(arr) - even
return even, odd

arr = [1, 2, 3, 4, 5]
print("Even and Odd:", count_even_odd(arr))
6. Find Second Largest Element
def second_largest(arr):
unique = list(set(arr))
unique.sort()
return unique[-2] if len(unique) >= 2 else None

arr = [10, 5, 20, 8]


print("Second Largest:", second_largest(arr))

7. Remove Duplicates from Sorted Array


def remove_duplicates(arr):
return list(dict.fromkeys(arr))

arr = [1, 1, 2, 2, 3]
print("Without Duplicates:", remove_duplicates(arr))

8. Left Rotate the Array by One


def rotate_left(arr):
return arr[1:] + arr[:1]

arr = [1, 2, 3, 4]
print("Left Rotated:", rotate_left(arr))

9. Move All Zeros to End


def move_zeros(arr):
non_zero = [x for x in arr if x != 0]
return non_zero + [0] * (len(arr) - len(non_zero))

arr = [0, 1, 0, 3, 12]


print("Zeros Moved:", move_zeros(arr))

10. Frequency of Elements


from collections import Counter

arr = [1, 2, 2, 3, 3, 3]
print("Frequencies:", dict(Counter(arr)))
11. Maximum Subarray Sum (Kadane’s Algorithm)
def max_subarray_sum(arr):
max_sum = current = arr[0]
for num in arr[1:]:
current = max(num, current + num)
max_sum = max(max_sum, current)
return max_sum

arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]


print("Max Subarray Sum:", max_subarray_sum(arr))

Step num current (max(num, current+num)) max_sum


Init -2 -2 -2
1 1 max(1, -2+1) = 1 1
2 -3 max(-3, 1-3) = -2 1
3 4 max(4, -2+4) = 4 4
4 -1 max(-1, 4-1) = 3 4
5 2 max(2, 3+2) = 5 5
6 1 max(1, 5+1) = 6 6
7 -5 max(-5, 6-5) = 1 6
8 4 max(4, 1+4) = 5 6

12. Find Missing Number in 1 to N


def find_missing(arr, n):
total = n * (n + 1) // 2
return total - sum(arr)

arr = [1, 2, 4, 5]
print("Missing Number:", find_missing(arr, 5))

13. Find Duplicate Number


def find_duplicate(arr):
seen = set()
for num in arr:
if num in seen:
return num
seen.add(num)

arr = [3, 1, 3, 4, 2]
print("Duplicate:", find_duplicate(arr))

You might also like