📌  相关文章
📜  由无限连接给定数组形成的数组中给定范围内的元素总和

📅  最后修改于: 2022-05-13 01:56:09.046000             🧑  作者: Mango

由无限连接给定数组形成的数组中给定范围内的元素总和

给定一个由N个正整数和两个正整数LR组成的数组arr[] (基于 1 的索引),如果给定数组arr[] ,任务是在[L, R]范围内找到数组元素的总和无限次连接到自身。

例子:

朴素方法:解决给定问题的最简单方法是使用变量i在范围[L, R]上进行迭代,并将arr[i % N]的值添加到每个索引的总和中。完成迭代后,将总和的值打印为结果总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the sum of elements
// in a given range of an infinite array
void rangeSum(int arr[], int N, int L, int R)
{
    // Stores the sum of array elements
    // from L to R
    int sum = 0;
 
    // Traverse from L to R
    for (int i = L - 1; i < R; i++) {
        sum += arr[i % N];
    }
 
    // Print the resultant sum
    cout << sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = sizeof(arr) / sizeof(arr[0]);
    rangeSum(arr, N, L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the sum of elements
    // in a given range of an infinite array
    static void rangeSum(int arr[], int N, int L, int R)
    {
       
        // Stores the sum of array elements
        // from L to R
        int sum = 0;
 
        // Traverse from L to R
        for (int i = L - 1; i < R; i++) {
            sum += arr[i % N];
        }
 
        // Print the resultant sum
        System.out.println(sum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 5, 2, 6, 9 };
        int L = 10, R = 13;
        int N = arr.length;
        rangeSum(arr, N, L, R);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to find the sum of elements
# in a given range of an infinite array
def rangeSum(arr, N, L, R):
   
    # Stores the sum of array elements
    # from L to R
    sum = 0
 
    # Traverse from L to R
    for i in range(L - 1,R,1):
        sum += arr[i % N]
 
    # Print the resultant sum
    print(sum)
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 2, 6, 9 ]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)
     
    # This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find the sum of elements
    // in a given range of an infinite array
    static void rangeSum(int[] arr, int N, int L, int R)
    {
 
        // Stores the sum of array elements
        // from L to R
        int sum = 0;
 
        // Traverse from L to R
        for (int i = L - 1; i < R; i++) {
            sum += arr[i % N];
        }
 
        // Print the resultant sum
        Console.Write(sum);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 5, 2, 6, 9 };
        int L = 10, R = 13;
        int N = arr.Length;
        rangeSum(arr, N, L, R);
    }
}
 
