
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find sum of the sum of all contiguous sublists in Python
Suppose we have a list of numbers called nums, now consider every contiguous subarray. Sum each of these subarray and return the sum of all these values. Finally, mod the result by 10 ** 9 + 7.
So, if the input is like nums = [3, 4, 6], then the output will be 43, as We have the following subarrays − [3] [4] [6] [3, 4] [4, 6] [3, 4, 6] The sum of all of these is 43.
To solve this, we will follow these steps −
- N:= size of nums
- ans:= 0
- for i in range 0 to size of nums, do
- n:= nums[i]
- ans := ans +(i+1) *(N-i) * n
- return (ans mod 1000000007)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): N=len(nums) ans=0 for i in range(len(nums)): n=nums[i] ans += (i+1) * (N-i) * n return ans%1000000007 ob = Solution() print(ob.solve([3, 4, 6]))
Input
[3, 4, 6]
Output
43
- Related Articles
- Program to find sum of contiguous sublist with maximum sum in Python
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Program to find sum of medians of all odd length sublists in C++
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find number of sublists whose sum is given target in python
- Program to find minimum largest sum of k sublists in C++
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find number of sublists with sum k in a binary list in Python
- Program to find sum of beauty of all substrings in Python
- Program to find the sum of all digits of given number in Python
- Python program to find the sum of all items in a dictionary
- Python Program to Find the Sum of all Nodes in a Tree
- Program to find sum of all elements of a tree in Python
- Program to find sum of all odd length subarrays in Python

Advertisements