Index with minimum sum of prefix and suffix sums in an array in C++
Let us consider an array of integers where the prefix sum is the sum of all elements from index 0 to i, and the suffix sum is the sum of all elements from index i to the end of the array. For each index i in the array, these sums are recorded. The challenge is to determine an index (i) at which the prefix and suffix sums at that index are as small as possible. In other terms, we wish to minimize the value of prefix_sum[0, i] + suffix_sum[i, n-1] for all valid indices i, where n is the array's size.
Key Points:
- PrefixSum(i): PrefixSum(i) is the total of the array's first i numbers.
- SuffixSum(i): SuffixSum(i) is the total of the array's last N – i + 1 number.
- For the array, 1-based indexing is taken into account. It indicates that the array's first element's index is 1.
Example:
Input : arr[] = {32, 57, 561, 16, 6 }
Output : 5
Explanation:
Presum[] = {31, 28, 49, 19, 121}
Postsum[] = { 28, 18, 16, 12, 65}
Presum[] + Postsum[] = {59, 46, 65, 31, 186}
It is clear that the min value of the sum of prefix and suffix sum is 31 at index 4.
Input: arr[] = { 13, 22, 65, 87, 93 }
Output: 1
Example:
Let us take an example to illustrate the Index with a minimum sum of prefix and suffix sums in an array in C++.
#include <bits/stdc++.h>
using namespace std;
int findIndexWithMinSum(const int arr[], int n) {
// Initialization of the min value
int minSum = arr[0];
int minIndex = 0;
// Find the minimum element in the array
for (int i = 1; i < n; i++) {
if (arr[i] < minSum) {
// store the index of the current minimum element
minSum = arr[i];
minIndex = i;
}
}
// Return the index of the minimum element (1-based index)
return minIndex + 1;
}
// Driver Code
int main() {
int arr[] = {31, 15, 111, 26, 56};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Index with minimum sum of prefix and suffix sums: " << findIndexWithMinSum(arr, n) << endl;
return 0;
}
Output:
Index with minimum sum of prefix and suffix sums: 2
Complexity Analysis:
Here is the complexity of the program.
Time Complexity: O(N)
Auxiliary Space: O(1)
Brute Force Mechanism:
The brute force method for solving this problem is to compute the prefixSum(i) and suffixSum(i) for each index i in the array. Next, determine the total of these two numbers and record the lowest value along with its index. Return the index containing the lowest sum at the end. It has the following steps:
- Set the minSum and minIndex variables to their maximum values that an integer can hold.
- Iterate through the array from index 1 to n.
- PrefixSum and suffixSum variables should be set to 0 for each index i.
- Calculate prefixSum, which is the sum of the elements from index 1 to index i, by iterating through the array from index 1 to i.
- SuffixSum is the sum of the elements from index n to index i, which is calculated by iterating over the array from index n to i.
- Determine the total of the prefix and suffix sums, and then assign it to a variable called sum.
- In the event that sum is less than minSum, update minIndex to i and minSum to sum.
- Finally, give back minIndex's value.
Example:
Let us take another example to illustrate the Index with a minimum sum of prefix and suffix sums in an array in C++.
#include <iostream>
#include <climits>
int findIndexWithMinSum(const int arr[], int n) {
int minIndex = -1;
int minSum = INT_MAX;
for (int i = 0; i < n; ++i) {
int prefixSum = 0;
int suffixSum = 0;
// Calculating prefix sum
for (int j = 0; j <= i; ++j) {
prefixSum += arr[j];
}
// Calculate suffix sum
for (int j = i; j < n; ++j) {
suffixSum += arr[j];
}
// Calculating total sum of prefix and suffix sums
int totalSum = prefixSum + suffixSum;
// Update minimum sum and index if the current total sum is smaller
if (totalSum < minSum) {
minSum = totalSum;
minIndex = i;
}
}
return minIndex;
}
int main() {
int arr[] = {13, 35, 21, 56, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int resultIndex = findIndexWithMinSum(arr, n);
std::cout << "Index with minimum sum of prefix and suffix sums: " << resultIndex << std::endl;
return 0;
}
Output:
Index with minimum sum of prefix and suffix sums: 4
Conclusion:
Many techniques can be used to solve the problem of locating the index in an array that has the lowest sum of the prefix and suffix sums. The brute-force method entails iterating through each index by calculating the prefix and suffix sums and figuring out the minimum sum. Despite being easy to understand and simple in theory, the time complexity of this method may make it ineffective for larger arrays. As an alternative, pre-computing prefix and suffix sums can lead to more optimized solutions and faster execution times. An appropriate approach can be chosen based on the desired balance between simplicity and efficiency and the size of the input array.