📌  相关文章
📜  用接下来的两个连续元素之和替换数组元素

📅  最后修改于: 2021-06-27 00:23:10             🧑  作者: Mango

给定大小为n的数组arr [] ,任务是以循环方式用接下来的两个连续元素之和替换数组的每个元素,即arr [0] = arr [1] + arr [2]arr [ 1] = arr [2] + arr [3] ,… arr [n – 1] = arr [0] + arr [1]
例子:

方法:将数组的第一个和第二个元素存储在变量firstsecond中。现在,对于除数组的最后一个元素和倒数第二个元素之外的每个元素,更新arr [i] = arr [i + 1] + arr [i + 2] 。然后将最后一个和倒数第二个元素更新为arr [n – 2] = arr [n – 1] + firstarr [n – 1] = first + second
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print the
// contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to update every element of
// the array as the sum of next two elements
void updateArr(int arr[], int n)
{
 
    // Invalid array
    if (n < 3)
        return;
 
    // First and second elements of the array
    int first = arr[0];
    int second = arr[1];
 
    // Update every element as required
    // except the last and the
    // second last element
    for (int i = 0; i < n - 2; i++)
        arr[i] = arr[i + 1] + arr[i + 2];
 
    // Update the last and the second
    // last element of the array
    arr[n - 2] = arr[n - 1] + first;
    arr[n - 1] = first + second;
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = { 3, 4, 2, 1, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    updateArr(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Utility function to print the
    // contents of an array
    static void printArr(int[] arr, int n)
    {
        for (int i = 0; i < n; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Function to update every element of
    // the array as the sum of next two elements
    static void updateArr(int[] arr, int n)
    {
 
        // Invalid array
        if (n < 3)
        {
            return;
        }
 
        // First and second elements of the array
        int first = arr[0];
        int second = arr[1];
 
        // Update every element as required
        // except the last and the
        // second last element
        for (int i = 0; i < n - 2; i++)
        {
            arr[i] = arr[i + 1] + arr[i + 2];
        }
 
        // Update the last and the second
        // last element of the array
        arr[n - 2] = arr[n - 1] + first;
        arr[n - 1] = first + second;
 
        // Print the updated array
        printArr(arr, n);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = {3, 4, 2, 1, 6};
        int n = arr.length;
        updateArr(arr, n);
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
 
# Utility function to print the
# contents of an array
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end = " ")
 
# Function to update every element of
# the array as the sum of next two elements
def updateArr(arr, n):
     
    # Invalid array
    if (n < 3):
        return
 
    # First and second elements of the array
    first = arr[0]
    second = arr[1]
 
    # Update every element as required
    # except the last and the
    # second last element
    for i in range(n - 2):
        arr[i] = arr[i + 1] + arr[i + 2]
 
    # Update the last and the second
    # last element of the array
    arr[n - 2] = arr[n - 1] + first
    arr[n - 1] = first + second
 
    # Print the updated array
    printArr(arr, n)
 
# Driver code
if __name__ == '__main__':
    arr = [3, 4, 2, 1, 6]
    n = len(arr)
    updateArr(arr, n)
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Utility function to print the
// contents of an array
static void printArr(int []arr, int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Function to update every element of
// the array as the sum of next two elements
static void updateArr(int []arr, int n)
{
 
    // Invalid array
    if (n < 3)
        return;
 
    // First and second elements of the array
    int first = arr[0];
    int second = arr[1];
 
    // Update every element as required
    // except the last and the
    // second last element
    for (int i = 0; i < n - 2; i++)
        arr[i] = arr[i + 1] + arr[i + 2];
 
    // Update the last and the second
    // last element of the array
    arr[n - 2] = arr[n - 1] + first;
    arr[n - 1] = first + second;
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
public static void Main()
{
    int []arr = { 3, 4, 2, 1, 6 };
    int n = arr.Length;
    updateArr(arr, n);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
6 3 7 9 7

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。