数组中前缀和后缀和的最小总和的索引
给定一个整数数组。任务是找到索引在prefixSum(i) + suffixSum(i)的值最小的数组中。
注意:
- PrefixSum(i) = 数组前 i 个数字的总和。
- SuffixSum(i) = 最后 N - i + 1 个数组的数字之和。
- 数组考虑基于 1 的索引。也就是说数组中第一个元素的索引是 1。
例子:
Input : arr[] = {3, 5, 1, 6, 6 }
Output : 3
Explanation:
Presum[] = {3, 8, 9, 15, 21}
Postsum[] = { 21, 18, 13, 12, 6}
Presum[] + Postsum[] = {24, 26, 22, 27, 27}
It is clear that the min value of sum of
prefix and suffix sum is 22 at index 3.
Input : arr[] = { 3, 2, 5, 7, 3 }
Output : 2
鉴于我们需要最小化PrefixSum[i] + SuffixSum[i]的值。这是第一个的总和元素和从头开始的元素。
如果仔细观察,可以看出:
PrefixSum[i] + SuffixSum[i] = Sum of all elements in array + arr[i](Element at i-th index)
由于数组中所有元素的总和对于每个索引都是相同的,因此PrefixSum[i] + SuffixSum[i]的值对于arr[i]的最小值将是最小值。
因此,任务减少到只找到数组最小元素的索引。
下面是上述方法的实现:
C++
// C++ program to find the index with
// minimum sum of prefix and suffix
// sums in an Array
#include
using namespace std;
int indexMinSum(int arr[], int n)
{
// Initialization of the min value
int min = arr[0];
int index = 0;
// Find minimum element in the array
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
// store the index of the
// current minimum element
min = arr[i];
index = i;
}
}
// return the index of min element
// 1-based index
return index + 1;
}
// Driver Code
int main()
{
int arr[] = { 6, 8, 2, 3, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << indexMinSum(arr, n);
return 0;
}
Java
// Java program to find the index with
// minimum sum of prefix and suffix
// sums in an Array
import java.io.*;
class GFG {
static int indexMinSum(int arr[], int n)
{
// Initialization of the min value
int min = arr[0];
int index = 0;
// Find minimum element in the array
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
// store the index of the
// current minimum element
min = arr[i];
index = i;
}
}
// return the index of min element
// 1-based index
return index + 1;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 6, 8, 2, 3, 5 };
int n =arr.length;
System.out.println( indexMinSum(arr, n));
}
}
// This code is contributed by inder_verma..
Python 3
# Python 3 program to find the index with
# minimum sum of prefix and suffix
# sums in an Array
def indexMinSum(arr, n):
# Initialization of the min value
min = arr[0]
index = 0
# Find minimum element in the array
for i in range(1, n) :
if (arr[i] < min) :
# store the index of the
# current minimum element
min = arr[i]
index = i
# return the index of min element
# 1-based index
return index + 1
# Driver Code
if __name__ == "__main__":
arr = [ 6, 8, 2, 3, 5 ]
n = len(arr)
print(indexMinSum(arr, n))
# This code is contributed by ita_c
C#
// C# program to find the index with
// minimum sum of prefix and suffix
// sums in an Array
using System;
class GFG
{
static int indexMinSum(int []arr, int n)
{
// Initialization of the min value
int min = arr[0];
int index = 0;
// Find minimum element in the array
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
// store the index of the
// current minimum element
min = arr[i];
index = i;
}
}
// return the index of min element
// 1-based index
return index + 1;
}
// Driver Code
static void Main()
{
int []arr = { 6, 8, 2, 3, 5 };
int n =arr.Length;
Console.WriteLine(indexMinSum(arr, n));
}
// This code is contributed by ANKITRAI1
}
PHP
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)