Product of all pairwise consecutive elements in an Array
Given an array of integers of N elements. The task is to print the product of all of the pairwise consecutive elements.
Pairwise consecutive pairs of an array of size N are (a[i], a[i+1]) for all
ranging from 0 to N-2
Examples:
Input : arr[] = {8, 5, 4, 3, 15, 20}
Output : 40, 20, 12, 45, 300
Input : arr[] = {5, 10, 15, 20}
Output : 50, 150, 300
The solution is to traverse the array and calculate and print the product of every pair (arr[i], arr[i+1]).
Below is the implementation of this approach:
C++
// C++ program to print the // product of the consecutive elements. #include <iostream> using namespace std; // Function to print pairwise // consecutive product void pairwiseProduct(int arr[], int n) { int prod = 1; for (int i = 0; i < n - 1; i++) { // multiply the alternate numbers prod = arr[i] * arr[i + 1]; printf(" %d ", prod); } } // Driver Code int main() { int arr[] = { 4, 10, 15, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); pairwiseProduct(arr, n); return 0; } |
Java
// Java program to print the product // of the consecutive elements. import java .io.*; class GFG { // Function to print pairwise // consecutive product static void pairwiseProduct(int[] arr, int n) { int prod = 1; for (int i = 0; i < n - 1; i++) { // multiply the alternate numbers prod = arr[i] * arr[i + 1]; System.out.print(prod + " "); } } // Driver Code public static void main(String[] args) { int[] arr = { 4, 10, 15, 5, 6 }; int n = arr.length; pairwiseProduct(arr, n); } } // This code is contributed // by anuj_67.. |
Python 3
# Python 3 program to print the # product of the consecutive elements. # Function to print pairwise # consecutive product def pairwiseProduct( arr, n): prod = 1 for i in range(n - 1) : # multiply the alternate numbers prod = arr[i] * arr[i + 1] print(prod, end = " ") # Driver Code if __name__=="__main__": arr = [ 4, 10, 15, 5, 6 ] n = len(arr) pairwiseProduct(arr, n) # This code is contributed # by ChitraNayal |
C#
// C# program to print the product // of the consecutive elements. using System; class GFG { // Function to print pairwise // consecutive product static void pairwiseProduct(int[] arr, int n) { int prod = 1; for (int i = 0; i < n - 1; i++) { // multiply the alternate numbers prod = arr[i] * arr[i + 1]; Console.Write(prod + " "); } } // Driver Code public static void Main() { int[] arr = { 4, 10, 15, 5, 6 }; int n = arr.Length; pairwiseProduct(arr, n); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP program to print the // product of the consecutive elements. // Function to print pairwise // consecutive product function pairwiseProduct(&$arr, $n) { $prod = 1; for ($i = 0; $i < $n - 1; $i++) { // multiply the alternate numbers $prod = $arr[$i] * $arr[$i + 1]; echo ($prod); echo (" "); } } // Driver Code $arr = array(4, 10, 15, 5, 6 ); $n = sizeof($arr); pairwiseProduct($arr, $n); // This code is contributed // by Shivi_Aggarwal ?> |
40 150 75 30
Time complexity : O(n)
Recommended Posts:
- Modulus of all pairwise consecutive elements in an Array
- Absolute Difference of all pairwise consecutive elements in an array
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Rearrange an array to minimize sum of product of consecutive pair elements
- Maximum sum of pairwise product in an array with negative allowed
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Replace array elements by sum of next two consecutive elements
- Find elements of array using XOR of consecutive elements
- Construct an array from GCDs of consecutive elements in given array
- Sum of consecutive two elements in a array
- Find minimum value to assign all array elements so that array product becomes greater
- Replacing an element makes array elements consecutive
- Generate original array from difference between every two consecutive elements
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Check if array elements are consecutive | Added Method 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



