📌  相关文章
📜  N的二进制表示形式中两个1之间的最大距离

📅  最后修改于: 2021-05-04 13:06:11             🧑  作者: Mango

给定数字N,任务是在给定N的二进制表示形式中找到两个1之间的最大距离。如果二进制表示形式包含少于两个1,则打印-1。

例子:

Input: N = 131
Output: 6
131 in binary = 10000011.
The maximum distance between two 1's = 6.

Input: N = 8
Output: -1
8 in binary = 01000.
It contains less than two 1's.

方法:

  • 首先找到N的二进制表示形式。
  • 对于计算出的每个位,检查其是否为“ 1”。
  • 存储在first_1中找到的第一个“ 1”和在last_1中找到的最后一个“ 1”的索引
  • 然后检查last_1是否小于或等于first_1。 N是2的幂的情况将是这种情况。因此,在这种情况下打印-1。
  • 在任何其他情况下,请找到last_1和first_1之间的差异。这将是所需的距离。

下面是上述方法的实现:

C++
// C++ program to find the
// Maximum distance between two 1's
// in Binary representation of N
  
#include 
using namespace std;
  
int longest_gap(int N)
{
  
    int distance = 0, count = 0,
        first_1 = -1, last_1 = -1;
  
    // Compute the binary representation
    while (N) {
  
        count++;
  
        int r = N & 1;
  
        if (r == 1) {
            first_1 = first_1 == -1
                          ? count
                          : first_1;
            last_1 = count;
        }
  
        N = N / 2;
    }
  
    // if N is a power of 2
    // then return -1
    if (last_1 <= first_1) {
        return -1;
    }
    // else find the distance
    // between the first position of 1
    // and last position of 1
    else {
        distance = (last_1 - first_1 - 1);
        return distance;
    }
}
  
// Driver code
int main()
{
    int N = 131;
    cout << longest_gap(N) << endl;
  
    N = 8;
    cout << longest_gap(N) << endl;
  
    N = 17;
    cout << longest_gap(N) << endl;
  
    N = 33;
    cout << longest_gap(N) << endl;
  
    return 0;
}


Java
// Java program to find the 
// Maximum distance between two 1's 
// in Binary representation of N 
class GFG
{
    static int longest_gap(int N) 
    { 
        int distance = 0, count = 0, 
            first_1 = -1, last_1 = -1; 
      
        // Compute the binary representation 
        while (N != 0) 
        { 
            count++; 
      
            int r = N & 1; 
      
            if (r == 1) 
            { 
                first_1 = first_1 == -1 ? 
                                  count : first_1; 
                last_1 = count; 
            } 
            N = N / 2; 
        } 
      
        // if N is a power of 2 
        // then return -1 
        if (last_1 <= first_1) 
        { 
            return -1; 
        } 
          
        // else find the distance 
        // between the first position of 1 
        // and last position of 1 
        else
        { 
            distance = (last_1 - first_1 - 1); 
            return distance; 
        } 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int N = 131; 
        System.out.println(longest_gap(N)); 
      
        N = 8; 
        System.out.println(longest_gap(N)); 
      
        N = 17; 
        System.out.println(longest_gap(N)); 
      
        N = 33; 
        System.out.println(longest_gap(N)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to find the
# Maximum distance between two 1's
# in Binary representation of N
def longest_gap(N):
  
    distance = 0
    count = 0
    first_1 = -1
    last_1 = -1
  
    # Compute the binary representation
    while (N > 0):
        count += 1
  
        r = N & 1
  
        if (r == 1):
            if first_1 == -1:
                first_1 = count
            else:
                first_1 = first_1
  
            last_1 = count
  
        N = N // 2
  
    # if N is a power of 2
    # then return -1
    if (last_1 <= first_1):
        return -1
          
    # else find the distance
    # between the first position of 1
    # and last position of 1
    else:
        distance = last_1 - first_1 - 1
        return distance
  
# Driver code
N = 131
print(longest_gap(N))
  
N = 8
print(longest_gap(N))
  
N = 17
print(longest_gap(N))
  
N = 33
print(longest_gap(N))
  
# This code is contributed by Mohit Kumar


C#
// C# program to find the 
// Maximum distance between two 1's 
// in Binary representation of N 
using System;
  
class GFG
{
    static int longest_gap(int N) 
    { 
        int distance = 0, count = 0, 
            first_1 = -1, last_1 = -1; 
      
        // Compute the binary representation 
        while (N != 0) 
        { 
            count++; 
      
            int r = N & 1; 
      
            if (r == 1) 
            { 
                first_1 = first_1 == -1 ? 
                                  count : first_1; 
                last_1 = count; 
            } 
            N = N / 2; 
        } 
      
        // if N is a power of 2 
        // then return -1 
        if (last_1 <= first_1) 
        { 
            return -1; 
        } 
          
        // else find the distance 
        // between the first position of 1 
        // and last position of 1 
        else
        { 
            distance = (last_1 - first_1 - 1); 
            return distance; 
        } 
    } 
      
    // Driver code 
    public static void Main (String []args) 
    { 
        int N = 131; 
        Console.WriteLine(longest_gap(N)); 
      
        N = 8; 
        Console.WriteLine(longest_gap(N)); 
      
        N = 17; 
        Console.WriteLine(longest_gap(N)); 
      
        N = 33; 
        Console.WriteLine(longest_gap(N)); 
    } 
}
  
// This code is contributed by Arnab Kundu


输出:
6
-1
3
4