From 641cc6daee9bfdacf857c32d3f860ee2113822d6 Mon Sep 17 00:00:00 2001 From: BernieYi Date: Tue, 28 Nov 2017 18:40:12 -0500 Subject: [PATCH 1/7] Add files via upload --- .../java/com/bernie/CountPairsInArray.java | 40 +++ .../com/bernie/CountPairsInArray_input.txt | 9 + src/main/java/com/bernie/FindingPosition.java | 55 ++++ .../java/com/bernie/FindingPosition_input.txt | 5 + .../com/bernie/IntersectionOfTwoArray.txt | 7 + .../com/bernie/IntersectionOfTwoArrays.java | 63 +++++ .../java/com/bernie/RearrangeAnArray.java | 31 +++ .../com/bernie/RearrangeAnArray_Input.txt | 7 + src/main/java/com/bernie/WordBoggle.java | 244 ++++++++++++++++++ src/main/java/com/bernie/WordBoggle.txt | 13 + 10 files changed, 474 insertions(+) create mode 100644 src/main/java/com/bernie/CountPairsInArray.java create mode 100644 src/main/java/com/bernie/CountPairsInArray_input.txt create mode 100644 src/main/java/com/bernie/FindingPosition.java create mode 100644 src/main/java/com/bernie/FindingPosition_input.txt create mode 100644 src/main/java/com/bernie/IntersectionOfTwoArray.txt create mode 100644 src/main/java/com/bernie/IntersectionOfTwoArrays.java create mode 100644 src/main/java/com/bernie/RearrangeAnArray.java create mode 100644 src/main/java/com/bernie/RearrangeAnArray_Input.txt create mode 100644 src/main/java/com/bernie/WordBoggle.java create mode 100644 src/main/java/com/bernie/WordBoggle.txt diff --git a/src/main/java/com/bernie/CountPairsInArray.java b/src/main/java/com/bernie/CountPairsInArray.java new file mode 100644 index 0000000..359e1d0 --- /dev/null +++ b/src/main/java/com/bernie/CountPairsInArray.java @@ -0,0 +1,40 @@ +package com.bernie.gfg; + +import java.util.Scanner; + +public class CountPairsInArray { + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int testCase = sc.nextInt(); // total number of test cases + for (int t=0; tarr[j]) + count++; + } + } + return count; + } +} diff --git a/src/main/java/com/bernie/CountPairsInArray_input.txt b/src/main/java/com/bernie/CountPairsInArray_input.txt new file mode 100644 index 0000000..f6a6a26 --- /dev/null +++ b/src/main/java/com/bernie/CountPairsInArray_input.txt @@ -0,0 +1,9 @@ +2 + +7 + +5 0 10 2 4 1 6 + +4 + +8 4 2 1 diff --git a/src/main/java/com/bernie/FindingPosition.java b/src/main/java/com/bernie/FindingPosition.java new file mode 100644 index 0000000..7200e02 --- /dev/null +++ b/src/main/java/com/bernie/FindingPosition.java @@ -0,0 +1,55 @@ +package com.bernie.gfg; + +import java.util.Scanner; + +public class FindingPosition { + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int testCases = sc.nextInt(); + while (testCases > 0) + { + //findEven(IntStream.range(1, sc.nextInt()+1).boxed().toArray( Integer[]::new )); + //findEvenByLog(sc.nextInt()); + int n = sc.nextInt(); + int k = 1; + while(n>=k){ + k = k*2; + } + System.out.println(k/2); + + testCases--; + } + + sc.close(); + } + + /* + * Using this function will cause "OutOfMemry" error or "TimeOutException" + * + */ +// private static void findEven(Integer[] source) +// { +// Integer[] result = Arrays.stream(source).filter(s -> (Arrays.asList(source).indexOf(s) % 2 == 1)).toArray(Integer[]::new); +// +// if (result.length==1) +// System.out.println(result[0]); +// else if (result.length>0) +// findEven(result); +// else +// return; +// } + + private static void findEvenByLog(int max) + { + int i=log(max, 2); + System.out.println(1 << i); + } + + private static int log(int x, int base) + { + return (int) (Math.log(x) / Math.log(base)); + } + +} diff --git a/src/main/java/com/bernie/FindingPosition_input.txt b/src/main/java/com/bernie/FindingPosition_input.txt new file mode 100644 index 0000000..93a5826 --- /dev/null +++ b/src/main/java/com/bernie/FindingPosition_input.txt @@ -0,0 +1,5 @@ +4 +5 +10 +32 +100000000 diff --git a/src/main/java/com/bernie/IntersectionOfTwoArray.txt b/src/main/java/com/bernie/IntersectionOfTwoArray.txt new file mode 100644 index 0000000..b90847a --- /dev/null +++ b/src/main/java/com/bernie/IntersectionOfTwoArray.txt @@ -0,0 +1,7 @@ +2 +5 3 +89 24 75 11 23 +88 2 22 +6 5 +2 1 5 6 3 4 +6 4 5 3 7 diff --git a/src/main/java/com/bernie/IntersectionOfTwoArrays.java b/src/main/java/com/bernie/IntersectionOfTwoArrays.java new file mode 100644 index 0000000..11a4227 --- /dev/null +++ b/src/main/java/com/bernie/IntersectionOfTwoArrays.java @@ -0,0 +1,63 @@ +package com.bernie.gfg; +import java.util.HashSet; +import java.util.Scanner; +import java.util.stream.Stream; + +public class IntersectionOfTwoArrays { + // Prints union of arr1[0..m-1] and arr2[0..n-1] + static void printUnion(int arr1[], int arr2[]) + { + HashSet hs = new HashSet<>(); + + for (int i = 0; i < arr1.length; i++) + hs.add(arr1[i]); + for (int i = 0; i < arr2.length; i++) + hs.add(arr2[i]); + System.out.println(hs); + } + + // Prints intersection of arr1[0..m-1] and arr2[0..n-1] + static void printIntersection(int arr1[], int arr2[]) + { + HashSet hs = new HashSet<>(); + + for (int i = 0; i < arr1.length; i++) + hs.add(arr1[i]); + boolean found = false; + for (int i = 0; i < arr2.length; i++) + if (hs.contains(arr2[i])) + { + System.out.print(arr2[i] + " "); + if (!found) + found = true; + } + if (!found) + System.out.print("Zero"); + } + + // Driver method to test the above function + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); sc.nextLine(); + while (t>0) + { + String[] sizes = sc.nextLine().split(" "); + + int[] arr1 = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); + int[] arr2 = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); + + System.out.println("Union of two arrays is : "); + printUnion(arr1, arr2); + + System.out.println("Intersection of two arrays is : "); + printIntersection(arr1, arr2); + + System.out.println(); + t--; + } + + sc.close(); + } + +} diff --git a/src/main/java/com/bernie/RearrangeAnArray.java b/src/main/java/com/bernie/RearrangeAnArray.java new file mode 100644 index 0000000..a2e485e --- /dev/null +++ b/src/main/java/com/bernie/RearrangeAnArray.java @@ -0,0 +1,31 @@ +package com.bernie.gfg; + +import java.util.Scanner; + +public class RearrangeAnArray { + + public static void main(String[] args) { + Scanner sc= new Scanner(System.in); + int testCases = sc.nextInt(); + for (int t=0; t0) + { + sc.nextInt(); + sc.nextLine(); + HashSet dic = new HashSet(Arrays.asList(sc.nextLine().split(" "))); + wb.dimension = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + wb.boggle = wb.getBoggle(wb.dimension, sc.nextLine().split(" ")); + wb.boggling(dic, wb.boggle, wb.dimension); + if (dic.isEmpty()) + System.out.println("-1"); + else + { + dic.stream().sorted().forEach(d -> System.out.print(d + " ")); + System.out.println(); + } + t--; + } + + sc.close(); + } + + String[][] getBoggle(int[] dimension, String[] source) + { + int rowSize = dimension[0]; + int columnSize = dimension[1]; + String[][] boggle = new String[rowSize][columnSize]; + for(int j = 0; j < rowSize; j++) + { + for(int i = 0; i < columnSize; i++) + { + boggle[j][i] = source[j*columnSize + i]; + } + } + return boggle; + } + + void boggling(HashSet dic, String[][] boggle, int[] dimension) + { + // Get the first characters of each word from dictionary + Map starters = new HashMap(); + dic.stream().forEach(d -> starters.put(d, d.substring(0, 1))); + + HashMap> meta = measure(boggle); + for (String word: starters.keySet()) + { + String key = starters.get(word); + if (meta.containsKey(key)) + { + List points = meta.get(key); + boolean found = false; + for (Point startPoint:points) + { + HashSet usedPoints = new HashSet(); + if (backtracking(startPoint, 0, word, usedPoints)) + { + found = true; + break; + } + } + if (!found) + dic.remove(word); + } + else + { + dic.remove(word); + } + } + + } + + + boolean backtracking(Point currentPoint, int currentIndex, String word, HashSet usedPoints) + { + currentIndex++; + List destinations = findNeighbours(currentPoint, dimension, usedPoints); + boolean isFound= false; + if (word.length()==currentIndex) + { + if (boggle[currentPoint.x][currentPoint.y].equals(word.substring(currentIndex-1, currentIndex))) + { + isFound = true; + return true; + } else + { + return false; + } + } + String nextKey = word.substring(currentIndex, currentIndex+1); + for (Point d:destinations) + { + if (boggle[d.x][d.y].equals(nextKey)) + { + if (word.length()==currentIndex+1) + { + isFound = true; + return true; + } + else + { + // update current position & previous point + usedPoints.add(currentPoint); + Point nextPoint = new Point(d.x, d.y); + isFound=backtracking(nextPoint, currentIndex, word, usedPoints); + if(!isFound) + continue; + else + return true; + } + } + } + return isFound; + + } + + HashMap> measure(String[][] boggle) + { + HashMap> boggleMeta = new HashMap>(); + for (int i=0; i locations = new ArrayList(); + locations.add(new Point(i, j)); + boggleMeta.put(boggle[i][j], locations); + } + } + return boggleMeta; + } + + void findNextKey(Map starters, int currentIndex) + { + for (String word: starters.keySet()) + { + String newKey = null; + if (currentIndex <= word.length()) + { + newKey = word.substring(currentIndex-1, currentIndex); + starters.put(word, newKey); + } else + starters.put(word, null); + } + } + + List findNeighbours(Point current, int[] dimension, HashSet usedPoints) + { + List destinations = new ArrayList(); + int MIN_X = 0; + int MIN_Y = 0; + int MAX_X = dimension[0]-1; + int MAX_Y = dimension[1]-1; + int thisPosX = current.x; + int thisPosY = current.y; + + int startPosX = (thisPosX - 1 < MIN_X) ? thisPosX : thisPosX-1; + int startPosY = (thisPosY - 1 < MIN_Y) ? thisPosY : thisPosY-1; + int endPosX = (thisPosX + 1 > MAX_X) ? thisPosX : thisPosX+1; + int endPosY = (thisPosY + 1 > MAX_Y) ? thisPosY : thisPosY+1; + + // See how many are alive + for (int rowNum=startPosX; rowNum<=endPosX; rowNum++) { + for (int colNum=startPosY; colNum<=endPosY; colNum++) + { + if (thisPosX!=rowNum || thisPosY!=colNum) // do not add the same location as its own neighbours + { + Point des = new Point(rowNum, colNum); + if (!usedPoints.contains(des)) // do not all the previous point + // All the neighbors will be grid[rowNum][colNum] + destinations.add(des); + } + } + } + + return destinations; + } + + +} diff --git a/src/main/java/com/bernie/WordBoggle.txt b/src/main/java/com/bernie/WordBoggle.txt new file mode 100644 index 0000000..edbaf1b --- /dev/null +++ b/src/main/java/com/bernie/WordBoggle.txt @@ -0,0 +1,13 @@ +3 +5 +bc d ccb f c +1 2 +f e +1 +dfd ded fd e dec df +4 2 +f f d e f b b e +2 +db bcd +5 2 +d d b f e c b c d c From 2971d812b1c13a02d3073c3e5e8962e449173284 Mon Sep 17 00:00:00 2001 From: BernieYi Date: Fri, 1 Dec 2017 14:21:05 -0500 Subject: [PATCH 2/7] Add files via upload --- src/main/java/com/bernie/Rotate2DArray.java | 37 +++++++++++++++++++++ src/main/java/com/bernie/Rotate2DArray.txt | 5 +++ 2 files changed, 42 insertions(+) create mode 100644 src/main/java/com/bernie/Rotate2DArray.java create mode 100644 src/main/java/com/bernie/Rotate2DArray.txt diff --git a/src/main/java/com/bernie/Rotate2DArray.java b/src/main/java/com/bernie/Rotate2DArray.java new file mode 100644 index 0000000..82fbac8 --- /dev/null +++ b/src/main/java/com/bernie/Rotate2DArray.java @@ -0,0 +1,37 @@ +package com.bernie.gfg; + +import java.util.Scanner; +import java.util.stream.Stream; + +public class Rotate2DArray { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t>0) + { + int size = sc.nextInt(); + sc.nextLine(); + int[][] data = new int[size][size]; + int[] source = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + for(int j = 0; j < size; j++) + { + for(int i = 0; i < size; i++) + { + data[j][i] = source[j*size + i]; + } + } + + for (int i=0; i=0; j--) + { + System.out.print(data[j][i] + " "); + } + System.out.println(); + t--; + } + + } + +} diff --git a/src/main/java/com/bernie/Rotate2DArray.txt b/src/main/java/com/bernie/Rotate2DArray.txt new file mode 100644 index 0000000..4f32eb9 --- /dev/null +++ b/src/main/java/com/bernie/Rotate2DArray.txt @@ -0,0 +1,5 @@ +2 +3 +1 2 3 4 5 6 7 8 9 +2 +56 96 91 54 From 8eece747bdd261b287d98dc52f417c4a407fde4c Mon Sep 17 00:00:00 2001 From: BernieYi Date: Mon, 4 Dec 2017 16:00:58 -0500 Subject: [PATCH 3/7] Add "gfg" folder Add "gfg" folder --- .classpath | 8 +- .settings/org.eclipse.buildship.core.prefs | 1 - .settings/org.eclipse.jdt.core.prefs | 12 + bin/.gitignore | 2 + bin/Library.class | Bin 330 -> 330 bytes bin/LibraryTest.class | Bin 630 -> 0 bytes bin/com/bernie/javaLab/thread/Factorial.class | Bin 1301 -> 1301 bytes .../bernie/{ => gfg}/CountPairsInArray.java | 80 +-- .../{ => gfg}/CountPairsInArray_input.txt | 18 +- .../com/bernie/{ => gfg}/FindingPosition.java | 110 ++-- .../{ => gfg}/FindingPosition_input.txt | 10 +- .../{ => gfg}/IntersectionOfTwoArray.txt | 14 +- .../{ => gfg}/IntersectionOfTwoArrays.java | 126 ++--- .../bernie/{ => gfg}/RearrangeAnArray.java | 62 +-- .../{ => gfg}/RearrangeAnArray_Input.txt | 14 +- .../com/bernie/{ => gfg}/Rotate2DArray.java | 74 +-- .../com/bernie/{ => gfg}/Rotate2DArray.txt | 10 +- .../java/com/bernie/{ => gfg}/WordBoggle.java | 488 +++++++++--------- .../java/com/bernie/{ => gfg}/WordBoggle.txt | 26 +- 19 files changed, 535 insertions(+), 520 deletions(-) create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 bin/.gitignore delete mode 100644 bin/LibraryTest.class rename src/main/java/com/bernie/{ => gfg}/CountPairsInArray.java (95%) rename src/main/java/com/bernie/{ => gfg}/CountPairsInArray_input.txt (78%) rename src/main/java/com/bernie/{ => gfg}/FindingPosition.java (95%) rename src/main/java/com/bernie/{ => gfg}/FindingPosition_input.txt (80%) rename src/main/java/com/bernie/{ => gfg}/IntersectionOfTwoArray.txt (88%) rename src/main/java/com/bernie/{ => gfg}/IntersectionOfTwoArrays.java (96%) rename src/main/java/com/bernie/{ => gfg}/RearrangeAnArray.java (95%) rename src/main/java/com/bernie/{ => gfg}/RearrangeAnArray_Input.txt (81%) rename src/main/java/com/bernie/{ => gfg}/Rotate2DArray.java (95%) rename src/main/java/com/bernie/{ => gfg}/Rotate2DArray.txt (87%) rename src/main/java/com/bernie/{ => gfg}/WordBoggle.java (95%) rename src/main/java/com/bernie/{ => gfg}/WordBoggle.txt (88%) diff --git a/.classpath b/.classpath index 26e610c..2429f58 100644 --- a/.classpath +++ b/.classpath @@ -1,8 +1,10 @@ - - - + + + + + diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs index 03931c0..e889521 100644 --- a/.settings/org.eclipse.buildship.core.prefs +++ b/.settings/org.eclipse.buildship.core.prefs @@ -1,3 +1,2 @@ -connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) connection.project.dir= eclipse.preferences.version=1 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..b76ee8e --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=9 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=9 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=9 diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..568b3ff --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/Library.class +/com/ diff --git a/bin/Library.class b/bin/Library.class index 2e6e624bda36e45eae3bb9ab9f93c82d73955ef0..fce7837fa057de59d97193effec490e5e2df7466 100644 GIT binary patch delta 17 ZcmX@bbc%`N)W2Q(7#J8#H*z>J0sug>1`+@O delta 17 ZcmX@bbc%`N)W2Q(7#J8#HgY&I0sug+1`z-N diff --git a/bin/LibraryTest.class b/bin/LibraryTest.class deleted file mode 100644 index a9ec6c8b4f6b074a8d2e9c4f99c02185a8645677..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 630 zcmZWm+fLg+5Iti8PMiQqC~4`9^rCX9gkN|Is)Q=_$)!@0Mk;TcWR+~>>`Lo(rG8dl z08yzQz(*lwjjN#cWoBpQ%$eiko7?L@0FLoCL_nw~YGj4|IgptPQ6V(v;)jS+p{Mbe z(OixlVeLd|sv0#yDl%H$7pqSUI8g*AMn0>YLn- zwo-SE83JEn-;ccrI?4A3qlHE_bg;>ICW()A< E4jp@m4*&oF diff --git a/bin/com/bernie/javaLab/thread/Factorial.class b/bin/com/bernie/javaLab/thread/Factorial.class index dccfa79993ac8ed62753085323d937c6e8ec8aeb..4674111bc5dbdb262819c24d54912cb9eddb50e8 100644 GIT binary patch delta 17 ZcmbQrHI<9w)W2Q(7#J8#H*)Z@0suOl1&06t delta 17 ZcmbQrHI<9w)W2Q(7#J8#HgfQ?0suOg1%?0s diff --git a/src/main/java/com/bernie/CountPairsInArray.java b/src/main/java/com/bernie/gfg/CountPairsInArray.java similarity index 95% rename from src/main/java/com/bernie/CountPairsInArray.java rename to src/main/java/com/bernie/gfg/CountPairsInArray.java index 359e1d0..50b2043 100644 --- a/src/main/java/com/bernie/CountPairsInArray.java +++ b/src/main/java/com/bernie/gfg/CountPairsInArray.java @@ -1,40 +1,40 @@ -package com.bernie.gfg; - -import java.util.Scanner; - -public class CountPairsInArray { - public static void main(String[] args) - { - Scanner sc = new Scanner(System.in); - int testCase = sc.nextInt(); // total number of test cases - for (int t=0; tarr[j]) - count++; - } - } - return count; - } -} +package com.bernie.gfg; + +import java.util.Scanner; + +public class CountPairsInArray { + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int testCase = sc.nextInt(); // total number of test cases + for (int t=0; tarr[j]) + count++; + } + } + return count; + } +} diff --git a/src/main/java/com/bernie/CountPairsInArray_input.txt b/src/main/java/com/bernie/gfg/CountPairsInArray_input.txt similarity index 78% rename from src/main/java/com/bernie/CountPairsInArray_input.txt rename to src/main/java/com/bernie/gfg/CountPairsInArray_input.txt index f6a6a26..3670537 100644 --- a/src/main/java/com/bernie/CountPairsInArray_input.txt +++ b/src/main/java/com/bernie/gfg/CountPairsInArray_input.txt @@ -1,9 +1,9 @@ -2 - -7 - -5 0 10 2 4 1 6 - -4 - -8 4 2 1 +2 + +7 + +5 0 10 2 4 1 6 + +4 + +8 4 2 1 diff --git a/src/main/java/com/bernie/FindingPosition.java b/src/main/java/com/bernie/gfg/FindingPosition.java similarity index 95% rename from src/main/java/com/bernie/FindingPosition.java rename to src/main/java/com/bernie/gfg/FindingPosition.java index 7200e02..bc43b32 100644 --- a/src/main/java/com/bernie/FindingPosition.java +++ b/src/main/java/com/bernie/gfg/FindingPosition.java @@ -1,55 +1,55 @@ -package com.bernie.gfg; - -import java.util.Scanner; - -public class FindingPosition { - - public static void main(String[] args) - { - Scanner sc = new Scanner(System.in); - int testCases = sc.nextInt(); - while (testCases > 0) - { - //findEven(IntStream.range(1, sc.nextInt()+1).boxed().toArray( Integer[]::new )); - //findEvenByLog(sc.nextInt()); - int n = sc.nextInt(); - int k = 1; - while(n>=k){ - k = k*2; - } - System.out.println(k/2); - - testCases--; - } - - sc.close(); - } - - /* - * Using this function will cause "OutOfMemry" error or "TimeOutException" - * - */ -// private static void findEven(Integer[] source) -// { -// Integer[] result = Arrays.stream(source).filter(s -> (Arrays.asList(source).indexOf(s) % 2 == 1)).toArray(Integer[]::new); -// -// if (result.length==1) -// System.out.println(result[0]); -// else if (result.length>0) -// findEven(result); -// else -// return; -// } - - private static void findEvenByLog(int max) - { - int i=log(max, 2); - System.out.println(1 << i); - } - - private static int log(int x, int base) - { - return (int) (Math.log(x) / Math.log(base)); - } - -} +package com.bernie.gfg; + +import java.util.Scanner; + +public class FindingPosition { + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int testCases = sc.nextInt(); + while (testCases > 0) + { + //findEven(IntStream.range(1, sc.nextInt()+1).boxed().toArray( Integer[]::new )); + //findEvenByLog(sc.nextInt()); + int n = sc.nextInt(); + int k = 1; + while(n>=k){ + k = k*2; + } + System.out.println(k/2); + + testCases--; + } + + sc.close(); + } + + /* + * Using this function will cause "OutOfMemry" error or "TimeOutException" + * + */ +// private static void findEven(Integer[] source) +// { +// Integer[] result = Arrays.stream(source).filter(s -> (Arrays.asList(source).indexOf(s) % 2 == 1)).toArray(Integer[]::new); +// +// if (result.length==1) +// System.out.println(result[0]); +// else if (result.length>0) +// findEven(result); +// else +// return; +// } + + private static void findEvenByLog(int max) + { + int i=log(max, 2); + System.out.println(1 << i); + } + + private static int log(int x, int base) + { + return (int) (Math.log(x) / Math.log(base)); + } + +} diff --git a/src/main/java/com/bernie/FindingPosition_input.txt b/src/main/java/com/bernie/gfg/FindingPosition_input.txt similarity index 80% rename from src/main/java/com/bernie/FindingPosition_input.txt rename to src/main/java/com/bernie/gfg/FindingPosition_input.txt index 93a5826..d99ab43 100644 --- a/src/main/java/com/bernie/FindingPosition_input.txt +++ b/src/main/java/com/bernie/gfg/FindingPosition_input.txt @@ -1,5 +1,5 @@ -4 -5 -10 -32 -100000000 +4 +5 +10 +32 +100000000 diff --git a/src/main/java/com/bernie/IntersectionOfTwoArray.txt b/src/main/java/com/bernie/gfg/IntersectionOfTwoArray.txt similarity index 88% rename from src/main/java/com/bernie/IntersectionOfTwoArray.txt rename to src/main/java/com/bernie/gfg/IntersectionOfTwoArray.txt index b90847a..004759f 100644 --- a/src/main/java/com/bernie/IntersectionOfTwoArray.txt +++ b/src/main/java/com/bernie/gfg/IntersectionOfTwoArray.txt @@ -1,7 +1,7 @@ -2 -5 3 -89 24 75 11 23 -88 2 22 -6 5 -2 1 5 6 3 4 -6 4 5 3 7 +2 +5 3 +89 24 75 11 23 +88 2 22 +6 5 +2 1 5 6 3 4 +6 4 5 3 7 diff --git a/src/main/java/com/bernie/IntersectionOfTwoArrays.java b/src/main/java/com/bernie/gfg/IntersectionOfTwoArrays.java similarity index 96% rename from src/main/java/com/bernie/IntersectionOfTwoArrays.java rename to src/main/java/com/bernie/gfg/IntersectionOfTwoArrays.java index 11a4227..b95c6b2 100644 --- a/src/main/java/com/bernie/IntersectionOfTwoArrays.java +++ b/src/main/java/com/bernie/gfg/IntersectionOfTwoArrays.java @@ -1,63 +1,63 @@ -package com.bernie.gfg; -import java.util.HashSet; -import java.util.Scanner; -import java.util.stream.Stream; - -public class IntersectionOfTwoArrays { - // Prints union of arr1[0..m-1] and arr2[0..n-1] - static void printUnion(int arr1[], int arr2[]) - { - HashSet hs = new HashSet<>(); - - for (int i = 0; i < arr1.length; i++) - hs.add(arr1[i]); - for (int i = 0; i < arr2.length; i++) - hs.add(arr2[i]); - System.out.println(hs); - } - - // Prints intersection of arr1[0..m-1] and arr2[0..n-1] - static void printIntersection(int arr1[], int arr2[]) - { - HashSet hs = new HashSet<>(); - - for (int i = 0; i < arr1.length; i++) - hs.add(arr1[i]); - boolean found = false; - for (int i = 0; i < arr2.length; i++) - if (hs.contains(arr2[i])) - { - System.out.print(arr2[i] + " "); - if (!found) - found = true; - } - if (!found) - System.out.print("Zero"); - } - - // Driver method to test the above function - public static void main(String[] args) - { - Scanner sc = new Scanner(System.in); - int t = sc.nextInt(); sc.nextLine(); - while (t>0) - { - String[] sizes = sc.nextLine().split(" "); - - int[] arr1 = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); - int[] arr2 = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); - - System.out.println("Union of two arrays is : "); - printUnion(arr1, arr2); - - System.out.println("Intersection of two arrays is : "); - printIntersection(arr1, arr2); - - System.out.println(); - t--; - } - - sc.close(); - } - -} +package com.bernie.gfg; +import java.util.HashSet; +import java.util.Scanner; +import java.util.stream.Stream; + +public class IntersectionOfTwoArrays { + // Prints union of arr1[0..m-1] and arr2[0..n-1] + static void printUnion(int arr1[], int arr2[]) + { + HashSet hs = new HashSet<>(); + + for (int i = 0; i < arr1.length; i++) + hs.add(arr1[i]); + for (int i = 0; i < arr2.length; i++) + hs.add(arr2[i]); + System.out.println(hs); + } + + // Prints intersection of arr1[0..m-1] and arr2[0..n-1] + static void printIntersection(int arr1[], int arr2[]) + { + HashSet hs = new HashSet<>(); + + for (int i = 0; i < arr1.length; i++) + hs.add(arr1[i]); + boolean found = false; + for (int i = 0; i < arr2.length; i++) + if (hs.contains(arr2[i])) + { + System.out.print(arr2[i] + " "); + if (!found) + found = true; + } + if (!found) + System.out.print("Zero"); + } + + // Driver method to test the above function + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); sc.nextLine(); + while (t>0) + { + String[] sizes = sc.nextLine().split(" "); + + int[] arr1 = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); + int[] arr2 = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); + + System.out.println("Union of two arrays is : "); + printUnion(arr1, arr2); + + System.out.println("Intersection of two arrays is : "); + printIntersection(arr1, arr2); + + System.out.println(); + t--; + } + + sc.close(); + } + +} diff --git a/src/main/java/com/bernie/RearrangeAnArray.java b/src/main/java/com/bernie/gfg/RearrangeAnArray.java similarity index 95% rename from src/main/java/com/bernie/RearrangeAnArray.java rename to src/main/java/com/bernie/gfg/RearrangeAnArray.java index a2e485e..0ded4e4 100644 --- a/src/main/java/com/bernie/RearrangeAnArray.java +++ b/src/main/java/com/bernie/gfg/RearrangeAnArray.java @@ -1,31 +1,31 @@ -package com.bernie.gfg; - -import java.util.Scanner; - -public class RearrangeAnArray { - - public static void main(String[] args) { - Scanner sc= new Scanner(System.in); - int testCases = sc.nextInt(); - for (int t=0; t0) - { - int size = sc.nextInt(); - sc.nextLine(); - int[][] data = new int[size][size]; - int[] source = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); - for(int j = 0; j < size; j++) - { - for(int i = 0; i < size; i++) - { - data[j][i] = source[j*size + i]; - } - } - - for (int i=0; i=0; j--) - { - System.out.print(data[j][i] + " "); - } - System.out.println(); - t--; - } - - } - -} +package com.bernie.gfg; + +import java.util.Scanner; +import java.util.stream.Stream; + +public class Rotate2DArray { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t>0) + { + int size = sc.nextInt(); + sc.nextLine(); + int[][] data = new int[size][size]; + int[] source = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + for(int j = 0; j < size; j++) + { + for(int i = 0; i < size; i++) + { + data[j][i] = source[j*size + i]; + } + } + + for (int i=0; i=0; j--) + { + System.out.print(data[j][i] + " "); + } + System.out.println(); + t--; + } + + } + +} diff --git a/src/main/java/com/bernie/Rotate2DArray.txt b/src/main/java/com/bernie/gfg/Rotate2DArray.txt similarity index 87% rename from src/main/java/com/bernie/Rotate2DArray.txt rename to src/main/java/com/bernie/gfg/Rotate2DArray.txt index 4f32eb9..aab2e72 100644 --- a/src/main/java/com/bernie/Rotate2DArray.txt +++ b/src/main/java/com/bernie/gfg/Rotate2DArray.txt @@ -1,5 +1,5 @@ -2 -3 -1 2 3 4 5 6 7 8 9 -2 -56 96 91 54 +2 +3 +1 2 3 4 5 6 7 8 9 +2 +56 96 91 54 diff --git a/src/main/java/com/bernie/WordBoggle.java b/src/main/java/com/bernie/gfg/WordBoggle.java similarity index 95% rename from src/main/java/com/bernie/WordBoggle.java rename to src/main/java/com/bernie/gfg/WordBoggle.java index 05821ed..791a038 100644 --- a/src/main/java/com/bernie/WordBoggle.java +++ b/src/main/java/com/bernie/gfg/WordBoggle.java @@ -1,244 +1,244 @@ -package com.bernie.gfg; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Scanner; -import java.util.stream.Stream; - -public class WordBoggle { - - WordBoggle() - { - } - - int[] dimension; - - String[][] boggle; - - class Point - { - int x; - int y; - - Point(){ - - } - - Point(int x, int y) - { - this.x = x; - this.y = y; - } - - @Override - public boolean equals(Object other) - { - if (this == other) - return true; - - if (!(other instanceof Point)) - return false; - - Point otherPoint = (Point) other; - return otherPoint.x == x && otherPoint.y == y; - } - - - @Override - public int hashCode() - { - return (Integer.toString(x) + "," + Integer.toString(y)).hashCode(); - } - - } - - public static void main(String[] args) { - - WordBoggle wb = new WordBoggle(); - Scanner sc = new Scanner(System.in); - int t = sc.nextInt(); - while (t>0) - { - sc.nextInt(); - sc.nextLine(); - HashSet dic = new HashSet(Arrays.asList(sc.nextLine().split(" "))); - wb.dimension = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); - wb.boggle = wb.getBoggle(wb.dimension, sc.nextLine().split(" ")); - wb.boggling(dic, wb.boggle, wb.dimension); - if (dic.isEmpty()) - System.out.println("-1"); - else - { - dic.stream().sorted().forEach(d -> System.out.print(d + " ")); - System.out.println(); - } - t--; - } - - sc.close(); - } - - String[][] getBoggle(int[] dimension, String[] source) - { - int rowSize = dimension[0]; - int columnSize = dimension[1]; - String[][] boggle = new String[rowSize][columnSize]; - for(int j = 0; j < rowSize; j++) - { - for(int i = 0; i < columnSize; i++) - { - boggle[j][i] = source[j*columnSize + i]; - } - } - return boggle; - } - - void boggling(HashSet dic, String[][] boggle, int[] dimension) - { - // Get the first characters of each word from dictionary - Map starters = new HashMap(); - dic.stream().forEach(d -> starters.put(d, d.substring(0, 1))); - - HashMap> meta = measure(boggle); - for (String word: starters.keySet()) - { - String key = starters.get(word); - if (meta.containsKey(key)) - { - List points = meta.get(key); - boolean found = false; - for (Point startPoint:points) - { - HashSet usedPoints = new HashSet(); - if (backtracking(startPoint, 0, word, usedPoints)) - { - found = true; - break; - } - } - if (!found) - dic.remove(word); - } - else - { - dic.remove(word); - } - } - - } - - - boolean backtracking(Point currentPoint, int currentIndex, String word, HashSet usedPoints) - { - currentIndex++; - List destinations = findNeighbours(currentPoint, dimension, usedPoints); - boolean isFound= false; - if (word.length()==currentIndex) - { - if (boggle[currentPoint.x][currentPoint.y].equals(word.substring(currentIndex-1, currentIndex))) - { - isFound = true; - return true; - } else - { - return false; - } - } - String nextKey = word.substring(currentIndex, currentIndex+1); - for (Point d:destinations) - { - if (boggle[d.x][d.y].equals(nextKey)) - { - if (word.length()==currentIndex+1) - { - isFound = true; - return true; - } - else - { - // update current position & previous point - usedPoints.add(currentPoint); - Point nextPoint = new Point(d.x, d.y); - isFound=backtracking(nextPoint, currentIndex, word, usedPoints); - if(!isFound) - continue; - else - return true; - } - } - } - return isFound; - - } - - HashMap> measure(String[][] boggle) - { - HashMap> boggleMeta = new HashMap>(); - for (int i=0; i locations = new ArrayList(); - locations.add(new Point(i, j)); - boggleMeta.put(boggle[i][j], locations); - } - } - return boggleMeta; - } - - void findNextKey(Map starters, int currentIndex) - { - for (String word: starters.keySet()) - { - String newKey = null; - if (currentIndex <= word.length()) - { - newKey = word.substring(currentIndex-1, currentIndex); - starters.put(word, newKey); - } else - starters.put(word, null); - } - } - - List findNeighbours(Point current, int[] dimension, HashSet usedPoints) - { - List destinations = new ArrayList(); - int MIN_X = 0; - int MIN_Y = 0; - int MAX_X = dimension[0]-1; - int MAX_Y = dimension[1]-1; - int thisPosX = current.x; - int thisPosY = current.y; - - int startPosX = (thisPosX - 1 < MIN_X) ? thisPosX : thisPosX-1; - int startPosY = (thisPosY - 1 < MIN_Y) ? thisPosY : thisPosY-1; - int endPosX = (thisPosX + 1 > MAX_X) ? thisPosX : thisPosX+1; - int endPosY = (thisPosY + 1 > MAX_Y) ? thisPosY : thisPosY+1; - - // See how many are alive - for (int rowNum=startPosX; rowNum<=endPosX; rowNum++) { - for (int colNum=startPosY; colNum<=endPosY; colNum++) - { - if (thisPosX!=rowNum || thisPosY!=colNum) // do not add the same location as its own neighbours - { - Point des = new Point(rowNum, colNum); - if (!usedPoints.contains(des)) // do not all the previous point - // All the neighbors will be grid[rowNum][colNum] - destinations.add(des); - } - } - } - - return destinations; - } - - -} +package com.bernie.gfg; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.stream.Stream; + +public class WordBoggle { + + WordBoggle() + { + } + + int[] dimension; + + String[][] boggle; + + class Point + { + int x; + int y; + + Point(){ + + } + + Point(int x, int y) + { + this.x = x; + this.y = y; + } + + @Override + public boolean equals(Object other) + { + if (this == other) + return true; + + if (!(other instanceof Point)) + return false; + + Point otherPoint = (Point) other; + return otherPoint.x == x && otherPoint.y == y; + } + + + @Override + public int hashCode() + { + return (Integer.toString(x) + "," + Integer.toString(y)).hashCode(); + } + + } + + public static void main(String[] args) { + + WordBoggle wb = new WordBoggle(); + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t>0) + { + sc.nextInt(); + sc.nextLine(); + HashSet dic = new HashSet(Arrays.asList(sc.nextLine().split(" "))); + wb.dimension = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + wb.boggle = wb.getBoggle(wb.dimension, sc.nextLine().split(" ")); + wb.boggling(dic, wb.boggle, wb.dimension); + if (dic.isEmpty()) + System.out.println("-1"); + else + { + dic.stream().sorted().forEach(d -> System.out.print(d + " ")); + System.out.println(); + } + t--; + } + + sc.close(); + } + + String[][] getBoggle(int[] dimension, String[] source) + { + int rowSize = dimension[0]; + int columnSize = dimension[1]; + String[][] boggle = new String[rowSize][columnSize]; + for(int j = 0; j < rowSize; j++) + { + for(int i = 0; i < columnSize; i++) + { + boggle[j][i] = source[j*columnSize + i]; + } + } + return boggle; + } + + void boggling(HashSet dic, String[][] boggle, int[] dimension) + { + // Get the first characters of each word from dictionary + Map starters = new HashMap(); + dic.stream().forEach(d -> starters.put(d, d.substring(0, 1))); + + HashMap> meta = measure(boggle); + for (String word: starters.keySet()) + { + String key = starters.get(word); + if (meta.containsKey(key)) + { + List points = meta.get(key); + boolean found = false; + for (Point startPoint:points) + { + HashSet usedPoints = new HashSet(); + if (backtracking(startPoint, 0, word, usedPoints)) + { + found = true; + break; + } + } + if (!found) + dic.remove(word); + } + else + { + dic.remove(word); + } + } + + } + + + boolean backtracking(Point currentPoint, int currentIndex, String word, HashSet usedPoints) + { + currentIndex++; + List destinations = findNeighbours(currentPoint, dimension, usedPoints); + boolean isFound= false; + if (word.length()==currentIndex) + { + if (boggle[currentPoint.x][currentPoint.y].equals(word.substring(currentIndex-1, currentIndex))) + { + isFound = true; + return true; + } else + { + return false; + } + } + String nextKey = word.substring(currentIndex, currentIndex+1); + for (Point d:destinations) + { + if (boggle[d.x][d.y].equals(nextKey)) + { + if (word.length()==currentIndex+1) + { + isFound = true; + return true; + } + else + { + // update current position & previous point + usedPoints.add(currentPoint); + Point nextPoint = new Point(d.x, d.y); + isFound=backtracking(nextPoint, currentIndex, word, usedPoints); + if(!isFound) + continue; + else + return true; + } + } + } + return isFound; + + } + + HashMap> measure(String[][] boggle) + { + HashMap> boggleMeta = new HashMap>(); + for (int i=0; i locations = new ArrayList(); + locations.add(new Point(i, j)); + boggleMeta.put(boggle[i][j], locations); + } + } + return boggleMeta; + } + + void findNextKey(Map starters, int currentIndex) + { + for (String word: starters.keySet()) + { + String newKey = null; + if (currentIndex <= word.length()) + { + newKey = word.substring(currentIndex-1, currentIndex); + starters.put(word, newKey); + } else + starters.put(word, null); + } + } + + List findNeighbours(Point current, int[] dimension, HashSet usedPoints) + { + List destinations = new ArrayList(); + int MIN_X = 0; + int MIN_Y = 0; + int MAX_X = dimension[0]-1; + int MAX_Y = dimension[1]-1; + int thisPosX = current.x; + int thisPosY = current.y; + + int startPosX = (thisPosX - 1 < MIN_X) ? thisPosX : thisPosX-1; + int startPosY = (thisPosY - 1 < MIN_Y) ? thisPosY : thisPosY-1; + int endPosX = (thisPosX + 1 > MAX_X) ? thisPosX : thisPosX+1; + int endPosY = (thisPosY + 1 > MAX_Y) ? thisPosY : thisPosY+1; + + // See how many are alive + for (int rowNum=startPosX; rowNum<=endPosX; rowNum++) { + for (int colNum=startPosY; colNum<=endPosY; colNum++) + { + if (thisPosX!=rowNum || thisPosY!=colNum) // do not add the same location as its own neighbours + { + Point des = new Point(rowNum, colNum); + if (!usedPoints.contains(des)) // do not all the previous point + // All the neighbors will be grid[rowNum][colNum] + destinations.add(des); + } + } + } + + return destinations; + } + + +} diff --git a/src/main/java/com/bernie/WordBoggle.txt b/src/main/java/com/bernie/gfg/WordBoggle.txt similarity index 88% rename from src/main/java/com/bernie/WordBoggle.txt rename to src/main/java/com/bernie/gfg/WordBoggle.txt index edbaf1b..299c3bf 100644 --- a/src/main/java/com/bernie/WordBoggle.txt +++ b/src/main/java/com/bernie/gfg/WordBoggle.txt @@ -1,13 +1,13 @@ -3 -5 -bc d ccb f c -1 2 -f e -1 -dfd ded fd e dec df -4 2 -f f d e f b b e -2 -db bcd -5 2 -d d b f e c b c d c +3 +5 +bc d ccb f c +1 2 +f e +1 +dfd ded fd e dec df +4 2 +f f d e f b b e +2 +db bcd +5 2 +d d b f e c b c d c From 275a700d493d363de12abd206ea56d2eae9526f2 Mon Sep 17 00:00:00 2001 From: BernieYi Date: Tue, 5 Dec 2017 13:13:00 -0500 Subject: [PATCH 4/7] Add for gfg practice --- .../java/com/bernie/gfg/OnceAndTwice.java | 47 +++++++++++++++++++ src/main/java/com/bernie/gfg/OnceAndTwice.txt | 7 +++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/com/bernie/gfg/OnceAndTwice.java create mode 100644 src/main/java/com/bernie/gfg/OnceAndTwice.txt diff --git a/src/main/java/com/bernie/gfg/OnceAndTwice.java b/src/main/java/com/bernie/gfg/OnceAndTwice.java new file mode 100644 index 0000000..3175761 --- /dev/null +++ b/src/main/java/com/bernie/gfg/OnceAndTwice.java @@ -0,0 +1,47 @@ +package com.bernie.gfg; + +import java.util.Scanner; +import java.util.stream.Stream; + +public class OnceAndTwice { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t>0) + { + int size = sc.nextInt(); + sc.nextLine(); + int[] array = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sorted().toArray(); + int count = 1; + if (size==1) + { + System.out.println(array[0]); + t--; + continue; + } + + for (int i=1; i Date: Mon, 11 Dec 2017 12:31:41 -0500 Subject: [PATCH 5/7] Add files via upload Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have same width and the width is 1 unit. https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram/0 --- src/main/java/com/bernie/gfg/MaxRectArea.java | 59 +++++++++++++++++++ src/main/java/com/bernie/gfg/MaxRectArea.txt | 3 + 2 files changed, 62 insertions(+) create mode 100644 src/main/java/com/bernie/gfg/MaxRectArea.java create mode 100644 src/main/java/com/bernie/gfg/MaxRectArea.txt diff --git a/src/main/java/com/bernie/gfg/MaxRectArea.java b/src/main/java/com/bernie/gfg/MaxRectArea.java new file mode 100644 index 0000000..61b2725 --- /dev/null +++ b/src/main/java/com/bernie/gfg/MaxRectArea.java @@ -0,0 +1,59 @@ +package com.bernie.gfg; + +import java.util.Scanner; +import java.util.stream.Stream; + +public class MaxRectArea { + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + + int t = sc.nextInt(); + while (t>0) + { + int size = sc.nextInt(); + sc.nextLine(); + int[] arr = Stream.of(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + System.out.println(maxArea(arr, size)); + t--; + } + + sc.close(); + } + + private static int maxArea(int[] arr, int size) + { + int maxArea = -1; + + for (int i=0; i=0 && arr[i-j]>=arr[i]) + length++; + else + leftEnded = true; + + if (!rightEnded && i+j=arr[i]) + length++; + else + rightEnded = true; + + if (leftEnded & rightEnded) + break; + } + + int area = length * hight; + if (area > maxArea) maxArea = area; + } + + return maxArea; + } + +} diff --git a/src/main/java/com/bernie/gfg/MaxRectArea.txt b/src/main/java/com/bernie/gfg/MaxRectArea.txt new file mode 100644 index 0000000..4ce6603 --- /dev/null +++ b/src/main/java/com/bernie/gfg/MaxRectArea.txt @@ -0,0 +1,3 @@ +1 +7 +6 2 5 4 5 1 6 From 8b9f238e6a9d7ca6fb2d6816a30cb84db756d43a Mon Sep 17 00:00:00 2001 From: BernieYi Date: Wed, 13 Dec 2017 12:28:26 -0500 Subject: [PATCH 6/7] Add files via upload Non Repeating Characters: ArrayList.add() keep the sequence of adding. --- .../bernie/gfg/NonRepeatingCharacters.java | 83 +++++++++++++++++++ .../com/bernie/gfg/NonRepeatingCharacters.txt | 7 ++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/com/bernie/gfg/NonRepeatingCharacters.java create mode 100644 src/main/java/com/bernie/gfg/NonRepeatingCharacters.txt diff --git a/src/main/java/com/bernie/gfg/NonRepeatingCharacters.java b/src/main/java/com/bernie/gfg/NonRepeatingCharacters.java new file mode 100644 index 0000000..0ec3ed5 --- /dev/null +++ b/src/main/java/com/bernie/gfg/NonRepeatingCharacters.java @@ -0,0 +1,83 @@ +package com.bernie.gfg; + +/** + * Given a string s consisting of lowercase Latin Letters, find the first non repeating character in s. + + Input: + + The first line contains T denoting the number of testcases. Then follows description of testcases. + Each case begins with a single integer N denoting the length of string. The next line contains the string s. + + Output: + + For each testcase, print the first non repeating character present in string. + + Print -1 if there is no non repeating character. + + Constraints: + + 1<=T<=50 + 1<=N<=100 + + Example: + + Input : + + 3 + 5 + hello + 12 + zxvczbtxyzvy + 6 + xxyyzz + + Output : + + h + c + -1 + + */ + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Scanner; + +public class NonRepeatingCharacters { + + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + + int t = sc.nextInt(); + while (t>0) + { + int length = sc.nextInt(); + sc.nextLine(); + String input = sc.nextLine(); + ArrayList singles = new ArrayList(); + HashSet multiples = new HashSet(); + for (int i=0; i Date: Wed, 13 Dec 2017 17:10:10 -0500 Subject: [PATCH 7/7] Add files via upload PriorityQueue can keep the order based on a defined comparator and use the head of the heap in order. --- .../bernie/gfg/LargestNumberfromArray.java | 92 +++++++++++++++++++ .../com/bernie/gfg/LargestNumberfromArray.txt | 6 ++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/com/bernie/gfg/LargestNumberfromArray.java create mode 100644 src/main/java/com/bernie/gfg/LargestNumberfromArray.txt diff --git a/src/main/java/com/bernie/gfg/LargestNumberfromArray.java b/src/main/java/com/bernie/gfg/LargestNumberfromArray.java new file mode 100644 index 0000000..adeb3c9 --- /dev/null +++ b/src/main/java/com/bernie/gfg/LargestNumberfromArray.java @@ -0,0 +1,92 @@ +package com.bernie.gfg; + + +/* + * Given a list of non negative integers, arrange them in such a manner that they form the largest number possible. + + The result is going to be very large, hence return the result in the form of a string. + + Input: + + The first line of input consists number of the test cases. The description of T test cases is as follows: + + The first line of each test case contains the size of the array, and the second line has the elements of the array. + + + Output: + + In each separate line print the largest number formed by arranging the elements of the array in the form of a string. + + + Constraints: + + 1 <= T <= 70 + 1 <= N <= 100 + 0 <= A[i] <= 1000 + + + Example: + + Input: + + 2 + 5 + 3 30 34 5 9 + 4 + 54 546 548 60 + + Output: + + 9534330 + 6054854654 + */ + + + +import java.util.PriorityQueue; +import java.util.Scanner; + +class Element implements Comparable +{ + int value; + Element(int value) + { + this.value = value; + } + + public int compareTo(Element e2) + { + String str1 = String.valueOf(this.value); + String str2 = String.valueOf(e2.value); + String str1str2 = str1+str2; + String str2str1 = str2+str1; + return Integer.valueOf(str2str1)-Integer.valueOf(str1str2); + } +} + +public class LargestNumberfromArray +{ + public static void main (String[] args) + { + Scanner scnr = new Scanner(System.in); + int testCases = scnr.nextInt(); + while(testCases-->0) + { + int testLen = scnr.nextInt(); + // An unbounded priority queue based on a priority heap + // The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time + PriorityQueue pq = new PriorityQueue(); + for(int i=0;i