Geeksforgeeks

Error

×

Given two integers N and M. Find the longest common contiguous subset in binary representation of both the numbers and print its decimal value.

**Example 1:**

**Input: **N = 10, M = 11
**Output:** 5
**Explanation**: 10 in Binary is "1010" and
11 in Binary is "1011". The longest common
contiguous subset is "101" which has a
decimal value of 5.

â€‹**Example 2:**

**Input**: N = 8, M = 16
**Output:** 8
**Explanation**: 8 in Binary is "1000" and
16 in Binary is "10000". The longest common
contiguous subset is "1000" which has a
decimal value of 8.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **longestCommon() **which takes two integers N and M as inputs and returns the Decimal representation of the longest common contiguous subset in the Binary representation of N and M.

**Note**: If there's a tie in the length of the longest common contiguous subset, return the one with the highest decimal value among them.

**Expected Time Complexity: **O((log(max (N, M))^{3}).

**Expected Auxiliary Space: **O((log(max (N, M))^{2}).

**Constraints:**

1<=N,M<=10^{18}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Longest common substring value of two numbers

...