📌  相关文章
📜  Find Array 通过将给定数组的每个元素与新数组中的最大元素添加到其左侧而形成

📅  最后修改于: 2021-09-07 02:14:31             🧑  作者: Mango

给定一个大小为N的数组A ,任务是找到通过将给定数组的每个元素与新数组中的最大元素添加到其左侧而形成的结果数组。
例子:

方法:
为了找到这样的数组,新数组的每个元素都会对[0, N-1]范围内的每个索引进行一一计算,遵循以下规则:

  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