`{"id":"b79c7d00-0482-4a06-9fd1-e449aeab3c41","name":"Kernighans Algorithm","description":"1. You are given a number n.\r\n2. You have to count the number of set bits in the given number. ","inputFormat":"A number n","outputFormat":"Number of set bits in n","constraints":"1 &lt;= n &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\nint main()\n{\n int n;cin>>n;\n \n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n \r\n //write your code here\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"58","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/I475waWiTK4?end=258","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"bb02d6a8-8662-40da-86c5-df0ac7d13192","name":"Kernighans Algorithm","slug":"kernighans-algorithm","type":1}],"next":{"id":"7ca64244-bf98-49c1-91e6-9254215c143b","name":"Kernighan's Algorithm MCQ","type":0,"slug":"kernighan-s-algorithm-mcq"},"prev":{"id":"426e0680-6ae9-41a3-9f00-4a0d694e1010","name":"Print Value Of Rsb Mask","type":3,"slug":"print-value-of-rsb-mask"}}}`

# Kernighans Algorithm

1. You are given a number n. 2. You have to count the number of set bits in the given number.

`{"id":"b79c7d00-0482-4a06-9fd1-e449aeab3c41","name":"Kernighans Algorithm","description":"1. You are given a number n.\r\n2. You have to count the number of set bits in the given number. ","inputFormat":"A number n","outputFormat":"Number of set bits in n","constraints":"1 &lt;= n &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\nint main()\n{\n int n;cin>>n;\n \n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n \r\n //write your code here\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"58","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/I475waWiTK4?end=258","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"bb02d6a8-8662-40da-86c5-df0ac7d13192","name":"Kernighans Algorithm","slug":"kernighans-algorithm","type":1}],"next":{"id":"7ca64244-bf98-49c1-91e6-9254215c143b","name":"Kernighan's Algorithm MCQ","type":0,"slug":"kernighan-s-algorithm-mcq"},"prev":{"id":"426e0680-6ae9-41a3-9f00-4a0d694e1010","name":"Print Value Of Rsb Mask","type":3,"slug":"print-value-of-rsb-mask"}}}`

Editor

# Kernighans Algorithm

easy

1. You are given a number n. 2. You have to count the number of set bits in the given number.

1 <= n <= 10^9

## Format

A number n

### Output

Number of set bits in n

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}58`

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}4 ```

Question Video

Discussions

Show Discussion

Related Resources