
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to get minimum sum of cards after discarding
Suppose we have five numbers in an array T. There are five cards and each card has a number written on them. The ith card has T[i] written on it. We can discard some cards and our goal is to minimize the sum of numbers written on remaining numbers. He is allowed to at most once discard two or three cards with the same number. We won't discard cards if it's impossible to choose two or three cards with the same number. We have to find the minimum sum possible.
So, if the input is like T = [7, 3, 7, 3, 20], then the output will be 26, because removing two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26.
Steps
To solve this, we will follow these steps −
n := 5 m := 0 Define an array k of size: 101 and fill with 0 for initialize i := 0, when i < n, update (increase i by 1), do: a := T[i] m := m + a (increase k[a] by 1) M := m for initialize i := 0, when i < 101, update (increase i by 1), do: if k[i] > 1, then: M := minimum of M and (m - i * (minimum of 3 and k[i])) return M
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> T){ int n = 5, m = 0, a; int k[101] = { 0 }; for (int i = 0; i < n; i++){ int a = T[i]; m += a; k[a]++; } int M = m; for (int i = 0; i < 101; i++) if (k[i] > 1){ M = min(M, m - i * (min(3, k[i]))); } return M; } int main(){ vector<int> T = { 7, 3, 7, 3, 20 }; cout << solve(T) << endl; }
Input
{ 7, 3, 7, 3, 20 }
Output
26
- Related Articles
- C++ code to find minimum stones after all operations
- C++ code to find minimum k to get more votes from students
- C++ code to count children who will get ball after each throw
- C++ program to count number of minimum coins needed to get sum k
- Find minimum possible digit sum after adding a number d in C++
- C++ code to find minimum arithmetic mean deviation
- C++ code to find position of students after coding contest
- C++ code to find minimal tiredness after meeting
- C++ code to find minimum difference between concerts durations
- C++ code to find minimum different digits to represent n
- C++ code to find minimum operations to make numbers c and d
- C++ code to find xth element after removing numbers
- C++ code to find tree height after n days
- C++ code to find minimum moves with weapons to kill enemy
- C++ code to find minimum jump to reach home by frog

Advertisements