Given an array **A** of size **N**, the task is to find the resultant array formed by adding each element of the given array with the largest element in the new array to its left.

**Examples:**

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

Output:{5, 6, 12, 9, 14}

Element A_{0}: No element if present at its left. Hence the element at 0th index of the resultant array = 5

Element A_{1}: Largest element to its left in the resultant array = 5. Hence the element at 1th index of the resultant array = 1 + 5 = 6

Element A_{2}: Largest element to its left in the resultant array = 6. Hence the element at 2nd index of the resultant array = 6 + 6 = 12

Element A_{3}: Largest element to its left in the resultant array = 12. Hence the element at 3rd index of the result array = -3 + 12 = 9

Element A_{4}: Largest element to its left in the resultant array = 12. Hence the element at 4th index of the result array = 2 + 12 = 14

Therefore the resultant array = {5, 6, 12, 9, 14}

Input:arr[] = {40, 12, 62}

Output:{40, 52, 114}

**Approach:**

Inorder to find such array, each element of the new array will be computed one by one for each index in the range [0, N-1], according to the following rules:

- For the starting index, i.e. 0, the new array will be empty. Hence there won’t be any largest element. In this case, the element at 0th index in the given array is copied down in the new array, i.e.
B

_{0}= A_{0}where A is the given array and B is the new array - For every other index in the range [1, N-1], first the largest element is found out to its left in the new array and then it is added to the corresponding element of the original array, i.e.,
B

_{i}= A_{i}+ max(B_{0}, B_{1}, ..., B_{i-1}) where A is the given array, B is the new array, and i is the current indexBelow is the implementation of the above approach:

## C++

`// C++ program to find Array formed by adding`

`// each element of given array with largest`

`// element in new array to its left`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// Function to find array B from array`

`// A such that Ai = Bi – max(B0…Bi-1)`

`void`

`find_array(`

`int`

`a[],`

`int`

`n)`

`{`

`// Initialising as 0 as first`

`// element will remain same`

`int`

`x = 0;`

`for`

`(`

`int`

`i = 0; i < n; i++) {`

`// restoring values of B`

`a[i] += x;`

`cout << a[i] <<`

`' '`

`;`

`// Find max value`

`x = max(x, a[i]);`

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

`int`

`a[] = {40, 12, 62};`

`int`

`n =`

`sizeof`

`(a) /`

`sizeof`

`(a[0]);`

`// Function call`

`find_array(a, n);`

`return`

`0;`

`}`

*chevron_right**filter_none*## Java

`// Java program to find Array formed by adding`

`// each element of given array with largest`

`// element in new array to its left`

`public`

`class`

`GFG`

`{`

`// Function to find array B from array`

`// A such that Ai = Bi – max(B0…Bi-1)`

`static`

`void`

`find_array(`

`int`

`[]a,`

`int`

`n)`

`{`

`// Initialising as 0 as first`

`// element will remain same`

`int`

`x =`

`0`

`;`

`for`

`(`

`int`

`i =`

`0`

`; i < n; i++) {`

`// restoring values of B`

`a[i] += x;`

`System.out.print(a[i] +`

`" "`

`);`

`// Find max value`

`x = Math.max(x, a[i]);`

`}`

`}`

`// Driver code`

`public`

`static`

`void`

`main(String []args)`

`{`

`int`

`[]a = {`

`40`

`,`

`12`

`,`

`62`

`};`

`int`

`n = a.length ;`

`// Function call`

`find_array(a, n);`

`}`

`}`

`// This code is contributed by Yash_R`

*chevron_right**filter_none*## Python3

`# Python3 program to find Array formed by adding`

`# each element of given array with largest`

`# element in new array to its left`

`# Function to find array B from array`

`# A such that Ai = Bi – max(B0…Bi-1)`

`def`

`find_array(a, n) :`

`# Initialising as 0 as first`

`# element will remain same`

`x`

`=`

`0`

`;`

`for`

`i`

`in`

`range`

`(n) :`

`# restoring values of B`

`a[i]`

`+`

`=`

`x;`

`print`

`(a[i],end`

`=`

`' '`

`);`

`# Find max value`

`x`

`=`

`max`

`(x, a[i]);`

`# Driver code`

`if`

`__name__`

`=`

`=`

`"__main__"`

`:`

`a`

`=`

`[`

`40`

`,`

`12`

`,`

`62`

`];`

`n`

`=`

`len`

`(a);`

`# Function call`

`find_array(a, n);`

`# This code is contributed by Yash_R`

*chevron_right**filter_none*## C#

`// C# program to find Array formed by adding`

`// each element of given array with largest`

`// element in new array to its left`

`using`

`System;`

`class`

`gfg`

`{`

`// Function to find array B from array`

`// A such that Ai = Bi – max(B0…Bi-1)`

`static`

`void`

`find_array(`

`int`

`[]a,`

`int`

`n)`

`{`

`// Initialising as 0 as first`

`// element will remain same`

`int`

`x = 0;`

`for`

`(`

`int`

`i = 0; i < n; i++) {`

`// restoring values of B`

`a[i] += x;`

`Console.Write(a[i] +`

`" "`

`);`

`// Find max value`

`x = Math.Max(x, a[i]);`

`}`

`}`

`// Driver code`

`public`

`static`

`void`

`Main(`

`string`

`[]args)`

`{`

`int`

`[]a = {40, 12, 62};`

`int`

`n = a.Length ;`

`// Function call`

`find_array(a, n);`

`}`

`}`

`// This code is contributed by Yash_R`

*chevron_right**filter_none***Output:**40 52 114

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:

- Array formed from difference of each element from the largest element in the given array
- Generate Array whose difference of each element with its left yields the given Array
- Maximize number of groups formed with size not smaller than its largest element
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Largest element smaller than current element on left for every element in Array
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Maximize the number of indices such that element is greater than element to its left
- Modify given array by reducing each element by its next smaller element
- Replace elements with absolute difference of smallest element on left and largest element on right
- Count distinct elements after adding each element of First Array with Second Array
- Find the difference of count of equal elements on the right and the left for each element
- Find the largest number that can be formed with the given digits
- Generate array having differences between count of occurrences of every array element on its left and right
- Count array elements having at least one smaller element on its left and right side
- 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
- Largest number in given Array formed by repeatedly combining two same elements
- Sum of product of each element with each element after it

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.