Minimum number of sets with numbers less than Y
Given a string of consecutive digits and a number Y, the task is to find the number of minimum sets such that every set follows the below rule:
- Set should contain consecutive numbers
- No digit can be used more than once.
- The number in the set should not be more than Y.
Examples:
Input: s = "1234", Y = 30
Output: 3
Three sets of {12, 3, 4}
Input: s = "1234", Y = 4
Output: 4
Four sets of {1}, {2}, {3}, {4}
Approach: The following problem can be solved using a greedy approach whose steps are given below:
- Iterate in the string, and concatenate the number to a variable(let say num) using num*10 + (s[i]-‘0’)
- If the number is not greater than Y, then mark f = 1.
- If the number exceeds Y, then increase count if f = 1 and re-initialize f as 0 and also initialise num as s[i]-‘0’ or num as 0.
- After iterating in the string completely, then increase count if f is 1.
Below is the implementation of the above approach:
C++
// C++ program to find the minimum number // sets with consecutive numbers and less than Y #include <bits/stdc++.h> using namespace std; // Function to find the minimum number of shets int minimumSets(string s, int y) { // Variable to count // the number of sets int cnt = 0; int num = 0; int l = s.length(); int f = 0; // Iterate in the string for (int i = 0; i < l; i++) { // Add the number to string num = num * 10 + (s[i] - '0'); // Mark that we got a number if (num <= y) f = 1; else // Every time it exceeds { // Check if previous was // anytime less than Y if (f) cnt += 1; // Current number num = s[i] - '0'; f = 0; // Check for current number if (num <= y) f = 1; else num = 0; } } // Check for last added number if (f) cnt += 1; return cnt; } // Driver Code int main() { string s = "1234"; int y = 30; cout << minimumSets(s, y); return 0; } |
chevron_right
filter_none
Java
// Java program to find the minimum number // sets with consecutive numbers and less than Y import java.util.*; class solution { // Function to find the minimum number of shets static int minimumSets(String s, int y) { // Variable to count // the number of sets int cnt = 0; int num = 0; int l = s.length(); boolean f = false; // Iterate in the string for (int i = 0; i < l; i++) { // Add the number to string num = num * 10 + (s.charAt(i) - '0'); // Mark that we got a number if (num <= y) f = true; else // Every time it exceeds { // Check if previous was // anytime less than Y if (f) cnt += 1; // Current number num = s.charAt(i) - '0'; f = false; // Check for current number if (num <= y) f = true; else num = 0; } } // Check for last added number if (f == true) cnt += 1; return cnt; } // Driver Code public static void main(String args[]) { String s = "1234"; int y = 30; System.out.println(minimumSets(s, y)); } } // This code is contributed by // Shashank_Sharma |
chevron_right
filter_none
Python3
# Python3 program to find the minimum number # sets with consecutive numbers and less than Y import math as mt # Function to find the minimum number of shets def minimumSets(s, y): # Variable to count the number of sets cnt = 0 num = 0 l = len(s) f = 0 # Iterate in the string for i in range(l): # Add the number to string num = num * 10 + (ord(s[i]) - ord('0')) # Mark that we got a number if (num <= y): f = 1 else: # Every time it exceeds # Check if previous was anytime # less than Y if (f): cnt += 1 # Current number num = ord(s[i]) - ord('0') f = 0 # Check for current number if (num <= y): f = 1 else: num = 0 # Check for last added number if (f): cnt += 1 return cnt # Driver Code s = "1234"y = 30print(minimumSets(s, y)) # This code is contributed by # Mohit kumar 29 |
chevron_right
filter_none
C#
// C# program to find the minimum number // sets with consecutive numbers and less than Y using System; class solution { // Function to find the minimum number of shets static int minimumSets(string s, int y) { // Variable to count // the number of sets int cnt = 0; int num = 0; int l = s.Length ; bool f = false; // Iterate in the string for (int i = 0; i < l; i++) { // Add the number to string num = num * 10 + (s[i] - '0'); // Mark that we got a number if (num <= y) f = true; else // Every time it exceeds { // Check if previous was // anytime less than Y if (f) cnt += 1; // Current number num = s[i] - '0'; f = false; // Check for current number if (num <= y) f = true; else num = 0; } } // Check for last added number if (f == true) cnt += 1; return cnt; } // Driver Code public static void Main() { string s = "1234"; int y = 30; Console.WriteLine(minimumSets(s, y)); } // This code is contributed by Ryuga } |
chevron_right
filter_none
PHP
<?php // PHP program to find the minimum number // sets with consecutive numbers and less than Y // Function to find the minimum number of shets function minimumSets($s, $y) { // Variable to count // the number of sets $cnt = 0; $num = 0; $l = strlen($s); $f = 0; // Iterate in the string for ($i = 0; $i < $l; $i++) { // Add the number to string $num = $num * 10 + ($s[$i] - '0'); // Mark that we got a number if ($num <= $y) $f = 1; else // Every time it exceeds { // Check if previous was // anytime less than Y if ($f) $cnt += 1; // Current number $num = $s[$i] - '0'; $f = 0; // Check for current number if ($num <= $y) $f = 1; else $num = 0; } } // Check for last added number if ($f) $cnt += 1; return $cnt; } // Driver Code $s = "1234"; $y = 30; echo(minimumSets($s, $y)); //This code is cntributed by Shivi_Aggarwal ?> |
chevron_right
filter_none
Output:
3
Recommended Posts:
- Possible two sets from first N natural numbers difference of sums as D
- Represent a number as sum of minimum possible psuedobinary numbers
- Minimum numbers (smaller than or equal to N) with sum S
- Minimum multiplications with {2, 3, 7} to make two numbers equal
- Minimum numbers needed to express every integer below N as a sum
- Minimum sum of two numbers formed from digits of an array
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Find minimum number to be divided to make a number a perfect square
- Maximum and minimum sums from two numbers with digit replacements
- Pair of prime numbers with a given sum and minimum absolute difference
- Find the minimum difference between Shifted tables of two numbers
- Minimum number of given powers of 2 required to represent a number
- Convert a number m to n using minimum number of given operations
- Minimum number of squares whose sum equals to given number n
- Cartesian Product of Two Sets
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.



