Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Sum of consecutive two elements in a array
Next article icon

Modulus of all pairwise consecutive elements in an Array

Last Updated : 09 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of N      elements. The task is to print the modulus of all of the pairwise consecutive elements. That is for all pair of consecutive elements say ((a[i], a[i+1])), print (a[i] % a[i+1]).

Note: Consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2.

Examples: 

Input: arr[] = {8, 5, 4, 3, 15, 20} Output: 3 1 1 3 15   Input: arr[] = {5, 10, 15, 20} Output: 5 10 15 

Approach: The solution is to traverse the array and calculate and print the modulus of every pair (arr[i], arr[i+1]).

Below is the implementation of the above approach: 

C++
// C++ program to print the modulus // of the consecutive elements #include <iostream> using namespace std;  // Function to print pairwise modulus // of consecutive elements void pairwiseModulus(int arr[], int n) {     for (int i = 0; i < n - 1; i++) {          // Modulus of consecutive numbers         cout << (arr[i] % arr[i + 1]) << " ";     } }  // Driver Code int main() {     int arr[] = { 8, 5, 4, 3, 15, 20 };     int n = sizeof(arr) / sizeof(arr[0]);      pairwiseModulus(arr, n);      return 0; } 
C
// C program to print the modulus // of the consecutive elements #include <stdio.h>  // Function to print pairwise modulus // of consecutive elements void pairwiseModulus(int arr[], int n) {     for (int i = 0; i < n - 1; i++) {          // Modulus of consecutive numbers         printf("%d ",arr[i] % arr[i + 1]);     } }  // Driver Code int main() {     int arr[] = { 8, 5, 4, 3, 15, 20 };     int n = sizeof(arr) / sizeof(arr[0]);      pairwiseModulus(arr, n);      return 0; }  // This code is contributed by kothavvsaakash. 
Java
// Java program to print the modulus // of the consecutive elements import java.util.*;  class Geeks {      // Function to print pairwise modulus // of consecutive elements static void pairwiseModulus(int arr[], int n) {     for (int i = 0; i < n - 1; i++) {          // Modulus of consecutive numbers         System.out.println((arr[i] % arr[i + 1]));     } }  // Driver Code public static void main(String args[]) {     int arr[] = { 8, 5, 4, 3, 15, 20 };     int n = arr.length;      pairwiseModulus(arr, n); } }  // This code is contributed by ankita_saini 
Python3
# Python 3 program to print the modulus # of the consecutive elements  # Function to print pairwise modulus # of consecutive elements def pairwiseModulus(arr, n):     for i in range(0, n - 1, 1):                  # Modulus of consecutive numbers         print((arr[i] % arr[i + 1]),                           end = " ")      # Driver Code if __name__ == '__main__':     arr = [8, 5, 4, 3, 15, 20]      n = len(arr)     pairwiseModulus(arr, n)  # This code is contributed  # by Surendra_Gangwar 
C#
// C# program to print the modulus // of the consecutive elements using System;  class Geeks {      // Function to print pairwise modulus // of consecutive elements static void pairwiseModulus(int[] arr, int n) {     for (int i = 0; i < n - 1; i++) {          // Modulus of consecutive numbers         Console.WriteLine((arr[i] % arr[i + 1]));     } }  // Driver Code public static void Main(String []args) {     int[] arr = {8, 5, 4, 3, 15, 20};     int n = arr.Length;      pairwiseModulus(arr, n); } }  // This code is contributed by ankita_saini 
PHP
<?php //PHP program to print the modulus  // of the consecutive elements   // Function to print pairwise modulus  // of consecutive elements  function pairwiseModulus( $arr, $n)  {      for ($i = 0; $i < $n - 1; $i++) {           // Modulus of consecutive numbers          echo  ($arr[$i] % $arr[$i + 1]), " ";      }  }   // Driver Code      $arr = array( 8, 5, 4, 3, 15, 20 );      $n = sizeof($arr) / sizeof($arr[0]);       pairwiseModulus($arr, $n);    // This code is contributed by ajit ?> 
JavaScript
<script> // javascript program to print the modulus // of the consecutive elementsclass Geeks {      // Function to print pairwise modulus     // of consecutive elements     function pairwiseModulus(arr , n) {         for (i = 0; i < n - 1; i++) {              // Modulus of consecutive numbers             document.write((arr[i] % arr[i + 1]) + " ");         }     }      // Driver Code              var arr = [ 8, 5, 4, 3, 15, 20 ];         var n = arr.length;          pairwiseModulus(arr, n);  // This code contributed by gauravrajput1  </script> 

