Dada una array arr de tamaño N y un número entero K , la tarea es encontrar la suma máxima de subarreglo de tamaño k entre todos los subarreglos contiguos (considerando también el subarreglo circular).
Ejemplos:
Entrada: arr = {18, 4, 3, 4, 5, 6, 7, 8, 2, 10}, k = 3
Salida:
suma circular máxima = 32
índice inicial = 9
índice final = 1
Explicación:
Suma máxima = 10 + 18 + 4 = 32Entrada: arr = {8, 2, 5, 9}, k = 4
Salida:
suma circular máxima = 24
índice inicial = 0
índice final = 3
Acercarse:
- Iterar el bucle hasta (n + k) veces y
- Tome (i % n) para manejar el caso cuando el índice de la array se vuelve mayor que n.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find maximum circular // subarray sum of size k #include <bits/stdc++.h> using namespace std; // Function to calculate // maximum sum void maxCircularSum(int arr[], int n, int k) { // k must be greater if (n < k) { cout << "Invalid"; return; } int sum = 0, start = 0, end = k - 1; // calculate the sum of first k elements. for (int i = 0; i < k; i++) { sum += arr[i]; } int ans = sum; for (int i = k; i < n + k; i++) { // add current element to sum // and subtract the first element // of the previous window. sum += arr[i % n] - arr[(i - k) % n]; if (sum > ans) { ans = sum; start = (i - k + 1) % n; end = i % n; } } cout << "max circular sum = " << ans << endl; cout << "start index = " << start << "\nend index = " << end << endl; } // Driver Code int main() { int arr[] = { 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; maxCircularSum(arr, n, k); return 0; }
Java
// Java program to find maximum circular // subarray sum of size k import java.util.*; class GFG { // Function to calculate // maximum sum static void maxCircularSum(int[] arr, int n, int k) { // k must be greater if (n < k) { System.out.println("Invalid"); return; } int sum = 0, start = 0, end = k - 1; // calculate the sum of first k elements. for (int i = 0; i < k; i++) sum += arr[i]; int ans = sum; for (int i = k; i < n + k; i++) { // add current element to sum // and subtract the first element // of the previous window. sum += arr[i % n] - arr[(i - k) % n]; if (sum > ans) { ans = sum; start = (i - k + 1) % n; end = i % n; } } System.out.println("max circular sum = " + ans); System.out.println("start index = " + start + "\nend index = " + end); } // Driver Code public static void main(String[] args) { int[] arr = { 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 }; int n = arr.length; int k = 3; maxCircularSum(arr, n, k); } } // This code is contributed by // sanjeev2552
Python3
# Python3 program to find maximum circular # subarray sum of size k # Function to calculate # maximum sum def maxCircularSum(arr, n, k) : # k must be greater if (n < k) : print("Invalid"); return; sum = 0; start = 0; end = k - 1; # calculate the sum of first k elements. for i in range(k) : sum += arr[i]; ans = sum; for i in range(k, n + k) : # add current element to sum # and subtract the first element # of the previous window. sum += arr[i % n] - arr[(i - k) % n]; if (sum > ans) : ans = sum; start = (i - k + 1) % n; end = i % n; print("max circular sum = ",ans); print("start index = ", start, "\nend index = ", end); # Driver Code if __name__ == "__main__" : arr = [ 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 ]; n = len(arr); k = 3; maxCircularSum(arr, n, k); # This code is contributed by AnkitRai01
C#
// C# program to find maximum circular // subarray sum of size k using System; class GFG { // Function to calculate // maximum sum static void maxCircularSum(int[] arr, int n, int k) { // k must be greater if (n < k) { Console.WriteLine("Invalid"); return; } int sum = 0, start = 0, end = k - 1; // calculate the sum of first k elements. for (int i = 0; i < k; i++) sum += arr[i]; int ans = sum; for (int i = k; i < n + k; i++) { // add current element to sum // and subtract the first element // of the previous window. sum += arr[i % n] - arr[(i - k) % n]; if (sum > ans) { ans = sum; start = (i - k + 1) % n; end = i % n; } } Console.WriteLine("max circular sum = " + ans); Console.WriteLine("start index = " + start + "\nend index = " + end); } // Driver Code public static void Main(String[] args) { int[] arr = { 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 }; int n = arr.Length; int k = 3; maxCircularSum(arr, n, k); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript program to find maximum circular // subarray sum of size k // Function to calculate // maximum sum function maxCircularSum(arr, n, k) { // k must be greater if (n < k) { document.write("Invalid"); return; } let sum = 0, start = 0, end = k - 1; // Calculate the sum of first k elements. for(let i = 0; i < k; i++) { sum += arr[i]; } let ans = sum; for(let i = k; i < n + k; i++) { // Add current element to sum // and subtract the first element // of the previous window. sum += arr[i % n] - arr[(i - k) % n]; if (sum > ans) { ans = sum; start = (i - k + 1) % n; end = i % n; } } document.write("max circular sum = " + ans + "<br>"); document.write("start index = " + start + "<br>end index = " + end + "<br>"); } // Driver Code let arr = [ 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 ]; let n = arr.length let k = 3; maxCircularSum(arr, n, k); // This code is contributed by _saurabh_jaiswal </script>
max circular sum = 32 start index = 9 end index = 1
Complejidad del tiempo:
Publicación traducida automáticamente
Artículo escrito por MilanViradia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA