
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find âkâ such that its modulus with each array element is same
In this article, we will be discussing a program to find an integer ‘k’, such that its modulus with each element of a given array is the same.
For example, let us suppose we have been given with an array,
arr = {12, 22, 32}
Then we have the output value of k = 1, 2, 5, 10.
Take the case of two values in the array ‘x’ and ‘y’ (x>y). Then we have (y+difference)%k = y%k. Solving this we get,
difference%k = 0
So, we will find all the divisors to the difference of the maximum and minimum element in the array and then check for each divisor if the remainder is same or not for every element in the array.
Example
#include<bits/stdc++.h> using namespace std; int equal_modulus (int arr[], int len) { sort(arr, arr + len); int diff = arr[len-1] - arr[0]; //vector to store all the divisors vector <int> divi; for (int i = 1; i*i <= diff; i++) { if (diff%i == 0) { divi.push_back(i); if (i != diff/i) divi.push_back(diff/i); } } //to check if remainder is equal for every element for (int i = 0; i < divi.size(); i++) { int temp = arr[0]%divi[i]; int j; for (j = 1; j < len; j++) if (arr[j] % divi[i] != temp) break; //to print the values of k if (j == len) cout << divi[i] <<" "; } return 0; } int main() { int arr[] = {12, 22, 32}; int len = sizeof(arr)/sizeof(arr[0]); cout << "The values of K :" << endl; equal_modulus(arr, len); return 0; }
Output
The values of K : 1 2 10 5
- Related Articles
- Finding âkâ such that its modulus with each array element is same in C++
- JAVA Program to Replace Each Element of Array with its Next Element
- C++ program to find unique pairs such that each element is less than or equal to N
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Find unique pairs such that each element is less than or equal to N 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 an element in array such that sum of left array is equal to sum of right array using c++
- Program to find an element in list whose value is same as its frequency in Python
- Python program to find k'th smallest element in a 2D array
- JavaScript Program to Find Mth element after K Right Rotations of an Array
- Program to find smallest index for which array element is also same as index in Python
- JavaScript Program to Find a triplet such that sum of two equals to third element
- Find minimum x such that (x % k) * (x / k) == n in C++
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome

Advertisements