├── Java 1D Array (Part 2).java ├── Java 1D Array.java ├── Java 2D Array,java ├── Java Anagrams.java ├── Java Arraylist.java ├── Java BitSet.java ├── Java Comparator.java ├── Java Currency Formatter.java ├── Java Datatypes.java ├── Java Date and time.java ├── Java Dequeue.java ├── Java End-of-file.java ├── Java Factory Pattern.java ├── Java Generics.java ├── Java Hashset.java ├── Java If-Else.java ├── Java Int to String.java ├── Java Loops I.java ├── Java Loops II.java ├── Java Output Formatting.java ├── Java Primality Test.java ├── Java Static Initializer Block.java ├── Java Stdin and Stdout I.java ├── Java Stdin and Stdout II.java ├── Java Strings Introduction.java ├── Java Subarray.java ├── Java Substring.java ├── LICENSE ├── Prime Checker.java ├── README.md └── Valid Username Regular Expression.java /Java 1D Array (Part 2).java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | 5 | public static boolean canWin(int leap, int[] game, int i) { 6 | if (i < 0 || game[i] == 1) 7 | return false; 8 | if (i + 1 >= game.length || i + leap >= game.length) 9 | return true; 10 | 11 | game[i] = 1; //flag 12 | 13 | return canWin(leap, game, i + leap) 14 | || canWin(leap, game, i + 1) 15 | || canWin(leap, game, i - 1); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner scan = new Scanner(System.in); 20 | int q = scan.nextInt(); 21 | while (q-- > 0) { 22 | int n = scan.nextInt(); 23 | int leap = scan.nextInt(); 24 | 25 | int[] game = new int[n]; 26 | for (int i = 0; i < n; i++) { 27 | game[i] = scan.nextInt(); 28 | } 29 | 30 | System.out.println((canWin(leap, game, 0)) ? "YES" : "NO"); 31 | } 32 | scan.close(); 33 | } 34 | } -------------------------------------------------------------------------------- /Java 1D Array.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Solution { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner scan = new Scanner(System.in); 8 | int n = scan.nextInt(); 9 | 10 | int[] a = new int[n]; 11 | for (int i = 0; i < n; ++i) { 12 | int val = scan.nextInt(); 13 | a[i] = val; 14 | } 15 | 16 | scan.close(); 17 | 18 | // Prints each sequential element in array a 19 | for (int i = 0; i < a.length; i++) { 20 | System.out.println(a[i]); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Java 2D Array,java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Solution { 4 | static final int SIZE = 6; 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int[][] A = new int[SIZE][SIZE]; 10 | for (int r = 0; r < SIZE; r++) { 11 | for (int c = 0; c < SIZE; c++) { 12 | A[r][c] = sc.nextInt(); 13 | } 14 | } 15 | 16 | int result = Integer.MIN_VALUE; 17 | for (int i = 1; i <= SIZE - 2; i++) { 18 | for (int j = 1; j <= SIZE - 2; j++) { 19 | result = Math.max(result, A[i - 1][j - 1] + A[i - 1][j] + A[i - 1][j + 1] + A[i][j] + A[i + 1][j - 1] 20 | + A[i + 1][j] + A[i + 1][j + 1]); 21 | } 22 | } 23 | System.out.println(result); 24 | 25 | sc.close(); 26 | } 27 | } -------------------------------------------------------------------------------- /Java Anagrams.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | static boolean isAnagram(String a, String b) { 4 | b=b.toUpperCase(); 5 | boolean ret = false; 6 | StringBuilder c= new StringBuilder(b); 7 | 8 | if(a.length()==b.length()){ 9 | for(int i=0; i> lines = new ArrayList>(); 10 | int n = in.nextInt(); 11 | for (int i = 0; i < n; i++) { 12 | List line = new ArrayList(); 13 | int d = in.nextInt(); 14 | for (int j = 0; j < d; j++) { 15 | line.add(in.nextInt()); 16 | } 17 | lines.add(line); 18 | } 19 | 20 | int q = in.nextInt(); 21 | for (int i = 0; i < q; i++) { 22 | int x = in.nextInt(); 23 | int y = in.nextInt(); 24 | if (y > lines.get(x - 1).size()) { 25 | System.out.println("ERROR!"); 26 | } else { 27 | System.out.println(lines.get(x - 1).get(y - 1)); 28 | } 29 | } 30 | 31 | in.close(); 32 | } 33 | } -------------------------------------------------------------------------------- /Java BitSet.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.BitSet; 3 | 4 | public class Solution { 5 | public static void main(String[] args) { 6 | Scanner scan = new Scanner(System.in); 7 | int N = scan.nextInt(); 8 | int M = scan.nextInt(); 9 | BitSet B1 = new BitSet(N); 10 | BitSet B2 = new BitSet(N); 11 | while (M-- > 0) { 12 | String str = scan.next(); 13 | int a = scan.nextInt(); 14 | int b = scan.nextInt(); 15 | switch (str) { 16 | case "AND": 17 | if (a == 1) { 18 | B1.and(B2); 19 | } else { 20 | B2.and(B1); 21 | } 22 | break; 23 | case "OR": 24 | if (a == 1) { 25 | B1.or(B2); 26 | } else { 27 | B2.or(B1); 28 | } 29 | break; 30 | case "XOR": 31 | if (a == 1) { 32 | B1.xor(B2); 33 | } else { 34 | B2.xor(B1); 35 | } 36 | break; 37 | case "FLIP": 38 | if (a == 1) { 39 | B1.flip(b); 40 | } else { 41 | B2.flip(b); 42 | } 43 | break; 44 | case "SET": 45 | if (a == 1) { 46 | B1.set(b); 47 | } else { 48 | B2.set(b); 49 | } 50 | break; 51 | default: 52 | break; 53 | } 54 | System.out.println(B1.cardinality() + " " + B2.cardinality()); 55 | } 56 | scan.close(); 57 | } 58 | } -------------------------------------------------------------------------------- /Java Comparator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Checker implements Comparator { 4 | @Override 5 | public int compare(Player p1, Player p2) { 6 | if (p1.score == p2.score) { 7 | return p1.name.compareTo(p2.name); 8 | } else { 9 | return p2.score - p1.score; // descending order 10 | } 11 | } 12 | } 13 | 14 | class Player { 15 | String name; 16 | int score; 17 | 18 | Player(String name, int score) { 19 | this.name = name; 20 | this.score = score; 21 | } 22 | } 23 | 24 | class Solution { 25 | public static void main(String[] args) { 26 | Scanner scan = new Scanner(System.in); 27 | int n = scan.nextInt(); 28 | 29 | Player[] player = new Player[n]; 30 | Checker checker = new Checker(); 31 | 32 | for (int i = 0; i < n; i++) { 33 | player[i] = new Player(scan.next(), scan.nextInt()); 34 | } 35 | scan.close(); 36 | 37 | Arrays.sort(player, checker); 38 | for (int i = 0; i < player.length; i++) { 39 | System.out.printf("%s %s\n", player[i].name, player[i].score); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Java Currency Formatter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | double payment = scanner.nextDouble(); 12 | scanner.close(); 13 | 14 | Locale INDIA = new Locale("en", "IN");//Creates a new Locale with English as the language and India as teh country 15 | 16 | 17 | String us = NumberFormat.getCurrencyInstance(Locale.US).format(payment); 18 | String india = NumberFormat.getCurrencyInstance(INDIA).format(payment); 19 | String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment); 20 | String france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment); 21 | System.out.println("US: " + us); 22 | System.out.println("India: " + india); 23 | System.out.println("China: " + china); 24 | System.out.println("France: " + france); 25 | } 26 | } -------------------------------------------------------------------------------- /Java Datatypes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | 5 | 6 | class Solution{ 7 | public static void main(String []argh) 8 | { 9 | 10 | 11 | 12 | Scanner sc = new Scanner(System.in); 13 | int t=sc.nextInt(); 14 | 15 | for(int i=0;i map = new HashMap(); 7 | ArrayDeque deque = new ArrayDeque(); 8 | 9 | Scanner scan = new Scanner(System.in); 10 | int n = scan.nextInt(); 11 | int m = scan.nextInt(); 12 | int max = 0; 13 | 14 | for (int i = 0; i < n; i++) { 15 | if (i >= m) { 16 | int old = deque.removeFirst(); 17 | if (map.get(old) == 1) { 18 | map.remove(old); 19 | } else { 20 | map.merge(old, -1, Integer::sum); 21 | } 22 | } 23 | 24 | /* Add new value */ 25 | int num = scan.nextInt(); 26 | deque.addLast(num); 27 | map.merge(num, 1, Integer::sum); 28 | 29 | max = Math.max(max, map.size()); 30 | if (max == m) { 31 | break; 32 | } 33 | } 34 | 35 | scan.close(); 36 | System.out.println(max); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Java End-of-file.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 11 | Scanner input = new Scanner(System.in); 12 | String line; 13 | int i = 1; 14 | while(input.hasNext()) 15 | { 16 | line = input.nextLine(); 17 | System.out.println(i + " "+ line); 18 | i++; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Java Factory Pattern.java: -------------------------------------------------------------------------------- 1 | return (order.equals("pizza")) ? new Pizza() : new Cake(); 2 | -------------------------------------------------------------------------------- /Java Generics.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.lang.reflect.Method; 3 | 4 | class Printer { 5 | public void printArray(T[] array) { 6 | for (T item : array) { 7 | System.out.println(item); 8 | } 9 | } 10 | } 11 | 12 | public class Solution { 13 | public static void main(String args[]) { 14 | Printer myPrinter = new Printer(); 15 | Integer[] intArray = { 1, 2, 3 }; 16 | String[] stringArray = {"Hello", "World"}; 17 | myPrinter.printArray(intArray); 18 | myPrinter.printArray(stringArray); 19 | int count = 0; 20 | for (Method method : Printer.class.getDeclaredMethods()) { 21 | String name = method.getName(); 22 | if (name.equals("printArray")) { 23 | count++; 24 | } 25 | } 26 | if (count > 1) { 27 | System.out.println("Method overloading is not allowed!"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Java Hashset.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | public static void main(String[] args) { 9 | Scanner s = new Scanner(System.in); 10 | int t = s.nextInt(); 11 | String [] pair_left = new String[t]; 12 | String [] pair_right = new String[t]; 13 | 14 | for (int i = 0; i < t; i++) { 15 | pair_left[i] = s.next(); 16 | pair_right[i] = s.next(); 17 | } 18 | s.close(); 19 | 20 | HashSet set = new HashSet(t); 21 | for (int i = 0; i < t; i++) { 22 | set.add(pair_left[i] + " " + pair_right[i]); 23 | System.out.println(set.size()); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Java If-Else.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | 11 | Scanner sc=new Scanner(System.in); 12 | int n=sc.nextInt(); 13 | String ans=""; 14 | if(n%2==1){ans = "Weird";} 15 | else if(n >= 2 && n <= 5){ans ="Not Weird";} 16 | else if(n >= 6 && n <= 20){ ans = "Weird";} 17 | else if(n>20){ans = "Not Weird";} 18 | 19 | System.out.println(ans); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Java Int to String.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.security.*; 3 | public class Solution { 4 | public static void main(String[] args) { 5 | 6 | DoNotTerminate.forbidExit(); 7 | 8 | try { 9 | Scanner in = new Scanner(System.in); 10 | int n = in .nextInt(); 11 | in.close(); 12 | String s = String.valueOf(n); 13 | 14 | if (n == Integer.parseInt(s)) { 15 | System.out.println("Good job"); 16 | } else { 17 | System.out.println("Wrong answer."); 18 | } 19 | } catch (DoNotTerminate.ExitTrappedException e) { 20 | System.out.println("Unsuccessful Termination!!"); 21 | } 22 | } 23 | } 24 | 25 | class DoNotTerminate { 26 | 27 | public static class ExitTrappedException extends SecurityException { 28 | 29 | private static final long serialVersionUID = 1; 30 | } 31 | 32 | public static void forbidExit() { 33 | final SecurityManager securityManager = new SecurityManager() { 34 | @Override 35 | public void checkPermission(Permission permission) { 36 | if (permission.getName().contains("exitVM")) { 37 | throw new ExitTrappedException(); 38 | } 39 | } 40 | }; 41 | System.setSecurityManager(securityManager); 42 | } 43 | } -------------------------------------------------------------------------------- /Java Loops I.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int N = in.nextInt(); 12 | for(int i = 1; i <=10; i++) 13 | { 14 | System.out.println(N + " x "+i+" = "+i*N); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Java Loops II.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | import java.lang.Math.*; 4 | 5 | class Solution{ 6 | public static void main(String []argh){ 7 | Scanner in = new Scanner(System.in); 8 | int t=in.nextInt(); 9 | for(int i=0;i 0 && H > 0; 5 | 6 | static{ 7 | if(!flag){ 8 | System.out.println("java.lang.Exception: Breadth and height must be positive"); 9 | } 10 | } -------------------------------------------------------------------------------- /Java Stdin and Stdout I.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | 5 | public static void main(String[] args) { 6 | Scanner scan = new Scanner(System.in); 7 | int a = scan.nextInt(); 8 | int b = scan.nextInt(); 9 | int c = scan.nextInt(); 10 | 11 | System.out.println(a); 12 | System.out.println(b); 13 | System.out.println(c); 14 | } 15 | } -------------------------------------------------------------------------------- /Java Stdin and Stdout II.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Solution { 4 | 5 | public static void main(String[] args) { 6 | Scanner scan = new Scanner(System.in); 7 | int i = scan.nextInt(); 8 | double d = scan.nextDouble(); 9 | scan.nextLine(); //gets rid of the \n newline after the integer so that we get the string with the next call 10 | String s = scan.nextLine(); 11 | 12 | 13 | System.out.println("String: " + s); 14 | System.out.println("Double: " + d); 15 | System.out.println("Int: " + i); 16 | } 17 | } -------------------------------------------------------------------------------- /Java Strings Introduction.java: -------------------------------------------------------------------------------- 1 | Scanner sc=new Scanner(System.in); 2 | String A=sc.next(); 3 | String B=sc.next(); 4 | /* Enter your code here. Print output to STDOUT. */ 5 | 6 | System.out.println(A.length()+B.length()); 7 | if(A.compareTo(B) < 0) 8 | { 9 | System.out.println("No"); 10 | A = A.substring(0,1).toUpperCase() + A.substring(1,A.length()); 11 | B = B.substring(0,1).toUpperCase() + B.substring(1,B.length()); 12 | System.out.println(A + " " + B); 13 | } 14 | else if (A.compareTo(B)==0) 15 | { 16 | System.out.println("No"); 17 | A = A.substring(0,1).toUpperCase() + A.substring(1,A.length()); 18 | B = B.substring(0,1).toUpperCase() + B.substring(1,B.length()); 19 | System.out.println(A + " " + B); 20 | } 21 | else 22 | { 23 | System.out.println("Yes"); 24 | A = A.substring(0,1).toUpperCase() + A.substring(1,A.length()); 25 | B = B.substring(0,1).toUpperCase() + B.substring(1,B.length()); 26 | System.out.println(A + " " + B); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Java Subarray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int size = in.nextInt(); 9 | int[] arr = new int[size]; 10 | int[][] arrM = new int[size][size]; 11 | int count = 0; 12 | for(int i=0;i set=new HashSet<>(); 44 | boolean overload=false; 45 | for(int i=0;i