2311. Longest Binary Subsequence Less Than or Equal to K LeetCode Solution

In this guide, you will get 2311. Longest Binary Subsequence Less Than or Equal to K LeetCode Solution with the best time and space complexity. The solution to Longest Binary Subsequence Less Than or Equal to K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Binary Subsequence Less Than or Equal to K solution in C++
  4. Longest Binary Subsequence Less Than or Equal to K solution in Java
  5. Longest Binary Subsequence Less Than or Equal to K solution in Python
  6. Additional Resources
2311. Longest Binary Subsequence Less Than or Equal to K LeetCode Solution image

Problem Statement of Longest Binary Subsequence Less Than or Equal to K

You are given a binary string s and a positive integer k.
Return the length of the longest subsequence of s that makes up a binary number less than or equal to k.
Note:

The subsequence can contain leading zeroes.
The empty string is considered to be equal to 0.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Example 1:

Input: s = “1001010”, k = 5
Output: 5
Explanation: The longest subsequence of s that makes up a binary number less than or equal to 5 is “00010”, as this number is equal to 2 in decimal.
Note that “00100” and “00101” are also possible, which are equal to 4 and 5 in decimal, respectively.
The length of this subsequence is 5, so 5 is returned.

Example 2:

Input: s = “00101001”, k = 1
Output: 6
Explanation: “000001” is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
The length of this subsequence is 6, so 6 is returned.

Constraints:

1 <= s.length <= 1000
s[i] is either '0' or '1'.
1 <= k <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2311. Longest Binary Subsequence Less Than or Equal to K LeetCode Solution in C++

class Solution {
 public:
  int longestSubsequence(string s, int k) {
    int oneCount = 0;
    int num = 0;
    int pow = 1;

    // Take as many 1s as possible from the right.
    for (int i = s.length() - 1; i >= 0 && num + pow <= k; --i) {
      if (s[i] == '1') {
        ++oneCount;
        num += pow;
      }
      pow *= 2;
    }

    return ranges::count(s, '0') + oneCount;
  }
};
/* code provided by PROGIEZ */

2311. Longest Binary Subsequence Less Than or Equal to K LeetCode Solution in Java

class Solution {
  public int longestSubsequence(String s, int k) {
    int oneCount = 0;
    int num = 0;
    int pow = 1;

    // Take as many 1s as possible from the right.
    for (int i = s.length() - 1; i >= 0 && num + pow <= k; --i) {
      if (s.charAt(i) == '1') {
        ++oneCount;
        num += pow;
      }
      pow *= 2;
    }

    return (int) s.chars().filter(c -> c == '0').count() + oneCount;
  }
}
// code provided by PROGIEZ

2311. Longest Binary Subsequence Less Than or Equal to K LeetCode Solution in Python

class Solution:
  def longestSubsequence(self, s: str, k: int) -> int:
    oneCount = 0
    num = 0
    pow = 1

    # Take as many 1s as possible from the right.
    for i in reversed(range(len(s))):
      if num + pow > k:
        break
      if s[i] == '1':
        oneCount += 1
        num += pow
      pow *= 2

    return s.count('0') + oneCount
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.