给定一个大小为N的数组A ,任务是找到通过将给定数组的每个元素与新数组中的最大元素添加到其左侧而形成的结果数组。
例子:
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}
方法:
为了找到这样的数组,新数组的每个元素都会对[0, N-1]范围内的每个索引进行一一计算,遵循以下规则:
- 对于起始索引,即 0,新数组将为空。因此不会有任何最大的元素。在这种情况下,给定数组中第 0 个索引处的元素被向下复制到新数组中,即
B0 = A0
where A is the given array and
B is the new array
2.对于 [1, N-1] 范围内的所有其他索引,首先在新数组中找到其左侧的最大元素,然后将其添加到原始数组的相应元素中,即,
Bi = Ai + max(B0, B1, ..., Bi-1)
where A is the given array,
B is the new array, and
i is the current index
下面是上述方法的实现:
C++
// C++ program to find Array formed by adding
// each element of given array with largest
// element in new array to its left
#include
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;
}
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
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
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
Javascript
40 52 114
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live