Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
Last Updated :
02 Mar, 2023
Given an array arr[] of integers, the task is to find the minimum number greater than the maximum element from the array which cannot be formed using the numbers in the array (adding elements to form some other number). If no such element exists then print -1.
Examples:
Input: arr[] = {2, 6, 9}
Output: -1
There is no such number greater than 9
that cannot be formed using 2, 6 and 9.
Input: arr[] = {6, 7, 15}
Output: 16
16 is the smallest number greater than 15 that
cannot be formed using 6, 7 and 15.
Approach: The problem is similar to the minimum coin change problem with slight modification. First sort the array in ascending order and find the maximum element max that will be the number present at the last index of the array. Check for the numbers in the range (max, 2 * max) for the answer. If a number in this range cannot be formed using the elements of the array then that number is the answer, but if all the numbers can be formed in this range then there exists no such number that cannot be formed using the elements of the array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function that returns the minimum
// number greater than maximum of the
// array that cannot be formed using the
// elements of the array
int findNumber(int arr[], int n)
{
// Sort the given array
sort(arr, arr + n);
// Maximum number in the array
int max = arr[n - 1];
// table[i] will store the minimum number of
// elements from the array to form i
int table[(2 * max) + 1];
table[0] = 0;
for (int i = 1; i < (2 * max) + 1; i++)
table[i] = INT_MAX;
int ans = -1;
// Calculate the minimum number of elements
// from the array required to form
// the numbers from 1 to (2 * max)
for (int i = 1; i < (2 * max) + 1; i++) {
for (int j = 0; j < n; j++) {
if (arr[j] <= i) {
int res = table[i - arr[j]];
if (res != INT_MAX && res + 1 < table[i])
table[i] = res + 1;
}
}
// If there exists a number greater than
// the maximum element of the array that can be
// formed using the numbers of array
if (i > arr[n - 1] && table[i] == INT_MAX) {
ans = i;
break;
}
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 6, 7, 15 };
int n = (sizeof(arr) / sizeof(int));
cout << findNumber(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
class GFG
{
// Function that returns the minimum
// number greater than maximum of the
// array that cannot be formed using the
// elements of the array
static int findNumber(int arr[], int n)
{
// Sort the given array
Arrays.sort(arr);
// Maximum number in the array
int max = arr[n - 1];
// table[i] will store the minimum number of
// elements from the array to form i
int table[] = new int[(2 * max) + 1];
table[0] = 0;
for (int i = 1; i < (2 * max) + 1; i++)
table[i] = Integer.MAX_VALUE;
int ans = -1;
// Calculate the minimum number of elements
// from the array required to form
// the numbers from 1 to (2 * max)
for (int i = 1; i < (2 * max) + 1; i++)
{
for (int j = 0; j < n; j++)
{
if (arr[j] <= i)
{
int res = table[i - arr[j]];
if (res != Integer.MAX_VALUE && res + 1 < table[i])
table[i] = res + 1;
}
}
// If there exists a number greater than
// the maximum element of the array that can be
// formed using the numbers of array
if (i > arr[n - 1] && table[i] == Integer.MAX_VALUE)
{
ans = i;
break;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 6, 7, 15 };
int n = arr.length;
System.out.println(findNumber(arr, n));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
# Function that returns the minimum
# number greater than Maximum of the
# array that cannot be formed using the
# elements of the array
def findNumber(arr, n):
# Sort the given array
arr = sorted(arr)
# Maximum number in the array
Max = arr[n - 1]
# table[i] will store the minimum number of
# elements from the array to form i
table = [10**9 for i in range((2 * Max) + 1)]
table[0] = 0
ans = -1
# Calculate the minimum number of elements
# from the array required to form
# the numbers from 1 to (2 * Max)
for i in range(1, 2 * Max + 1):
for j in range(n):
if (arr[j] <= i):
res = table[i - arr[j]]
if (res != 10**9 and res + 1 < table[i]):
table[i] = res + 1
# If there exists a number greater than
# the Maximum element of the array that can be
# formed using the numbers of array
if (i > arr[n - 1] and table[i] == 10**9):
ans = i
break
return ans
# Driver code
arr = [6, 7, 15]
n = len(arr)
print(findNumber(arr, n))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that returns the minimum
// number greater than maximum of the
// array that cannot be formed using the
// elements of the array
static int findNumber(int[] arr, int n)
{
// Sort the given array
Array.Sort(arr);
// Maximum number in the array
int max = arr[n - 1];
// table[i] will store the minimum number of
// elements from the array to form i
int[] table = new int[(2 * max) + 1];
table[0] = 0;
for (int i = 1; i < (2 * max) + 1; i++)
table[i] = int.MaxValue;
int ans = -1;
// Calculate the minimum number of elements
// from the array required to form
// the numbers from 1 to (2 * max)
for (int i = 1; i < (2 * max) + 1; i++)
{
for (int j = 0; j < n; j++)
{
if (arr[j] <= i)
{
int res = table[i - arr[j]];
if (res != int.MaxValue && res + 1 < table[i])
table[i] = res + 1;
}
}
// If there exists a number greater than
// the maximum element of the array that can be
// formed using the numbers of array
if (i > arr[n - 1] && table[i] == int.MaxValue)
{
ans = i;
break;
}
}
return ans;
}
// Driver code
public static void Main()
{
int[] arr = { 6, 7, 15 };
int n = arr.Length;
Console.WriteLine(findNumber(arr, n));
}
}
/* This code contributed by Code_Mech */
PHP
<?PHP
// PHP implementation of the approach
// Function that returns the minimum
// number greater than maximum of the
// array that cannot be formed using the
// elements of the array
function findNumber($arr, $n)
{
// Sort the given array
sort($arr);
// Maximum number in the array
$max = $arr[$n - 1];
// table[i] will store the minimum number of
// elements from the array to form i
$table = array((2 * $max) + 1);
$table[0] = 0;
for ($i = 1; $i < (2 * $max) + 1; $i++)
$table[$i] = PHP_INT_MAX;
$ans = -1;
// Calculate the minimum number of elements
// from the array required to form
// the numbers from 1 to (2 * max)
for ($i = 1; $i < (2 * $max) + 1; $i++)
{
for ($j = 0; $j < $n; $j++)
{
if ($arr[$j] <= $i)
{
$res = $table[$i - $arr[$j]];
if ($res != PHP_INT_MAX && $res + 1 < $table[$i])
$table[$i] = $res + 1;
}
}
// If there exists a number greater than
// the maximum element of the array that can be
// formed using the numbers of array
if ($i > $arr[$n - 1] && $table[$i] == PHP_INT_MAX)
{
$ans = $i;
break;
}
}
return $ans;
}
// Driver code
{
$arr = array(6, 7, 15 );
$n = sizeof($arr);
echo(findNumber($arr, $n));
}
/* This code contributed by Code_Mech*/
JavaScript
<script>
// Javascript implementation of the approach
// Function that returns the minimum
// number greater than maximum of the
// array that cannot be formed using the
// elements of the array
function findNumber(arr, n)
{
// Sort the given array
arr.sort((a, b) => a - b);
// Maximum number in the array
var max = arr[n - 1];
// table[i] will store the minimum number of
// elements from the array to form i
var table = Array((2 * max) + 1).fill(0);
table[0] = 0;
for(i = 1; i < (2 * max) + 1; i++)
table[i] = Number.MAX_VALUE;
var ans = -1;
// Calculate the minimum number of elements
// from the array required to form
// the numbers from 1 to (2 * max)
for(i = 1; i < (2 * max) + 1; i++)
{
for(j = 0; j < n; j++)
{
if (arr[j] <= i)
{
var res = table[i - arr[j]];
if (res != Number.MAX_VALUE &&
res + 1 < table[i])
table[i] = res + 1;
}
}
// If there exists a number greater than
// the maximum element of the array that can be
// formed using the numbers of array
if (i > arr[n - 1] &&
table[i] == Number.MAX_VALUE)
{
ans = i;
break;
}
}
return ans;
}
// Driver code
var arr = [ 6, 7, 15 ];
var n = arr.length;
document.write(findNumber(arr, n));
// This code is contributed by umadevi9616
</script>
Time Complexity : O(MAX*N+N*log(N))
Auxiliary Space: O(MAX)
Similar Reads
Minimum value that can't be formed using OR of elements of the Array Given an array of positive integers arr[]. The task is to Find the minimum positive value which cannot be formed using OR operator on any subset of the array. Examples: Input: arr[] = {2, 3}Output: 1Explanation: Since 1 is missing from array and we cannot make it using OR for any other subset of thi
6 min read
Form smallest number using indices of numbers chosen from Array with sum less than S Given an array arr[] and an integer S, the task is to choose the maximum count of numbers from the array such that the sum of numbers is less than S and form the smallest possible number using their indicesNote: Any element can be chosen any number of times. Examples: Input: arr[] = {3, 4, 2, 4, 6,
6 min read
Remove minimum elements from the array such that 2*min becomes more than max Given an unsorted array, arr[]. The task is to find the minimum number of removals required such that twice the minimum element in the array is greater than or equal to the maximum in the array.Examples: Input: arr[] = [4, 5, 100, 9, 10, 11, 12, 15, 200]Output: 4Explanation: In the given array 4 ele
7 min read
Check if Array with mean X can be made using N elements of given Array Given an array arr[] and two integers N and X, the task is to find if it is possible to create an array using N distinct elements from arr[] such that the mean of the newly formed array is X. Examples: Input: N = 5, X = 8, arr[] = {1, 10, 3, 2, 6, 7, 4, 5}Output: YESExplanation: Many arrays using 5
6 min read
Minimize maximum array element possible by at most K splits on the given array Given an array arr[] consisting of N positive integers and a positive integer K, the task is to minimize the maximum element present in the array by splitting at most K array elements into two numbers equal to their value. Examples: Input: arr[] = {2, 4, 8, 2}, K = 4Output: 2Explanation:Following se
9 min read
Minimum element whose n-th power is greater than product of an array of size n Given an array of n integers. Find minimum x which is to be assigned to every array element such that product of all elements of this new array is strictly greater than product of all elements of the initial array. Examples: Input: 4 2 1 10 6 Output: 4 Explanation: Product of elements of initialarra
8 min read
Minimize operations to make minimum value of one array greater than maximum value of the other Given two arrays A[] and B[] consisting of N and M integers, the task is to find the minimum number of operations required to make the minimum element of the array A[] at least the maximum element of the array B[] such that in each operation any array element A[] can be incremented by 1 or any array
6 min read
Maximize the minimum element of Array by reducing elements one by one Given an array arr[] containing N integers. In each operation, a minimum integer is chosen from the array and deleted from the array after subtracting it from the remaining elements. The task is to find the maximum of minimum values of the array after any number of such operations. Examples: Input:
6 min read
Minimum value to be assigned to the elements so that sum becomes greater than initial sum Given an array arr[] of N elements, the task is to update all the elements of the given array to some value X such that the sum of all the updated array elements is strictly greater than the sum of all the elements of the initial array and X is the minimum possible. Examples: Input: arr[] = {4, 2, 1
4 min read
Minimum count of elements to be inserted in Array to form all values in [1, K] using subset sum Given a sorted array arr[] consisting of N integers, and an integer K, the task is to find the minimum number of elements to be inserted in the array such that any value in the range [1, K] can be formed by adding elements of any subset of the modified array. Examples: Input: arr[] = {1, 3}, K = 6Ou
7 min read