Dados L y R. La tarea es encontrar el número en cuya representación binaria todos los bits entre el índice L-th y R-th están activados y el resto de los bits están desactivados. La representación binaria es de 32 bits.
Ejemplos:
Entrada: L = 2, R = 5
Salida: 60
Explicación: La representación binaria es
0..0111100 => 60Entrada: L = 1, R = 3
Salida: 14
Explicación: La representación binaria es
0..01110 => 14
Enfoque ingenuo: el enfoque ingenuo para encontrar el número es iterar de i = L a i = R y calcular la suma de todas las potencias de 2 i .
El siguiente programa ilustra el enfoque ingenuo:
C++
// CPP program to print the integer // with all the bits set in range L-R // Naive Approach #include <bits/stdc++.h> using namespace std; // Function to return the integer // with all the bits set in range L-R int getInteger(int L, int R) { int number = 0; // iterate from L to R // and add all powers of 2 for (int i = L; i <= R; i++) number += pow(2, i); return number; } // Driver Code int main() { int L = 2, R = 5; cout << getInteger(L, R); return 0; }
Java
// Java program to print the // integer with all the bits // set in range L-R Naive Approach import java.io.*; class GFG { // Function to return the // integer with all the // bits set in range L-R static int getInteger(int L, int R) { int number = 0; // iterate from L to R // and add all powers of 2 for (int i = L; i <= R; i++) number += Math.pow(2, i); return number; } // Driver Code public static void main (String[] args) { int L = 2, R = 5; System.out.println(getInteger(L, R)); } } // This code is contributed by anuj_67..
Python3
# Python 3 program to print the integer # with all the bits set in range L-R # Naive Approach from math import pow # Function to return the integer # with all the bits set in range L-R def getInteger(L, R): number = 0 # iterate from L to R # and add all powers of 2 for i in range(L, R + 1, 1): number += pow(2, i) return number # Driver Code if __name__ == '__main__': L = 2 R = 5 print(int(getInteger(L, R))) # This code is contributed by # Surendra_Gangwar
C#
// C# program to print the // integer with all the bits // set in range L-R Naive Approach using System; class GFG { // Function to return the // integer with all the // bits set in range L-R static int getInteger(int L, int R) { int number = 0; // iterate from L to R // and add all powers of 2 for (int i = L; i <= R; i++) number += (int)Math.Pow(2, i); return number; } // Driver Code public static void Main () { int L = 2, R = 5; Console.Write(getInteger(L, R)); } } // This code is contributed // by shiv_bhakt.
PHP
<?php // PHP program to print // the integer with all // the bits set in range // L-R Naive Approach // Function to return the // integer with all the // bits set in range L-R function getInteger($L, $R) { $number = 0; // iterate from L to R // and add all powers of 2 for ($i = $L; $i <= $R; $i++) $number += pow(2, $i); return $number; } // Driver Code $L = 2; $R = 5; echo getInteger($L, $R); // This code is contributed // by shiv_bhakt. ?>
Javascript
<script> // Javascript program to print the integer // with all the bits set in range L-R // Naive Approach // Function to return the integer // with all the bits set in range L-R function getInteger(L, R) { var number = 0; // iterate from L to R // and add all powers of 2 for (var i = L; i <= R; i++) number += Math.pow(2, i); return number; } // Driver Code var L = 2, R = 5; document.write( getInteger(L, R)); </script>
Producción:
60
Un enfoque eficiente es calcular el número con todos los bits (R) configurados desde la derecha y restar el número con todos los bits (L-1) configurados desde la derecha para obtener el número requerido.
1. Calcule el número que tiene todos los bits establecidos de R desde la derecha usando la fórmula a continuación.
(1 << (R+1)) - 1.
2. Reste el número que tiene todos los bits establecidos (L-1) desde la derecha.
(1<<L) - 1
Por lo tanto, calculando ((1<<(R+1))-1)-((1<<L)-1), obtenemos la fórmula final como:
(1<<(R+1))-(1<<L)
El siguiente programa ilustra el enfoque eficiente:
C++
// CPP program to print the integer // with all the bits set in range L-R // Efficient Approach #include <bits/stdc++.h> using namespace std; // Function to return the integer // with all the bits set in range L-R int setbitsfromLtoR(int L, int R) { return (1 << (R + 1)) - (1 << L); } // Driver Code int main() { int L = 2, R = 5; cout << setbitsfromLtoR(L, R); return 0; }
Java
// Java program to print // the integer with all // the bits set in range // L-R Efficient Approach import java.io.*; class GFG { // Function to return the // integer with all the // bits set in range L-R static int setbitsfromLtoR(int L, int R) { return (1 << (R + 1)) - (1 << L); } // Driver Code public static void main (String[] args) { int L = 2, R = 5; System.out.println(setbitsfromLtoR(L, R)); } } // This code is contributed // by shiv_bhakt.
Python3
# Python3 program to print # the integer with all the # bits set in range L-R # Efficient Approach # Function to return the # integer with all the # bits set in range L-R def setbitsfromLtoR(L, R): return ((1 << (R + 1)) - (1 << L)) # Driver Code L = 2 R = 5 print(setbitsfromLtoR(L, R)) # This code is contributed # by Smita
C#
// C# program to print // the integer with all // the bits set in range // L-R Efficient Approach using System; class GFG { // Function to return the // integer with all the // bits set in range L-R static int setbitsfromLtoR(int L, int R) { return (1 << (R + 1)) - (1 << L); } // Driver Code public static void Main () { int L = 2, R = 5; Console.WriteLine(setbitsfromLtoR(L, R)); } } // This code is contributed // by shiv_bhakt.
PHP
<?php // PHP program to print // the integer with all // the bits set in range // L-R Efficient Approach // Function to return the // integer with all the // bits set in range L-R function setbitsfromLtoR($L, $R) { return (1 << ($R + 1)) - (1 << $L); } // Driver Code $L = 2; $R = 5; echo setbitsfromLtoR($L, $R); // This code is contributed // by shiv_bhakt. ?>
Javascript
<script> // Javascript program to print the integer // with all the bits set in range L-R // Efficient Approach // Function to return the integer // with all the bits set in range L-R function setbitsfromLtoR(L, R) { return (1 << (R + 1)) - (1 << L); } // Driver Code var L = 2, R = 5; document.write( setbitsfromLtoR(L, R)); // This code is contributed by famously. </script>
Producción:
60
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por aganjali10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA