Dada una array de enteros arr , la tarea es encontrar la diferencia absoluta actual de elementos en posiciones de índice pares e impares por separado.
Nota: la indexación basada en 0 se considera para la array. Ese es el índice del primer elemento en la array es cero.
Ejemplos:
Entrada: arr[] = {1, 2, 3, 4, 5, 6}
Salida: Diferencia absoluta de índice par: 3
Diferencia absoluta de índice impar: 4
Explicación:
aquí, los elementos indexados pares son 1, 3 y 5
Por lo tanto, el índice par absoluto la diferencia será (|1 – 3| = 2) => (|2 – 5| = 3)
De manera similar, los elementos indexados impares son 2, 4 y 6
y la diferencia absoluta impar será (|2 – 4| = 2) = > (|2 – 6| = 4)
Entrada: arr[] = {10, 20, 30, 40, 50, 60, 70}
Salida: Diferencia absoluta del índice par: 40
Diferencia absoluta del índice impar: 40
Enfoque: recorra la array y mantenga dos variables pares e impares para almacenar la diferencia absoluta de elementos de índices pares e impares, respectivamente. Mientras atraviesa, verifique si el índice actual es par, es decir (i%2) == 0 . Imprime los resultados al final.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find absolute difference of elements // at even and odd index positions separately #include <bits/stdc++.h> using namespace std; // Function to calculate absolute difference void EvenOddAbsoluteDifference(int arr[], int n) { int even = 0; int odd = 0; for (int i = 0; i < n; i++) { // Loop to find even, odd absolute difference if (i % 2 == 0) even = abs(even - arr[i]); else odd = abs(odd - arr[i]); } cout << "Even Index absolute difference : " << even; cout << endl; cout << "Odd Index absolute difference : " << odd; } // Driver Code int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); EvenOddAbsoluteDifference(arr, n); return 0; }
C
// C program to find absolute difference of elements // at even and odd index positions separately #include <stdio.h> int abs(int a) { int abs = a; if(abs < 0) abs = abs * (-1); return abs; } // Function to calculate absolute difference void EvenOddAbsoluteDifference(int arr[], int n) { int even = 0; int odd = 0; for (int i = 0; i < n; i++) { // Loop to find even, odd absolute difference if (i % 2 == 0) even = abs(even - arr[i]); else odd = abs(odd - arr[i]); } printf("Even Index absolute difference : %d\n",even); printf("Odd Index absolute difference : %d\n",odd); } // Driver Code int main() { int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); EvenOddAbsoluteDifference(arr, n); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to find absolute difference of elements // at even and odd index positions separately public class GFG{ // Function to calculate absolute difference static void EvenOddAbsoluteDifference(int arr[], int n) { int even = 0; int odd = 0; for (int i = 0; i < n; i++) { // Loop to find even, odd absolute difference if (i % 2 == 0) even = Math.abs(even - arr[i]); else odd = Math.abs(odd - arr[i]); } System.out.println("Even Index absolute difference : " + even); System.out.println("Odd Index absolute difference : " + odd); } // Driver Code public static void main(String []args){ int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = arr.length; EvenOddAbsoluteDifference(arr, n); } // This code is contributed by ANKITRAI1 }
Python3
# Python 3 program to find absolute difference of # elements at even and odd index positions separately # Function to calculate absolute difference def EvenOddAbsoluteDifference(arr, n): even = 0 odd = 0 for i in range(0, n, 1): # Loop to find even, odd absolute # difference if (i % 2 == 0): even = abs(even - arr[i]); else: odd = abs(odd - arr[i]); print("Even Index absolute difference :", even) print("Odd Index absolute difference :", odd) # Driver Code if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6] n = len(arr) EvenOddAbsoluteDifference(arr, n) # This code is contributed by # Sahil_shelangia
C#
// C# program to find absolute difference // of elements at even and odd index // positions separately using System; class GFG { // Function to calculate absolute difference static void EvenOddAbsoluteDifference(int []arr, int n) { int even = 0; int odd = 0; for (int i = 0; i < n; i++) { // Loop to find even, odd // absolute difference if (i % 2 == 0) even = Math.Abs(even - arr[i]); else odd = Math.Abs(odd - arr[i]); } Console.WriteLine("Even Index absolute " + "difference : " + even); Console.WriteLine("Odd Index absolute " + "difference : " + odd); } // Driver Code static public void Main () { int []arr = { 1, 2, 3, 4, 5, 6 }; int n = arr.Length; EvenOddAbsoluteDifference(arr, n); } } // This code is contributed by Sachin
PHP
<?php // PHP program to find absolute // difference of elements at even // and odd index positions separately // Function to calculate absolute // difference function EvenOddAbsoluteDifference($arr, $n) { $even = 0; $odd = 0; for ($i = 0; $i < $n; $i++) { // Loop to find even, odd // absolute difference if ($i % 2 == 0) $even = abs($even - $arr[$i]); else $odd = abs($odd - $arr[$i]); } echo "Even Index absolute difference : ", $even; echo "\n"; echo "Odd Index absolute difference : ", $odd; } // Driver Code $arr = array( 1, 2, 3, 4, 5, 6 ); $n = sizeof($arr); EvenOddAbsoluteDifference($arr, $n); // This code is contributed by Sachin ?>
Javascript
<script> // Javascript program to find absolute difference // of elements at even and odd index // positions separately // Function to calculate absolute difference function EvenOddAbsoluteDifference(arr, n) { let even = 0; let odd = 0; for (let i = 0; i < n; i++) { // Loop to find even, odd // absolute difference if (i % 2 == 0) even = Math.abs(even - arr[i]); else odd = Math.abs(odd - arr[i]); } document.write("Even Index absolute " + "difference : " + even + "</br>"); document.write("Odd Index absolute " + "difference : " + odd + "</br>"); } let arr = [ 1, 2, 3, 4, 5, 6 ]; let n = arr.length; EvenOddAbsoluteDifference(arr, n); </script>
Even Index absolute difference : 3 Odd Index absolute difference : 4
Complejidad de tiempo: O(n), donde n representa el tamaño de la array dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Publicación traducida automáticamente
Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA