Dada una array arr[] de N enteros, la tarea es encontrar la suma de todos los elementos entre dos ceros en la array dada. Si es posible, imprima toda la suma, de lo contrario imprima «-1» .
Nota: No hay un cero continuo en la array dada.
Ejemplos:
Entrada: arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 } Salida: 7 8 7
Explicación :
La
suma de elementos entre cada cero es:
3 + 4 = 7
4 + 4 = 8
2 + 1 + 4 = 7Entrada: arr[] = { 1, 3, 4, 6, 0}
Salida: -1
Acercarse:
- Recorre la array dada arr[] y encuentra el primer índice con el elemento 0.
- Si aparece algún elemento con el valor cero, comience a almacenar la suma de los elementos que le siguen en un vector (por ejemplo, A[] ) hasta que aparezca el siguiente cero.
- Repita los pasos anteriores para cada cero que se produzca.
- Imprime los elementos almacenados en A[].
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include "bits/stdc++.h" using namespace std; // Function to find the sum between two // zeros in the given array arr[] void sumBetweenZero(int arr[], int N) { int i = 0; // To store the sum of element // between two zeros vector<int> A; // To store the sum int sum = 0; // Find first index of 0 for (i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array arr[] for (; i < N; i++) { // If 0 occurs then add it to A[] if (arr[i] == 0) { A.push_back(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for (int i = 0; i < A.size(); i++) { cout << A[i] << ' '; } // If there is no such element print -1 if (A.size() == 0) cout << "-1"; } // Driver Code int main() { int arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call sumBetweenZero(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the sum between two // zeros in the given array arr[] static void sumBetweenZero(int arr[], int N) { int i = 0; // To store the sum of element // between two zeros Vector<Integer> A = new Vector<Integer>(); // To store the sum int sum = 0; // Find first index of 0 for(i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array arr[] for(; i < N; i++) { // If 0 occurs then add it to A[] if (arr[i] == 0) { A.add(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for(int j = 0; j < A.size(); j++) { System.out.print(A.get(j) + " "); } // If there is no such element print -1 if (A.size() == 0) System.out.print("-1"); } // Driver Code public static void main(String[] args) { int arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 }; int N = arr.length; // Function call sumBetweenZero(arr, N); } } // This code is contributed by gauravrajput1
Python3
#Python3 program for the above approach # Function to find the sum between two # zeros in the given array arr[] def sumBetweenZero(arr, N): i = 0 # To store the sum of the element # between two zeros A = [] # To store the sum sum = 0 # Find first index of 0 for i in range(N): if (arr[i] == 0): i += 1 break k = i # Traverse the given array arr[] for i in range(k, N, 1): # If 0 occurs then add it to A[] if (arr[i] == 0): A.append(sum) sum = 0 # Else add element to the sum else: sum += arr[i] # Print all the sum stored in A for i in range(len(A)): print(A[i], end = ' ') # If there is no such element print -1 if (len(A) == 0): print("-1") # Driver Code if __name__ == '__main__': arr = [ 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 ] N = len(arr) # Function call sumBetweenZero(arr, N) # This code is contributed by Bhupendra_Singh
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the sum between two // zeros in the given array []arr static void sumBetweenZero(int []arr, int N) { int i = 0; // To store the sum of element // between two zeros List<int> A = new List<int>(); // To store the sum int sum = 0; // Find first index of 0 for(i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array []arr for(; i < N; i++) { // If 0 occurs then add it to []A if (arr[i] == 0) { A.Add(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for(int j = 0; j < A.Count; j++) { Console.Write(A[j] + " "); } // If there is no such element print -1 if (A.Count == 0) Console.Write("-1"); } // Driver Code public static void Main(String[] args) { int []arr = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 }; int N = arr.Length; // Function call sumBetweenZero(arr, N); } } // This code is contributed by gauravrajput1
Javascript
<script> // Javascript program for the above approach // Function to find the sum between two // zeros in the given array arr[] function sumBetweenZero(arr, N) { let i = 0; // To store the sum of element // between two zeros let A = new Array(); // To store the sum let sum = 0; // Find first index of 0 for (i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array arr[] for (; i < N; i++) { // If 0 occurs then add it to A[] if (arr[i] == 0) { A.push(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for (let i = 0; i < A.length; i++) { document.write(A[i] + ' '); } // If there is no such element print -1 if (A.length == 0) document.write("-1"); } // Driver Code let arr = [1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3]; let N = arr.length; // Function call sumBetweenZero(arr, N); // This code is contributed by _saurabh_jaiswal </script>
7 8 7
Complejidad de tiempo: O(N) , donde N es la longitud de la array.
Publicación traducida automáticamente
Artículo escrito por ShivaTeja2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA