-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem 10.py
More file actions
27 lines (22 loc) · 1011 Bytes
/
Copy pathProblem 10.py
File metadata and controls
27 lines (22 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
""" Problem 10: Given a list [4,2,-3,1,6] Write a python program to find if there is a sub-list with sum equal to zero? """
# the given list
list = [4, 2, -3, 1, 6]
# found_sublist is initialized to False to keep track of until a sublist has been found.
found_sublist = False
# Iterate over each possible starting index of the list
for i in range(len(list)):
sublist_sum = 0
# Iterate over possible ending indices of the list starting from i
for j in range(i, len(list)):
sublist_sum += list[j]
# Check if the sublist_sum equals zero
if sublist_sum == 0:
found_sublist = True
break # Exit inner loop if sublist found
if found_sublist:
break # Exit outer loop if sublist found
# Check if a sublist with sum equal to zero was found
if found_sublist:
print("There is a sublist with sum equal to zero.")
else:
print("No sublist with sum equal to zero found.")