Dada una array arr , la tarea es contar el número total de bits establecidos en todos los números de esa array arr .
Ejemplo:
Entrada: arr[] = {1, 2, 5, 7}
Salida: 7
Explicación: El número de bits establecidos en {1, 2, 5, 7} son {1, 1, 2, 3} respectivamenteEntrada: arr[] = {0, 4, 9, 8}
Salida: 4
Enfoque: siga los pasos a continuación para resolver este problema:
- Cree una variable cnt para almacenar la respuesta e inicialícela con 0 .
- Recorra cada elemento de la array arr .
- Ahora, para cada elemento, digamos x , ejecuta un bucle mientras sea mayor que 0 .
- Extraiga el último bit de x usando (x&1) y luego desplace x a la derecha un solo bit.
- Devuelve cnt como la respuesta a este problema.
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 count the total number of set bits // in an array of integers int totalSetBits(vector<int>& arr) { int cnt = 0; for (auto x : arr) { // While x is greater than 0 while (x > 0) { // Adding last bit to cnt cnt += (x & 1); // Right shifting x by a single bit x >>= 1; } } return cnt; } // Driver Code int main() { vector<int> arr = { 1, 2, 5, 7 }; cout << totalSetBits(arr); }
Java
// Java code for the above approach import java.util.*; class GFG{ // Function to count the total number of set bits // in an array of integers static int totalSetBits(int[] arr) { int cnt = 0; for (int x : arr) { // While x is greater than 0 while (x > 0) { // Adding last bit to cnt cnt += (x & 1); // Right shifting x by a single bit x >>= 1; } } return cnt; } // Driver Code public static void main(String[] args) { int[] arr = { 1, 2, 5, 7 }; System.out.print(totalSetBits(arr)); } } // This code is contributed by shikhasingrajput
Python3
# python code for the above approach # Function to count the total number of set bits # in an array of integers def totalSetBits(arr): cnt = 0 for x in arr: # While x is greater than 0 while (x > 0): # Adding last bit to cnt cnt += (x & 1) # Right shifting x by a single bit x >>= 1 return cnt # Driver Code if __name__ == "__main__": arr = [1, 2, 5, 7] print(totalSetBits(arr)) # This code is contributed by rakeshsahni
C#
// C# code for the above approach using System; class GFG { // Function to count the total number of set bits // in an array of integers static int totalSetBits(int[] arr) { int cnt = 0; for (int x = 0; x < arr.Length; x++) { // While x is greater than 0 while (arr[x] > 0) { // Adding last bit to cnt cnt += (arr[x] & 1); // Right shifting x by a single bit arr[x] >>= 1; } } return cnt; } // Driver Code public static void Main(string[] args) { int[] arr = { 1, 2, 5, 7 }; Console.WriteLine(totalSetBits(arr)); } } // This code is contributed by ukasp.
Javascript
<script> // JavaScript code for the above approach // Function to count the total number of set bits // in an array of integers function totalSetBits(arr) { let cnt = 0; for(let x of arr) { // While x is greater than 0 while (x > 0) { // Adding last bit to cnt cnt += (x & 1); // Right shifting x by a single bit x >>= 1; } } return cnt; } // Driver Code let arr = [ 1, 2, 5, 7 ]; document.write(totalSetBits(arr)); // This code is contributed by Potta Lokesh </script>
Producción
7
Complejidad temporal: O(N)
Espacio auxiliar: O(1)