├── 2311-longest-binary-subsequence-less-than-or-equal-to-k ├── 2311-longest-binary-subsequence-less-than-or-equal-to-k.cpp ├── NOTES.md └── README.md ├── 875. Koko_Eating_Bananas_Solution_CPP └── Koko_eating_bananas_solution.cpp ├── Algorithms ├── BLogs ├── Blog └── Binary Search.pdf ├── Bubble_Sort.cpp ├── C++ Resources ├── CONTRIBUTING.md ├── CPP Seaching Algorithms ├── Binary Search.cpp └── Linear Search.cpp ├── CSS Animations ├── CodeForces ├── Codeforces Solution ├── 41b ├── 4A_Watermelon.py ├── D_Line.cpp ├── Planet.cpp └── minimum_notation.cpp ├── Convert └── ConvertFtToMm.cpp ├── Determinant_solver.cpp ├── Flood_fill_algo.cpp ├── Gaming Projects ├── Graphs ├── BFS.cpp ├── DFS.cpp ├── Disjisktra.cpp ├── Tarjan_Algorithm.cpp ├── check_star_graph.cpp └── kruskals.cpp ├── Hashing_Techiniques ├── Double_Hashing.c ├── Linear_Probing.c ├── Quadratic_Probing.c └── hashing.c ├── Insertion_sort.cpp ├── Java Resources ├── Java ├── CONTRIBUTING.md ├── kadaneAlgorithm.java ├── queue │ ├── ArrayQueue.java │ ├── LinkedQueue.java │ └── Queue.java └── stack │ ├── ArrayStack.java │ ├── LinkedStack.java │ ├── RPN.java │ └── Stack.java ├── Laptop Price Prediction ├── Leetcode ├── Leetcode.txt ├── MaxSumNonAdjacent.cpp ├── Merge_sort.cpp ├── MyProects ├── Nth_fibonacci_using_Binet'sFormula.cpp ├── Python Resources ├── Quick_sort.cpp ├── README.md ├── React └── myapp │ ├── .gitignore │ ├── README.md │ ├── package-lock.json │ ├── package.json │ ├── public │ ├── favicon.ico │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt │ ├── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ ├── reportWebVitals.js │ └── setupTests.js │ └── yarn.lock ├── Searching-Algo ├── BFS.c ├── MergeSort.cpp ├── QuickSort.cpp ├── binarysearch.cpp ├── bubllesort.cpp ├── countSort.cpp ├── insertionSort.cpp ├── linearsearch.cpp └── selectionSort.cpp ├── Segment Tree └── segmentTree.cpp ├── Selection_Sort.cpp ├── Slide_Puzzle-Game ├── Readme.md ├── mylinkedlist.h ├── myqueue.h └── solution.cpp ├── Sorting Algos ├── BubbleSort.cpp ├── Counting_Sort.cpp ├── HeapSort.cpp ├── MergeSort.cpp ├── QuickSort.cpp ├── Selection sort.c └── ShellSort.cpp ├── Word Ladder II.cpp ├── css bubbles in cup animation └── index.html ├── lambda_in_cplusplus.cpp ├── leetcode q ├── Climbing_Stairs.cpp ├── FirstandLastPosition.java ├── Interleaving Strings.cpp ├── Subarrays with K Different Integers.cpp ├── Zig_Zag_Conversion.cpp ├── reverseNodes.cpp ├── twosumII.cpp ├── valid-sudoku.cpp └── wildcard_matching.cpp └── sort an array /2311-longest-binary-subsequence-less-than-or-equal-to-k/2311-longest-binary-subsequence-less-than-or-equal-to-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestSubsequence(string s, int k) { 4 | 5 | 6 | int ans = 0; 7 | 8 | // reverse to simulate indexing 1001010 => first one has a index of 0 but while forming 9 | // decimal number it will add 2^6 to the result. 10 | // So, reverse the string to get the actual indexing 11 | // reverse = 0101001 12 | // now the last one has a index of 6 and it will contribute 2^6, 13 | 14 | reverse(s.begin(), s.end()); 15 | 16 | for(int i = 0; i < s.size(); i++) 17 | { 18 | // add all zeroes as the previous one's add according to the one and the latter zeroes 19 | // act as leading zeroes 20 | if (s[i] == '0') ans++; 21 | 22 | // bit manipulation to check the maximum 23 | else if (i < 31) 24 | { 25 | // if k is greater, then that 1 bit counts 26 | if (k >= (1 << i)) 27 | { 28 | k -= (1 << i); 29 | ans++; 30 | } 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /2311-longest-binary-subsequence-less-than-or-equal-to-k/NOTES.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /2311-longest-binary-subsequence-less-than-or-equal-to-k/README.md: -------------------------------------------------------------------------------- 1 |
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:
6 | 7 |0
.14 |
Example 1:
15 | 16 |Input: s = "1001010", k = 5 17 | Output: 5 18 | 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. 19 | Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively. 20 | The length of this subsequence is 5, so 5 is returned. 21 |22 | 23 |
Example 2:
24 | 25 |Input: s = "00101001", k = 1 26 | Output: 6 27 | 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. 28 | The length of this subsequence is 6, so 6 is returned. 29 |30 | 31 |
32 |
Constraints:
33 | 34 |1 <= s.length <= 1000
s[i]
is either '0'
or '1'
.1 <= k <= 109