Minimum difference between values in an array with factors 1 to M
Last Updated :
03 Apr, 2023
Given an array of nums of length N and an integer M, the task is to find the minimum difference between two elements of nums such that the numbers coming between these two integers (including the selected numbers) have all factors 1, 2, 3, ..., M. Return the minimum difference if it does not exist then return -1.
Examples:
Input: N = 5, M = 7, nums = [6, 4, 5, 3, 7]
Output: 3
Explanation: First number will be 4 and the second will be 7, so the numbers between 4 and 7 are: [4, 5, 7]
Factors of 4 --> (1, 2, 4)
Factors of 5 --> (1, 5)
Factors of 7 --> (1, 7)
So, it contains every number from 1, 2, 3, 4, 5, 6, 7(as M = 7)
Input: N = 4, M = 2, nums = [3, 7, 2, 9]
Output: 0
Explanation: First number will be 2 and Second number is also 2
Factors of 2 --> (1, 2)
So it contains 1, 2(as M = 2)
Input: N = 3, M = 4, nums = [1, 3, 7]
Output: -1
Approach: To solve the problem follow the below-given steps:
- Sort the given array in a non-decreasing array
- Find factors of every number present in the given array.
- Now we will use two pointer approach to solve the problem and we will keep track of factors in Hash Map factorFreq.
- Start from the 0th index for both the ith and jth pointer.
- Now while factorFreq size is less than M, we will keep incrementing the jth pointer.
- If the size of factorFreq is equal to M then compare the difference between the jth value and ith value and increment the ith value, make sure you don't forget to update the factorFreq.
- Return the min value if exist.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to find the factors of a given integer
vector<int> factorsOfNumber(int n)
{
vector<int> factors = { 1 };
if (n == 1)
return factors;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (n / i == i) {
factors.push_back(i);
}
else {
factors.push_back(i);
factors.push_back(n / i);
}
}
}
factors.push_back(n);
return factors;
}
// Function to find the minimum difference of two selected
// integers
int findMinDifference(int nums[], int N, int M)
{
// Sorting given array
sort(nums, nums + N);
// Factors will contain the
// factors of the given integers
// in nums array
vector<int> factors[N];
for (int i = 0; i < N; i++) {
factors[i] = factorsOfNumber(nums[i]);
}
// factorFreq will map the
// frequency of factors which is
// less than or equal to M
map<int, int> factorFreq;
int i = 0;
int j = 0;
// minDiff will contain our
// final answer
int minDiff = INT_MAX;
// Using two pointer
while (i < N && j < N) {
// If factorFreq size is equal
// to M, then we can increment
// the pointer i
if (factorFreq.size() == M) {
// Updating minDiff
minDiff = min(minDiff, (nums[j - 1] - nums[i]));
// Updating factorFreq
for (int k = 0; k < factors[i].size(); k++) {
if (factors[i][k] <= M) {
factorFreq[factors[i][k]]--;
if (factorFreq[factors[i][k]] == 0)
factorFreq.erase(factors[i][k]);
}
}
i++;
}
// Otherwise we will increment
// the j pointer
else {
// Updating factorFreq
for (int k = 0; k < factors[j].size(); k++) {
if (factors[j][k] <= M) {
factorFreq[factors[j][k]]++;
}
}
j++;
}
}
// Checking if i<N but factorFreq
// size is still equal to M, means
// we can still increment i
while (i < N && factorFreq.size() == M) {
minDiff = min(minDiff, (nums[j - 1] - nums[i]));
for (int k = 0; k < factors[i].size(); k++) {
if (factors[i][k] <= M) {
factorFreq[factors[i][k]]--;
if (factorFreq[factors[i][k]] == 0)
factorFreq.erase(factors[i][k]);
}
}
i++;
}
// Check if two numbers exist or
// not and return the value
if (minDiff == INT_MAX)
return -1;
else
return minDiff;
}
// Driver code
int main()
{
int N = 5;
int M = 7;
int nums[] = { 6, 4, 5, 3, 7 };
cout << findMinDifference(nums, N, M) << endl;
return 0;
}
Java
// Java algorithm for the above approach
import java.util.*;
class GFG {
// Driver Code
public static void main(String[] args)
{
int N = 5;
int M = 7;
int[] nums = { 6, 4, 5, 3, 7 };
System.out.println(findMinDifference(nums, N, M));
}
// Function to find the minimum
// Difference of two selected integers
public static int findMinDifference(int[] nums, int N,
int M)
{
// Sorting given array
Arrays.sort(nums);
// Factors will contain the
// factors of the given integers
// in nums array
List<Integer>[] factors = new ArrayList[N];
for (int i = 0; i < N; i++) {
factors[i] = factorsOfNumber(nums[i]);
}
// factorFreq will map the
// frequency of factors which is
// less than or equal to M
Map<Integer, Integer> factorFreq = new HashMap<>();
int i = 0;
int j = 0;
// minDiff will contain our
// final answer
int minDiff = Integer.MAX_VALUE;
// Using two pointer
while (i < N && j < N) {
// If factorFreq size is equal
// to M, then we can increment
// the pointer i
if (factorFreq.size() == M) {
// Updating minDiff
minDiff = Math.min(minDiff,
(nums[j - 1] - nums[i]));
// Updating factorFreq
for (int k = 0; k < factors[i].size();
k++) {
if (factors[i].get(k) <= M) {
factorFreq.put(
factors[i].get(k),
factorFreq.get(
factors[i].get(k))
- 1);
if (factorFreq.get(
factors[i].get(k))
== 0)
factorFreq.remove(
factors[i].get(k));
}
}
i++;
}
// Otherwise we will increment
// the j pointer
else {
// Updating factorFreq
for (int k = 0; k < factors[j].size();
k++) {
if (factors[j].get(k) <= M) {
factorFreq.put(
factors[j].get(k),
factorFreq.getOrDefault(
factors[j].get(k), 0)
+ 1);
}
}
j++;
}
}
// Checking if i<N bus factorFreq
// size is still equal to M, means
// we can still increment i
while (i < N && factorFreq.size() == M) {
minDiff = Math.min(minDiff,
(nums[j - 1] - nums[i]));
for (int k = 0; k < factors[i].size(); k++) {
if (factors[i].get(k) <= M) {
factorFreq.put(
factors[i].get(k),
factorFreq.get(factors[i].get(k))
- 1);
if (factorFreq.get(factors[i].get(k))
== 0)
factorFreq.remove(
factors[i].get(k));
}
}
i++;
}
// Check if two numbers exist or
// not and return the value
if (minDiff == Integer.MAX_VALUE)
return -1;
else
return minDiff;
}
// Function tpo find the factor
// of the given integer
public static List<Integer> factorsOfNumber(int n)
{
List<Integer> fact = new ArrayList<>();
fact.add(1);
if (n == 1)
return fact;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (n / i == i) {
fact.add(i);
}
else {
fact.add(i);
fact.add(n / i);
}
}
}
fact.add(n);
return fact;
}
}
Python3
# Python code implementation:
import math
from collections import defaultdict
def findMinDifference(nums, N, M):
# Sorting given array
nums.sort()
# Factors will contain the factors of the given integers
# in nums array
factors = []
for i in range(N):
factors.append(factorsOfNumber(nums[i]))
# factorFreq will map the frequency of factors which is
# less than or equal to M
factorFreq = defaultdict(int)
i = 0
j = 0
# minDiff will contain our final answer
minDiff = math.inf
# Using two pointer
while i < N and j < N:
# If factorFreq size is equal to M, then we can
# increment the pointer i
if len(factorFreq) == M:
# Updating minDiff
minDiff = min(minDiff, nums[j-1] - nums[i])
# Updating factorFreq
for k in range(len(factors[i])):
if factors[i][k] <= M:
factorFreq[factors[i][k]] -= 1
if factorFreq[factors[i][k]] == 0:
del factorFreq[factors[i][k]]
i += 1
# Otherwise we will increment the j pointer
else:
# Updating factorFreq
for k in range(len(factors[j])):
if factors[j][k] <= M:
factorFreq[factors[j][k]] += 1
j += 1
# Checking if i<N bus factorFreq size is still equal
# to M, means we can still increment i
while i < N and len(factorFreq) == M:
minDiff = min(minDiff, nums[j-1] - nums[i])
for k in range(len(factors[i])):
if factors[i][k] <= M:
factorFreq[factors[i][k]] -= 1
if factorFreq[factors[i][k]] == 0:
del factorFreq[factors[i][k]]
i += 1
# Check if two numbers exist or not and return the value
if minDiff == math.inf:
return -1
else:
return minDiff
# Function to find the factor of the given integer
def factorsOfNumber(n):
fact = [1]
if n == 1:
return fact
for i in range(2, int(math.sqrt(n))+1):
if n % i == 0:
if n // i == i:
fact.append(i)
else:
fact.append(i)
fact.append(n // i)
fact.append(n)
return fact
N = 5
M = 7
nums = [6, 4, 5, 3, 7]
print(findMinDifference(nums, N, M))
# This code is contributed by lokesh.
C#
// C# code for the approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
// Driver's code
static void Main(string[] args)
{
// Input
int N = 5;
int M = 7;
int[] nums = { 6, 4, 5, 3, 7 };
// Function call
Console.WriteLine(findMinDifference(nums, N, M));
}
// Function to find the factors of a given integer
static List<int> factorsOfNumber(int n)
{
List<int> factors = new List<int>{ 1 };
if (n == 1)
return factors;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (n / i == i) {
factors.Add(i);
}
else {
factors.Add(i);
factors.Add(n / i);
}
}
}
factors.Add(n);
return factors;
}
// Function to find the minimum difference of two selected
// integers
static int findMinDifference(int[] nums, int N, int M)
{
// Sorting given array
Array.Sort(nums);
// Factors will contain the factors of the given
// integers in nums array
List<List<int> > factors = new List<List<int> >();
for (int k = 0; k < N; k++) {
factors.Add(factorsOfNumber(nums[k]));
}
// factorFreq will map the frequency of factors
// which is less than or equal to M
Dictionary<int, int> factorFreq
= new Dictionary<int, int>();
int i = 0;
int j = 0;
// minDiff will contain our final answer
int minDiff = int.MaxValue;
// Using two pointer
while (i < N && j < N) {
// If factorFreq size is equal to M, then we can
// increment the pointer i
if (factorFreq.Count == M) {
// Updating minDiff
minDiff = Math.Min(minDiff,
nums[j - 1] - nums[i]);
// Updating factorFreq
for (int k = 0; k < factors[i].Count; k++) {
if (factors[i][k] <= M) {
factorFreq[factors[i][k]] -= 1;
if (factorFreq[factors[i][k]]
== 0) {
factorFreq.Remove(
factors[i][k]);
}
}
}
i += 1;
}
// Otherwise we will increment the j pointer
else {
// Updating factorFreq
for (int k = 0; k < factors[j].Count; k++) {
if (factors[j][k] <= M) {
if (factorFreq.ContainsKey(
factors[j][k])) {
factorFreq[factors[j][k]] += 1;
}
else {
factorFreq.Add(factors[j][k],
1);
}
}
}
j += 1;
}
}
// Checking if i<N bus factorFreq size is still
// equal to M, means we can still increment i
while (i < N && factorFreq.Count == M) {
minDiff
= Math.Min(minDiff, nums[j - 1] - nums[i]);
for (int k = 0; k < factors[i].Count; k++) {
if (factors[i][k] <= M) {
factorFreq[factors[i][k]] -= 1;
if (factorFreq[factors[i][k]] == 0) {
factorFreq.Remove(factors[i][k]);
}
}
}
i += 1;
}
// Check if two numbers exist or not and return the
// value
if (minDiff == int.MaxValue)
return -1;
else
return minDiff;
}
}
JavaScript
// Javascript code implementation
function findMinDifference(nums, N, M){
// Sorting given array
nums.sort(function(a, b){return a - b});
// Factors will contain the factors of the given integers
// in nums array
var factors = [];
for (var i = 0; i < N; i++){
factors.push(factorsOfNumber(nums[i]));
}
// factorFreq will map the frequency of factors which is
// less than or equal to M
var factorFreq = {};
var i = 0;
var j = 0;
// minDiff will contain our final answer
var minDiff = Infinity;
// Using two pointer
while (i < N && j < N){
// If factorFreq size is equal to M, then we can
// increment the pointer i
if (Object.keys(factorFreq).length === M){
// Updating minDiff
minDiff = Math.min(minDiff, nums[j-1] - nums[i]);
// Updating factorFreq
for (var k = 0; k < factors[i].length; k++){
if (factors[i][k] <= M){
factorFreq[factors[i][k]] -= 1;
if (factorFreq[factors[i][k]] === 0){
delete factorFreq[factors[i][k]];
}
}
}
i += 1;
// Otherwise we will increment the j pointer
} else {
// Updating factorFreq
for (var k = 0; k < factors[j].length; k++){
if (factors[j][k] <= M){
if (factorFreq[factors[j][k]]){
factorFreq[factors[j][k]] += 1;
} else {
factorFreq[factors[j][k]] = 1;
}
}
}
j += 1;
}
}
// Checking if i<N bus factorFreq size is still equal
// to M, means we can still increment i
while (i < N && Object.keys(factorFreq).length === M){
minDiff = Math.min(minDiff, nums[j-1] - nums[i]);
for (var k = 0; k < factors[i].length; k++){
if (factors[i][k] <= M){
factorFreq[factors[i][k]] -= 1;
if (factorFreq[factors[i][k]] === 0){
delete factorFreq[factors[i][k]];
}
}
}
i += 1;
}
// Check if two numbers exist or not and return the value
if (minDiff === Infinity){
return -1;
} else {
return minDiff;
}
}
// Function to find the factor of the given integer
function factorsOfNumber(n){
var fact = [1];
if (n === 1){
return fact;
}
for (var i = 2; i <= Math.floor(Math.sqrt(n)); i++){
if (n % i === 0){
if (n / i === i){
fact.push(i);
} else {
fact.push(i);
fact.push(n / i);
}
}
}
fact.push(n);
return fact;
}
var N = 5;
var M = 7;
var nums = [6, 4, 5, 3, 7];
console.log(findMinDifference(nums, N, M));
Time Complexity: O(N* sqrt(N))
Auxiliary Space: O(N * sqrt(N))
Similar Reads
Minimum difference between maximum and minimum value of Array with given Operations Given an array arr[] and an integer K. The following operations can be performed on any array element: Multiply the array element with K.If the element is divisible by K, then divide it by K. The above two operations can be applied any number of times including zero on any array element. The task is
9 min read
Generate N sized Array with mean K and minimum difference between min and max Given two integers N and X, the task is to find an output array arr[] containing distinct integers of length N such that their average is K and the difference between the minimum and the maximum is minimum possible. Input: N = 4, X = 8Output :- 6 7 9 10Explanation: Clearly the mean of 6, 7, 9, 10 is
6 min read
Minimum sum of all differences between unique pairs in the Array Given an array arr[] consisting of N integers, the task is to find the minimum sum of all absolute differences between unique pairs of elements in the array after updating the array arr[]. To update the array, any two elements from the array can be chosen in any order. 1 is subtracted from the first
7 min read
Find minimum difference between any two elements (pair) in given array Given an unsorted array, find the minimum difference between any pair in the given array. Examples : Input: {1, 5, 3, 19, 18, 25}Output: 1Explanation: Minimum difference is between 18 and 19 Input: {30, 5, 20, 9}Output: 4Explanation: Minimum difference is between 5 and 9 Input: {1, 19, -4, 31, 38, 2
15+ min read
Find minimum difference between any two elements | Set 2 Given an unsorted array arr[] of size n, the task is to find the minimum difference between any pair in the given array. Input: arr[] = {1, 2, 3, 4} Output: 1 The possible absolute differences are: {1, 2, 3, 1, 2, 1}Input: arr[] = {10, 2, 5, 4} Output: 1 Approach: Traverse through the array and crea
8 min read
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 inte
7 min read
Minimize the difference between the maximum and minimum values of the modified array Given an array A of n integers and integer X. You may choose any integer between -X\leq k\leq X , and add k to A[i] for each 0\leq i \leq n-1 . The task is to find the smallest possible difference between the maximum value of A and the minimum value of A after updating array A. Examples: Input: arr[
5 min read
Minimum distance between any special pair in the given array Given an array arr[] of N integers, the task is to find the minimum possible absolute difference between indices of a special pair. A special pair is defined as a pair of indices (i, j) such that if arr[i] ? arr[j], then there is no element X (where arr[i] < X < arr[j]) present in between indi
9 min read
Split array into K Subarrays to minimize sum of difference between min and max Given a sorted array arr[] of size N and integer K, the task is to split the array into K non-empty subarrays such that the sum of the difference between the maximum element and the minimum element of each subarray is minimized. Note: Every element of the array must be included in one subarray and e
6 min read
Minimum distance between any two equal elements in an Array Given an array arr, the task is to find the minimum distance between any two same elements in the array. If no such element is found, return -1. Examples: Input: arr = {1, 2, 3, 2, 1} Output: 2 Explanation: There are two matching pairs of values: 1 and 2 in this array. Minimum Distance between two 1
11 min read