📌  相关文章
📜  生成Array,其每个元素与左边的差产生给定的Array

📅  最后修改于: 2021-05-18 00:14:26             🧑  作者: Mango

给定一个整数N和一个(N – 1)个整数的arr1 [] ,任务是找到范围[1,N]中N个整数的序列arr2 [] ,使得arr1 [i] = arr2 [i + 1] – arr2 [i] 。序列arr1 []中的整数在[-N,N]范围内。
例子:

方法:
请按照以下步骤解决问题:

  1. 假设arr2 []的第一个元素为X。
  2. 下一个元素将是X + arr1 [0]
  3. 可以表示arr2 []的其余元素,即wrt X。
  4. 已知序列arr2 []可以包含[1,N]范围内的整数。因此,最小可能的整数将是1
  5. 可以根据X找出arr2 []的最小数量,并将其等于1来找到X的值。
  6. 最后,使用X的值,可以找出arr2 []中的所有其他数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sequence
void find_seq(int arr[],
              int m, int n) {
    int b[n];
    int x = 0;
 
    // initializing 1st element
    b[0] = x;
 
    // Creating sequence in
    // terms of x
    for (int i = 0;
         i < n - 1; i++) {
 
        b[i + 1] = x +
                   arr[i] + b[i];
    }
 
    int mn = n;
 
    // Finding min element
    for (int i = 0; i < n; i++)
    {
        mn = min(mn, b[i]);
    }
 
    // Finding value of x
    x = 1 - mn;
 
    // Creating original sequence
    for (int i = 0; i < n; i++) {
        b[i] += x;
    }
 
    // Output original sequence
    for (int i = 0; i < n; i++) {
        cout << b[i] << " ";
    }
    cout << endl;
}
 
// Driver function
int main()
{
    int N = 3;
    int arr[] = { -2, 1 };
 
    int M = sizeof(arr) / sizeof(int);
    find_seq(arr, M, N);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
     
// Function to find the sequence
static void find_seq(int arr[], int m,
                                int n)
{
    int b[] = new int[n];
    int x = 0;
 
    // Initializing 1st element
    b[0] = x;
 
    // Creating sequence in
    // terms of x
    for(int i = 0; i < n - 1; i++)
    {
       b[i + 1] = x + arr[i] + b[i];
    }
 
    int mn = n;
 
    // Finding min element
    for(int i = 0; i < n; i++)
    {
       mn = Math.min(mn, b[i]);
    }
 
    // Finding value of x
    x = 1 - mn;
 
    // Creating original sequence
    for(int i = 0; i < n; i++)
    {
       b[i] += x;
    }
 
    // Output original sequence
    for(int i = 0; i < n; i++)
    {
        System.out.print(b[i] + " ");
    }
    System.out.println();
}
     
// Driver code
public static void main (String[] args)
{
    int N = 3;
    int arr[] = new int[]{ -2, 1 };
    int M = arr.length;
     
    find_seq(arr, M, N);
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach
 
# Function to find the sequence
def find_seq(arr, m, n):
     
    b = []
    x = 0
     
    # Initializing 1st element
    b.append(x)
     
    # Creating sequence in
    # terms of x
    for i in range(n - 1):
        b.append(x + arr[i] + b[i])
         
    mn = n
     
    # Finding min element
    for i in range(n):
        mn = min(mn, b[i])
         
    # Finding value of x
    x = 1 - mn
         
    # Creating original sequence
    for i in range(n):
        b[i] += x
         
    # Output original sequence
    for i in range(n):
        print(b[i], end = ' ')
     
    print()
     
# Driver code
if __name__=='__main__':
     
    N = 3
    arr = [ -2, 1 ]
    M = len(arr)
     
    find_seq(arr, M, N)
 
# This code is contributed by rutvik_56


C#
// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to find the sequence
static void find_seq(int []arr, int m,
                                int n)
{
    int []b = new int[n];
    int x = 0;
 
    // Initializing 1st element
    b[0] = x;
 
    // Creating sequence in
    // terms of x
    for(int i = 0; i < n - 1; i++)
    {
       b[i + 1] = x + arr[i] + b[i];
    }
 
    int mn = n;
 
    // Finding min element
    for(int i = 0; i < n; i++)
    {
       mn = Math.Min(mn, b[i]);
    }
 
    // Finding value of x
    x = 1 - mn;
 
    // Creating original sequence
    for(int i = 0; i < n; i++)
    {
       b[i] += x;
    }
 
    // Output original sequence
    for(int i = 0; i < n; i++)
    {
       Console.Write(b[i] + " ");
    }
    Console.WriteLine();
}
     
// Driver code
public static void Main(String[] args)
{
    int N = 3;
    int []arr = new int[]{ -2, 1 };
    int M = arr.Length;
     
    find_seq(arr, M, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3 1 2

时间复杂度: O(N)
辅助空间: O(1)