📜  使用数组中的数字无法形成的大于数组最大值的最小数字

📅  最后修改于: 2021-09-17 16:12:08             🧑  作者: Mango

给定一个整数数组arr[] ,任务是从数组中找到大于最大元素的最小数,该数不能使用数组中的数字形成(添加元素以形成其他数字)。如果不存在这样的元素,则打印-1

例子:

方法:该问题类似于最小硬币变化问题,稍作修改。首先按升序对数组进行排序,并找到将出现在数组最后一个索引处的数字的最大元素max 。检查范围内的数字(最大,2 * 最大)以获得答案。如果无法使用数组元素形成此范围内的数字,则该数字就是答案,但如果可以在此范围内形成所有数字,则不存在使用数组元素无法形成的数字。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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
 $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


输出:
16

时间复杂度: O(MAX * N)
辅助空间: O(MAX)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程