-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7305 from kiruba-r11/feature-1
Added Count Set Bits in Kotlin
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.util.Scanner | ||
|
||
/* | ||
A Kotlin Program to find the XOR of 1 to N. | ||
*/ | ||
|
||
fun main(args: Array <String>) { | ||
|
||
val input = Scanner(System.`in`) | ||
|
||
// Get the required input from the user | ||
println("Enter the number to count no. of set bits in it : ") | ||
val n: Int = input.nextInt() | ||
|
||
val result = countSetBits(n) | ||
println("The no. of set bits is : $result") | ||
|
||
} | ||
|
||
// Defining a function that will take an number and return the count of set bits in it | ||
fun countSetBits(n: Int): Int { | ||
var count = 0 | ||
var num = n | ||
|
||
// In every iteration, the last set bit of the number is made unset | ||
// Therefore, the count of set bits, would be the no. of times the last set bit can be removed until zero occurs | ||
while(num != 0) { | ||
num = num and (num - 1); | ||
count++ | ||
} | ||
|
||
return count | ||
} | ||
|
||
/* | ||
Sample Test Cases: | ||
Test case 1: | ||
Enter the number to count no. of set bits in it : | ||
10453 | ||
The no. of set bits is : 7 | ||
Test Case 2: | ||
Enter the number to count no. of set bits in it : | ||
1023 | ||
The no. of set bits is : 10 | ||
*/ | ||
|
||
/* | ||
Time complexity: O(No. of Set Bits) | ||
Space complexity: O(1) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters