Given an array arr[] and an integer S, the task is to choose maximum count of numbers from the array such that sum of numbers is less than S and form the smallest possible number using their indices
Note: Any element can be choosen any number of times.
Examples:
Input: arr[] = {3, 4, 2, 4, 6, 5, 4, 2, 3}, S = 13
Output: 133333
Explanation:
Elements chosen – 3 + 2 + 2 + 2 + 2 + 2 = 13
Therefore, Concatenation of indices – 133333Input: arr[] = {18, 21, 22, 51, 13, 14, 17, 15, 17}, S = 50
Output: 115
Approach: The idea is to find the maximum count of the elements that can be chosen which can be computed for the number using
. Finally, the minimum indices that can be choose multiple times is computed by taking the minimum digit in the number for each digit place.
Below is the implementation of the above approach:
C++
// C++ implementation to find // minimum number which // have a maximum length #include <bits/stdc++.h> using namespace std; // Function to find the // minimum number which // have maximum length string max_number(int arr[], int sum) { int frac[9]; int maxi = INT_MIN; string ans; int pos; // Find Maximum length // of number for (int i = 0; i < 9; i++) { frac[i] = sum / arr[i]; if (frac[i] > maxi) { pos = i; maxi = frac[i]; } } ans.insert(0, string(maxi, (pos + 1) + '0')); sum -= maxi * arr[pos]; // Find minimum number WHich // have maximum length for (int i = 0; i < maxi; i++) { for (int j = 1; j <= 9; j++) { if (sum + arr[pos] - arr[j - 1] >= 0) { ans[i] = (j + '0'); sum += arr[pos] - arr[j - 1]; break; } } } if (maxi == 0) { return 0; } else { return ans; } } // Driver Code int main() { int arr[9] = { 3, 4, 2, 4, 6, 5, 4, 2, 3 }; int s = 13; cout << max_number(arr, s); return 0; } |
Java
// Java implementation to find // minimum number which // have a maximum length class GFG{ // Function to find the // minimum number which // have maximum length static String max_number(int arr[], int sum) { int frac[] = new int[9]; int maxi = Integer.MIN_VALUE; StringBuilder ans = new StringBuilder(); int pos = 0; // Find Maximum length // of number for(int i = 0; i < 9; i++) { frac[i] = sum / arr[i]; if (frac[i] > maxi) { pos = i; maxi = frac[i]; } } for(int i = 0; i < maxi; i++) { ans.append((char)((pos + 1) + '0')); } sum -= maxi * arr[pos]; // Find minimum number WHich // have maximum length for(int i = 0; i < maxi; i++) { for(int j = 1; j <= 9; j++) { if (sum + arr[pos] - arr[j - 1] >= 0) { ans.setCharAt(i, (char)(j + '0')); sum += arr[pos] - arr[j - 1]; break; } } } if (maxi == 0) { return "0"; } else { return ans.toString(); } } // Driver Code public static void main(String str[]) { int arr[] = { 3, 4, 2, 4, 6, 5, 4, 2, 3 }; int s = 13; System.out.println(max_number(arr, s)); } } // This code is contributed by rutvik_56 |
133333
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Arrange given numbers to form the smallest number
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Number of valid indices in the permutation of first N natural numbers
- Form the smallest number using at most one swap operation
- Probability for three randomly chosen numbers to be in AP
- Lexicographically smallest string which differs from given strings at exactly K indices
- Find sum of f(s) for all the chosen sets from the given array
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Smallest number divisible by first n numbers
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Arrange given numbers to form the biggest number | Set 2
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Smallest n digit number divisible by given three numbers
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Probability of getting a perfect square when a random number is chosen in a given range
- Smallest prime number missing in an array
- Find the GCD of N Fibonacci Numbers with given Indices
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

