📌  相关文章
📜  给定范围内 AP 的元素总和

📅  最后修改于: 2021-09-03 03:50:43             🧑  作者: Mango

[L, R]的形式给出arrQ查询中的算术级数,其中L是范围的左边界, R是右边界。任务是找到给定范围内的 AP 元素的总和。
注意:范围是 1-indexed 且 1 ≤ L, R ≤ N,其中 N 是 arr 的大小。
例子:

方法:由于给定的序列是一个等差数列,因此可以通过两个步骤轻松有效地找到总和:

  1. 将范围的第一个元素乘以范围内的元素数。
  2. (d*k*(k+1))/2 添加到其中,其中d是 AP 的共同差值, k是(范围内的元素数 – 1),它对应于间隙的数量。

例如:
假设 a[i] 是范围的第一个元素, d是 AP 的公差, k+1是给定范围内的元素数。
那么范围的总和将是

下面是上述方法的实现:

CPP
// C++ program to find the sum of elements
// of an AP in the given range
 
#include 
using namespace std;
 
// Function to find sum in the given range
int findSum(int* arr, int n,
            int left, int right)
{
    // Find the value of k
    int k = right - left;
 
    // Find the common difference
    int d = arr[1] - arr[0];
 
    // Find the sum
    int ans = arr[left - 1] * (k + 1);
    ans = ans + (d * (k * (k + 1))) / 2;
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
    int queries = 3;
    int q[queries][2] = { { 2, 4 },
                          { 2, 6 },
                          { 5, 6 } };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    for (int i = 0; i < queries; i++)
        cout << findSum(arr, n, q[i][0], q[i][1])
             << endl;
}


Java
// Java program to find the sum of elements
// of an AP in the given range
class GFG{
  
// Function to find sum in the given range
static int findSum(int []arr, int n,
            int left, int right)
{
    // Find the value of k
    int k = right - left;
  
    // Find the common difference
    int d = arr[1] - arr[0];
  
    // Find the sum
    int ans = arr[left - 1] * (k + 1);
    ans = ans + (d * (k * (k + 1))) / 2;
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
    int queries = 3;
    int q[][] = { { 2, 4 },
                          { 2, 6 },
                          { 5, 6 } };
    int n = arr.length;
  
    for (int i = 0; i < queries; i++)
        System.out.print(findSum(arr, n, q[i][0], q[i][1])
             +"\n");
}
}
 
// This code is contributed by Princi Singh


Python3
# Python program to find the sum of elements
# of an AP in the given range
 
# Function to find sum in the given range
def findSum(arr, n, left, right):
 
    # Find the value of k
    k = right - left;
 
    # Find the common difference
    d = arr[1] - arr[0];
 
    # Find the sum
    ans = arr[left - 1] * (k + 1);
    ans = ans + (d * (k * (k + 1))) // 2;
 
    return ans;
 
# Driver code
if __name__ == '__main__':
    arr = [ 2, 4, 6, 8, 10, 12, 14, 16 ];
    queries = 3;
    q = [[ 2, 4 ],[ 2, 6 ],[ 5, 6 ]];
    n = len(arr);
 
    for i in range(queries):
        print(findSum(arr, n, q[i][0], q[i][1]));
 
# This code is contributed by sapnasingh4991


C#
// C# program to find the sum of elements
// of an AP in the given range
using System;
 
class GFG{
   
// Function to find sum in the given range
static int findSum(int []arr, int n,
            int left, int right)
{
    // Find the value of k
    int k = right - left;
   
    // Find the common difference
    int d = arr[1] - arr[0];
   
    // Find the sum
    int ans = arr[left - 1] * (k + 1);
    ans = ans + (d * (k * (k + 1))) / 2;
   
    return ans;
}
   
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 4, 6, 8, 10, 12, 14, 16 };
    int queries = 3;
    int [,]q = { { 2, 4 },
                          { 2, 6 },
                          { 5, 6 } };
    int n = arr.Length;
   
    for (int i = 0; i < queries; i++)
        Console.Write(findSum(arr, n, q[i,0], q[i,1])
             +"\n");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
18
40
22

时间复杂度: O(1)
空间复杂度: O(1)