📌  相关文章
📜  查找给定数组的前缀和后缀平均值之间差异最小的索引

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

查找给定数组的前缀和后缀平均值之间差异最小的索引

给定一个大小为N的数组arr[] ,任务是找到一个索引i ,使得前缀平均值和后缀平均值之间的差异最小,即直到i的元素的平均值(在 [0, i] 范围内)和i之后的元素(在 [i+1, N) 范围内)是最小的。

例子:

方法:该问题可以基于前缀和思想解决,如下所示:

按照以下步骤解决上述问题:

  • 初始化并清空prefix_sum[]数组以存储前缀和。
  • 遍历数组并在每个索引 i 处找到前缀和。
  • 初始化min_daymin_diff = INT_MAX以存储日期和最小差异。
  • 遍历前缀和数组,计算左平均值和右平均值并将其存储在 diff 变量中。
    • 检查差异是否小于min_diff并相应地存储更新变量min_diff
  • 处理完所有后,打印min_diff

下面是上述方法的实现:

C++
// C++ program for the above approach.
 
#include 
using namespace std;
 
// Function to find the day
// with minimum stock price change
void find_the_day(int arr[], int n)
{
 
    // Initialize and empty prefix_sum array
    int prefix_sum[n];
    prefix_sum[0] = arr[0];
 
    // Find the prefix sum of the array
    for (int i = 1; i < n; i++) {
        prefix_sum[i] = arr[i]
                        + prefix_sum[i - 1];
    }
 
    // Initialize min_diff to store
    // the minimum diff of
    // left_avg and right_avg
    int min_diff = INT_MAX;
 
    // Initialize the min_day to
    // store the day with min price change
    int min_day;
    for (int i = 0; i < n; i++) {
 
        // Calculate left avg till day i
        float left_avg
            = prefix_sum[i] / (i + 1);
        float right_avg;
 
        // Calculate right avg after day i
        if (i == n - 1)
            right_avg = 0;
        else
            right_avg = (prefix_sum[n - 1]
                         - prefix_sum[i])
                        / (n - i - 1);
 
        // Store the price change in the diff
        float diff
            = left_avg - right_avg;
 
        // Store the day with
        // minimum stock price change
        if (diff < min_diff) {
            min_diff = diff;
            min_day = i + 1;
        }
    }
 
    // Print the day
    cout << min_day << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 15, 5, 10, 15, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    find_the_day(arr, N);
    return 0;
}


Java
// JAVA program for the above approach.
import java.util.*;
class GFG
{
   
    // Function to find the day
    // with minimum stock price change
    public static void find_the_day(int arr[], int n)
    {
 
        // Initialize and empty prefix_sum array
        int prefix_sum[] = new int[n];
        prefix_sum[0] = arr[0];
 
        // Find the prefix sum of the array
        for (int i = 1; i < n; i++) {
            prefix_sum[i] = arr[i] + prefix_sum[i - 1];
        }
 
        // Initialize min_diff to store
        // the minimum diff of
        // left_avg and right_avg
        float min_diff = Float.MAX_VALUE;
 
        // Initialize the min_day to
        // store the day with min price change
        int min_day = 0;
        for (int i = 0; i < n; i++) {
 
            // Calculate left avg till day i
            float left_avg = prefix_sum[i] / (i + 1);
            float right_avg;
 
            // Calculate right avg after day i
            if (i == n - 1)
                right_avg = 0;
            else
                right_avg
                    = (prefix_sum[n - 1] - prefix_sum[i])
                      / (n - i - 1);
 
            // Store the price change in the diff
            float diff = left_avg - right_avg;
 
            // Store the day with
            // minimum stock price change
            if (diff < min_diff) {
                min_diff = (float)(diff);
                min_day = i + 1;
            }
        }
 
        // Print the day
        System.out.println(min_day);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 15, 5, 10, 15, 5 };
        int N = arr.length;
 
        // Function call
        find_the_day(arr, N);
    }
}
 
// This code is contributed by Taranpreet


Python3
# Python program for the above approach.
 
# Function to find the day
# with minimum stock price change
import sys
 
def find_the_day(arr, n):
 
    # Initialize and empty prefix_sum array
    prefix_sum = [0]*n
    prefix_sum[0] = arr[0]
 
    # Find the prefix sum of the array
    for i in range(1,n):
        prefix_sum[i] = arr[i] + prefix_sum[i - 1]
 
    # Initialize min_diff to store
    # the minimum diff of
    # left_avg and right_avg
    min_diff = sys.maxsize
 
    # Initialize the min_day to
    # store the day with min price change
    for i in range(n):
 
        # Calculate left avg till day i
        left_avg = prefix_sum[i] / (i + 1)
 
        # Calculate right avg after day i
        if (i == n - 1):
            right_avg = 0
        else:
            right_avg = (prefix_sum[n - 1]
                         - prefix_sum[i])/ (n - i - 1)
 
        # Store the price change in the diff
        diff = left_avg - right_avg
 
        # Store the day with
        # minimum stock price change
        if (diff < min_diff):
            min_diff = diff
            min_day = i + 1
 
    # Print the day
    print(min_day)
 
# Driver code
 
arr = [ 15, 5, 10, 15, 5 ]
N = len(arr)
 
# Function call
find_the_day(arr, N)
 
# This code is contributed by shinjanpatra


C#
// C# program for the above approach.
using System;
class GFG
{
 
  // Function to find the day
  // with minimum stock price change
  static void find_the_day(int []arr, int n)
  {
 
    // Initialize and empty prefix_sum array
    int []prefix_sum = new int[n];
    prefix_sum[0] = arr[0];
 
    // Find the prefix sum of the array
    for (int i = 1; i < n; i++) {
      prefix_sum[i] = arr[i] + prefix_sum[i - 1];
    }
 
    // Initialize min_diff to store
    // the minimum diff of
    // left_avg and right_avg
    float min_diff = Single.MaxValue;
 
    // Initialize the min_day to
    // store the day with min price change
    int min_day = 0;
    for (int i = 0; i < n; i++) {
 
      // Calculate left avg till day i
      float left_avg = prefix_sum[i] / (i + 1);
      float right_avg;
 
      // Calculate right avg after day i
      if (i == n - 1)
        right_avg = 0;
      else
        right_avg
        = (prefix_sum[n - 1] - prefix_sum[i])
        / (n - i - 1);
 
      // Store the price change in the diff
      float diff = left_avg - right_avg;
 
      // Store the day with
      // minimum stock price change
      if (diff < min_diff) {
        min_diff = (float)(diff);
        min_day = i + 1;
      }
    }
 
    // Print the day
    Console.WriteLine(min_day);
  }
 
  // Driver code
  public static void Main()
  {
    int []arr = { 15, 5, 10, 15, 5 };
    int N = arr.Length;
 
    // Function call
    find_the_day(arr, N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

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