Finding ‘k’ such that its modulus with each array element is same
Last Updated :
27 Jan, 2023
Given an array of n integers .We need to find all ‘k’ such that
arr[0] % k = arr[1] % k = ....... = arr[n-1] % k
Examples:
Input : arr[] = {6, 38, 34}
Output : 1 2 4
6%1 = 38%1 = 34%1 = 0
6%2 = 38%2 = 34%2 = 0
6%4 = 38%4 = 34%2 = 2
Input : arr[] = {3, 2}
Output : 1
Suppose the array contains only two elements a and b (b>a). So we can write b = a + d where d is a positive integer and ‘k’ be a number such that b%k = a%k.
(a + d)%k = a%k
a%k + d%k = a%k
d%k = 0
Now what we get from the above calculation is that ‘k’ should be a divisor of difference between the two numbers.
Now what we have to do when we have an array of integers
- Find out the difference ‘d’ between maximum and minimum element of the array
- Find out all the divisors of ‘d’
- Step 3: For each divisor check if arr[i]%divisor(d) is same or not .if it is same print it.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void printEqualModNumbers ( int arr[], int n)
{
sort(arr, arr + n);
int d = arr[n-1] - arr[0];
if (d==0){
cout<< "Infinite solution" ;
return ;
}
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);
}
}
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] << " " ;
}
}
int main()
{
int arr[] = {38, 6, 34};
int n = sizeof (arr)/ sizeof (arr[0]);
printEqualModNumbers(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Vector;
class Test
{
static void printEqualModNumbers ( int arr[], int n)
{
Arrays.sort(arr);
int d = arr[n- 1 ] - arr[ 0 ];
if (d== 0 ){
System.out.println( "Infinite solution" );
return ;
}
Vector<Integer> v = new Vector<>();
for ( int i= 1 ; i*i<=d; i++)
{
if (d%i == 0 )
{
v.add(i);
if (i != d/i)
v.add(d/i);
}
}
for ( int i= 0 ; i<v.size(); i++)
{
int temp = arr[ 0 ]%v.get(i);
int j;
for (j= 1 ; j<n; j++)
if (arr[j] % v.get(i) != temp)
break ;
if (j == n)
System.out.print(v.get(i) + " " );
}
}
public static void main(String args[])
{
int arr[] = { 38 , 6 , 34 };
printEqualModNumbers(arr, arr.length);
}
}
|
Python3
def printEqualModNumbers(arr, n):
arr.sort();
d = arr[n - 1 ] - arr[ 0 ];
/ / Case when all the array elements are same
if (d = = 0 ):
print ( "Infinite solution" )
return
v = [];
i = 1 ;
while (i * i < = d):
if (d % i = = 0 ):
v.append(i);
if (i ! = d / i):
v.append(d / i);
i + = 1 ;
for i in range ( len (v)):
temp = arr[ 0 ] % v[i];
j = 1 ;
while (j < n):
if (arr[j] % v[i] ! = temp):
break ;
j + = 1 ;
if (j = = n):
print (v[i], end = " " );
arr = [ 38 , 6 , 34 ];
printEqualModNumbers(arr, len (arr));
|
C#
using System;
using System.Collections;
class Test
{
static void printEqualModNumbers ( int []arr, int n)
{
Array.Sort(arr);
int d = arr[n-1] - arr[0];
if (d==0){
Console.write( "Infinite solution" );
return ;
}
ArrayList v = new ArrayList();
for ( int i=1; i*i<=d; i++)
{
if (d%i == 0)
{
v.Add(i);
if (i != d/i)
v.Add(d/i);
}
}
for ( int i=0; i<v.Count; i++)
{
int temp = arr[0]%( int )v[i];
int j;
for (j=1; j<n; j++)
if (arr[j] % ( int )v[i] != temp)
break ;
if (j == n)
Console.Write(v[i] + " " );
}
}
public static void Main()
{
int []arr = {38, 6, 34};
printEqualModNumbers(arr, arr.Length);
}
}
|
PHP
<?php
function printEqualModNumbers ( $arr , $n )
{
sort( $arr );
$d = $arr [ $n -1] - $arr [0];
if (d==0){
print ( "Infinite solution" );
return ;
}
$v = array ();
for ( $i =1; $i * $i <= $d ; $i ++)
{
if ( $d % $i == 0)
{
array_push ( $v , $i );
if ( $i != $d / $i )
array_push ( $v , $d / $i );
}
}
for ( $i =0; $i < count ( $v ); $i ++)
{
$temp = $arr [0]% $v [ $i ];
$j =1;
for (; $j < $n ; $j ++)
if ( $arr [ $j ] % $v [ $i ] != $temp )
break ;
if ( $j == $n )
print ( $v [ $i ]. " " );
}
}
$arr = array (38, 6, 34);
printEqualModNumbers( $arr , count ( $arr ));
?>
|
Javascript
<script>
function printEqualModNumbers (arr, n)
{
arr.sort((a, b) => a - b);
d = arr[n-1] - arr[0];
if (d==0){
document.write( "Infinite solution" );
return ;
}
v = new Array();
for (i=1; i*i<=d; i++)
{
if (d%i == 0)
{
v.push(i);
if (i != d/i)
v.push(d/i);
}
}
for (i=0; i< v.length; i++)
{
temp = arr[0]%v[i];
j=1;
for (; j<n; j++)
if (arr[j] % v[i] != temp)
break ;
if (j == n)
document.write(v[i] + " " );
}
}
let arr = new Array(38, 6, 34);
printEqualModNumbers(arr, arr.length);
</script>
|
Time Complexity: O(nlog(n))
Since the given array has to be sorted for the given problem, we use the sorting algorithm which takes O(nlog(n)) time.
Space Complexity: O(n)
We use a vector to store all the divisors of the difference of the first and the last element of the sorted array. This has a space complexity of O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...