Friday, September 5, 2025
HomeData Modelling & AIReplace array elements by sum of next two consecutive elements

Replace array elements by sum of next two consecutive elements

Given an array arr[] of size n, The task is to replace every element of the array by the sum of next two consecutive elements in a circular manner i.e. arr[0] = arr[1] + arr[2], arr[1] = arr[2] + arr[3], … arr[n – 1] = arr[0] + arr[1].

Examples: 

Input: arr[] = {3, 4, 2, 1, 6} 
Output: 6 3 7 9 7
Input: arr[] = {5, 2, 1, 3, 8} 
Output: 3 4 11 13 7 

Approach: 

Store the first and second elements of the array in variables first and second. Now for every element except the last and the second last element of the array, update arr[i] = arr[i + 1] + arr[i + 2]. Then update the last and the second last element as arr[n – 2] = arr[n – 1] + first and arr[n – 1] = first + second.

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
 
#include <bits/stdc++.h>
 
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




<?php
// PHP implementation of the approach
 
// Utility function to print the
// contents of an array
function printArr($arr, $n)
{
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i], " ";
}
 
// Function to update every element
// of the array as the sum of next
// two elements
function 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 = 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
$arr = array (3, 4, 2, 1, 6 );
$n = sizeof($arr);
updateArr($arr, $n);
 
// This code is contributed by ajit.
?>


Javascript




<script>
// Javascript implementation of the approach
 
// Utility function to print the
// contents of an array
function printArr( arr, n)
{
    for (let i = 0; i < n; i++)
        document.write(arr[i] + " ");
}
 
// Function to update every element of
// the array as the sum of next two elements
function updateArr( arr, n)
{
 
    // Invalid array
    if (n < 3)
        return;
 
    // First and second elements of the array
    let first = arr[0];
    let second = arr[1];
 
    // Update every element as required
    // except the last and the
    // second last element
    for (let 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
    let arr = [ 3, 4, 2, 1, 6 ];
    let n = arr.length;
    updateArr(arr, n);
 
// This code is contributed by jana_sayantan.
     
</script>


Output: 

6 3 7 9 7

 

Time complexity: O(n) where n is length of given array

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS