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 2e6e624..fce7837 100644 Binary files a/bin/Library.class and b/bin/Library.class differ diff --git a/bin/LibraryTest.class b/bin/LibraryTest.class deleted file mode 100644 index a9ec6c8..0000000 Binary files a/bin/LibraryTest.class and /dev/null differ diff --git a/bin/com/bernie/javaLab/thread/Factorial.class b/bin/com/bernie/javaLab/thread/Factorial.class index dccfa79..4674111 100644 Binary files a/bin/com/bernie/javaLab/thread/Factorial.class and b/bin/com/bernie/javaLab/thread/Factorial.class differ diff --git a/src/main/java/com/bernie/gfg/CountPairsInArray.java b/src/main/java/com/bernie/gfg/CountPairsInArray.java new file mode 100644 index 0000000..50b2043 --- /dev/null +++ b/src/main/java/com/bernie/gfg/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/gfg/CountPairsInArray_input.txt b/src/main/java/com/bernie/gfg/CountPairsInArray_input.txt new file mode 100644 index 0000000..3670537 --- /dev/null +++ b/src/main/java/com/bernie/gfg/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/gfg/FindingPosition.java b/src/main/java/com/bernie/gfg/FindingPosition.java new file mode 100644 index 0000000..bc43b32 --- /dev/null +++ b/src/main/java/com/bernie/gfg/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/gfg/FindingPosition_input.txt b/src/main/java/com/bernie/gfg/FindingPosition_input.txt new file mode 100644 index 0000000..d99ab43 --- /dev/null +++ b/src/main/java/com/bernie/gfg/FindingPosition_input.txt @@ -0,0 +1,5 @@ +4 +5 +10 +32 +100000000 diff --git a/src/main/java/com/bernie/gfg/IntersectionOfTwoArray.txt b/src/main/java/com/bernie/gfg/IntersectionOfTwoArray.txt new file mode 100644 index 0000000..004759f --- /dev/null +++ b/src/main/java/com/bernie/gfg/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/gfg/IntersectionOfTwoArrays.java b/src/main/java/com/bernie/gfg/IntersectionOfTwoArrays.java new file mode 100644 index 0000000..b95c6b2 --- /dev/null +++ b/src/main/java/com/bernie/gfg/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/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;i0) + { + 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 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; i0) + { + 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; i0) + { + 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/gfg/Rotate2DArray.txt b/src/main/java/com/bernie/gfg/Rotate2DArray.txt new file mode 100644 index 0000000..aab2e72 --- /dev/null +++ b/src/main/java/com/bernie/gfg/Rotate2DArray.txt @@ -0,0 +1,5 @@ +2 +3 +1 2 3 4 5 6 7 8 9 +2 +56 96 91 54 diff --git a/src/main/java/com/bernie/gfg/WordBoggle.java b/src/main/java/com/bernie/gfg/WordBoggle.java new file mode 100644 index 0000000..791a038 --- /dev/null +++ b/src/main/java/com/bernie/gfg/WordBoggle.java @@ -0,0 +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; + } + + +} diff --git a/src/main/java/com/bernie/gfg/WordBoggle.txt b/src/main/java/com/bernie/gfg/WordBoggle.txt new file mode 100644 index 0000000..299c3bf --- /dev/null +++ b/src/main/java/com/bernie/gfg/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