Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82e7579

Browse files
Add recursive max function for list
Implement recursive function to find max element in a list.
1 parent c67e0d1 commit 82e7579

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎12_Recursion/recursive.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Find maximum element in a list using recursion.
2+
3+
def max_recursive(lst):
4+
# Base case: agar list me sirf ek element hai, wahi max hoga
5+
if len(lst) == 1:
6+
return lst[0]
7+
8+
# Recursive case: first element aur baki list ka max compare karo
9+
sub_max = max_recursive(lst[1:])
10+
return lst[0] if lst[0] > sub_max else sub_max
11+
12+
# Example
13+
numbers = [5, 2, 9, 1, 7]
14+
print("Maximum element:", max_recursive(numbers))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /