Dada una array arr[] que contiene N enteros y una array consultas[] que contiene Q consultas en forma de {L, R} , la tarea es contar el número total de bits establecidos de L a R en la array arr para cada consulta.
Ejemplo:
Entrada: arr[]={1, 2, 3, 4, 5, 6}, queries[]={{0, 2}, {1, 1}, {3, 5}}
Salida:
4
2
5
Explicación:
Consulta 1 (0, 2): los elementos {1, 2, 3} tienen bits establecidos {1, 1, 2} respectivamente. Entonces sum=1+1+2=4
Consulta 1 (1, 1): El elemento {2} ha establecido el bit {1}. Entonces sum=1
Consulta 1 (3, 5): los elementos {4, 5, 6} tienen bits establecidos {1, 2, 2} respectivamente. entonces suma=1+2+2=5Entrada: arr[]={2, 4, 3, 5, 6}, consultas[]={{0, 1}, {2, 4}}
Salida:
2
6
Enfoque: Para resolver esta pregunta, siga los pasos a continuación:
- Cree un vector, digamos onBits , en el que cada i -ésimo elemento representará el número de bits establecidos desde arr[0] hasta arr[i] .
- Ejecute un bucle de i=0 a i<N y, en cada iteración, encuentre el número de bits establecidos en arr[i] , diga bits y haga onBits[i]=onBits[i-1] + bits .
- Ahora recorra las consultas de array y para cada consulta {L, R} , el número de bits establecidos de L a R es onBits[R]-onBits[L-1] .
- Escriba la respuesta de acuerdo con la observación anterior.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to return the number // of set bits from L to R int setBitsinLtoR(int L, int R, vector<int>& onBits) { if (L == 0) { return onBits[R]; } return onBits[R] - onBits[L - 1]; } // Function to find the number // of set bits for Q queries void totalSetBits(vector<int>& arr, vector<pair<int, int> >& queries) { int N = arr.size(); vector<int> onBits(N, 0); onBits[0] = __builtin_popcount(arr[0]); for (int i = 1; i < N; ++i) { onBits[i] = onBits[i - 1] + __builtin_popcount(arr[i]); } // Finding for Q queries for (auto x : queries) { int L = x.first; int R = x.second; cout << setBitsinLtoR(L, R, onBits) << endl; } } // Driver Code int main() { vector<int> arr = { 1, 2, 3, 4, 5, 6 }; vector<pair<int, int> > queries = { { 0, 2 }, { 1, 1 }, { 3, 5 } }; totalSetBits(arr, queries); }
Java
// Java code for the above approach import java.util.*; class GFG{ static class pair { int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function to return the number // of set bits from L to R static int setBitsinLtoR(int L, int R,int []onBits) { if (L == 0) { return onBits[R]; } return onBits[R] - onBits[L - 1]; } // Function to find the number // of set bits for Q queries static void totalSetBits(int[] arr, pair[] queries) { int N = arr.length; int []onBits = new int[N]; onBits[0] = Integer.bitCount(arr[0]); for (int i = 1; i < N; ++i) { onBits[i] = onBits[i - 1] + Integer.bitCount(arr[i]); } // Finding for Q queries for (pair x : queries) { int L = x.first; int R = x.second; System.out.print(setBitsinLtoR(L, R, onBits) +"\n"); } } // Driver Code public static void main(String[] args) { int []arr = { 1, 2, 3, 4, 5, 6 }; pair []queries = { new pair( 0, 2 ), new pair( 1, 1 ), new pair( 3, 5 ) }; totalSetBits(arr, queries); } } // This code is contributed by 29AjayKumar
Python3
# Python code for the above approach def __builtin_popcount(n): count = 0 while (n): count += n & 1 n >>= 1 return count # Function to return the number # of set bits from L to R def setBitsinLtoR(L, R, onBits): if (L == 0): return onBits[R]; return onBits[R] - onBits[L - 1]; # Function to find the number # of set bits for Q queries def totalSetBits(arr, queries): N = len(arr); onBits = [0] * N onBits[0] = __builtin_popcount(arr[0]); for i in range(1, N): onBits[i] = onBits[i - 1] + __builtin_popcount(arr[i]); # Finding for Q queries for x in queries: L = x[0]; R = x[1]; print(setBitsinLtoR(L, R, onBits)) # Driver Code arr = [ 1, 2, 3, 4, 5, 6 ]; queries = [ [ 0, 2 ], [ 1, 1 ], [ 3, 5 ] ]; totalSetBits(arr, queries); # This code is contributed by gfgking
C#
// C# code for the above approach using System; class GFG{ class pair { public int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function to return the number // of set bits from L to R static int setBitsinLtoR(int L, int R, int []onBits) { if (L == 0) { return onBits[R]; } return onBits[R] - onBits[L - 1]; } // Function to find the number // of set bits for Q queries static void totalSetBits(int[] arr, pair[] queries) { int N = arr.Length; int []onBits = new int[N]; onBits[0] = bitCount(arr[0]); for(int i = 1; i < N; ++i) { onBits[i] = onBits[i - 1] + bitCount(arr[i]); } // Finding for Q queries foreach (pair x in queries) { int L = x.first; int R = x.second; Console.Write(setBitsinLtoR(L, R, onBits) + "\n"); } } static int bitCount(int x) { int setBits = 0; while (x != 0) { x = x & (x - 1); setBits++; } return setBits; } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4, 5, 6 }; pair []queries = { new pair(0, 2), new pair(1, 1), new pair(3, 5) }; totalSetBits(arr, queries); } } // This code is contributed by shikhasingrajput
Javascript
<script> // JavaScript code for the above approach function __builtin_popcount(x) { return((x).toString(2).split(''). filter(x => x == '1').length); } // Function to return the number // of set bits from L to R function setBitsinLtoR(L, R, onBits) { if (L == 0) { return onBits[R]; } return onBits[R] - onBits[L - 1]; } // Function to find the number // of set bits for Q queries function totalSetBits(arr, queries) { let N = arr.length; let onBits = new Array(N).fill(0); onBits[0] = __builtin_popcount(arr[0]); for(let i = 1; i < N; ++i) { onBits[i] = onBits[i - 1] + __builtin_popcount(arr[i]); } // Finding for Q queries for(let x of queries) { let L = x[0]; let R = x[1]; document.write( setBitsinLtoR(L, R, onBits) + '<br>') } } // Driver Code let arr = [ 1, 2, 3, 4, 5, 6 ]; let queries = [ [ 0, 2 ], [ 1, 1 ], [ 3, 5 ] ]; totalSetBits(arr, queries); // This code is contributed by Potta Lokesh </script>
4 1 5
Complejidad de tiempo: O(Q*N*logK), donde N es el tamaño del arreglo arr, Q es el número de consultas y K es el número de bits
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por shandilraunak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA