Maximize array sum by X increments when each element is divided by 10
Last Updated :
24 Dec, 2021
Given an array arr[] consisting of N non-negative elements and an integer X, the task is to make X increments such that the value of array sum when each element is divided by 10, i.e.
is maximized. Print the maximum value of
possible.
Note: The value of any element can’t be increased beyond 1000.
Examples:
Input: N = 4, X = 6, arr[] = {4, 8, 8, 8}
Output: 3
Explanation:
Convert the given array to {4, 10, 10, 10} by incrementing arr[1], arr[2] and arr[3] twice each.
Now
is 0 + 1 + 1 + 1 = 3.
Input: N = 3, X = 122, arr[] = {3, 11, 14}
Output: 15
Approach:
- For all the elements, calculate the number of increments required to increase the number to the next multiple of 10 and store these values in an array, say V.
- Calculate the maximum number of times that an element can be incremented by 10 and keep its value <= 1000 and add this value to a variable, say increments which is initialized to 0.
- Sort the array V to make it non-decreasing.
- Then for each value in V, perform the required moves, and increase some element to the next multiple of 10, this increases the answer by 1.
- Do this, while the total moves performed, do not exceed X.
- After going through all elements of V if still some moves are remaining then add to the answer minimum between increments and (remaining moves)/10 .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximizeval10( int a[],
int n, int k)
{
int increments = 0;
int ans = 0;
vector< int > v;
for ( int i = 0; i < n; i++) {
ans += (a[i] / 10);
if (a[i] == 1000)
continue ;
else {
v.push_back(10 - a[i] % 10);
increments += (100
- ((a[i]) / 10)
- 1);
}
}
sort(v.begin(), v.end());
int sum = 0;
for ( int i = 0; i < v.size();
i++) {
sum += v[i];
if (sum <= k) {
ans++;
}
else
break ;
}
if (sum < k) {
int remaining = k - sum;
ans += min(increments,
remaining / 10);
}
cout << ans;
}
int main()
{
int N = 4;
int X = 6;
int A[N] = { 4, 8, 8, 8 };
maximizeval10(A, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void maximizeval10( int [] a, int n,
int k)
{
int increments = 0 ;
int ans = 0 ;
Vector<Integer> v = new Vector<>();
for ( int i = 0 ; i < n; i++)
{
ans += (a[i] / 10 );
if (a[i] == 1000 )
continue ;
else
{
v.add( 10 - a[i] % 10 );
increments += ( 100 - ((a[i]) /
10 ) - 1 );
}
}
Collections.sort(v);
int sum = 0 ;
for ( int i = 0 ; i < v.size(); i++)
{
sum += v.get(i);
if (sum <= k)
{
ans++;
}
else
break ;
}
if (sum < k)
{
int remaining = k - sum;
ans += Math.min(increments,
remaining / 10 );
}
System.out.print(ans);
}
public static void main(String[] args)
{
int N = 4 ;
int X = 6 ;
int A[] = { 4 , 8 , 8 , 8 };
maximizeval10(A, N, X);
}
}
|
Python3
def maximizeval10(a, n, k):
increments = 0
ans = 0
v = []
for i in range (n):
ans + = (a[i] / / 10 )
if (a[i] = = 1000 ):
continue
else :
v.append( 10 - a[i] % 10 )
increments + = ( 100 - ((a[i]) / /
10 ) - 1 );
v.sort()
sum = 0
for i in range ( len (v)):
sum + = v[i]
if ( sum < = k):
ans + = 1
else :
break
if ( sum < k):
remaining = k - sum
ans + = min (increments,
remaining / / 10 )
print (ans)
if __name__ = = "__main__" :
N = 4
X = 6
A = [ 4 , 8 , 8 , 8 ]
maximizeval10(A, N, X)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void maximizeval10( int [] a,
int n,
int k)
{
int increments = 0;
int ans = 0;
List< int > v = new List< int >();
for ( int i = 0; i < n; i++)
{
ans += (a[i] / 10);
if (a[i] == 1000)
continue ;
else
{
v.Add(10 - a[i] % 10);
increments += (100 - ((a[i]) /
10) - 1);
}
}
v.Sort();
int sum = 0;
for ( int i = 0; i < v.Count; i++)
{
sum += v[i];
if (sum <= k)
{
ans++;
}
else
break ;
}
if (sum < k)
{
int remaining = k - sum;
ans += Math.Min(increments,
remaining / 10);
}
Console.Write(ans);
}
public static void Main(String[] args)
{
int N = 4;
int X = 6;
int []A = {4, 8, 8, 8};
maximizeval10(A, N, X);
}
}
|
Javascript
<script>
function maximizeval10(a,n,k)
{
let increments = 0;
let ans = 0;
let v = [];
for (let i = 0; i < n; i++)
{
ans += Math.floor(a[i] / 10);
if (a[i] == 1000)
continue ;
else
{
v.push(10 - a[i] % 10);
increments += (100 - (Math.floor(a[i]) / 10) - 1);
}
}
v.sort( function (a,b){ return a-b;});
let sum = 0;
for (let i = 0; i < v.length; i++)
{
sum += v[i];
if (sum <= k)
{
ans++;
}
else
break ;
}
if (sum < k)
{
let remaining = k - sum;
ans += Math.min(increments,
Math.floor(remaining / 10));
}
document.write(ans);
}
let N = 4;
let X = 6;
let A=[4, 8, 8, 8];
maximizeval10(A, N, X);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space complexity: O(N)
Share your thoughts in the comments
Please Login to comment...