Eliminar un bit de un número binario para obtener el valor máximo

Dado un número binario, la tarea es eliminar exactamente un bit de tal manera que, después de eliminarlo, el número binario resultante sea el mayor de todas las opciones.
Ejemplos: 
 

Input: 110
Output: 11
    As 110 = 6 in decimal, 
    the option is to remove either 0 or 1.
    So the possible combinations are 10, 11
    The max number is 11 = 3 in decimal.  

Input:1001
Output: 101

Acercarse:
 

  1. Recorre el número binario de izquierda a derecha.
  2. Encuentre el bit 0 menos redundante, ya que este bit tendrá el menor efecto en el número binario resultante.
  3. Omita este bit o elimínelo.
  4. El resto de los bits dan el número binario de valor máximo.

A continuación se muestra la implementación del enfoque anterior:
Programa: 
 

C++

// C++ program to find next maximum binary number
// with one bit removed
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum binary number
int printMaxAfterRemoval(string s)
{
    bool flag = false;
    int n = s.length();
 
    // Traverse the binary number
    for (int i = 0; i < n; i++) {
 
        // Try finding a 0 and skip it
        if (s[i] == '0' && flag == false) {
            flag = true;
            continue;
        }
        else
            cout << s[i];
    }
}
 
// Driver code
int main()
{
 
    // Get the binary number
    string s = "1001";
 
    // Find the maximum binary number
    printMaxAfterRemoval(s);
}

Java

// Java program to find next maximum binary number
// with one bit removed
 
import java.io.*;
 
class GFG {
  
// Function to find the maximum binary number
static int printMaxAfterRemoval(String s)
{
    boolean flag = false;
    int n = s.length();
 
    // Traverse the binary number
    for (int i = 0; i < n; i++) {
 
        // Try finding a 0 and skip it
        if (s.charAt(i) == '0' && flag == false) {
            flag = true;
            continue;
        }
        else
            System.out.print( s.charAt(i));
    }
  return 0;
}
 
// Driver code
    public static void main (String[] args) {
            // Get the binary number
    String s = "1001";
 
    // Find the maximum binary number
    printMaxAfterRemoval(s);
    }
}
// This code is contributed by anuj_67..

Python3

# Python3 program to find next maximum 
# binary number with one bit removed
 
# Function to find the maximum
# binary number
def printMaxAfterRemoval(s):
 
    flag = False
    n = len(s)
 
    # Traverse the binary number
    for i in range(0, n):
 
        # Try finding a 0 and skip it
        if s[i] == '0' and flag == False:
            flag = True
            continue
         
        else:
            print(s[i], end = "")
 
# Driver code
if __name__ == "__main__":
 
    # Get the binary number
    s = "1001"
 
    # Find the maximum binary number
    printMaxAfterRemoval(s)
 
# This code is contributed
# by Rituraj Jain

C#

// C# program to find next maximum
// binary number with one bit removed
using System;
 
class GFG
{
// Function to find the maximum
// binary number
static int printMaxAfterRemoval(String s)
{
    bool flag = false;
    int n = s.Length;
 
    // Traverse the binary number
    for (int i = 0; i < n; i++)
    {
 
        // Try finding a 0 and skip it
        if (s[i] == '0' && flag == false)
        {
            flag = true;
            continue;
        }
        else
            Console.Write(s[i]);
    }
    return 0;
}
 
 
// Driver Code
static void Main()
{
    // Get the binary number
    String s = "1001";
     
    // Find the maximum binary number
    printMaxAfterRemoval(s);
}
}
 
// This code is contributed by Ryuga.

PHP

<?php
// PHP program to find next maximum
// binary number with one bit removed
 
// Function to find the maximum
// binary number
function printMaxAfterRemoval($s)
{
    $flag = false;
    $n = strlen($s);
 
    // Traverse the binary number
    for ($i = 0; $i < $n; $i++)
    {
 
        // Try finding a 0 and skip it
        if ($s[$i] == '0' && $flag == false)
        {
            $flag = true;
            continue;
        }
        else
            echo $s[$i];
    }
}
 
// Driver code
 
// Get the binary number
$s = "1001";
 
// Find the maximum binary number
printMaxAfterRemoval($s);
 
// This code is contributed
// by Akanksha Rai
?>

Javascript

<script>   
    // Javascript program to find next maximum
    // binary number with one bit removed
     
    // Function to find the maximum
    // binary number
    function printMaxAfterRemoval(s)
    {
        let flag = false;
        let n = s.length;
 
        // Traverse the binary number
        for (let i = 0; i < n; i++)
        {
 
            // Try finding a 0 and skip it
            if (s[i] == '0' && flag == false)
            {
                flag = true;
                continue;
            }
            else
                document.write(s[i]);
        }
        return 0;
    }
     
    // Get the binary number
    let s = "1001";
       
    // Find the maximum binary number
    printMaxAfterRemoval(s);
     
</script>
Producción: 

101

 

Complejidad de tiempo: O(n)
 

Publicación traducida automáticamente

Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *