Sum of consecutive two elements in a array
Last Updated :
20 Feb, 2023
Given an array print sum of the pairwise consecutive elements.
Examples:
Input : 8, 5, 4, 3, 15, 20
Output : 13, 9, 7, 18, 35
Input : 5, 10, 15, 20
Output : 15, 25, 35
The solution is to traverse the array and saving the sum of consecutive numbers in the variable sum.
Implementation:
C++
// C++ program to print the
// sum of the consecutive elements.
#include <stdio.h>
#include <stdlib.h>
// Function to print pairwise sum
void pairwiseSum(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n - 1; i++)
{
// adding the alternate numbers
sum = arr[i] + arr[i + 1];
printf(" %d ", sum);
}
}
// Driver function to test function
int main()
{
int arr[] = {4, 10, 15, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
pairwiseSum(arr, n);
return 0;
}
Java
// Java program to print the
// sum of the consecutive elements.
class Arraysum {
// Function to print Alternatesum
static void pairwiseSum(int arr[], int n)
{
int sum = 0;
for (int i = 0; i + 1 < n; i++)
{
// adding the alternate numbers
sum = arr[i] + arr[i + 1];
System.out.print(sum + " ");
}
}
/*driver function to test function*/
public static void main(String[] args)
{
int arr[] = {4, 10, 15, 5, 6};
int n = arr.length;
pairwiseSum(arr, n);
}
}
Python3
# Python3 program to print the
# sum of the consecutive elements.
# Function to print alternate sum
def pairwiseSum(lst, n):
sum = 0;
for i in range(len(lst)-1):
# adding the alternate numbers
sum = lst[i] + lst[i + 1]
print (sum, end = " ")
# driver function to test function
arr =[4, 10, 15, 5, 6]
size = len(arr)
pairwiseSum(arr, size)
C#
// C# program to print the
// sum of the consecutive elements.
using System;
class Arraysum {
// Function to print Alternatesum
static void pairwiseSum(int []arr, int n)
{
int sum = 0;
for (int i = 0; i + 1 < n; i++)
{
// adding the alternate numbers
sum = arr[i] + arr[i + 1];
Console.Write(sum + " ");
}
}
// Driver function
public static void Main()
{
int []arr = {4, 10, 15, 5, 6};
int n = arr.Length;
pairwiseSum(arr, n);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to print the
// sum of the consecutive elements.
// Function to print pairwise sum
function pairwiseSum($arr, $n)
{
$sum = 0;
for ($i = 0; $i < $n - 1; $i++)
{
// adding the alternate numbers
$sum = $arr[$i] + $arr[$i + 1];
echo $sum," ";
}
}
// Driver Code
$arr = array (4, 10, 15, 5, 6);
$n = sizeof($arr) ;
pairwiseSum($arr, $n);
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to print the
// sum of the consecutive elements.
// Function to print Alternatesum
function pairwiseSum(arr, n)
{
let sum = 0;
for(let i = 0; i + 1 < n; i++)
{
// Adding the alternate numbers
sum = arr[i] + arr[i + 1];
document.write(sum + " ");
}
}
// Driver code
let arr = [ 4, 10, 15, 5, 6 ];
let n = arr.length;
pairwiseSum(arr, n);
// This code is contributed by divyesh072019
</script>
Time Complexity: O(n) where n is no of elements in given array
Auxiliary Space: O(1) because it is using constant space for variables
Similar Reads
Sum of all elements in an array between zeros Given an array arr[] of N integers, the task is to find the sum of all elements between two zeros in the given array. If possible, then print all the sum, else print "-1".Note: There is no continuous zero in the given array. Examples: Input: arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 } Output:
7 min read
Sum of minimum difference between consecutive elements of an array Given an array of pairs where each pair represents a range, the task is to find the sum of the minimum difference between the consecutive elements of an array where the array is filled in the below manner: Each element of an array lies in the range given at its corresponding index in the range array
10 min read
Construct sum-array with sum of elements in given range You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {
7 min read
Express a number as sum of consecutive numbers Given a number N, write a function to express N as sum of two or more consecutive positive numbers. If there is no solution, output -1. If there are multiple solution, then print one of them.Examples: Input : N = 10 Output : 4 + 3 + 2 + 1 Input : N = 8 Output : -1 Input : N = 24 Output : 9 + 8 + 7 R
5 min read
Find all ranges of consecutive numbers from Array Given a sorted array arr[] consisting of N integers without any duplicates, the task is to find the ranges of consecutive numbers from that array.Examples: Input: arr[] = {1, 2, 3, 6, 7} Output: 1->3, 6->7 Explanation: There are two ranges of consecutive number from that array. Range 1 = 1 -
10 min read