Requisito previo: conceptos básicos de array
Dada una array, escriba un programa para encontrar la suma de los valores de las posiciones de índice pares e impares por separado.
Ejemplos:
Input : arr = {1, 2, 3, 4, 5, 6} Output :Even index positions sum 9 Odd index positions sum 12 Explanation: Here, n = 6 so there will be 3 even index positions and 3 odd index positions in an array Even = 1 + 3 + 5 = 9 Odd = 2 + 4 + 6 = 12 Input : arr = {10, 20, 30, 40, 50, 60, 70} Output : Even index positions sum 160 Odd index positions sum 170 Explanation: Here, n = 7 so there will be 3 odd index positions and 4 even index positions in an array Even = 10 + 30 + 50 + 70 = 160 Odd = 20 + 40 + 60 = 120
Implementación:
C++
// CPP program to find out // Sum of elements at even and // odd index positions separately #include <iostream> using namespace std; // Function to calculate sum void EvenOddSum(int arr[], int n) { int even = 0; int odd = 0; for (int i = 0; i < n; i++) { // Loop to find even, odd sum if (i % 2 == 0) even += arr[i]; else odd += arr[i]; } cout << "Even index positions sum " << even; cout << "\nOdd index positions sum " << odd; } // Driver function int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); EvenOddSum(arr, n); return 0; }
Java
// Java program to find out // Sum of elements at even and // odd index positions separately import java.io.*; class EvenOddSum { public static void main(String args[]) { int arr[] = { 1, 2, 3, 4, 5, 6 }; int even = 0, odd = 0; // Loop to find even, odd sum for (int i = 0; i < arr.length; i++) { if (i % 2 == 0) even += arr[i]; else odd += arr[i]; } System.out.println("Even index positions sum: " + even); System.out.println("Odd index positions sum: " + odd); } }
Python3
# Python program to find out # Sum of elements at even and # odd index positions separately # Function to calculate Sum def EvenOddSum(a, n): even = 0 odd = 0 for i in range(n): # Loop to find even, odd Sum if i % 2 == 0: even += a[i] else: odd += a[i] print ("Even index positions sum ", even) print ("nOdd index positions sum ", odd) # Driver Function arr = [1, 2, 3, 4, 5, 6] n = len(arr) EvenOddSum(arr, n) # This code is contributed by Sachin Bisht
C#
// C# program to find out // Sum of elements at even and // odd index positions separately using System; public class GFG { public static void Main() { int[] arr = { 1, 2, 3, 4, 5, 6 }; int even = 0, odd = 0; // Loop to find even, odd sum for (int i = 0; i < arr.Length; i++) { if (i % 2 == 0) even += arr[i]; else odd += arr[i]; } Console.WriteLine("Even index positions" + " sum: " + even); Console.WriteLine("Odd index positions " + "sum: " + odd); } } // This code is contributed by Sam007.
PHP
<?php // PHP program to find out // Sum of elements at even and // odd index positions separately // Function to calculate sum function EvenOddSum($arr, $n) { $even = 0; $odd = 0; for ($i = 0; $i < $n; $i++) { // Loop to find even, odd sum if ($i % 2 == 0) $even += $arr[$i]; else $odd += $arr[$i]; } echo("Even index positions sum " . $even); echo("\nOdd index positions sum " . $odd); } // Driver Code $arr = array( 1, 2, 3, 4, 5, 6 ); $n = sizeof($arr); EvenOddSum($arr, $n); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript program to find out // Sum of elements at even and // odd index positions separately // Function to calculate sum function EvenOddSum(arr, n) { let even = 0; let odd = 0; for (let i = 0; i < n; i++) { // Loop to find even, odd sum if (i % 2 == 0) even += arr[i]; else odd += arr[i]; } document.write("Even index positions sum " + even); document.write("<br>" + "Odd index positions sum " + odd); } // Driver function let arr = [ 1, 2, 3, 4, 5, 6 ]; let n = arr.length; EvenOddSum(arr, n); // This code is contributed by Mayank Tyagi </script>
Even index positions sum 9 Odd index positions sum 12
Complejidad de tiempo: O(longitud(arr))
Espacio auxiliar: 0(1)
Método 2: Usando el corte en python:
Calcule la suma de todos los índices pares utilizando el corte y repita lo mismo con los índices impares e imprima la suma.
A continuación se muestra la implementación:
Python3
# Python program to find out # Sum of elements at even and # odd index positions separately # Function to calculate Sum def EvenOddSum(a, n): even_sum = sum(a[::2]) odd_sum = sum(a[1::2]) print("Even index positions sum", even_sum) print("Odd index positions sum", odd_sum) # Driver Function arr = [1, 2, 3, 4, 5, 6] n = len(arr) EvenOddSum(arr, n) # This code is contributed by vikkycirus
Javascript
<script> // Javascript program to find out // Sum of elements at even and // odd index positions separately // Function to calculate sum function EvenOddSum(arr, n) { let even = 0; let odd = 0; for (let i = 0; i < n; i++) { // Loop to find even, odd sum if (i % 2 == 0) even += arr[i]; else odd += arr[i]; } document.write("Even index positions sum " + even); document.write("<br>" + "Odd index positions sum " + odd); } // Driver function let arr = [ 1, 2, 3, 4, 5, 6 ]; let n = arr.length; EvenOddSum(arr, n); // This code is contributed by avijitmondal1998. </script>
Even index positions sum 9 Odd index positions sum 12
Complejidad temporal: O(1)
Espacio auxiliar: 0(1)
Este artículo es una contribución de Rishabh Jain . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA