Minimize sum of given array by removing all occurrences of a single digit
Last Updated :
22 Feb, 2023
Given an array arr[] of size N, the task is to minimize the sum by removing all the occurrences of a single digit.
Examples:
Input: arr[] = {34, 23, 85, 93}
Output: 100
Explanation: Removing the occurrences of the digit 3 from each element of the array modifies arr[] to {4, 2, 85, 9}. Therefore, minimized sum of the array = 4 + 2 + 85 + 9 = 100.
Input: arr[] = {434, 863, 342, 121}
Output: 293
Approach: The idea is to remove all occurrences of each possible digit ( [0, 9] ) one by one and calculate the sum of the array after removal of each of them. Finally, find the minimum of these sums. Follow the steps below to solve the problem:
- Initialize a variable, say minSum, to store the minimum sum and curSum to store the sum obtained after removing all occurrences of a digit.
- Iterate over the digits in the range [0, 9] and perform the following:
- Traverse the array arr[] and check for the minimum sum by removing every digit.
- After removing the digits from the string, convert the string back to an integer and add it to curSum.
- Update the value of minSum after each iteration.
- Print the value of minSum as the required answer.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int remove ( int N, int digit)
{
string strN = to_string(N);
string ans = "" ;
for ( char i:strN)
{
if ((i - '0' ) == digit)
{
continue ;
}
ans += i;
}
return stoi(ans);
}
void getMin(vector< int > arr)
{
int minSum = INT_MAX;
for ( int i = 0; i < 10; i++)
{
int curSum = 0;
for ( int num :arr)
curSum += remove (num, i);
minSum = min(minSum, curSum);
}
cout << minSum;
}
int main()
{
vector< int > arr = {34, 23, 85, 93};
getMin(arr);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int remove( int N, int digit)
{
String strN = String.valueOf(N);
String ans = "" ;
for ( char i:strN.toCharArray())
{
if ((i - '0' ) == digit)
{
continue ;
}
ans += i;
}
return Integer.parseInt(ans);
}
static void getMin( int [] arr)
{
int minSum = Integer.MAX_VALUE;
for ( int i = 0 ; i < 10 ; i++)
{
int curSum = 0 ;
for ( int num :arr)
curSum += remove(num, i);
minSum = Math.min(minSum, curSum);
}
System.out.print(minSum);
}
public static void main(String[] args)
{
int [] arr = { 34 , 23 , 85 , 93 };
getMin(arr);
}
}
|
Python3
def remove(N, digit):
strN = str (N)
ans = ''
for i in strN:
if int (i) = = digit:
continue
ans + = i
return int (ans)
def getMin(arr):
minSum = float ( 'inf' )
for i in range ( 10 ):
curSum = 0
for num in arr:
curSum + = remove(num, i)
minSum = min (minSum, curSum)
print (minSum)
arr = [ 34 , 23 , 85 , 93 ]
getMin(arr)
|
C#
using System;
public class GFG
{
static int remove( int N, int digit)
{
String strN = N.ToString();
String ans = "" ;
foreach ( char i in strN.ToCharArray())
{
if ((i - '0' ) == digit)
{
continue ;
}
ans += i;
}
return Int32.Parse(ans);
}
static void getMin( int [] arr)
{
int minSum = Int32.MaxValue;
for ( int i = 0; i < 10; i++)
{
int curSum = 0;
foreach ( int num in arr)
curSum += remove(num, i);
minSum = Math.Min(minSum, curSum);
}
Console.WriteLine(minSum);
}
static public void Main (){
int [] arr = {34, 23, 85, 93};
getMin(arr);
}
}
|
Javascript
<script>
function remove(N, digit)
{
var strN = N.toString();
var ans = "" ;
var i;
for (i=0;i<strN.length;i++){
if ((strN.charCodeAt(i) - 48) == digit)
{
continue ;
}
ans += strN[i];
}
return parseInt(ans);
}
function getMin(arr)
{
var minSum = 1000000000;
var i,j;
for (i = 0; i < 10; i++)
{
var curSum = 0;
for (j=0;j<arr.length;j++){
curSum += remove(arr[j], i);
}
minSum = Math.min(minSum, curSum);
}
document.write(minSum);
}
var arr = [34, 23, 85, 93];
getMin(arr);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...