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++
#include <bits/stdc++.h>
using namespace std;
int findNumber( int arr[], int n)
{
sort(arr, arr + n);
int max = arr[n - 1];
int table[(2 * max) + 1];
table[0] = 0;
for ( int i = 1; i < (2 * max) + 1; i++)
table[i] = INT_MAX;
int ans = -1;
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 (i > arr[n - 1] && table[i] == INT_MAX) {
ans = i;
break ;
}
}
return ans;
}
int main()
{
int arr[] = { 6, 7, 15 };
int n = ( sizeof (arr) / sizeof ( int ));
cout << findNumber(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int findNumber( int arr[], int n)
{
Arrays.sort(arr);
int max = arr[n - 1 ];
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 ;
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 (i > arr[n - 1 ] && table[i] == Integer.MAX_VALUE)
{
ans = i;
break ;
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 6 , 7 , 15 };
int n = arr.length;
System.out.println(findNumber(arr, n));
}
}
|
Python3
def findNumber(arr, n):
arr = sorted (arr)
Max = arr[n - 1 ]
table = [ 10 * * 9 for i in range (( 2 * Max ) + 1 )]
table[ 0 ] = 0
ans = - 1
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 (i > arr[n - 1 ] and table[i] = = 10 * * 9 ):
ans = i
break
return ans
arr = [ 6 , 7 , 15 ]
n = len (arr)
print (findNumber(arr, n))
|
C#
using System;
class GFG
{
static int findNumber( int [] arr, int n)
{
Array.Sort(arr);
int max = arr[n - 1];
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;
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 (i > arr[n - 1] && table[i] == int .MaxValue)
{
ans = i;
break ;
}
}
return ans;
}
public static void Main()
{
int [] arr = { 6, 7, 15 };
int n = arr.Length;
Console.WriteLine(findNumber(arr, n));
}
}
|
PHP
<?PHP
function findNumber( $arr , $n )
{
sort( $arr );
$max = $arr [ $n - 1];
$table = array ((2 * $max ) + 1);
$table [0] = 0;
for ( $i = 1; $i < (2 * $max ) + 1; $i ++)
$table [ $i ] = PHP_INT_MAX;
$ans = -1;
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 ( $i > $arr [ $n - 1] && $table [ $i ] == PHP_INT_MAX)
{
$ans = $i ;
break ;
}
}
return $ans ;
}
{
$arr = array (6, 7, 15 );
$n = sizeof( $arr );
echo (findNumber( $arr , $n ));
}
|
Javascript
<script>
function findNumber(arr, n)
{
arr.sort((a, b) => a - b);
var max = arr[n - 1];
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;
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 (i > arr[n - 1] &&
table[i] == Number.MAX_VALUE)
{
ans = i;
break ;
}
}
return ans;
}
var arr = [ 6, 7, 15 ];
var n = arr.length;
document.write(findNumber(arr, n));
</script>
|
Time Complexity : O(MAX*N+N*log(N))
Auxiliary Space: O(MAX)
Last Updated :
02 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...