

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
In this problem, we are given an array, are of n elements. Our task is to create a program to find the maximum modulo of all pairs of array where arr[i] >= arr[j].
Here, we have to find the maximum value of arr[i] % arr[j] where arr[i] >= arr[j].
Let’s take an example to understand the problem,
Input − arr[] = {3, 5, 9}
Output − 4
Explanation −
All possible Pairs arr[i] and arr[j], 5, 3 => 5%3 = 2 9, 3 => 9%3 = 0 9, 5 => 9%5 = 4
To solve this problem, a simple and direct approach will be run two nested loops and find the modulo for every possible pair. Then, find the maximum of them. But, this solution will not be efficient as its complexity will be of order O(n^2).
An effective approach will be applied on sorted array. The algorithm will we applied in the following manner −
For every element arr[j] in the array, we will find values which are multiples of arr[j] say x till we find value greater than the largest element of the array. Then, we will find all value of an array such that arr[i] < x. Find arr[i] % arr[j], and store the maximum of modulo value in the maxModulo variable after each operation.
Let’s solve an example using this solution which will show the functioning of the algorithm −
arr = {3, 5, 9} arr[j] = 3 for j = 0, x = {6, 9} For x = 6, arr[i] = 5, arr[i]%arr[j] = 6%5 = 2, maxModulo = 2 For x = 9, arr[i] = 9, arr[i]%arr[j] = 9%3 = 0, maxModulo = 2 arr[j] = 5 for j = 1, x = {10} For x = 10, arr[i] = 9, arr[i]%arr[j] = 9%5 = 4, maxModulo =4
Example
Program to find the maximum modulo of all pairs of array where arr[i] >= arr[j] −
#include <bits/stdc++.h> using namespace std; int maxModulo(int arr[], int n) { int maxModulo = 0; sort(arr, arr + n); for (int j = n - 2; j >= 0; --j) { if (maxModulo >= arr[j]) break; if (arr[j] == arr[j + 1]) continue; for (int k = 2 * arr[j]; k <= arr[n - 1] + arr[j]; k += arr[j]) { int i = lower_bound(arr, arr + n, k) - arr; maxModulo = max(maxModulo, arr[i - 1] % arr[j]); } } return maxModulo; } int main() { int arr[] = {3, 5, 9}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The maximum modulo of all pairs is "<<maxModulo(arr, n); }
Output
The maximum modulo of all pairs is 4
- Related Questions & Answers
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1] - when elements are from 1 to n in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Maximum sum of i * arr[i] among all rotations of a given array in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Maximize the sum of arr[i]*i in C++