Dado un número entero N , la tarea es encontrar el N-ésimo número natural con exactamente dos bits establecidos.
Ejemplos:
Entrada: N = 4
Salida: 9
Explicación:
Números con exactamente dos bits establecidos: 3, 5, 6, 9, 10, 12, … El
cuarto número es 9
Entrada: N = 15
Salida: 48
Enfoque ingenuo:
- Ejecute un bucle a través de todos los números naturales y, para cada número, verifique si tiene dos bits establecidos o no contando los bits establecidos en un número .
- Imprime el número N con dos bits establecidos.
Enfoque eficiente:
- Encuentre el bit establecido más a la izquierda encontrando la partición a la que pertenece N (la partición ‘i’ tiene números ‘i’).
- Para encontrar el otro bit establecido, primero tendremos que encontrar la distancia de N desde el último número de la partición anterior. En función de su diferencia, establecemos el bit correspondiente.
k = k | (1<<(i))
- A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Code to find the Nth number // with exactly two bits set #include <bits/stdc++.h> using namespace std; // Function to find the Nth number // with exactly two bits set void findNthNum(long long int N) { long long int bit_L = 1, last_num = 0; // Keep incrementing until // we reach the partition of 'N' // stored in bit_L while (bit_L * (bit_L + 1) / 2 < N) { last_num = last_num + bit_L; bit_L++; } // set the rightmost bit // based on bit_R int bit_R = N - last_num - 1; cout << (1 << bit_L) + (1 << bit_R) << endl; } // Driver code int main() { long long int N = 13; findNthNum(N); return 0; }
Java
// Java Code to find the Nth number // with exactly two bits set class GFG{ // Function to find the Nth number // with exactly two bits set static void findNthNum(int N) { int bit_L = 1, last_num = 0; // Keep incrementing until // we reach the partition of 'N' // stored in bit_L while (bit_L * (bit_L + 1) / 2 < N) { last_num = last_num + bit_L; bit_L++; } // set the rightmost bit // based on bit_R int bit_R = N - last_num - 1; System.out.print((1 << bit_L) + (1 << bit_R) +"\n"); } // Driver code public static void main(String[] args) { int N = 13; findNthNum(N); } } // This code is contributed by Princi Singh
Python3
# Python Code to find the Nth number # with exactly two bits set # Function to find the Nth number # with exactly two bits set def findNthNum(N): bit_L = 1; last_num = 0; # Keep incrementing until # we reach the partition of 'N' # stored in bit_L while (bit_L * (bit_L + 1) / 2 < N): last_num = last_num + bit_L; bit_L+=1; # set the rightmost bit # based on bit_R bit_R = N - last_num - 1; print((1 << bit_L) + (1 << bit_R)); # Driver code if __name__ == '__main__': N = 13; findNthNum(N); # This code contributed by PrinciRaj1992
C#
// C# Code to find the Nth number // with exactly two bits set using System; class GFG{ // Function to find the Nth number // with exactly two bits set static void findNthNum(int N) { int bit_L = 1, last_num = 0; // Keep incrementing until // we reach the partition of 'N' // stored in bit_L while (bit_L * (bit_L + 1) / 2 < N) { last_num = last_num + bit_L; bit_L++; } // set the rightmost bit // based on bit_R int bit_R = N - last_num - 1; Console.Write((1 << bit_L) + (1 << bit_R) +"\n"); } // Driver code public static void Main(String[] args) { int N = 13; findNthNum(N); } } // This code is contributed by Princi Singh
Javascript
<script> // JavaScript Code to find the Nth number // with exactly two bits set // Function to find the Nth number // with exactly two bits set function findNthNum(N) { let bit_L = 1, last_num = 0; // Keep incrementing until // we reach the partition of 'N' // stored in bit_L while (bit_L * (bit_L + 1) / 2 < N) { last_num = last_num + bit_L; bit_L++; } // set the rightmost bit // based on bit_R let bit_R = N - last_num - 1; document.write((1 << bit_L) + (1 << bit_R) + "<br>"); } // Driver code let N = 13; findNthNum(N); // This code is contributed by Mayank Tyagi </script>
Producción:
36
- Complejidad de tiempo: O (Partición de número)
Publicación traducida automáticamente
Artículo escrito por abdul_basith y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA