From 970be086b70dfded772fdc80f21294c60b8e0c96 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 6 Dec 2017 11:33:14 +0900 Subject: [PATCH 01/17] Updated OctalToDecimal.java --- Conversions/OctalToDecimal.java | 43 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Conversions/OctalToDecimal.java b/Conversions/OctalToDecimal.java index 637b8f2e..2c909857 100644 --- a/Conversions/OctalToDecimal.java +++ b/Conversions/OctalToDecimal.java @@ -7,42 +7,41 @@ import java.util.Scanner; * */ public class OctalToDecimal { - + /** * Main method * - * @param args Command line arguments + * @param args + * Command line arguments */ public static void main(String args[]) { Scanner sc = new Scanner(System.in); - int o = sc.nextInt(); - System.out.println("Decimal equivalent: " + convertOctalToDecimal(o)); + System.out.print("Octal Input: "); + String inputOctal = sc.nextLine(); + int result = convertOctalToDecimal(inputOctal); + if (result != -1) + System.out.println("Result convertOctalToDecimal : " + result); sc.close(); } - + /** - * This method converts an octal number to - * a decimal number. + * This method converts an octal number to a decimal number. * - * @param o The octal number + * @param inputOctal + * The octal number * @return The decimal number */ - public static int convertOctalToDecimal(int o) { - System.out.print("Octal Input: "); - // Read the input from the console which we are expecting as an octal number: - Scanner s = new Scanner(System.in); - String inputHex = s.nextLine(); - try{ + public static int convertOctalToDecimal(String inputOctal) { + + try { // Actual conversion of Octal to Decimal: - Integer outputDecimal = Integer.parseInt(inputHex, 8); - System.out.println("Decimal Equivalent : " + outputDecimal); - } - catch(NumberFormatException ne){ - // Printing a warning message if the input is not a valid octal number: + Integer outputDecimal = Integer.parseInt(inputOctal, 8); + return outputDecimal; + } catch (NumberFormatException ne) { + // Printing a warning message if the input is not a valid octal + // number: System.out.println("Invalid Input, Expecting octal number 0-7"); - } - finally{ - s.close(); + return -1; } } } \ No newline at end of file From 0a3b445b0857ce6684ea0c15e790e69ba25b4e53 Mon Sep 17 00:00:00 2001 From: SeonJae Date: Sat, 9 Dec 2017 16:58:43 +0900 Subject: [PATCH 02/17] modified error --- Conversions/HexaDecimalToBinary.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Conversions/HexaDecimalToBinary.java b/Conversions/HexaDecimalToBinary.java index e43bb1de..29608e1d 100644 --- a/Conversions/HexaDecimalToBinary.java +++ b/Conversions/HexaDecimalToBinary.java @@ -3,7 +3,7 @@ import java.util.*; import java.util.Scanner; import javax.swing.*; -public class HexaToBin { +public class HexaDecimalToBinary { private final int LONG_BITS = 8; @@ -28,10 +28,10 @@ public class HexaToBin { //Testing Numbers: String[] hexNums = {"1", "A1", "ef", "BA", "AA", "BB", "19", "01", "02", "03", "04"}; - Convert objConvert = new Convert(); + HexaDecimalToBinary objConvert = new HexaDecimalToBinary(); for (String num : hexNums) { objConvert.convert(num); } } -} \ No newline at end of file +} From 1133f2a278e032ec18816bddee36be7245aaa79a Mon Sep 17 00:00:00 2001 From: cnuhoya Date: Mon, 11 Dec 2017 12:15:54 +0900 Subject: [PATCH 03/17] modify comment verticies to vertices --- Data Structures/Graphs/MatrixGraphs.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures/Graphs/MatrixGraphs.java b/Data Structures/Graphs/MatrixGraphs.java index f064c012..5723bf38 100644 --- a/Data Structures/Graphs/MatrixGraphs.java +++ b/Data Structures/Graphs/MatrixGraphs.java @@ -120,7 +120,7 @@ class AdjacencyMatrixGraph { } /** - * this gives a list of verticies in the graph and their adjacencies + * this gives a list of vertices in the graph and their adjacencies * * @return returns a string describing this graph */ From 5c10b903ffb6c99d32801c07824918d41f8c8550 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 11 Dec 2017 13:36:42 +0900 Subject: [PATCH 04/17] Updated krishnamurthy.java --- Others/krishnamurthy.java | 52 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/Others/krishnamurthy.java b/Others/krishnamurthy.java index 66685bcc..52480eb0 100644 --- a/Others/krishnamurthy.java +++ b/Others/krishnamurthy.java @@ -1,30 +1,28 @@ import java.util.Scanner; -class krishnamurthy -{ - int fact(int n) - { - int i,p=1; - for(i=n;i>=1;i--) - p=p*i; - return p; - } - public static void main(String args[]) - { - Scanner sc=new Scanner(System.in); - int a,b,s=0; - System.out.print("Enter the number : "); - a=sc.nextInt(); - int n=a; - while(a>0) - { - b=a%10; - s=s+fact(b); - a=a/10; - } - if(s==n) - System.out.print(n+" is a krishnamurthy number"); - else - System.out.print(n+" is not a krishnamurthy number"); - } +class krishnamurthy { + static int fact(int n) { + int i, p = 1; + for (i = n; i >= 1; i--) + p = p * i; + return p; + } + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + int a, b, s = 0; + System.out.print("Enter the number : "); + a = sc.nextInt(); + int n = a; + while (a > 0) { + b = a % 10; + s = s + fact(b); + a = a / 10; + } + if (s == n) + System.out.print(n + " is a krishnamurthy number"); + else + System.out.print(n + " is not a krishnamurthy number"); + sc.close(); + } } From 9d183e62555622200f175787f73dd710d01fa496 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 14 Dec 2017 11:57:43 +0900 Subject: [PATCH 05/17] Moditied Others folder .java file name and class name --- Others/Dijkshtra.java | 2 +- Others/InsertDeleteInArray.java | 2 +- Others/RootPrecision.java | 2 +- Others/StackPostfixNotation.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Others/Dijkshtra.java b/Others/Dijkshtra.java index 741c9bd3..05011dd4 100644 --- a/Others/Dijkshtra.java +++ b/Others/Dijkshtra.java @@ -9,7 +9,7 @@ import java.util.Arrays; import java.util.Scanner; import java.util.Stack; -public class Solution { +public class Dijkshtra { public static void main(String[] args) throws IOException { Scanner in =new Scanner(System.in); diff --git a/Others/InsertDeleteInArray.java b/Others/InsertDeleteInArray.java index 12bbdc19..85702066 100644 --- a/Others/InsertDeleteInArray.java +++ b/Others/InsertDeleteInArray.java @@ -1,5 +1,5 @@ import java.util.*; -public class Array { +public class InsertDeleteInArray { public static void main(String[] args) { Scanner s = new Scanner(System.in); // Input statement diff --git a/Others/RootPrecision.java b/Others/RootPrecision.java index 0ae00de0..b792d692 100644 --- a/Others/RootPrecision.java +++ b/Others/RootPrecision.java @@ -4,7 +4,7 @@ import java.text.*; import java.math.*; import java.util.regex.*; -public class Solution { +public class RootPrecision { public static void main(String[] args) { //take input diff --git a/Others/StackPostfixNotation.java b/Others/StackPostfixNotation.java index c04b0ac6..be77eead 100644 --- a/Others/StackPostfixNotation.java +++ b/Others/StackPostfixNotation.java @@ -1,6 +1,6 @@ import java.util.*; -public class Postfix { +public class StackPostfixNotation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +" From 707f6f1ea831cd9be5ef872299496b5d4bf8d210 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 15 Dec 2017 15:13:13 +0900 Subject: [PATCH 06/17] Modified Data Structures/Graphs folder .java file name and class name --- Data Structures/Graphs/BFS.java | 2 +- Data Structures/Graphs/DFS.java | 2 +- Data Structures/Graphs/PrimMST.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Data Structures/Graphs/BFS.java b/Data Structures/Graphs/BFS.java index 4f3bd62d..d06a6606 100644 --- a/Data Structures/Graphs/BFS.java +++ b/Data Structures/Graphs/BFS.java @@ -6,7 +6,7 @@ import java.util.*; * @author Unknown * */ -public class bfs{ +public class BFS{ /** * The BFS implemented in code to use. diff --git a/Data Structures/Graphs/DFS.java b/Data Structures/Graphs/DFS.java index 8ceba166..747fcbed 100644 --- a/Data Structures/Graphs/DFS.java +++ b/Data Structures/Graphs/DFS.java @@ -7,7 +7,7 @@ import java.util.*; * */ -public class dfs{ +public class DFS{ /** * Implementation in code of a DFS diff --git a/Data Structures/Graphs/PrimMST.java b/Data Structures/Graphs/PrimMST.java index 9d8c2d35..25695050 100644 --- a/Data Structures/Graphs/PrimMST.java +++ b/Data Structures/Graphs/PrimMST.java @@ -100,7 +100,7 @@ class PrimMST | / \ | (3)-------(4) 9 */ - MST t = new MST(); + PrimMST t = new PrimMST(); int graph[][] = new int[][] {{0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, From 6dcce9f3e070586fb266c410c5bf7516ed019dd5 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 16 Dec 2017 19:04:10 +0900 Subject: [PATCH 07/17] Modified Data Structures/Trees folder .java file name and class name --- Data Structures/Trees/AVLTree.java | 4 ++-- Data Structures/Trees/LevelOrderTraversal.java | 6 +++--- Data Structures/Trees/LevelOrderTraversalQueue.java | 4 ++-- Data Structures/Trees/PrintTopViewofTree.java | 2 +- Data Structures/Trees/ValidBSTOrNot.java | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Data Structures/Trees/AVLTree.java b/Data Structures/Trees/AVLTree.java index a3bde289..4bcf402d 100644 --- a/Data Structures/Trees/AVLTree.java +++ b/Data Structures/Trees/AVLTree.java @@ -1,4 +1,4 @@ -public class AVLtree { +public class AVLTree { private Node root; @@ -200,7 +200,7 @@ public class AVLtree { } public static void main(String[] args) { - AVLtree tree = new AVLtree(); + AVLTree tree = new AVLTree(); System.out.println("Inserting values 1 to 10"); for (int i = 1; i < 10; i++) diff --git a/Data Structures/Trees/LevelOrderTraversal.java b/Data Structures/Trees/LevelOrderTraversal.java index 845ab376..8cb304f1 100644 --- a/Data Structures/Trees/LevelOrderTraversal.java +++ b/Data Structures/Trees/LevelOrderTraversal.java @@ -9,12 +9,12 @@ class Node } } -class BinaryTree +public class LevelOrderTraversal { // Root of the Binary Tree Node root; - public BinaryTree() + public LevelOrderTraversal() { root = null; } @@ -65,7 +65,7 @@ class BinaryTree /* Driver program to test above functions */ public static void main(String args[]) { - BinaryTree tree = new BinaryTree(); + LevelOrderTraversal tree = new LevelOrderTraversal(); tree.root= new Node(1); tree.root.left= new Node(2); tree.root.right= new Node(3); diff --git a/Data Structures/Trees/LevelOrderTraversalQueue.java b/Data Structures/Trees/LevelOrderTraversalQueue.java index 4f263c95..ce7ad745 100644 --- a/Data Structures/Trees/LevelOrderTraversalQueue.java +++ b/Data Structures/Trees/LevelOrderTraversalQueue.java @@ -14,7 +14,7 @@ class Node { } /* Class to print Level Order Traversal */ -class BinaryTree { +public class LevelOrderTraversalQueue { Node root; @@ -49,7 +49,7 @@ class BinaryTree { { /* creating a binary tree and entering the nodes */ - BinaryTree tree_level = new BinaryTree(); + LevelOrderTraversalQueue tree_level = new LevelOrderTraversalQueue(); tree_level.root = new Node(1); tree_level.root.left = new Node(2); tree_level.root.right = new Node(3); diff --git a/Data Structures/Trees/PrintTopViewofTree.java b/Data Structures/Trees/PrintTopViewofTree.java index a429f8a6..27016ee2 100644 --- a/Data Structures/Trees/PrintTopViewofTree.java +++ b/Data Structures/Trees/PrintTopViewofTree.java @@ -78,7 +78,7 @@ class Tree } // Driver class to test above methods -public class Main +public class PrintTopViewofTree { public static void main(String[] args) { diff --git a/Data Structures/Trees/ValidBSTOrNot.java b/Data Structures/Trees/ValidBSTOrNot.java index 0f71a46a..4b2c08a1 100644 --- a/Data Structures/Trees/ValidBSTOrNot.java +++ b/Data Structures/Trees/ValidBSTOrNot.java @@ -10,7 +10,7 @@ class Node } } -public class BinaryTree +public class ValidBSTOrNot { //Root of the Binary Tree Node root; @@ -47,7 +47,7 @@ public class BinaryTree /* Driver program to test above functions */ public static void main(String args[]) { - BinaryTree tree = new BinaryTree(); + ValidBSTOrNot tree = new ValidBSTOrNot(); tree.root = new Node(4); tree.root.left = new Node(2); tree.root.right = new Node(5); From 1a953fe4560a83708cf16c7dd012a097f6a5fb19 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 17 Dec 2017 01:15:38 +0900 Subject: [PATCH 08/17] Modified Data Dynamic Programming folder .java file name and class name --- Dynamic Programming/LevenshteinDistance.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dynamic Programming/LevenshteinDistance.java b/Dynamic Programming/LevenshteinDistance.java index 8a5f7249..be0e7c43 100644 --- a/Dynamic Programming/LevenshteinDistance.java +++ b/Dynamic Programming/LevenshteinDistance.java @@ -6,7 +6,7 @@ * */ -public class Levenshtein_distance{ +public class LevenshteinDistance{ private static int minimum(int a, int b, int c){ if(a < b && a < c){ return a; From 58e1bca4e81326d9cb6cfcc01d1c8bae87d6dbe0 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 17 Dec 2017 01:20:22 +0900 Subject: [PATCH 09/17] Modified Data Misc folder .java file name and class name --- Misc/heap_sort.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/heap_sort.java b/Misc/heap_sort.java index 59d362e5..991d689b 100644 --- a/Misc/heap_sort.java +++ b/Misc/heap_sort.java @@ -1,4 +1,4 @@ -public class HeapSort +public class heap_sort { public void sort(int arr[]) { @@ -64,7 +64,7 @@ public class HeapSort int arr[] = {12, 11, 13, 5, 6, 7}; int n = arr.length; - HeapSort ob = new HeapSort(); + heap_sort ob = new heap_sort(); ob.sort(arr); System.out.println("Sorted array is"); From 58ddf8be6a1fc60682d435037d3f43db0d5a4dda Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sun, 17 Dec 2017 07:50:55 -0800 Subject: [PATCH 10/17] Delete FindingPrimes.java #363 --- Others/FindingPrimes.java | 45 --------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 Others/FindingPrimes.java diff --git a/Others/FindingPrimes.java b/Others/FindingPrimes.java deleted file mode 100644 index f7cc14d7..00000000 --- a/Others/FindingPrimes.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * The Sieve of Eratosthenes is an algorithm use to find prime numbers, - * up to a given value. - * Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif - * (This illustration is also in the github repository) - * - * @author Unknown - * - */ -public class FindingPrimes{ - /** - * The Main method - * - * @param args Command line arguments - */ - public static void main(String args[]){ - SOE(20); //Example: Finds all the primes up to 20 - } - - /** - * The method implementing the Sieve of Eratosthenes - * - * @param n Number to perform SOE on - */ - public static void SOE(int n){ - boolean sieve[] = new boolean[n]; - - int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n - - sieve[0] = false; - sieve[1] = false; - for(int i = 2; i < n; i++) - sieve[i] = true; //Set every index to true except index 0 and 1 - - for(int i = 2; i< check; i++){ - if(sieve[i]==true) //If i is a prime - for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime) - sieve[j] = false; //Set every multiple of i to false - } - for(int i = 0; i< n; i++){ - if(sieve[i]==true) - System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19 - } - } -} \ No newline at end of file From 651ba389a2542394f6b86abc50cf1084fd965f7b Mon Sep 17 00:00:00 2001 From: JeonSeongBae Date: Tue, 19 Dec 2017 19:15:30 +0900 Subject: [PATCH 11/17] add public and change class_name --- Others/countwords.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Others/countwords.java b/Others/countwords.java index 065c9a10..30806e13 100644 --- a/Others/countwords.java +++ b/Others/countwords.java @@ -7,7 +7,7 @@ import java.util.Scanner; * @author Marcus * */ - class CountTheWords{ + public class countwords{ public static void main(String[] args){ Scanner input = new Scanner(System.in); From 153b9fe1cb62b88d5106c205b091372a0eacf665 Mon Sep 17 00:00:00 2001 From: JeonSeongBae Date: Tue, 19 Dec 2017 19:30:46 +0900 Subject: [PATCH 12/17] add closeing the scanner --- Others/FibToN.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Others/FibToN.java b/Others/FibToN.java index e7b7a9a2..1d1efdc1 100644 --- a/Others/FibToN.java +++ b/Others/FibToN.java @@ -9,7 +9,7 @@ public class FibToN { // print fibonacci sequence less than N int first = 0, second = 1; //first fibo and second fibonacci are 0 and 1 respectively - + scn.close(); while(first <= N){ //print first fibo 0 then add second fibo into it while updating second as well From 1e38751a6b840895c03b5c13c2db05aac7b1d3a9 Mon Sep 17 00:00:00 2001 From: JeonSeongBae Date: Tue, 19 Dec 2017 19:32:50 +0900 Subject: [PATCH 13/17] add closing the scanner --- Others/FloydTriangle.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Others/FloydTriangle.java b/Others/FloydTriangle.java index 815dbd29..f8d479a8 100644 --- a/Others/FloydTriangle.java +++ b/Others/FloydTriangle.java @@ -6,7 +6,7 @@ class FloydTriangle { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows which you want in your Floyd Triangle: "); int r = sc.nextInt(), n = 0; - + sc.close(); for(int i=0; i < r; i++) { for(int j=0; j <= i; j++) { System.out.print(++n + " "); From 55b9080e546a5ee022bc183f831bfa2b11b7255d Mon Sep 17 00:00:00 2001 From: NISHITA97 Date: Sat, 30 Dec 2017 02:11:15 +0530 Subject: [PATCH 14/17] =?UTF-8?q?Brian=20Kernighan=E2=80=99s=20Algorithm?= =?UTF-8?q?=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Others/BrianKernighanAlgorithm.java | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Others/BrianKernighanAlgorithm.java diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java new file mode 100644 index 00000000..361cdc05 --- /dev/null +++ b/Others/BrianKernighanAlgorithm.java @@ -0,0 +1,56 @@ +import java.util.Scanner; + +/** + * + * @author Nishita Aggarwal + * + * Brian Kernighan’s Algorithm + * algorithm to count the number of set bits in a given number + * Subtraction of 1 from a number toggles all the bits (from + * right to left) till the rightmost set bit(including the + * rightmost set bit). + * So if we subtract a number by 1 and do bitwise & with + * itself (n & (n-1)), we unset the rightmost set bit. + * If we do n & (n-1) in a loop and count the no of times loop + * executes we get the set bit count. + * Number of iterations of the loop is equal to the number of + * set bits in a given integer. + * + * Time Complexity: O(logn) + * + */ + + +public class BrianKernighanAlgorithm { + + /** + * @param num: number in which we count the set bits + * + * @return int: Number of set bits + * */ + static int countSetBits(int num) + { + int cnt = 0; + while(num != 0) + { + num = num & (num-1); + cnt++; + } + return cnt; + } + + + /** + * + * @param args : command line arguments + * + */ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + int setBitCount = countSetBits(num); + System.out.println(setBitCount); + sc.close(); + } +} From 8db4ef901c8fef6bc90d59773256c5c912f4dff9 Mon Sep 17 00:00:00 2001 From: Nishita Aggarwal Date: Sat, 30 Dec 2017 02:14:48 +0530 Subject: [PATCH 15/17] indentation improved --- Others/BrianKernighanAlgorithm.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java index 361cdc05..a4ea69ce 100644 --- a/Others/BrianKernighanAlgorithm.java +++ b/Others/BrianKernighanAlgorithm.java @@ -4,7 +4,7 @@ import java.util.Scanner; * * @author Nishita Aggarwal * - * Brian Kernighan’s Algorithm + * Brian Kernighan’s Algorithm * algorithm to count the number of set bits in a given number * Subtraction of 1 from a number toggles all the bits (from * right to left) till the rightmost set bit(including the @@ -24,10 +24,10 @@ import java.util.Scanner; public class BrianKernighanAlgorithm { /** - * @param num: number in which we count the set bits - * - * @return int: Number of set bits - * */ + * @param num: number in which we count the set bits + * + * @return int: Number of set bits + * */ static int countSetBits(int num) { int cnt = 0; From 5cf220362a37c982a26dc81f2e87159fe99b2495 Mon Sep 17 00:00:00 2001 From: Nishita Aggarwal Date: Sun, 31 Dec 2017 15:02:56 +0530 Subject: [PATCH 16/17] spacing improved --- Others/BrianKernighanAlgorithm.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java index a4ea69ce..eaf43093 100644 --- a/Others/BrianKernighanAlgorithm.java +++ b/Others/BrianKernighanAlgorithm.java @@ -5,16 +5,18 @@ import java.util.Scanner; * @author Nishita Aggarwal * * Brian Kernighan’s Algorithm + * * algorithm to count the number of set bits in a given number + * * Subtraction of 1 from a number toggles all the bits (from * right to left) till the rightmost set bit(including the * rightmost set bit). * So if we subtract a number by 1 and do bitwise & with - * itself (n & (n-1)), we unset the rightmost set bit. + * itself i.e. (n & (n-1)), we unset the rightmost set bit. + * * If we do n & (n-1) in a loop and count the no of times loop * executes we get the set bit count. - * Number of iterations of the loop is equal to the number of - * set bits in a given integer. + * * * Time Complexity: O(logn) * From 1567b58a46c2a7f7d92af4e8cb0c712d78569a08 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Sun, 31 Dec 2017 06:47:51 -0800 Subject: [PATCH 17/17] Update BrianKernighanAlgorithm.java --- Others/BrianKernighanAlgorithm.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java index eaf43093..cb27eec1 100644 --- a/Others/BrianKernighanAlgorithm.java +++ b/Others/BrianKernighanAlgorithm.java @@ -8,14 +8,11 @@ import java.util.Scanner; * * algorithm to count the number of set bits in a given number * - * Subtraction of 1 from a number toggles all the bits (from - * right to left) till the rightmost set bit(including the + * Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the * rightmost set bit). - * So if we subtract a number by 1 and do bitwise & with - * itself i.e. (n & (n-1)), we unset the rightmost set bit. + * So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit. * - * If we do n & (n-1) in a loop and count the no of times loop - * executes we get the set bit count. + * If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count. * * * Time Complexity: O(logn)