给定一个由N 个正整数组成的数组arr[] ,任务是检查每个数组元素是否可以用数字4和5 表示。如果可能,则打印每个数组元素所需的4和5的总数。否则,打印-1。
例子:
Input: arr[] = {12, 11, 9}
Output: 3 -1 2
Explanation:
- arr[0]( =12): Print 3 as it can be represented by using 3 fours i.e. 4 + 4 + 4 = 12.
- arr[1](= 11): Print -1 as it cannot be represented by any combination of 4 and 5.
- arr[2](= 9): Print 2 as it can be represented by using one 4 and one 5 i.e. 4 + 5 = 9.
Input: arr[] = {7, 15, 17, 22}
Output: -1 3 4 5
方法:这个问题可以使用贪心方法和一点数学来解决。请按照以下步骤解决问题:
- 初始化一个向量,比如ans,为{-1} ,其中ans[i]存储数组arr[]的值arr[i]的可能答案的答案。
- 使用变量i在范围[0, N-1] 上迭代并执行以下步骤:
- 如果arr[i]小于4,则继续。
- 初始化两个变量,比如sum为INT_MAX以存储形成arr[i]和cnt所需的4和5的数字计数为0,以保持当前因子的计数为4。
- 使用变量j在范围[0, arr[i]] 上迭代并执行以下步骤:
- 如果(arr[i] – j)可被5整除,则将sum的值设为sum和(cnt + (arr[i] – j)/5)的最小值。
- 将cnt的值增加1并将j的值增加4 。
- 如果sum不等于INT_MAX,则将向量ans中ans[i]的值设为sum。
- 最后,完成上述步骤后,打印向量ans 中的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the count of the
// combination of 4 or 5 required to
// make the arr[i] for each 0 < i < N
void sumOfCombinationOf4OR5(vector arr, int N)
{
// Vector to store the answer
vector ans(N, -1);
// Iterate in the range[0, N-1]
for (int i = 0; i < N; i++) {
if (arr[i] < 4) {
continue;
}
// Initialize sum to store the count
// of numbers and cnt for the current
// factor of 4
int sum = INT_MAX, cnt = 0;
// Iterate in the range[0, arr[i]] with
// increment of 4
for (int j = 0; j <= arr[i]; j += 4) {
// Check if arr[i] - j(the current factor
// of 4) is divisible by 5 or not
if ((arr[i] - j) % 5 == 0) {
sum = min(sum, cnt + (arr[i] - j) / 5);
}
cnt++;
}
// If sum is not maximum
// then answer is found
if (sum != INT_MAX)
ans[i] = sum;
}
// Finally, print the required answer
for (auto num : ans)
cout << num << " ";
}
// Driver Code
int main()
{
// Given Input
vector arr = { 7, 15, 17, 22 };
int N = arr.size();
// Function Call
sumOfCombinationOf4OR5(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to print the count of the
// combination of 4 or 5 required to
// make the arr[i] for each 0 < i < N
static void sumOfCombinationOf4OR5(int[] arr, int N)
{
// Vector to store the answer
int[] ans = new int[N];
Arrays.fill(ans, -1);
// Iterate in the range[0, N-1]
for (int i = 0; i < N; i++) {
if (arr[i] < 4) {
continue;
}
// Initialize sum to store the count
// of numbers and cnt for the current
// factor of 4
int sum = Integer.MAX_VALUE;
int cnt = 0;
// Iterate in the range[0, arr[i]] with
// increment of 4
for (int j = 0; j <= arr[i]; j += 4) {
// Check if arr[i] - j(the current factor
// of 4) is divisible by 5 or not
if ((arr[i] - j) % 5 == 0) {
sum = Math.min(sum,
cnt + (arr[i] - j) / 5);
}
cnt++;
}
// If sum is not maximum
// then answer is found
if (sum != Integer.MAX_VALUE)
ans[i] = sum;
}
// Finally, print the required answer
for (int num : ans)
System.out.printf(num + " ");
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int[] arr = { 7, 15, 17, 22 };
int N = arr.length;
// Function Call
sumOfCombinationOf4OR5(arr, N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to print the count of the
# combination of 4 or 5 required to
# make the arr[i] for each 0 < i < N
def sumOfCombinationOf4OR5(arr, N):
# Vector to store the answer
ans = [-1 for i in range(N)]
# Iterate in the range[0, N-1]
for i in range(N):
if (arr[i] < 4):
continue
# Initialize sum to store the count
# of numbers and cnt for the current
# factor of 4
sum = 10**9
cnt = 0
# Iterate in the range[0, arr[i]] with
# increment of 4
for j in range(0, arr[i] + 1, 4):
# Check if arr[i] - j(the current factor
# of 4) is divisible by 5 or not
if ((arr[i] - j) % 5 == 0):
sum = min(sum, cnt + (arr[i] - j) // 5)
cnt += 1
# If sum is not maximum
# then answer is found
if (sum != 10**9):
ans[i] = sum
# Finally, print the required answer
for num in ans:
print(num, end = " ")
# Driver Code
# Given Input
arr = [ 7, 15, 17, 22 ]
N = len(arr)
# Function Call
sumOfCombinationOf4OR5(arr, N)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print the count of the
// combination of 4 or 5 required to
// make the arr[i] for each 0 < i < N
static void sumOfCombinationOf4OR5(int []arr, int N)
{
// Vector to store the answer
int []ans = new int[N];
for(int i = 0; i < N; i++)
ans[i] = -1;
// Iterate in the range[0, N-1]
for (int i = 0; i < N; i++) {
if (arr[i] < 4) {
continue;
}
// Initialize sum to store the count
// of numbers and cnt for the current
// factor of 4
int sum = Int32.MaxValue, cnt = 0;
// Iterate in the range[0, arr[i]] with
// increment of 4
for (int j = 0; j <= arr[i]; j += 4) {
// Check if arr[i] - j(the current factor
// of 4) is divisible by 5 or not
if ((arr[i] - j) % 5 == 0) {
sum = Math.Min(sum, cnt + (int)(arr[i] - j) / 5);
}
cnt++;
}
// If sum is not maximum
// then answer is found
if (sum != Int32.MaxValue)
ans[i] = sum;
}
// Finally, print the required answer
foreach(int num in ans)
Console.Write(num + " ");
}
// Driver Code
public static void Main()
{
// Given Input
int []arr = {7, 15, 17, 22 };
int N = arr.Length;
// Function Call
sumOfCombinationOf4OR5(arr, N);
}
}
// This code is contributed by ipg2016107.
Javascript
// This code is contributed by _saurabh_jaiswal.
输出
-1 3 4 5
时间复杂度: O(N*M),其中M是数组arr[]的最大元素。
辅助空间: O(N)
注意:上述方法可以在空间复杂度方面进一步优化,因为在上述方法中打印前存储结果是可选的。此后,空间复杂度将优化为 O(1)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。