Dada una array de enteros. La tarea es encontrar el índice en la array en el que el valor de prefixSum(i) + suffixSum(i) es mínimo.
Nota :
- PrefixSum(i) = La suma de los primeros i números de la array.
- SuffixSum(i) = la suma de los últimos N – i + 1 números de la array.
- La indexación basada en 1 se considera para la array. Ese es un índice del primer elemento en la array es 1.
Ejemplos:
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
Dado que necesitamos minimizar el valor de PrefixSum[i] + SuffixSum[i] . Esa es la suma de los primeros elementos y los elementos del final.
Si se observa cuidadosamente, se puede ver que:
PrefixSum[i] + SuffixSum[i] = Suma de todos los elementos en el arreglo + arr[i] (Elemento en el i-ésimo índice)
Dado que la suma de todos los elementos de la array será la misma para cada índice, el valor de PrefixSum[i] + SuffixSum[i] será mínimo para el valor mínimo de arr[i] .
Por lo tanto, la tarea se reduce a encontrar solo el índice del elemento mínimo de la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the index with // minimum sum of prefix and suffix // sums in an Array #include <bits/stdc++.h> 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
<?php // PHP program to find the index with // minimum sum of prefix and suffix // sums in an Array function indexMinSum($arr, $n) { // Initialization of the // min value $min = $arr[0]; $index = 0; // Find minimum element in // the array for ($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 $arr = array(6, 8, 2, 3, 5 ); $n = sizeof($arr); echo indexMinSum($arr, $n); // This code is contributed by Sachin ?>
Javascript
<script> // JavaScript program to find the index with // minimum sum of prefix and suffix // sums in an Array function indexMinSum(arr,n) { // Initialization of the min value let min = arr[0]; let index = 0; // Find minimum element in the array for (let 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 let arr=[6, 8, 2, 3, 5]; let n =arr.length; document.write( indexMinSum(arr, n)); // This code is contributed by avanitrachhadiya2155 </script>
3
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA