Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
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*/ |
16
Recommended Posts:
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array in O(n)
- Minimum number of consecutive sequences that can be formed in an array
- Maximum number formed from array with K number of adjacent swaps allowed
- Maximum subarray sum in array formed by repeating the given array k times
- Minimum and Maximum prime numbers in an array
- Find minimum value to assign all array elements so that array product becomes greater
- Maximum and minimum of an array using minimum number of comparisons
- Minimum and Maximum element of an array which is divisible by a given number k
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum removals from array to make GCD greater
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Minimum element whose n-th power is greater than product of an array of size n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



