Minimize sum of given array by removing all occurrences of a single digit | C ++ program for super ugly number ; Function to remove each digit from the given integer ; Convert into string ; Stores final string ; Traverse the string ; Append it to the final string ; Return integer value ; Function to find the minimum sum by removing occurences of each digit ; Iterate in range [ 0 , 9 ] ; Traverse the array ; Update the minimum sum ; Print the minimized sum ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE 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 ; }