Dada una array arr[] , la tarea es encontrar la array formada por la diferencia de cada elemento del elemento más grande en la array dada.
Ejemplo:
Entrada: arr[] = {3, 6, 9, 2,6}
Salida: {6, 3, 0, 7, 3}
Explicación:
Elemento más grande de la array = 9
Por lo tanto, la diferencia de arr[i] de 9:
Elemento 1: 9 – 3 = 6
Elemento 2: 9 – 6 = 3
Elemento 3: 9 – 9 = 0
Elemento 4: 9 – 2 = 7
Elemento 5: 9 – 6 = 3
Por lo tanto, la salida será {6, 3, 0 , 7, 3}Entrada: arr[] = {7, 2, 5, 6, 3, 1, 6, 9}
Salida: {2, 7, 4, 3, 6, 8, 3, 0}
Enfoque:
Encuentre el mayor de n elementos en una array y guárdelo en una variable mayor. Ahora comprueba la diferencia entre el elemento más grande y los otros elementos de la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the array formed // from the difference of each element // from the largest element in the given array #include <iostream> using namespace std; int difference(int arr[], int n) { // Initializing current largest // as the first element. int largest = arr[0]; int i; // For loop to compute // the largest element for (i = 0; i < n; i++) { // Checking if the current element // is greater than the defined largest if (largest < arr[i]) largest = arr[i]; } // For loop to replace the elements // in the array with the difference for (i = 0; i < n; i++) arr[i] = largest - arr[i]; // For loop to print the elements for (i = 0; i < n; i++) cout << arr[i] << " "; } // Driver code int main() { int arr[] = { 10, 5, 9, 3, 2 }; int n = sizeof(arr) / sizeof(arr[0]); difference(arr, n); return 0; }
Java
// Java program to find the array formed // from the difference of each element // from the largest element in the given array import java.util.*; class GFG { static void difference(int arr[], int n) { // Initializing current largest // as the first element. int largest = arr[0]; int i; // For loop to compute // the largest element for (i = 0; i < n; i++) { // Checking if the current element // is greater than the defined largest if (largest < arr[i]) largest = arr[i]; } // For loop to replace the elements // in the array with the difference for (i = 0; i < n; i++) arr[i] = largest - arr[i]; // For loop to print the elements for (i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Driver code public static void main(String[] args) { int arr[] = { 10, 5, 9, 3, 2 }; int n = arr.length; difference(arr, n); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program to find the array formed # from the difference of each element # from the largest element in the given array def difference(arr, n): # Initializing current largest # as the first element. largest = arr[0]; i = 0; # For loop to compute # the largest element for i in range(n): # Checking if the current element # is greater than the defined largest if (largest < arr[i]): largest = arr[i]; # For loop to replace the elements # in the array with the difference for i in range(n): arr[i] = largest - arr[i]; # For loop to print the elements for i in range(n): print(arr[i], end = " "); # Driver code if __name__ == '__main__': arr = [ 10, 5, 9, 3, 2 ]; n = len(arr); difference(arr, n); # This code is contributed by Rajput-Ji
C#
// C# program to find the array formed // from the difference of each element // from the largest element in the given array using System; class GFG { static void difference(int []arr, int n) { // Initializing current largest // as the first element. int largest = arr[0]; int i; // For loop to compute // the largest element for (i = 0; i < n; i++) { // Checking if the current element // is greater than the defined largest if (largest < arr[i]) largest = arr[i]; } // For loop to replace the elements // in the array with the difference for (i = 0; i < n; i++) arr[i] = largest - arr[i]; // For loop to print the elements for (i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Driver code public static void Main(String[] args) { int []arr = { 10, 5, 9, 3, 2 }; int n = arr.Length; difference(arr, n); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript program to find the array formed // from the difference of each element // from the largest element in the given array function difference(arr, n) { // Initializing current largest // as the first element. let largest = arr[0]; let i; // For loop to compute // the largest element for (i = 0; i < n; i++) { // Checking if the current element // is greater than the defined largest if (largest < arr[i]) largest = arr[i]; } // For loop to replace the elements // in the array with the difference for (i = 0; i < n; i++) arr[i] = largest - arr[i]; // For loop to print the elements for (i = 0; i < n; i++) document.write(arr[i] + " "); } // Driver code let arr = [10, 5, 9, 3, 2]; let n = arr.length; difference(arr, n); </script>
0 5 1 7 8