Find Array formed by adding each element of given array with largest element in new array to its left
Last Updated :
05 Nov, 2021
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 A0: No element if present at its left. Hence the element at 0th index of the resultant array = 5
Element A1: Largest element to its left in the resultant array = 5. Hence the element at 1th index of the resultant array = 1 + 5 = 6
Element A2: Largest element to its left in the resultant array = 6. Hence the element at 2nd index of the resultant array = 6 + 6 = 12
Element A3: Largest element to its left in the resultant array = 12. Hence the element at 3rd index of the result array = -3 + 12 = 9
Element A4: 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:
In order to find such an 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.
B0 = A0
where A is the given array and
B is the new array
2.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.,
Bi = Ai + max(B0, B1, ..., Bi-1)
where A is the given array,
B is the new array, and
i is the current index
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_array( int a[], int n)
{
int x = 0;
for ( int i = 0; i < n; i++) {
a[i] += x;
cout << a[i] << ' ' ;
x = max(x, a[i]);
}
}
int main()
{
int a[] = {40, 12, 62};
int n = sizeof (a) / sizeof (a[0]);
find_array(a, n);
return 0;
}
|
Java
public class GFG
{
static void find_array( int []a, int n)
{
int x = 0 ;
for ( int i = 0 ; i < n; i++) {
a[i] += x;
System.out.print(a[i] + " " );
x = Math.max(x, a[i]);
}
}
public static void main(String []args)
{
int []a = { 40 , 12 , 62 };
int n = a.length ;
find_array(a, n);
}
}
|
Python3
def find_array(a, n) :
x = 0 ;
for i in range (n) :
a[i] + = x;
print (a[i],end = ' ' );
x = max (x, a[i]);
if __name__ = = "__main__" :
a = [ 40 , 12 , 62 ];
n = len (a);
find_array(a, n);
|
C#
using System;
class gfg
{
static void find_array( int []a, int n)
{
int x = 0;
for ( int i = 0; i < n; i++) {
a[i] += x;
Console.Write(a[i] + " " );
x = Math.Max(x, a[i]);
}
}
public static void Main( string []args)
{
int []a = {40, 12, 62};
int n = a.Length ;
find_array(a, n);
}
}
|
Javascript
<script>
function find_array(a, n)
{
let x = 0;
for (let i = 0; i < n; i++) {
a[i] += x;
document.write(a[i] + ' ' );
x = Math.max(x, a[i]);
}
}
let a = [40, 12, 62];
let n = a.length;
find_array(a, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...