- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Finding ‘k’ such that its modulus with each array element is same in C++

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.

## Example

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; }

## Output

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

1 2

## Conclusion

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

- Related Articles
- C++ program to find ‘k’ such that its modulus with each array element is same
- 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++
- Count pairs in an array such that at least one element is prime in C++
- Find unique pairs such that each element is less than or equal to N in C++
- Count Derangements (Permutation such that no element appears in its original position) in C++
- Place k elements such that minimum distance is maximized in C++
- Find an element in array such that sum of left array is equal to sum of right array using c++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- C++ program to find unique pairs such that each element is less than or equal to N
- Check for an array element that is coprime with all others in C++