Given a list ‘L’, a sum ‘S’ and number of elements to take at a time ‘m’. The task is to find how many ways sum s can be found by adding any m contiguous elements.

**Examples:**

Input :1 2 1 3 2 3 2Output :2Input :1 1 1 1 1 1 3 2Output :0

For example 1, we have to find a sum 3 with the help of any 2 contiguous elements of the list. This can be done in two ways 1+2 and 2+1 so answer is 2

**Method 1: (Simple)**

Starting from beginning we select first m elements and find their sum if sum = s then increment the count by one, and increment index position by one select next m elements and repeat the same until the whole list is covered.

`# Python program to find number of ` `# m contiguous elements of a list ` `# with sum s ` ` ` ` ` `def` `SearchWay(l, s, m): ` ` ` ` ` `# initialise sum and ` ` ` `# count to 0 ` ` ` `sum1 ` `=` `0` ` ` `count ` `=` `0` ` ` ` ` `# iterate from start of ` ` ` `# list to end ` ` ` `for` `i ` `in` `range` `(` `len` `(l)` `-` `m ` `+` `1` `): ` ` ` ` ` `# get sum f m elements ` ` ` `# starting from index i ` ` ` `for` `j ` `in` `range` `(i, i ` `+` `m): ` ` ` `sum1 ` `+` `=` `l[j] ` ` ` ` ` `# if the sum of elements equal ` ` ` `# s increment count ` ` ` `if` `sum1 ` `=` `=` `s: ` ` ` `count ` `+` `=` `1` ` ` ` ` `sum1 ` `=` `0` ` ` ` ` `return` `count ` ` ` `# Driver's code ` `l ` `=` `[` `1` `, ` `2` `, ` `1` `, ` `3` `, ` `2` `] ` `s ` `=` `3` `m ` `=` `2` ` ` `ans ` `=` `SearchWay(l, s, m) ` `print` `(ans) ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time complexicity:** O(n^2) in worst case.

**Method 2: (Efficient)**

Initialize a variable index as 0 and curr_sum as the first element. index indicates the count of contiguous elements and curr_sum indicates the sum of the current index elements. Start from the second element and add all elements one by one to the curr_sum. If curr_sum becomes equal to the sum and index becomes equal to m, then increment the value of count by 1. If index exceeds the m, then remove trailing elements and initialize index as m-1.

`# Python program to find number of ` `# m contiguous elements of a list ` `# with sum s ` ` ` ` ` `def` `SearchWay(l, s, m): ` ` ` ` ` `n ` `=` `len` `(l) ` ` ` ` ` `# Initialize curr_sum as ` ` ` `# value of first element ` ` ` `# and starting point as 0 ` ` ` `count ` `=` `start ` `=` `0` ` ` `curr_sum ` `=` `l[` `0` `] ` ` ` ` ` `# Initialize the index as 1 ` ` ` `index ` `=` `1` ` ` ` ` `# Add elements one by ` ` ` `# one to curr_sum and ` ` ` `# if the index exceeds ` ` ` `# the m, then remove ` ` ` `# starting element ` ` ` `# and change the value ` ` ` `# of index as m-1 ` ` ` `i ` `=` `1` ` ` `while` `i <` `=` `n: ` ` ` ` ` `# If curr_sum becomes ` ` ` `# equal to sum, then ` ` ` `# increment count by 1 ` ` ` `if` `curr_sum ` `=` `=` `s ` `and` `index ` `=` `=` `m: ` ` ` `count ` `+` `=` `1` ` ` ` ` `# If index exceeds ` ` ` `# the m, then remove ` ` ` `# the starting elements ` ` ` `while` `index >` `=` `m: ` ` ` `curr_sum ` `-` `=` `l[start] ` ` ` `start ` `+` `=` `1` ` ` `index ` `=` `m` `-` `1` ` ` ` ` `# Add this element ` ` ` `# to curr_sum and ` ` ` `# increment index ` ` ` `if` `i < n: ` ` ` `curr_sum ` `+` `=` `l[i] ` ` ` `index ` `+` `=` `1` ` ` `i ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver's code ` `l ` `=` `[` `1` `, ` `2` `, ` `1` `, ` `3` `, ` `2` `] ` `s ` `=` `3` `m ` `=` `2` ` ` `ans ` `=` `SearchWay(l, s, m) ` `print` `(ans) ` |

*chevron_right*

*filter_none*

**Output:**

2

The time complexity of method 2 looks more than O(n), but if we take a closer look at the program, then we can figure out the time complexity is O(n). We can prove it by counting the number of operations performed on every element of arr[] in the worst case. There are at most 2 operations performed on every element: (a) the element is added to the curr_sum (b) the element is subtracted from curr_sum. So the upper bound on the number of operations is 2n which is O(n).

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python - Check whether the given List forms Contiguous Distinct Sub-Array or Not
- Python program to find sum of elements in list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Contiguous Boolean Range
- Python | Check if Non-None values are contiguous
- Python | Find sum of frequency of given elements in the list
- Python | Maximum sum of elements of list in a list of lists
- Python | Ways to sum list of lists and return sum list
- Create a contiguous flattened NumPy array
- Python program to find N largest elements from a list
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python program to find the frequency of the elements which are common in a list of strings
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python program to find Cumulative sum of a list
- Python program to find sum of absolute difference between all pairs in a list
- Python program to find the sum of all even and odd digits of an integer list
- Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.