Output
3 1 1 3 15 

Complexity Analysis:

  • Time complexity: O(n)
  •  Auxiliary Space: O(1)

Next Article
Sum of consecutive two elements in a array

V

VishalBachchas
Improve
Article Tags :
  • Technical Scripter
  • Data Structures
  • DSA
  • Arrays
  • Technical Scripter 2018
  • school-programming
Practice Tags :
  • Arrays
  • Data Structures

Similar Reads

  • 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 [Tex]i [/Tex]ranging from 0 to N-2 Examples: Input : arr[] = {8, 5, 4, 3, 15, 20} Output : 40, 20, 12, 45
    4 min read
  • Absolute Difference of all pairwise consecutive elements in an array
    Given an array of integers of N elements. The task is to print the absolute difference of all of the pairwise consecutive elements. Pairwise consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2 Examples: Input: arr[] = {8, 5, 4, 3, 15, 20}Output: 3, 1, 1, 12, 5I
    4 min read
  • Sum of consecutive two elements in a array
    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++ Code // C
    3 min read
  • Replacing an element makes array elements consecutive
    Given an array of positive distinct integers. We need to find the only element whose replacement with any other value makes array elements distinct consecutive. If it is not possible to make array elements consecutive, return -1. Examples : Input : arr[] = {45, 42, 46, 48, 47} Output : 42 Explanatio
    8 min read
  • Find missing element in a sorted array of consecutive numbers
    Given an array arr[] of n distinct integers. Elements are placed sequentially in ascending order with one element missing. The task is to find the missing element.Examples: Input: arr[] = {1, 2, 4, 5, 6, 7, 8, 9} Output: 3Input: arr[] = {-4, -3, -1, 0, 1, 2} Output: -2Input: arr[] = {1, 2, 3, 4} Out
    7 min read
  • Check if stack elements are pairwise consecutive
    Given a stack of integers, write a function pairWiseConsecutive() that checks whether numbers in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should ret
    10 min read
  • Check if Queue Elements are pairwise consecutive
    Given a Queue of integers. The task is to check if consecutive elements in the queue are pairwise consecutive. Examples: Input : 1 2 5 6 9 10 Output : Yes Input : 2 3 9 11 8 7 Output : No Approach: Using two stacks : Transfer all elements of the queue to one auxiliary stack aux.Now, transfer the ele
    7 min read
  • Count number of pairs not divisible by any element in the array
    Given an array arr[] of size N, the task is to count the number of pairs of integers (i, j) for which there does not exist an integer k such that arr[i] is divisible by arr[k] and arr[j] is divisible by arr[k], such that k can be any index between [0, N - 1]. Examples: Input: N = 4, arr[] = {2, 4, 5
    5 min read
  • Check if an array can be split into subsets of K consecutive elements
    Given an array arr[] and integer K, the task is to split the array into subsets of size K, such that each subset consists of K consecutive elements. Examples: Input: arr[] = {1, 2, 3, 6, 2, 3, 4, 7, 8}, K = 3 Output: true Explanation: The given array of length 9 can be split into 3 subsets {1, 2, 3}
    5 min read
  • Find elements of array using XOR of consecutive elements
    Given an array arr[] in which XOR of every 2 consecutive elements of the original array is given i.e if the total number of elements in the original array is [Tex]n [/Tex]then the size of this XOR array would be n-1. The first element in the original array is also given. The task is to find out the
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences