
- 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
Minimum number of sets with numbers less than Y in C++
Problem statement
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.
Example
If str = “1234” and Y = 20 then answer is 3 as below sets are created −
{12} {3} and {4}
Algorithm
- Convert string to number
- 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
Example
#include <iostream> #include <string> using namespace std; int getMinSets(string str, int y) { int cnt = 0; int num = 0; int l = str.length(); int f = 0; for (int i = 0; i < l; ++i) { num = num * 10 + str[i] - 48; if (num <= y) { f = 1; continue; } if (f) { ++cnt; } num = str[i] - '0'; f = 0; if (num <= y) { f = 1; } else { num = 0; } } if (f) { ++cnt; } return cnt; } int main() { string str = "1234"; int y = 20; cout << "Minimum sets = " << getMinSets(str, y) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Minimum sets = 3
- Related Articles
- Count all the numbers less than 10^6 whose minimum prime factor is N C++
- 8 times a number $p$ is $x$ less than a number \( y \). Express this statement using literal numbers, numbers and sign of basic operations.
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Print all numbers less than N with at-most 2 unique digits in C++
- Nearest prime less than given number n C++
- Count pairs with sum as a prime number and less than n in C++
- Print all Prime Quadruplet of a number less than it in C++
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++
- C++ Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
- Find all factorial numbers less than or equal to n in C++
- Print all prime numbers less than or equal to N in C++
- Minimum numbers which is smaller than or equal to N and with sum S in C++
- Count pairs with bitwise OR less than Max in C++
- Count ordered pairs with product less than N in C++
- Count the number of words having sum of ASCII values less than and greater than k in C++

Advertisements