Dada una array de enteros de N elementos. La tarea es imprimir la diferencia absoluta de todos los elementos consecutivos por pares. Los pares consecutivos por pares de una array de tamaño N son (a[i], a[i+1]) para todos los i que van de 0 a N-2
Ejemplos:
Input: arr[] = {8, 5, 4, 3, 15, 20} Output: 3, 1, 1, 12, 5 Input: arr[] = {5, 10, 15, 20} Output: 5, 5, 5
Enfoque: La solución es atravesar la array y calcular e imprimir la diferencia absoluta de cada par (arr[i], arr[i+1]).
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to print the absolute // difference of the consecutive elements #include <iostream> using namespace std; // Function to print pairwise absolute // difference of consecutive elements void pairwiseDifference(int arr[], int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = abs(arr[i] - arr[i + 1]); cout << diff << " "; } } // Driver Code int main() { int arr[] = { 4, 10, 15, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); pairwiseDifference(arr, n); return 0; }
C
// C program to print the absolute // difference of the consecutive elements #include <stdio.h> int abs(int a) { int abs = a; if(abs < 0) abs = abs * (-1); return abs; } // Function to print pairwise absolute // difference of consecutive elements void pairwiseDifference(int arr[], int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = abs(arr[i] - arr[i + 1]); printf("%d ",diff); } } // Driver Code int main() { int arr[] = { 4, 10, 15, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); pairwiseDifference(arr, n); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to print the absolute // difference of the consecutive elements class GFG{ // Function to print pairwise absolute // difference of consecutive elements static void pairwiseDifference(int arr[], int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = Math.abs(arr[i] - arr[i + 1]); System.out.print(diff+" "); } } // Driver Code public static void main(String[] args) { int arr[] = { 4, 10, 15, 5, 6 }; int n = arr.length; pairwiseDifference(arr, n); } } // This code is contributed by mits
Python 3
# Python 3 program to print the absolute # difference of the consecutive elements # Function to print pairwise absolute # difference of consecutive elements def pairwiseDifference(arr, n): for i in range(n - 1) : # absolute difference between # consecutive numbers diff = abs(arr[i] - arr[i + 1]) print(diff , end = " ") # Driver Code if __name__=="__main__": arr = [ 4, 10, 15, 5, 6 ] n = len(arr) pairwiseDifference(arr, n) # This code is contributed # by ChitraNayal
C#
// C# program to print the absolute // difference of the consecutive elements using System; class GFG{ // Function to print pairwise absolute // difference of consecutive elements static void pairwiseDifference(int []arr, int n) { int diff; for (int i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = Math.Abs(arr[i] - arr[i + 1]); Console.WriteLine(diff+" "); } } // Driver Code public static void Main(String[] args) { int []arr = { 4, 10, 15, 5, 6 }; int n = arr.Length; pairwiseDifference(arr, n); } }
PHP
<?php // PHP program to print the absolute // difference of the consecutive elements // Function to print pairwise absolute // difference of consecutive elements function pairwiseDifference($arr, $n) { $diff = 0; for ($i = 0; $i < $n - 1; $i++) { // absolute difference between // consecutive numbers $diff = abs($arr[$i] - $arr[$i + 1]); echo $diff." "; } } // Driver Code $arr = array( 4, 10, 15, 5, 6 ); $n = sizeof($arr); pairwiseDifference($arr, $n); // This code is contributed by mits ?>
Javascript
<script> // javascript program to print the absolute // difference of the consecutive elements // Function to print pairwise absolute // difference of consecutive elements function pairwiseDifference(arr , n) { var diff; for (i = 0; i < n - 1; i++) { // absolute difference between // consecutive numbers diff = Math.abs(arr[i] - arr[i + 1]); document.write(diff + " "); } } // Driver Code var arr = [ 4, 10, 15, 5, 6 ]; var n = arr.length; pairwiseDifference(arr, n); // This code contributed by umadevi9616 </script>
Producción:
6 5 10 1
Complejidad temporal: O(n), Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA