给定一个大小为n的整数数组’arr []’,找到给定数组的所有子数组的和。
例子 :
Input : arr[] = {1, 2, 3}
Output : 20
Explanation : {1} + {2} + {3} + {2 + 3} +
{1 + 2} + {1 + 2 + 3} = 20
Input : arr[] = {1, 2, 3, 4}
Output : 50
方法1(简单解决方案)
一个简单的解决方案是生成所有子数组并计算它们的总和。
以下是上述想法的实现。
C++
// Simple C++ program to compute sum of
// subarray elements
#include
using namespace std;
// Computes sum all sub-array
long int SubArraySum(int arr[], int n)
{
long int result = 0,temp=0;
// Pick starting point
for (int i=0; i
Java
// Simple Java program to compute sum of
// subarray elements
class GFG {
// Computes sum all sub-array
public static long SubArraySum(int arr[], int n)
{
long result = 0,temp=0;
// Pick starting point
for (int i = 0; i < n; i ++)
{
// Pick ending point
temp=0;
for (int j = i; j < n; j ++)
{
// sum subarray between current
// starting and ending points
temp+=arr[j];
result += temp ;
}
}
return result ;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = {1, 2, 3} ;
int n = arr.length;
System.out.println("Sum of SubArray : " +
SubArraySum(arr, n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python 3
# Python3 program to compute
# sum of subarray elements
# Computes sum all sub-array
def SubArraySum(arr, n):
temp,result = 0,0
# Pick starting point
for i in range(0, n):
# Pick ending point
temp=0;
for j in range(i, n):
# sum subarray between
# current starting and
# ending points
temp+=arr[j]
result += temp
return result
# driver program
arr = [1, 2, 3]
n = len(arr)
print ("Sum of SubArray :"
,SubArraySum(arr, n))
# This code is contributed by
# TheGameOf256.
C#
// Simple C# program to compute sum of
// subarray elements
using System;
class GFG {
// Computes sum all sub-array
public static long SubArraySum(int []arr,
int n)
{
long result = 0,temp=0;
// Pick starting point
for (int i = 0; i < n; i ++)
{
// Pick ending point
temp=0;
for (int j = i; j < n; j ++)
{
// sum subarray between current
// starting and ending points
temp+=arr[j];
result += temp ;
}
}
return result ;
}
// Driver Code
public static void Main()
{
int []arr = {1, 2, 3} ;
int n = arr.Length;
Console.Write("Sum of SubArray : " +
SubArraySum(arr, n));
}
}
// This code is contributed by nitin mittal
PHP
Javascript
C++
// Efficient C++ program to compute sum of
// subarray elements
#include
using namespace std;
//function compute sum all sub-array
long int SubArraySum( int arr[] , int n )
{
long int result = 0;
// computing sum of subarray using formula
for (int i=0; i
Java
// Efficient Java program to compute sum of
// subarray elements
class GFG {
//function compute sum all sub-array
public static long SubArraySum( int arr[] , int n )
{
long result = 0;
// computing sum of subarray using formula
for (int i=0; i
Python 3
#Python3 code to compute
# sum of subarray elements
#function compute sum
# all sub-array
def SubArraySum(arr, n ):
result = 0
# computing sum of subarray
# using formula
for i in range(0, n):
result += (arr[i] * (i+1) * (n-i))
# return all subarray sum
return result
# driver program
arr = [1, 2, 3]
n = len(arr)
print ("Sum of SubArray : ",
SubArraySum(arr, n))
# This code is contributed by
# TheGameOf256.
C#
// Efficient C# program
// to compute sum of
// subarray elements
using System;
class GFG
{
// function compute
// sum all sub-array
public static long SubArraySum(int []arr ,
int n )
{
long result = 0;
// computing sum of
// subarray using formula
for (int i = 0; i < n; i++)
result += (arr[i] *
(i + 1) * (n - i));
// return all subarray sum
return result ;
}
// Driver code
static public void Main ()
{
int []arr = {1, 2, 3} ;
int n = arr.Length;
Console.WriteLine("Sum of SubArray: " +
SubArraySum(arr, n));
}
}
// This code is contributed by akt_mit
PHP
输出:
Sum of SubArray : 20
时间复杂度: O(n 2 )
方法2(有效的解决方案)
如果我们仔细观察,就会观察到一种模式。举个例子
arr[] = [1, 2, 3], n = 3
All subarrays : [1], [1, 2], [1, 2, 3],
[2], [2, 3], [3]
here first element 'arr[0]' appears 3 times
second element 'arr[1]' appears 4 times
third element 'arr[2]' appears 3 times
Every element arr[i] appears in two types of subsets:
i) In subarrays beginning with arr[i]. There are
(n-i) such subsets. For example [2] appears
in [2] and [2, 3].
ii) In (n-i)*i subarrays where this element is not
first element. For example [2] appears in
[1, 2] and [1, 2, 3].
Total of above (i) and (ii) = (n-i) + (n-i)*i
= (n-i)(i+1)
For arr[] = {1, 2, 3}, sum of subarrays is:
arr[0] * ( 0 + 1 ) * ( 3 - 0 ) +
arr[1] * ( 1 + 1 ) * ( 3 - 1 ) +
arr[2] * ( 2 + 1 ) * ( 3 - 2 )
= 1*3 + 2*4 + 3*3
= 20
以下是上述想法的实现。
C++
// Efficient C++ program to compute sum of
// subarray elements
#include
using namespace std;
//function compute sum all sub-array
long int SubArraySum( int arr[] , int n )
{
long int result = 0;
// computing sum of subarray using formula
for (int i=0; i
Java
// Efficient Java program to compute sum of
// subarray elements
class GFG {
//function compute sum all sub-array
public static long SubArraySum( int arr[] , int n )
{
long result = 0;
// computing sum of subarray using formula
for (int i=0; i
的Python 3
#Python3 code to compute
# sum of subarray elements
#function compute sum
# all sub-array
def SubArraySum(arr, n ):
result = 0
# computing sum of subarray
# using formula
for i in range(0, n):
result += (arr[i] * (i+1) * (n-i))
# return all subarray sum
return result
# driver program
arr = [1, 2, 3]
n = len(arr)
print ("Sum of SubArray : ",
SubArraySum(arr, n))
# This code is contributed by
# TheGameOf256.
C#
// Efficient C# program
// to compute sum of
// subarray elements
using System;
class GFG
{
// function compute
// sum all sub-array
public static long SubArraySum(int []arr ,
int n )
{
long result = 0;
// computing sum of
// subarray using formula
for (int i = 0; i < n; i++)
result += (arr[i] *
(i + 1) * (n - i));
// return all subarray sum
return result ;
}
// Driver code
static public void Main ()
{
int []arr = {1, 2, 3} ;
int n = arr.Length;
Console.WriteLine("Sum of SubArray: " +
SubArraySum(arr, n));
}
}
// This code is contributed by akt_mit
的PHP
输出 :
Sum of SubArray : 20