From 10f748774db216c741f5bd7cb894e4512b1abb91 Mon Sep 17 00:00:00 2001 From: Akanksha Bhardwaj Date: Wed, 26 Oct 2016 13:38:47 +0530 Subject: [PATCH 1/3] Add solution to problem 5.1 'Insertion' Problem Statement : You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. That is, if M = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because M could not fully fit between bit 3 and bit 2. Input: N 10000000000, M 10011, i 2, j 6 Output: N = 10001001100 --- src/TechInterview/BitManipulation_1.java | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/TechInterview/BitManipulation_1.java diff --git a/src/TechInterview/BitManipulation_1.java b/src/TechInterview/BitManipulation_1.java new file mode 100644 index 0000000..0e7c9df --- /dev/null +++ b/src/TechInterview/BitManipulation_1.java @@ -0,0 +1,34 @@ +import java.util.Scanner; +/** + * This class has solution to problem #5.1 'Insertion' + * of chapter 'BitManipulation' from book 'Cracking the Coding Interview'. + * @author akanksha bhardwaj + */ +public class BitManipulation { + + public void insertion(String N, String M, int i, int j) { + int n = Integer.parseInt(N,2); + int m = Integer.parseInt(M,2); + /* divide new number in three parts + * first part from number n ,all bits from the most significant bit through j+1 (inclusive) + * second part from number m complete + * third part from number n , all bits from i-1 through 0 (inclusive) + */ + int first = n & (-1 << (j+1)); + int second = m << i; + int third = n & ( (1 << i) - 1); + int num = (first | second ) | third ; + System.out.println(Integer.toBinaryString(num)); + } + public static void main(String[] args) { + + BitManipulation bm = new BitManipulation(); + Scanner sc = new Scanner(System.in); + String N = sc.next(); + String M = sc.next(); + int i = sc.nextInt(); + int j = sc.nextInt(); + bm.insertion(N , M , i, j); + + } +} From e1516a126a3b9710cdba0c5b05ef10154c8d6215 Mon Sep 17 00:00:00 2001 From: Akanksha Bhardwaj Date: Thu, 27 Oct 2016 01:11:18 +0530 Subject: [PATCH 2/3] Add solution to problem 5.3 'Flip Bit to Win' --- src/TechInterview/BitManipulation2.java | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/TechInterview/BitManipulation2.java diff --git a/src/TechInterview/BitManipulation2.java b/src/TechInterview/BitManipulation2.java new file mode 100644 index 0000000..a40b022 --- /dev/null +++ b/src/TechInterview/BitManipulation2.java @@ -0,0 +1,41 @@ +import static java.lang.Integer.max; +import java.util.Scanner; +/** + * This class has solution to the problem #5.3 Flip Bit to Win + * of chapter 'BitManipulation' from book 'Cracking the Coding Interview'. + * @author akanksha bhardwaj + */ +public class BitManipulation2 { + + public void flipToWin(int n) { + String s = Integer.toBinaryString(n); + int l = s.length(); + char[] c = s.toCharArray(); + int prev_zindex = -1 ; // store index to previously encourtered 0 bit + int max_seq = 0 , curr_seq = 0; + for(int i = 0 ; i < l; i++) { + if(c[i] == '0') { + if(prev_zindex == -1) { // first 0 bit encountered + curr_seq = i + 1; + } + else { + max_seq = max(max_seq , curr_seq); // update answer with sequence value according to previous 0 bit + curr_seq = i - prev_zindex ; // update current sequence length according to this 0 bit + } + prev_zindex = i; + } + else { + curr_seq += 1; + } + } + max_seq = max(max_seq , curr_seq); + System.out.println(max_seq); + } + + public static void main(String[] args) { + BitManipulation2 bm = new BitManipulation2(); + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + bm.flipToWin(n); + } +} From 33b2ea0c4b6b6cfec5422aa5d29f17d20532e487 Mon Sep 17 00:00:00 2001 From: Akanksha Bhardwaj Date: Thu, 27 Oct 2016 01:13:03 +0530 Subject: [PATCH 3/3] fix file name --- .../{BitManipulation_1.java => BitManipulation1.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/TechInterview/{BitManipulation_1.java => BitManipulation1.java} (92%) diff --git a/src/TechInterview/BitManipulation_1.java b/src/TechInterview/BitManipulation1.java similarity index 92% rename from src/TechInterview/BitManipulation_1.java rename to src/TechInterview/BitManipulation1.java index 0e7c9df..6ab0873 100644 --- a/src/TechInterview/BitManipulation_1.java +++ b/src/TechInterview/BitManipulation1.java @@ -4,7 +4,7 @@ * of chapter 'BitManipulation' from book 'Cracking the Coding Interview'. * @author akanksha bhardwaj */ -public class BitManipulation { +public class BitManipulation1 { public void insertion(String N, String M, int i, int j) { int n = Integer.parseInt(N,2); @@ -22,7 +22,7 @@ public void insertion(String N, String M, int i, int j) { } public static void main(String[] args) { - BitManipulation bm = new BitManipulation(); + BitManipulation1 bm = new BitManipulation1(); Scanner sc = new Scanner(System.in); String N = sc.next(); String M = sc.next();