Find minimum K such that difference between any Array pair is not a multiple of K
Given an array arr[] of length N. Find a positive integer K such that the difference between any pair of array elements is not a multiple of K.
Examples:
Input: arr[] = {11, 20, 13}
Output: 4
?Explanation: The possible differences are A1 ? A2 = 9, A1 ? A3 = 2 , and A2 ? A3 = 7. The least positive integer not a multiple of any of these differences is 4.
Input: A = {21, 34, 23}
Output: 3
Approach: The problem can be solved based on the following observation:
Let M be the maximum element in an array. So difference between any pair of the array will also be ? M. Therefore, finding an element in the range of 2 to M is the required task.
Follow the steps mentioned below to implement the idea:
- Iterate a loop over the array arr[] to find the maximum element (say mx) in the array.
- After that, iterate a for loop from 1 to mx:
- Store true in diff if at least one bit is set in (arr&temp).any().
- Otherwise, diff store false.
- Iterate d from n to mx and set found = true.
- Again Iterate j from d to mx and increment j = j+d and check the value of diff, if it is true then set found = false.
- Otherwise, set found = true.
- If found is true then print d such that the difference between any pair of array elements is not a multiple of d.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200003;
int d, n, mx;
bitset<MAX> arr, temp, diff;
int t;
int x;
void find( int a[], int n)
{
mx = 0;
arr = 0;
diff = 0;
for ( int i = 0; i < n; i++) {
arr.set(a[i]);
mx = max(mx, a[i]);
}
temp = arr;
for ( int d = 1; d <= mx; d++) {
temp <<= 1;
diff[d] = (arr & temp).any();
}
d = n;
for (; d <= mx; ++d) {
bool found = true ;
for ( int j = d; j < mx; j += d) {
if (diff[j]) {
found = false ;
break ;
}
}
if (found) {
cout << d << '\n' ;
return ;
}
}
}
int main()
{
int A[] = { 11, 20, 13 };
int N = sizeof (A) / sizeof (A[0]);
find(A, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static final int MAX = 200003 ;
static int d, n, mx, t, x;
static int [] arr, temp, diff;
public static void find( int [] a, int n)
{
mx = 0 ;
arr = new int [MAX];
diff = new int [MAX];
for ( int i = 0 ; i < n; i++) {
arr[a[i]] = 1 ;
mx = Math.max(mx, a[i]);
}
temp = Arrays.copyOf(arr, arr.length);
for (d = 1 ; d <= mx; d++) {
temp = shiftLeft(temp);
for ( int i = 0 ; i < MAX; i++) {
diff[d] = diff[d] | (arr[i] & temp[i]);
}
}
d = n;
while (d <= mx) {
boolean found = true ;
for ( int j = d; j < mx; j += d) {
if (diff[j] != 0 ) {
found = false ;
break ;
}
}
if (found) {
System.out.println(d);
return ;
}
d += 1 ;
}
}
private static int [] shiftLeft( int [] arr)
{
int [] result = new int [arr.length];
for ( int i = 1 ; i < arr.length; i++) {
result[i - 1 ] = arr[i];
}
result[result.length - 1 ] = 0 ;
return result;
}
public static void main(String[] args)
{
int [] A = { 11 , 20 , 13 };
int N = A.length;
find(A, N);
}
}
|
Python3
import math
MAX = 200003
d = n = mx = 0
arr = temp = diff = None
t = x = 0
def find(a, n):
global d, mx, arr, temp, diff
mx = 0
arr = [ 0 ] * MAX
diff = [ 0 ] * MAX
for i in range (n):
arr[a[i]] = 1
mx = max (mx, a[i])
temp = arr.copy()
for d in range ( 1 , mx + 1 ):
temp = [ 0 ] + temp[: - 1 ]
for i in range ( MAX ):
diff[d] = diff[d] or (arr[i] and temp[i])
d = n
while d < = mx:
found = True
for j in range (d, mx, d):
if diff[j]:
found = False
break
if found:
print (d)
return
d + = 1
if __name__ = = "__main__" :
A = [ 11 , 20 , 13 ]
N = len (A)
find(A, N)
|
C#
using System;
public class GFG {
static int MAX = 200003;
static int d, n, mx, t, x;
static int [] arr, temp, diff;
public static void Find( int [] a, int n)
{
mx = 0;
arr = new int [MAX];
diff = new int [MAX];
for ( int i = 0; i < n; i++) {
arr[a[i]] = 1;
mx = Math.Max(mx, a[i]);
}
temp = ( int [])arr.Clone();
for (d = 1; d <= mx; d++) {
temp = ShiftLeft(temp);
for ( int i = 0; i < MAX; i++) {
diff[d] |= (arr[i] & temp[i]);
}
}
d = n;
while (d <= mx) {
bool found = true ;
for ( int j = d; j < mx; j += d) {
if (diff[j] != 0) {
found = false ;
break ;
}
}
if (found) {
Console.WriteLine(d);
return ;
}
d += 1;
}
}
private static int [] ShiftLeft( int [] arr)
{
int [] result = new int [arr.Length];
for ( int i = 1; i < arr.Length; i++) {
result[i - 1] = arr[i];
}
result[result.Length - 1] = 0;
return result;
}
static public void Main()
{
int [] A = { 11, 20, 13 };
int N = A.Length;
Find(A, N);
}
}
|
Javascript
const MAX = 200003;
let d = n = mx = 0;
let arr = temp = diff = null ;
let t = x = 0;
function find(a, n) {
let mx = 0;
let arr = new Array(MAX).fill(0);
let diff = new Array(MAX).fill(0);
for (let i = 0; i < n; i++) {
arr[a[i]] = 1;
mx = Math.max(mx, a[i]);
}
let temp = [...arr];
for (let d = 1; d <= mx; d++) {
temp.unshift(0);
temp.pop();
for (let i = 0; i < MAX; i++) {
diff[d] = diff[d] || (arr[i] && temp[i]);
}
}
d = n;
while (d <= mx) {
let found = true ;
for (let j = d; j < mx; j += d) {
if (diff[j]) {
found = false ;
break ;
}
}
if (found) {
console.log(d);
return ;
}
d += 1;
}
}
let A = [11, 20, 13];
let N = A.length;
find(A, N);
|
Time Complexity: O(M * logM)
Auxiliary Space: O(1)
Related Articles:
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...