- 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

In this tutorial, we are going to write a program that finds a number such that its modulus with each array element is same. Let's see an example.

**Input** − arr = {10, 4, 2}

**Output** − 1 2

If there are two numbers **x, y** and **x > y**, assume that **x - y = d.**

Then the **x = y + d**.

Let's say we have a number **k** such that x%k = y%k. Apply modulo **k** for the above equation and find the value **d**.

x%k = (y+d)%k y%k = y%k +d%k d%k = 0

From the above calculation, if the number **k** is the divisor of the difference between the **x** and **y**. Then it will be a divisor of the numbers **x** and **y**.

Let's apply the same concept to the array of elements. And find the k value. See the steps to solve the problem.

Initialize the array with numbers

Here, the

**d**will be the difference between the max and min values of the array elements.Sort the values of the array using sort method.

Find the difference between the last and first numbers.

If the difference is zero, then all the numbers are same. Then the result of module with any number gives the same result.

Else find the divisors of the number

**d**. And store them.Iterate over all the divisors and find number whose modulo with all the array elements are same.

Let's see the code.

#include <bits/stdc++.h> using namespace std; void findNumbers(int arr[], int n) { sort(arr, arr + n); int d = arr[n - 1] - arr[0]; // check whether all elements are same or not if (d == 0) { cout << "Infinite number of k's"; return; } // finding the divisors of d vector <int> v; for (int i = 1; i * i <= d; i++) { if (d % i == 0) { v.push_back(i); if (i != d / i) { v.push_back(d / i); } } } // findind the k's for (int i = 0; i < v.size(); i++) { int temp = arr[0] % v[i]; int j; for (j = 1; j < n; j++) { if (arr[j] % v[i] != temp) { break; } } if (j == n) cout << v[i] << " "; } cout << endl; } int main() { int arr[] = {10, 4, 2}; findNumbers(arr, 3); return 0; }

If you run the above code, then you will get the following result.

1 2

If you have any queries in the tutorial, mention them in the comment section.

- Related Questions & Answers
- C++ program to find ‘k’ such that its modulus with each array element is same
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Place K-knights such that they do not attack each other in C++
- K-th digit in ‘a’ raised to power ‘b’ in C++
- Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Find unique pairs such that each element is less than or equal to N in C++
- Place k elements such that minimum distance is maximized in C++
- Count Derangements (Permutation such that no element appears in its original position) in C++
- K’th Smallest/Largest Element in Unsorted Array in C++
- Python program to replace first ‘K’ elements by ‘N’
- Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++

Advertisements