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 the number in a range having maximum product of the digits
- Find maximum product of digits among numbers less than or equal to N
- 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
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find maximum number that can be formed using digits of a given number
- Find count of digits in a number that divide the number
- Find the average of k digits from the beginning and l digits from the end of the given number
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count digits in a factorial | Set 1
- Count numbers with same first and last digits
- Count digits in a factorial | Set 2
- Maximum possible time that can be formed from four digits
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.