求 [L, R] 范围内 Array 元素的乘积之和
给定一个数组arr[]和两个整数L和R 。任务是找到[L, R]范围内所有对(i, j)的乘积之和,使得i ≤ j 。
Input: arr[] = { 1, 3, 5, 8 }, L = 0, R = 2
Output: 58
Explanation: As 1*1 + 1*3 + 1*5 + 3*3 + 3*5 + 5*5 = 58
Input: arr[] = { 2, 1, 4, 5, 3, 2, 1 }, L = 1, R = 5
Output: 140
天真的方法:蛮力方法可以通过使用两个嵌套循环将索引相乘并将总和存储在一个变量中来直接实现。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to return the sum
// of (arr[i]*arr[j]) for all i and j
// between the index L and R
int sum_of_products(int arr[], int N, int L,
int R)
{
int sum = 0;
for (int i = L; i <= R; i++) {
for (int j = i; j <= R; j++) {
sum += arr[i] * arr[j];
}
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int L = 0;
int R = 2;
cout << sum_of_products(arr, N, L, R);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
public class GFG {
// Function to return the sum
// of (arr[i]*arr[j]) for all i and j
// between the index L and R
static int sum_of_products(int[] arr, int N, int L,
int R)
{
int sum = 0;
for (int i = L; i <= R; i++) {
for (int j = i; j <= R; j++) {
sum += arr[i] * arr[j];
}
}
return sum;
}
// Driver code
public static void main(String args[])
{
int[] arr = { 1, 3, 5, 8 };
int N = arr.length;
int L = 0;
int R = 2;
System.out.println(sum_of_products(arr, N, L, R));
}
}
// This code is contributed by Samim Hossain Mondal.
Python
# C++ implementation of the above approach
# Function to return the sum
# of (arr[i]*arr[j]) for all i and j
# between the index L and R
def sum_of_products(arr, N, L, R):
sum = 0
for i in range(L, R + 1):
for j in range(i, R + 1):
sum = sum + (arr[i] * arr[j])
return sum
# Driver code
arr = [ 1, 3, 5, 8 ]
N = len(arr)
L = 0
R = 2
print(sum_of_products(arr, N, L, R))
# This code is contributed by Samim Hossain Mondal.
C#
// C# implementation of the above approach
using System;
class GFG {
// Function to return the sum
// of (arr[i]*arr[j]) for all i and j
// between the index L and R
static int sum_of_products(int[] arr, int N, int L,
int R)
{
int sum = 0;
for (int i = L; i <= R; i++) {
for (int j = i; j <= R; j++) {
sum += arr[i] * arr[j];
}
}
return sum;
}
// Driver code
public static void Main()
{
int[] arr = { 1, 3, 5, 8 };
int N = arr.Length;
int L = 0;
int R = 2;
Console.WriteLine(sum_of_products(arr, N, L, R));
}
}
// This code is contributed by ukasp.
Javascript
// JavaScript code for the above approach
// Function to return the sum
// of (arr[i]*arr[j]) for all i and j
// between the index L and R
function sum_of_products(arr, N, L, R) {
let sum = 0;
for (let i = L; i <= R; i++) {
for (let j = i; j <= R; j++) {
sum += arr[i] * arr[j];
}
}
return sum;
}
// Driver code
let arr = [1, 3, 5, 8];
let N = arr.length
let L = 0;
let R = 2;
document.write(sum_of_products(arr, N, L, R));
// This code is contributed by Potta Lokesh
58
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
int sum_of_products(int arr[], int n, int L,
int R)
{
int sum = 0;
// Pre-calculating Prefix sum
int prefix_sum[n];
prefix_sum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i];
}
// Using prefix sum to find
// summation of products
for (int i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i]
* (prefix_sum[R]
- prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int L = 0;
int R = 2;
cout << sum_of_products(arr, N, L, R);
return 0;
}
Java
// Java code to implement above approach
import java.util.*;
class GFG{
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
static int sum_of_products(int arr[], int n, int L,
int R)
{
int sum = 0;
// Pre-calculating Prefix sum
int []prefix_sum = new int[n];
prefix_sum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i];
}
// Using prefix sum to find
// summation of products
for (int i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i]
* (prefix_sum[R]
- prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 5, 8 };
int N = arr.length;
int L = 0;
int R = 2;
System.out.print(sum_of_products(arr, N, L, R));
}
}
// This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
static int sum_of_products(int[] arr, int n, int L,
int R)
{
int sum = 0;
// Pre-calculating Prefix sum
int []prefix_sum = new int[n];
prefix_sum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i];
}
// Using prefix sum to find
// summation of products
for (int i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i]
* (prefix_sum[R]
- prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 3, 5, 8 };
int N = arr.Length;
int L = 0;
int R = 2;
Console.Write(sum_of_products(arr, N, L, R));
}
}
// This code is contributed by ukasp.
Javascript
// javascript code to implement above approach
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
function sum_of_products(arr , n , L , R) {
var sum = 0;
// Pre-calculating Prefix sum
var prefix_sum = Array(n).fill(0);
prefix_sum[0] = arr[0];
for (i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
}
// Using prefix sum to find
// summation of products
for (i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i] * (prefix_sum[R] - prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver code
var arr = [ 1, 3, 5, 8 ];
var N = arr.length;
var L = 0;
var R = 2;
document.write(sum_of_products(arr, N, L, R));
// This code is contributed by umadevi9616
输出
58
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:这个问题可以通过使用前缀和技术有效地解决。在此方法中,将前缀和存储在预计算中,然后从 L 到 R 迭代一个循环,并将该索引的相应前缀和乘以最后一个索引。
Basically 1*1+1*3+1*5+3*3+3*5+5*5 can be written as 1*(1+3+5)+3*(3+5)+5*(5) = 1*(prefix_sum from 1 to 5)+3*(prefix_sum from 3 to 5)+5*(prefix sum from 5 to 5)
下面是上述方法的实现。
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
int sum_of_products(int arr[], int n, int L,
int R)
{
int sum = 0;
// Pre-calculating Prefix sum
int prefix_sum[n];
prefix_sum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i];
}
// Using prefix sum to find
// summation of products
for (int i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i]
* (prefix_sum[R]
- prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int L = 0;
int R = 2;
cout << sum_of_products(arr, N, L, R);
return 0;
}
Java
// Java code to implement above approach
import java.util.*;
class GFG{
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
static int sum_of_products(int arr[], int n, int L,
int R)
{
int sum = 0;
// Pre-calculating Prefix sum
int []prefix_sum = new int[n];
prefix_sum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i];
}
// Using prefix sum to find
// summation of products
for (int i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i]
* (prefix_sum[R]
- prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 5, 8 };
int N = arr.length;
int L = 0;
int R = 2;
System.out.print(sum_of_products(arr, N, L, R));
}
}
// This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
static int sum_of_products(int[] arr, int n, int L,
int R)
{
int sum = 0;
// Pre-calculating Prefix sum
int []prefix_sum = new int[n];
prefix_sum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1]
+ arr[i];
}
// Using prefix sum to find
// summation of products
for (int i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i]
* (prefix_sum[R]
- prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 3, 5, 8 };
int N = arr.Length;
int L = 0;
int R = 2;
Console.Write(sum_of_products(arr, N, L, R));
}
}
// This code is contributed by ukasp.
Javascript
// javascript code to implement above approach
// Function to return the sum of
// (arr[i]*arr[j]) for all i and j
// between the index L and R
function sum_of_products(arr , n , L , R) {
var sum = 0;
// Pre-calculating Prefix sum
var prefix_sum = Array(n).fill(0);
prefix_sum[0] = arr[0];
for (i = 1; i < n; i++) {
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
}
// Using prefix sum to find
// summation of products
for (i = L; i <= R; i++) {
// if-else for i==0 case
// in prefix sum
if (i != 0)
sum += arr[i] * (prefix_sum[R] - prefix_sum[i - 1]);
else
sum += arr[i] * (prefix_sum[R]);
}
return sum;
}
// Driver code
var arr = [ 1, 3, 5, 8 ];
var N = arr.length;
var L = 0;
var R = 2;
document.write(sum_of_products(arr, N, L, R));
// This code is contributed by umadevi9616
输出
58
时间复杂度: 在)
辅助空间: O(N)