Given the count of digits 1, 2, 3, 4. Using these digits you are allowed to only form numbers 234 and 12. The task is to find the maximum possible sum that can be obtained after forming the numbers.
Note: The aim is only to maximize the sum, even if some of the digits left unused.
Input : c1 = 5, c2 = 2, c3 = 3, c4 = 4 Output : 468 Explanation : We can form two 234s Input : c1 = 5, c2 = 3, c3 = 1, c4 = 5 Output : 258 Explanation : We can form one 234 and two 12s
Approach : An efficient approach is to first try to make 234’s. The possible number of 234s are minimum of c2, c3, c4. After this, with remaining 1’s and 2’s try to form 12s.
Below is the implementation of the above approach :
// C# program to maximum possible sum
// Function to find the maximum possible sum
static int Maxsum(int c1, int c2, int c3, int c4)
// To store required sum
int sum = 0;
// Number of 234’s can be formed
int two34 = Math.Min(c2, Math.Min(c3, c4));
// Sum obtained with 234s
sum = two34 * 234;
// Remaining 2’s
c2 -= two34;
// Sum obtained with 12s
sum +=Math.Min(c2, c1) * 12;
// Return the requied sum
// Driver code
public static void Main()
int c1 = 5, c2 = 2, c3 = 3, c4 = 4;
Console.WriteLine(Maxsum(c1, c2, c3, c4));
// This code is contributed
// by Akanksha Rai
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find maximum product of digits among numbers less than or equal to N
- Find the number in a range having maximum product of the digits
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the count of maximum contiguous Even numbers
- Find maximum number that can be formed using digits of a given number
- Find count of digits in a number that divide the number
- Count digits in a factorial | Set 1
- Count digits in a factorial | Set 2
- Count numbers with same first and last digits
- Maximum possible time that can be formed from four digits
- Count of numbers from range [L, R] whose sum of digits is Y
- Count total number of digits from 1 to n
- Count digits in given number N which divide N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.