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++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; const int MAX = 200003; int d, n, mx; bitset<MAX> arr, temp, diff; int t; int x; // Function to smallest positive integer d void find( int a[], int n) { mx = 0; arr = 0; diff = 0; // find maximum element in an array for ( int i = 0; i < n; i++) { arr.set(a[i]); mx = max(mx, a[i]); } // Iterate d from 1 to mx temp = arr; for ( int d = 1; d <= mx; d++) { temp <<= 1; diff[d] = (arr & temp).any(); } // Iterate from n to mx 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 = true if (found) { cout << d << '\n' ; return ; } } } // Driver Code int main() { int A[] = { 11, 20, 13 }; int N = sizeof (A) / sizeof (A[0]); // Function Call find(A, N); return 0; } |
Java
// Java code to implement the same approach 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; // Function to find smallest positive integer d public static void find( int [] a, int n) { mx = 0 ; arr = new int [MAX]; diff = new int [MAX]; // Find maximum element in the array for ( int i = 0 ; i < n; i++) { arr[a[i]] = 1 ; mx = Math.max(mx, a[i]); } // Iterate d from 1 to mx 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]); } } // Iterate from n to mx 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 = true if (found) { System.out.println(d); return ; } d += 1 ; } } // Helper function to shift array elements to left 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; // Function Call find(A, N); } } // This code is contributed by sankar. |
Python3
# Python code to implement the same approach import math MAX = 200003 d = n = mx = 0 arr = temp = diff = None t = x = 0 # Function to find smallest positive integer d def find(a, n): global d, mx, arr, temp, diff mx = 0 arr = [ 0 ] * MAX diff = [ 0 ] * MAX # Find maximum element in the array for i in range (n): arr[a[i]] = 1 mx = max (mx, a[i]) # Iterate d from 1 to mx 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]) # Iterate from n to mx d = n while d < = mx: found = True for j in range (d, mx, d): if diff[j]: found = False break # If found = true if found: print (d) return d + = 1 # Driver Code if __name__ = = "__main__" : A = [ 11 , 20 , 13 ] N = len (A) # Function Call find(A, N) # This code is contributed by shivamsharma215 |
C#
// C# code implementation: using System; public class GFG { static int MAX = 200003; static int d, n, mx, t, x; static int [] arr, temp, diff; // Function to find smallest positive integer d public static void Find( int [] a, int n) { mx = 0; arr = new int [MAX]; diff = new int [MAX]; // Find maximum element in the array for ( int i = 0; i < n; i++) { arr[a[i]] = 1; mx = Math.Max(mx, a[i]); } // Iterate d from 1 to mx 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]); } } // Iterate from n to mx 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 = true if (found) { Console.WriteLine(d); return ; } d += 1; } } // Helper function to shift array elements to left 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() { // Code int [] A = { 11, 20, 13 }; int N = A.Length; // Function Call Find(A, N); } } // This code is contributed by karthik. |
Javascript
const MAX = 200003; let d = n = mx = 0; let arr = temp = diff = null ; let t = x = 0; // Function to find smallest positive integer d function find(a, n) { let mx = 0; let arr = new Array(MAX).fill(0); let diff = new Array(MAX).fill(0); // Find maximum element in the array for (let i = 0; i < n; i++) { arr[a[i]] = 1; mx = Math.max(mx, a[i]); } // Iterate d from 1 to mx 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]); } } // Iterate from n to mx d = n; while (d <= mx) { let found = true ; for (let j = d; j < mx; j += d) { if (diff[j]) { found = false ; break ; } } // If found = true if (found) { console.log(d); return ; } d += 1; } } // Driver Code let A = [11, 20, 13]; let N = A.length; // Function Call find(A, N); |
4
Time Complexity: O(M * logM)
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...