Given an array **arr[]**, the task is to find the array formed from the difference of each element from the largest element in the given array.

**Example: **

Input:arr[] = {3, 6, 9, 2,6}

Output:{6, 3, 0, 7, 3}

Explanation:

Largest element of the array = 9

Therefore difference of arr[i] from 9:

Element 1: 9 – 3 = 6

Element 2: 9 – 6 = 3

Element 3: 9 – 9 = 0

Element 4: 9 – 2 = 7

Element 5: 9 – 6 = 3

Hence the output will be {6, 3, 0, 7, 3}

Input:arr[] = {7, 2, 5, 6, 3, 1, 6, 9}

Output:{2, 7, 4, 3, 6, 8, 3, 0}

**Approach:**

Find the largest of n elements in an array and store it in a variable largest. Now check the difference between largest and the other elements in the array.

Below is the implementation of the above approach:

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 prthe 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

0 5 1 7 8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find Array formed by adding each element of given array with largest element in new array to its left
- Form an array of distinct elements with each element as sum of an element from each array
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Sum of product of each element with each element after it
- Largest number in given Array formed by repeatedly combining two same elements
- Maximize number of groups formed with size not smaller than its largest element
- Generate Array whose difference of each element with its left yields the given Array
- Largest Divisor for each element in an array other than 1 and the number itself
- Find the largest number that can be formed with the given digits
- Area of the largest square that can be formed from the given length sticks using Hashing
- Largest square which can be formed using given rectangular blocks
- Replace each element by the difference of the total size of the array and frequency of that element
- Minimum element of each row and each column in a matrix
- Largest element smaller than current element on left for every element in Array
- Replace elements with absolute difference of smallest element on left and largest element on right
- Find just strictly greater element from first array for each element in second array
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Largest component size in a graph formed by connecting non-co-prime nodes
- Maximize 3rd element sum in quadruplet sets formed from given Array
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.