// This code is contributed by ukasp.


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the sum of elements
// in a given range of an infinite array
void rangeSum(int arr[], int N, int L,
              int R)
{
    // Stores the prefix sum
    int prefix[N + 1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    cout << rightsum - leftsum;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = sizeof(arr) / sizeof(arr[0]);
    rangeSum(arr, N, L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the sum of elements
// in a given range of an infinite array
static void rangeSum(int arr[], int N, int L, int R)
{
   
    // Stores the prefix sum
    int prefix[] = new int[N+1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    System.out.print( rightsum - leftsum);
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = arr.length;
    rangeSum(arr, N, L, R);
 
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python 3 program for the above approach
 
# Function to find the sum of elements
# in a given range of an infinite array
def rangeSum(arr, N, L, R):
   
    # Stores the prefix sum
    prefix = [0 for i in range(N + 1)]
    prefix[0] = 0
 
    # Calculate the prefix sum
    for i in range(1,N+1,1):
        prefix[i] = prefix[i - 1] + arr[i - 1]
 
    # Stores the sum of elements
    # from 1 to L-1
    leftsum = ((L - 1) // N) * prefix[N] + prefix[(L - 1) % N]
 
    # Stores the sum of elements
    # from 1 to R
    rightsum = (R // N) * prefix[N] + prefix[R % N]
 
    # Print the resultant sum
    print(rightsum - leftsum)
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 2, 6, 9]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the sum of elements
// in a given range of an infinite array
static void rangeSum(int []arr, int N, int L, int R)
{
   
    // Stores the prefix sum
    int []prefix = new int[N+1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    Console.Write( rightsum - leftsum);
}
 
// Driver Code
public static void Main (String[] args)
{
    int []arr = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = arr.Length;
    rangeSum(arr, N, L, R);
 
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
22

时间复杂度: O(R – L)
辅助空间: O(1)

高效方法:上述方法也可以通过使用前缀和进行优化。请按照以下步骤解决问题:

  • 初始化一个数组,比如大小为(N + 1)prefix[] ,所有元素都为0s
  • 使用变量i遍历数组arr[]并将prefix[i]更新为prefix[i – 1]arr[i – 1]的总和。
  • 现在, [L, R]范围内的元素总和由下式给出:
  • 初始化一个变量,例如leftSum((L – 1)/N)*prefix[N] + prefix[(L – 1)%N]以存储范围[1, L-1]中元素的总和。
  • 类似地,将另一个变量rightSum初始化为(R/N)*prefix[N] + prefix[R%N]以存储[1, R]范围内元素的总和。
  • 完成上述步骤后,将(rightSum – leftSum)的值打印为给定范围[L, R]内元素的总和。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the sum of elements
// in a given range of an infinite array
void rangeSum(int arr[], int N, int L,
              int R)
{
    // Stores the prefix sum
    int prefix[N + 1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    cout << rightsum - leftsum;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = sizeof(arr) / sizeof(arr[0]);
    rangeSum(arr, N, L, R);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find the sum of elements
// in a given range of an infinite array
static void rangeSum(int arr[], int N, int L, int R)
{
   
    // Stores the prefix sum
    int prefix[] = new int[N+1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    System.out.print( rightsum - leftsum);
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = arr.length;
    rangeSum(arr, N, L, R);
 
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python 3 program for the above approach
 
# Function to find the sum of elements
# in a given range of an infinite array
def rangeSum(arr, N, L, R):
   
    # Stores the prefix sum
    prefix = [0 for i in range(N + 1)]
    prefix[0] = 0
 
    # Calculate the prefix sum
    for i in range(1,N+1,1):
        prefix[i] = prefix[i - 1] + arr[i - 1]
 
    # Stores the sum of elements
    # from 1 to L-1
    leftsum = ((L - 1) // N) * prefix[N] + prefix[(L - 1) % N]
 
    # Stores the sum of elements
    # from 1 to R
    rightsum = (R // N) * prefix[N] + prefix[R % N]
 
    # Print the resultant sum
    print(rightsum - leftsum)
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 2, 6, 9]
    L = 10
    R = 13
    N = len(arr)
    rangeSum(arr, N, L, R)
 
    # This code is contributed by SURENDRA_GANGWAR.

C#

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the sum of elements
// in a given range of an infinite array
static void rangeSum(int []arr, int N, int L, int R)
{
   
    // Stores the prefix sum
    int []prefix = new int[N+1];
    prefix[0] = 0;
 
    // Calculate the prefix sum
    for (int i = 1; i <= N; i++) {
        prefix[i] = prefix[i - 1]
                    + arr[i - 1];
    }
 
    // Stores the sum of elements
    // from 1 to L-1
    int leftsum
        = ((L - 1) / N) * prefix[N]
          + prefix[(L - 1) % N];
 
    // Stores the sum of elements
    // from 1 to R
    int rightsum = (R / N) * prefix[N]
                   + prefix[R % N];
 
    // Print the resultant sum
    Console.Write( rightsum - leftsum);
}
 
// Driver Code
public static void Main (String[] args)
{
    int []arr = { 5, 2, 6, 9 };
    int L = 10, R = 13;
    int N = arr.Length;
    rangeSum(arr, N, L, R);
 
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出:
22

时间复杂度: O(N)
辅助空间: O(N)