Minimum length
Practice
3.6 (8 votes)
String
Brute force
Algorithms
String searching
Binary search
String algorithms
Problem
92% Success 2559 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are given an integer N. We call a string S good if it satisfies the following conditions:
- \(S[i] = a \text{ or } S[i] = b \text{ } \forall \text{ } 1 \leq i \leq len(S)\)
- \(S[i] \leq S[i + 1] \text{ } \forall \text{ } 1\leq i \leq len(S) - 1\). Recall that \(a < b\) according to the numbering of ASCII table.
Here \(len(S)\) denotes the length of string $$S$$. For example, the strings "\(aabb\)", "\(abb\)", "\(aaa\)", "\(bbb\)", "\(a\)" are good but strings "\(aba\)", "\(bab\)", "\(bba\)" are not.
Your task is to find the minimum length of a good string which has atleast N distinct substrings.
Notes
- A substring of a string is a contiguous subsequence of that string. For example, "hacker" is subtring of "hackerearth", but "hackr" is not.
- Two substrings \(X\) and \(Y\) are different if:
- They are of different length.
- If they are of the same length, then there is an index \(i \) such that \(X[i] \neq Y[i]\).
Input
- The first line contains a single integer T denoting the number of test cases.
- For each test case, the only line of input contains a single integer N.
Output
For each test case (in a separate line), output the minimum length of a good string which has at least N distinct substrings.
Constraints
\(1 ≤ T ≤ 5000 \\ 1 ≤ N ≤ 10^9\)
Submissions
Please login to view your submissions
Similar Problems
Points:20
4 votes
Tags:
String SearchingAlgorithmsReal worldString Algorithms
Points:20
42 votes
Tags:
Data StructuresEasySortingString Manipulationapproved
Points:20
3 votes
Tags:
String AlgorithmsReal worldAlgorithmsString Searching
Editorial