Longest common substring in binary representation of two numbers
Last Updated :
15 Jul, 2022
Given two integers n and m. Find the longest contiguous subset in binary representation of both the numbers and their 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 :
- substr C++
- 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
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;
}
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;
}
int longestCommon( int n, int m)
{
int mx = -INT_MAX;
string s1 = convertToBinary(n);
string s2 = convertToBinary(m);
string res;
int len = s1.length();
int l = len;
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 (res == "" )
return -1;
return getDecimal(res);
}
int main()
{
int n = 10, m = 11;
cout << "longest common decimal value : "
<< longestCommon(m, n) << endl;
return 0;
}
|
Java
public class GFG
{
static int longestCommon( int n, int m)
{
int mx = -Integer.MAX_VALUE;
String s1 = Integer.toBinaryString(n);
String s2 = Integer.toBinaryString(m);
String res = null ;
int len = s1.length();
int l = len;
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 (res == "" )
return - 1 ;
return Integer.parseInt(res, 2 );
}
public static void main(String[] args)
{
int n = 10 ;
int m = 11 ;
System.out.println( "Longest common decimal value : "
+longestCommon(m, n));
}
}
|
Python3
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
def convertToBinary(n):
return bin (n)[ 2 :]
def longestCommon(n, m):
mx = - 10 * * 9
s1 = convertToBinary(n)
s2 = convertToBinary(m)
res = ""
lenn = len (s1)
l = lenn
while (lenn > 0 ):
for i in range (l - lenn + 1 ):
temp = s1[i:lenn + 1 ]
tlenn = len (temp)
if (tlenn > mx and ( s2.find(temp) ! = - 1 )):
res = temp
mx = tlenn
lenn = lenn - 1
if (res = = ""):
return - 1
return getDecimal(res)
n = 10
m = 11
print ( "longest common decimal value : " ,
longestCommon(m, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int longestCommon( int n, int m)
{
int mx = - int .MaxValue;
String s1 = Convert.ToString(n, 2);
String s2 = Convert.ToString(m, 2);;
String res = null ;
int len = s1.Length;
int l = len;
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 (res == "" )
return -1;
return Convert.ToInt32(res, 2);
}
public static void Main(String[] args)
{
int n = 10;
int m = 11;
Console.WriteLine( "Longest common decimal value : "
+longestCommon(m, n));
}
}
|
Javascript
<script>
function longestCommon(n,m)
{
let mx = -Number.MAX_VALUE;
let s1 = (n >>> 0).toString(2);
let s2 = (m >>> 0).toString(2);
let res = null ;
let len = s1.length;
let l = len;
while (len > 0)
{
for (let i = 0; i < l - len + 1; i++)
{
let temp = s1.substring(i, i + len);
let tlen = temp.length;
if (tlen > mx && s2.includes(temp))
{
res = temp;
mx = tlen;
}
}
len = len - 1;
}
if (res == "" )
return -1;
return parseInt(res, 2);
}
let n = 10;
let m = 11;
document.write( "Longest common decimal value : "
+longestCommon(m, n));
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...