Find minimum K such that difference between any Array pair is not a multiple of K Last Updated : 24 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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); Output4 Time Complexity: O(M * logM)Auxiliary Space: O(1) Related Articles: Introduction to Arrays - Data Structures and Algorithms Tutorials Comment More infoAdvertise with us Next Article Find minimum K such that difference between any Array pair is not a multiple of K A aarohirai2616 Follow Improve Article Tags : Greedy DSA Arrays Practice Tags : ArraysGreedy Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like