📌  相关文章
📜  找到具有共同差D的Array的前缀和后缀和的位置形成AP

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

找到具有共同差D的Array的前缀和后缀和的位置形成AP

给定一个大小为N的数组arr[]和一个正整数D ,任务是找到一个元素在arr[]中的位置i ,使得前缀和 arr[i] 和后缀和在算术级数中差 D,即 arr[i] – sum(arr[0, . . ., i-1]) = sum(arr[i+1, . . ., N-1]) – arr[i] = D。如果不存在这样的位置,则返回-1。

例子

方法:给定的问题可以通过使用基于以下观察的线性搜索方法来解决:

  • 如果数组的大小小于3 ,则不可能有序列,因此只需返回-1
  • 计算数组arr[]的总和并将其存储在Sum中。
    • 如果Sum % 3 != 0 ,则返回0
    • 初始化一个变量说Mid = Sum / 3来存储 AP 系列的中间元素。
  • 从索引i = 1迭代arr[]N – 2
    • 计算直到i-1的前缀总和(比如 temp)
    • 如果temp等于Mid – D则后缀 sum 为Mid + D 。所以返回位置i+1
    • 否则返回-1。
  • 如果循环终止并且arr[]中没有元素等于 mid 然后简单地返回 -1。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if there is
// an element forming A.P. series
// having common difference d
int checkArray(int arr[], int N, int d)
{
 
    // If size of array is less than
    // three then return -1
    if (N < 3)
        return -1;
 
    // Initialize the variables
    int i, Sum = 0, temp = 0;
 
    // Calculate total sum of array
    for (i = 0; i < N; i++)
        Sum += arr[i];
 
    if (Sum % 3 != 0)
        return 0;
 
    // Calculate Middle element of A.P. series
    int Mid = Sum / 3;
 
    // Iterate over the range
    for (i = 1; i < N - 1; i++) {
 
        // Store the first element of A.P.
        // series in the variable temp
        temp += arr[i - 1];
 
        if (arr[i] == Mid) {
 
            // Return position of middle element
            // of the A.P. series if the first
            // element is in A.P. with middle element
            // having common difference d
            if (temp == Mid - d)
                return i + 1;
 
            // Else return 0
            else
                return 0;
        }
    }
 
    // If middle element is not found in arr[]
    return 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 6, 20, 10, 15, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int D = 10;
 
    // Function call
    cout << checkArray(arr, N, D) << endl;
    return 0;
}


Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
 
  // Function to check if there is
  // an element forming A.P. series
  // having common difference d
  public static int checkArray(int arr[], int N, int d)
  {
 
    // If size of array is less than
    // three then return -1
    if (N < 3)
      return -1;
 
    // Initialize the variables
    int i, Sum = 0, temp = 0;
 
    // Calculate total sum of array
    for (i = 0; i < N; i++)
      Sum += arr[i];
 
    if (Sum % 3 != 0)
      return 0;
 
    // Calculate Middle element of A.P. series
    int Mid = Sum / 3;
 
    // Iterate over the range
    for (i = 1; i < N - 1; i++) {
 
      // Store the first element of A.P.
      // series in the variable temp
      temp += arr[i - 1];
 
      if (arr[i] == Mid) {
 
        // Return position of middle element
        // of the A.P. series if the first
        // element is in A.P. with middle element
        // having common difference d
        if (temp == Mid - d)
          return i + 1;
 
        // Else return 0
        else
          return 0;
      }
    }
 
    // If middle element is not found in arr[]
    return 0;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 4, 6, 20, 10, 15, 5 };
    int N = arr.length;
    int D = 10;
 
    // Function call
    System.out.println(checkArray(arr, N, D));
  }
}
 
// This code is contributed by Taranpreet


Python3
#Python program for the above approach
 
# Function to check if there is
# an element forming A.P. series
# having common difference d
def checkArray(arr, N, d):
 
    # If size of array is less than
    # three then return -1
    if (N < 3):
        return -1
 
    # Initialize the variables
    i = 0
    Sum = 0
    temp = 0
 
    # Calculate total sum of array
    for i in range (N):
        Sum += arr[i]
 
    if (Sum % 3 != 0):
        return 0
 
    # Calculate Middle element of A.P. series
    Mid = Sum / 3
 
    # Iterate over the range
    for i in range(1, N - 1):
 
        # Store the first element of A.P.
        # series in the variable temp
        temp += arr[i - 1]
 
        if (arr[i] == Mid):
 
            # Return position of middle element
            # of the A.P. series if the first
            # element is in A.P. with middle element
            # having common difference d
            if (temp == Mid - d):
                return i + 1
 
            # Else return 0
            else:
                return 0
 
    # If middle element is not found in arr[]
    return 0
 
# Driver Code
if __name__ == "__main__":
   
    arr = [ 4, 6, 20, 10, 15, 5 ]
    N = len(arr)
    D = 10
 
    # Function call
    print(checkArray(arr, N, D))
  
# This code is contributed by hrithikgarg03188.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to check if there is
  // an element forming A.P. series
  // having common difference d
  static int checkArray(int []arr, int N, int d)
  {
 
    // If size of array is less than
    // three then return -1
    if (N < 3)
      return -1;
 
    // Initialize the variables
    int i, Sum = 0, temp = 0;
 
    // Calculate total sum of array
    for (i = 0; i < N; i++)
      Sum += arr[i];
 
    if (Sum % 3 != 0)
      return 0;
 
    // Calculate Middle element of A.P. series
    int Mid = Sum / 3;
 
    // Iterate over the range
    for (i = 1; i < N - 1; i++) {
 
      // Store the first element of A.P.
      // series in the variable temp
      temp += arr[i - 1];
 
      if (arr[i] == Mid) {
 
        // Return position of middle element
        // of the A.P. series if the first
        // element is in A.P. with middle element
        // having common difference d
        if (temp == Mid - d)
          return i + 1;
 
        // Else return 0
        else
          return 0;
      }
    }
 
    // If middle element is not found in arr[]
    return 0;
  }
 
  // Driver Code
  public static void Main()
  {
    int []arr = { 4, 6, 20, 10, 15, 5 };
    int N = arr.Length;
    int D = 10;
 
    // Function call
    Console.WriteLine(checkArray(arr, N, D));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3

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