Count the combination of 4s and/or 5s required to make each Array element 0
Last Updated :
29 Jul, 2021
Given an array arr[] consisting of N positive integers, the task is to check if each array element can be represented in terms of numbers 4 and 5. If it is possible, then print the total count of 4 and 5 needed for each array element. Otherwise, print -1.
Examples:
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
Approach: The problem can be solved using greedy approach and a bit of mathematics. Follow the steps below to solve the problem:
- Initialize a vector say ans, as {-1} where the ans[i] stores the answer to a possible answer for the value arr[i] of the array arr[].
- Iterate over a range [0, N-1] using the variable i and perform the following steps:
- If arr[i] is less than 4, then, continue.
- Initialize two variables, say sum as INT_MAX to store the count of numbers of 4 and 5 needed to form arr[i] and cnt as 0 to keep the count of the current factor of 4.
- Iterate over a range [0, arr[i]] using the variable j and perform the following steps:
- If (arr[i] – j) is divisible by 5, then, set the value of the sum to the minimum of sum and (cnt + (arr[i] – j)/5).
- Increase the value of cnt by 1 and j by 4.
- If the sum is not equal to INT_MAX, then, set the value of ans[i] in the vector ans as sum.
- Finally, after completing the above steps, print the values in the vector ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumOfCombinationOf4OR5(vector< int > arr, int N)
{
vector< int > ans(N, -1);
for ( int i = 0; i < N; i++) {
if (arr[i] < 4) {
continue ;
}
int sum = INT_MAX, cnt = 0;
for ( int j = 0; j <= arr[i]; j += 4) {
if ((arr[i] - j) % 5 == 0) {
sum = min(sum, cnt + (arr[i] - j) / 5);
}
cnt++;
}
if (sum != INT_MAX)
ans[i] = sum;
}
for ( auto num : ans)
cout << num << " " ;
}
int main()
{
vector< int > arr = { 7, 15, 17, 22 };
int N = arr.size();
sumOfCombinationOf4OR5(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static void sumOfCombinationOf4OR5( int [] arr, int N)
{
int [] ans = new int [N];
Arrays.fill(ans, - 1 );
for ( int i = 0 ; i < N; i++) {
if (arr[i] < 4 ) {
continue ;
}
int sum = Integer.MAX_VALUE;
int cnt = 0 ;
for ( int j = 0 ; j <= arr[i]; j += 4 ) {
if ((arr[i] - j) % 5 == 0 ) {
sum = Math.min(sum,
cnt + (arr[i] - j) / 5 );
}
cnt++;
}
if (sum != Integer.MAX_VALUE)
ans[i] = sum;
}
for ( int num : ans)
System.out.printf(num + " " );
}
public static void main(String[] args)
{
int [] arr = { 7 , 15 , 17 , 22 };
int N = arr.length;
sumOfCombinationOf4OR5(arr, N);
}
}
|
Python3
def sumOfCombinationOf4OR5(arr, N):
ans = [ - 1 for i in range (N)]
for i in range (N):
if (arr[i] < 4 ):
continue
sum = 10 * * 9
cnt = 0
for j in range ( 0 , arr[i] + 1 , 4 ):
if ((arr[i] - j) % 5 = = 0 ):
sum = min ( sum , cnt + (arr[i] - j) / / 5 )
cnt + = 1
if ( sum ! = 10 * * 9 ):
ans[i] = sum
for num in ans:
print (num, end = " " )
arr = [ 7 , 15 , 17 , 22 ]
N = len (arr)
sumOfCombinationOf4OR5(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void sumOfCombinationOf4OR5( int []arr, int N)
{
int []ans = new int [N];
for ( int i = 0; i < N; i++)
ans[i] = -1;
for ( int i = 0; i < N; i++) {
if (arr[i] < 4) {
continue ;
}
int sum = Int32.MaxValue, cnt = 0;
for ( int j = 0; j <= arr[i]; j += 4) {
if ((arr[i] - j) % 5 == 0) {
sum = Math.Min(sum, cnt + ( int )(arr[i] - j) / 5);
}
cnt++;
}
if (sum != Int32.MaxValue)
ans[i] = sum;
}
foreach ( int num in ans)
Console.Write(num + " " );
}
public static void Main()
{
int []arr = {7, 15, 17, 22 };
int N = arr.Length;
sumOfCombinationOf4OR5(arr, N);
}
}
|
Javascript
<script>
function sumOfCombinationOf4OR5(arr, N) {
let ans = new Array(N).fill(-1);
for (let i = 0; i < N; i++) {
if (arr[i] < 4) {
continue ;
}
let sum = Number.MAX_SAFE_INTEGER, cnt = 0;
for (let j = 0; j <= arr[i]; j += 4) {
if ((arr[i] - j) % 5 == 0) {
sum = Math.min(sum, cnt + Math.floor((arr[i] - j) / 5));
}
cnt++;
}
if (sum != Number.MAX_SAFE_INTEGER)
ans[i] = sum;
}
for (let num of ans)
document.write(num + " " );
}
let arr = [7, 15, 17, 22];
let N = arr.length;
sumOfCombinationOf4OR5(arr, N);
</script>
|
Time Complexity: O(N*M) where M is the maximum element of the array arr[].
Auxiliary Space: O(N)
Note: The above approach can be further optimized in terms of space complexity as storing the result before printing is optional in the above approach. Thereafter, the space complexity will be optimized to O(1).
Share your thoughts in the comments
Please Login to comment...