Given two integers n and m. Find the longest contiguous subset in binary representation of both the numbers and its decimal value.
Input : n = 10, m = 11 Output : 5 Explanation : Binary representation of 10 -> 1010 11 -> 1011 longest common substring in both is 101 and decimal value of 101 is 5
Input : n = 8, m = 16 Output : 8 Explanation : Binary representation of 8 -> 1000 16 -> 10000 longest common substring in both is 1000 and decimal value of 1000 is 8
Input : n = 0, m = 8 Output : 9 Explanation : Binary representation of 0 -> 0 8 -> 1000 longest common substring in both is 0 and decimal value of 0 is 0
We convert given numbers to their binary representations and store binary representations in two strings. Once we get strings, we find the longest common substring by trying all length substrings starting from maximum possible length.
longest common decimal value : 5
Optimizations to above approach:
The above solution can be optimized by methods discussed in below posts:
Dynamic Programming | Set 29 (Longest Common Substring)
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- SequenceMatcher in Python for Longest Common Substring
- Longest Common Substring (Space optimized DP solution)
- Print the longest common substring
- Longest Common Substring | DP-29
- Longest Common Substring in an Array of Strings
- Find longest sequence of 1's in binary representation with one flip
- Length of the Longest Consecutive 1s in Binary Representation
- Check if two strings have a common substring
- Longest Common Prefix using Binary Search
- Longest substring with K unique characters using Binary Search
- Longest substring consisting of vowels using Binary Search
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Longest substring of vowels with no two adjacent alphabets same
- Maximum 0's between two immediate 1's in binary representation
- Maximum distance between two 1's in Binary representation of N
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Count numbers have all 1s together in binary representation
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring