Given two integers n and m. Find the longest contiguous subset in binary representation of both the numbers and its decimal value.
Example 1:
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
Example 2:
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
Example 3:
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
Question Source :https://www.geeksforgeeks.org/citrix-interview-experience-set-5-campus/
Prerequisite :
1) substr C++
2) find C++
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.
C++
// CPP program to find longest contiguous // subset in binary representation of given // two numbers n and m #include <bits/stdc++.h> using namespace std; // utility function which returns // decimal value of binary representation int getDecimal(string s) { int len = s.length(); int ans = 0; int j = 0; for (int i = len - 1; i >= 0; i--) { if (s[i] == '1') ans += pow(2, j); j += 1; } return ans; } // Utility function which convert decimal // number to its binary representation string convertToBinary(int n) { string temp; while (n > 0) { int rem = n % 2; temp.push_back(48 + rem); n = n / 2; } reverse(temp.begin(), temp.end()); return temp; } // utility function to check all the // substrings and get the longest substring. int longestCommon(int n, int m) { int mx = -INT_MAX; // maximum length string s1 = convertToBinary(n); string s2 = convertToBinary(m); string res; // final resultant string int len = s1.length(); int l = len; // for every substring of s1, // check if its length is greater than // previously found string // and also it is present in string s2 while (len > 0) { for (int i = 0; i < l - len + 1; i++) { string temp = s1.substr(i, len); int tlen = temp.length(); if (tlen > mx && s2.find(temp) != string::npos) { res = temp; mx = tlen; } } len = len - 1; } // If there is no common string if (res == "") return -1; return getDecimal(res); } // driver program int main() { int n = 10, m = 11; cout << "longest common decimal value : " << longestCommon(m, n) << endl; return 0; } |
Java
// Java program to find longest contiguous // subset in binary representation of given // two numbers n and m public class GFG { // utility function to check all the // substrings and get the longest substring. static int longestCommon(int n, int m) { int mx = -Integer.MAX_VALUE; // maximum length String s1 = Integer.toBinaryString(n); String s2 = Integer.toBinaryString(m); String res = null; // final resultant string int len = s1.length(); int l = len; // for every substring of s1, // check if its length is greater than // previously found string // and also it is present in string s2 while (len > 0) { for (int i = 0; i < l - len + 1; i++) { String temp = s1.substring(i, i + len); int tlen = temp.length(); if (tlen > mx && s2.contains(temp)) { res = temp; mx = tlen; } } len = len - 1; } // If there is no common string if(res == "") return -1; return Integer.parseInt(res,2); } // driver program to test above function public static void main(String[] args) { int n = 10; int m = 11; System.out.println("Longest common decimal value : " +longestCommon(m, n)); } } // This code is Contributed by Sumit Ghosh |
Python3
# Python3 program to find longest contiguous # subset in binary representation of given # two numbers n and m # utility function which returns # decimal value of binary representation def getDecimal(s): lenn = len(s) ans = 0 j = 0 for i in range(lenn - 1, -1, -1): if (s[i] == '1'): ans += pow(2, j) j += 1 return ans # Utility function which convert decimal # number to its binary representation def convertToBinary(n): return bin(n)[2:] # utility function to check all the # substrings and get the longest substring. def longestCommon(n, m): mx = -10**9 # maximum lenngth s1 = convertToBinary(n) s2 = convertToBinary(m) #print(s1,s2) res="" # final resultant string lenn = len(s1) l = lenn # for every subof s1, # check if its lenngth is greater than # previously found string # and also it is present in s2 while (lenn > 0): for i in range(l - lenn + 1): temp = s1[i:lenn + 1] # print(temp) tlenn = len(temp) if (tlenn > mx and( s2.find(temp) != -1)): res = temp mx = tlenn lenn = lenn - 1 # If there is no common string if (res == ""): return -1 return getDecimal(res) # Driver Code n = 10m = 11print("longest common decimal value : ", longestCommon(m, n)) # This code is contributed by Mohit Kumar |
C#
// C# program to find longest contiguous // subset in binary representation of given // two numbers n and m using System; using System.Collections.Generic; class GFG { // utility function to check all the // substrings and get the longest substring. static int longestCommon(int n, int m) { int mx = -int.MaxValue; // maximum length String s1 = Convert.ToString(n, 2); String s2 = Convert.ToString(m, 2);; String res = null; // final resultant string int len = s1.Length; int l = len; // for every substring of s1, // check if its length is greater than // previously found string // and also it is present in string s2 while (len > 0) { for (int i = 0; i < l - len + 1; i++) { String temp = s1.Substring(i, len); int tlen = temp.Length; if (tlen > mx && s2.Contains(temp)) { res = temp; mx = tlen; } } len = len - 1; } // If there is no common string if(res == "") return -1; return Convert.ToInt32(res, 2); } // Driver code public static void Main(String[] args) { int n = 10; int m = 11; Console.WriteLine("Longest common decimal value : " +longestCommon(m, n)); } } // This code contributed by Rajput-Ji |
Output:
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 contribute@geeksforgeeks.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.
Recommended Posts:
- Length of the Longest Consecutive 1s in Binary Representation
- Find longest sequence of 1's in binary representation with one flip
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- 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
- Common prime factors of two numbers
- Check if binary representation of a number is palindrome
- Decimal representation of given binary string is divisible by 5 or not
- Binary representation of next number
- Binary representation of next greater number with same number of 1's and 0's
- Check if binary representation of a given number and its complement are anagram
- Binary representation of previous number
- Prime Number of Set Bits in Binary Representation | Set 2
- Decimal representation of given binary string is divisible by 10 or not
- Largest number with binary representation is m 1's and m-1 0's
- Prime Number of Set Bits in Binary Representation | Set 1
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Occurrences of a pattern in binary representation of a number

