├── README.md └── Java ├── Introduction ├── WelcomeToJava.java ├── InOut.java ├── InOut2.java ├── EndOfFile.java ├── Loops.java ├── FormattedOutput.java ├── StaticBlock.java ├── IfElse.java ├── IntToString.java └── DataTypes.java ├── Data-Structure ├── Hashset.java ├── Map.java ├── 1DArray.java ├── 2DArray.java ├── Generics.java ├── JavaComparator.java ├── ArrList.java └── JavaSort.java ├── BigNumber ├── BigInteger.java └── BigDecimal.java ├── Strings ├── Reverse.java ├── PatternSyntax.java ├── Compare.java ├── StringToken.java ├── Anagrams.java ├── TagContent.java ├── Regex.java ├── intro.java ├── UserName.java └── DuplicateWords.java ├── ExceptionHandling ├── TryCatch.java └── ExceptionHandling.java ├── Algorithms ├── aVeryBigSum.java ├── CountingValleys.java ├── sockMerchant.java └── jumpingOnClouds.java └── Advanced ├── Varargs.java ├── PrimeChecker.java └── Factory.java /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank 2 | My submissions to [HackerRank](https://www.hackerrank.com/) Challenges 3 | -------------------------------------------------------------------------------- /Java/Introduction/WelcomeToJava.java: -------------------------------------------------------------------------------- 1 | /* 2 | Welcome to the world of Java! Just print "Hello World." and "Hello Java." in two separate lines to complete this challenge. 3 | 4 | The code stub in the editor already creates the main function and solution class. All you have to do is copy and paste the following lines inside the main function. 5 | 6 | System.out.println("Hello World."); 7 | System.out.println("Hello Java."); 8 | Sample Output 9 | 10 | Hello World. 11 | Hello Java. 12 | */ 13 | 14 | public class WelcomeToJava { 15 | 16 | public static void main(String []argv) 17 | { 18 | System.out.println("Hello World."); 19 | System.out.println("Hello Java."); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Java/Introduction/InOut.java: -------------------------------------------------------------------------------- 1 | /* 2 | One way to take input from stdin is to use the Scanner class and read in from System.in. 3 | 4 | You can write your output to stdout by simply using the System.out.println(String) function. 5 | 6 | In this problem, you need to read 3 integers from stdin and print them in stdout. 7 | 8 | Sample input: 9 | 42 10 | 100 11 | 125 12 | 13 | Sample output: 14 | 42 15 | 100 16 | 125 17 | To make the problem easier for you, part of the code is already provided in the editor. 18 | */ 19 | 20 | import java.util.*; 21 | 22 | public class InOut { 23 | 24 | public static void main(String[] args) { 25 | Scanner sc=new Scanner(System.in); 26 | int a=sc.nextInt(); 27 | int b=sc.nextInt(); 28 | int c=sc.nextInt(); 29 | 30 | System.out.println(a); 31 | System.out.println(b); 32 | System.out.println(c); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Java/Data-Structure/Hashset.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a=c and b=d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. 3 | 4 | Note: Brute force solution will not earn full points. 5 | 6 | */ 7 | 8 | import java.io.*; 9 | import java.util.*; 10 | import java.text.*; 11 | import java.math.*; 12 | import java.util.regex.*; 13 | 14 | public class Hashset { 15 | 16 | public static void main(String[] args) { 17 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 18 | Scanner s = new Scanner(System.in); 19 | int sum = s.nextInt(); 20 | s.nextLine(); 21 | HashSet hs = new HashSet(); 22 | for (int i=0;i = 0; i--) { 29 | x=x.concat(""+n.charAt(i)); 30 | } 31 | if (n.equals(x)) { 32 | a = true; 33 | } 34 | return a; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Java/Introduction/InOut2.java: -------------------------------------------------------------------------------- 1 | /* 2 | For this exercise, you need to read inputs from stdin and print them to stdout. 3 | 4 | Input Format 5 | 6 | There are three lines of input. 7 | Line one contains an integer. 8 | Line two contains a double. 9 | Line three contains a String. 10 | 11 | Output Format 12 | 13 | On the first line, print String: followed by the unaltered input String. 14 | On the second line, print Double: followed by the unaltered input double. 15 | On the third line, print Int: followed by the unaltered input integer. 16 | */ 17 | 18 | import java.util.Scanner; 19 | 20 | public class InOut2 { 21 | 22 | public static void main(String[] args) { 23 | Scanner sc=new Scanner(System.in); 24 | int x=sc.nextInt(); 25 | double y=sc.nextDouble(); 26 | sc.nextLine(); 27 | String s=sc.nextLine(); 28 | 29 | System.out.println("String: "+s); 30 | System.out.println("Double: "+y); 31 | System.out.println("Int: "+x); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Java/ExceptionHandling/TryCatch.java: -------------------------------------------------------------------------------- 1 | /* 2 | This problem will test your knowledge on try-catch block. 3 | 4 | You will be given two integers xx and yy as input, you have to compute x/y. If x and y are not 32 bit signed integers or if y is zero, exception will occur and you have to report it. Read sample Input/Output to know what to report in case of exceptions. 5 | */ 6 | 7 | import java.io.*; 8 | import java.util.*; 9 | import java.text.*; 10 | import java.math.*; 11 | import java.util.regex.*; 12 | 13 | public class TryCatch { 14 | 15 | public static void main(String[] args) { 16 | Scanner in = new Scanner(System.in); 17 | try { 18 | int y = in.nextInt(); 19 | int z = in.nextInt(); 20 | int r = y/z; 21 | System.out.print(r); 22 | } 23 | catch (InputMismatchException e) { 24 | System.out.print("java.util.InputMismatchException"); 25 | } 26 | catch (Exception e) { 27 | System.out.print(e); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Java/Introduction/EndOfFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | The challenge here is to read nn lines of input until you reach EOF, then number and print all nn lines of content. 3 | 4 | Hint: Java's Scanner.hasNext() method is helpful for this problem. 5 | 6 | Input Format 7 | Read some unknown nn lines of input from stdin(System.in) until you reach EOF; each line of input contains a non-empty String. 8 | 9 | Output Format 10 | For each line, print the line number, followed by a single space, and then the line content received as input: 11 | 12 | k This is the line read as input for line number 'k'. 13 | Sample Input 14 | 15 | Hello world 16 | I am a file 17 | Read me until end-of-file. 18 | Sample Output 19 | 20 | 1 Hello world 21 | 2 I am a file 22 | 3 Read me until end-of-file. 23 | */ 24 | 25 | import java.io.*; 26 | import java.util.*; 27 | 28 | public class EndOfFile { 29 | 30 | public static void main(String []args) 31 | { 32 | Scanner sc = new Scanner(System.in); 33 | int i=1; 34 | while (sc.hasNext()) { 35 | String s=sc.nextLine(); 36 | System.out.println(i + " " + s); 37 | i++; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Java/Data-Structure/1DArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | This problem will test your knowledge on java array. 3 | You are given an array of nn integers. A sub-array is "Negative" if sum of all the integers in that sub-array is negative. Count the number of "Negative sub-arrays" in the input array. 4 | 5 | Note: Subarrays are contiguous chunks of the main array. For example if the array is {1,2,3,5} then some of the subarrays are {1}, {1,2,3}, {2,3,5}, {1,2,3,5} etc. But {1,2,5} is not an subarray as it is not contiguous. 6 | */ 7 | 8 | import java.math.BigDecimal; 9 | import java.util.*; 10 | 11 | class 1DArray{ 12 | 13 | public static void main(String []argh) 14 | { 15 | Scanner sc= new Scanner(System.in); 16 | int n=sc.nextInt(); 17 | int []s=new int[n]; 18 | int count = 0; 19 | for(int i=0;i test) { 34 | test = count; 35 | } 36 | } 37 | } 38 | System.out.println(test); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Java/Strings/PatternSyntax.java: -------------------------------------------------------------------------------- 1 | /* 2 | In this problem, you are given a pattern. You have to check whether the syntax of the given pattern is valid. 3 | 4 | Note: In this problem, a regex is only valid if you can compile it using the Pattern.compile method. 5 | 6 | Input Format 7 | The first line of input contains an integer NN, denoting the number of testcases. The next NN lines contain a string of any printable characters representing the pattern of a regex. 8 | 9 | Output Format 10 | For each testcase, print "Valid" if the syntax of the given pattern is correct. Otherwise, print "Invalid". Do not print the quotes. 11 | */ 12 | 13 | import java.util.Scanner; 14 | import java.util.regex.*; 15 | 16 | public class PatternSyntax 17 | { 18 | public static void main(String[] args){ 19 | Scanner in = new Scanner(System.in); 20 | int n = in.nextInt(); 21 | in.nextLine(); 22 | for (int i = 0; i < n; i++) { 23 | String pattern = in.nextLine(); 24 | try { 25 | 26 | Pattern p = Pattern.compile(pattern); 27 | System.out.println("Valid"); 28 | } 29 | catch (PatternSyntaxException e) { 30 | System.out.println("Invalid"); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Java/Introduction/Loops.java: -------------------------------------------------------------------------------- 1 | /* 2 | In this problem you will test your knowledge of Java loops. Given three integers aa, bb, and nn, output the following series: 3 | 4 | a+20b,a+20b+21b,......,a+20b+21b+...+2n−1ba+20b,a+20b+21b,......,a+20b+21b+...+2n−1b 5 | 6 | Input Format 7 | The first line will contain the number of testcases t. Each of the next tt lines will have three integers, aa, bb, and nn. 8 | 9 | Constraints: 10 | 0≤t≤5000≤t≤500 11 | 0≤a,b≤500≤a,b≤50 12 | 1≤n≤151≤n≤15 13 | 14 | Output Format 15 | Print the answer to each test case in separate lines. 16 | 17 | Sample Input 18 | 2 19 | 0 2 10 20 | 5 3 5 21 | Sample Output 22 | 2 6 14 30 62 126 254 510 1022 2046 23 | 8 14 26 50 98 24 | */ 25 | 26 | import java.io.*; 27 | import java.util.*; 28 | 29 | public class Loops { 30 | 31 | public static void main(String[] args) { 32 | Scanner sc = new Scanner (System.in); 33 | int t = sc.nextInt(); 34 | for (int i = 0; i < t; i++) { 35 | int a = sc.nextInt(); 36 | int b = sc.nextInt(); 37 | int n = sc.nextInt(); 38 | for (int j = 0; j < n; j++) { 39 | a += (int) Math.pow(2, j)*b; 40 | System.out.print(a +" "); 41 | } 42 | System.out.println(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Java/Strings/Compare.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string, find out the lexicographically smallest and largest substring of length k. 3 | 4 | [Note: Lexicographic order is also known as alphabetic order dictionary order. So "ball" is smaller than "cat", "dog" is smaller than "dorm". Capital letter always comes before smaller letter, so "Happy" is smaller than "happy" and "Zoo" is smaller than "ball".] 5 | 6 | Input Format 7 | First line will consist a string containing english alphabets which has at most 1000 characters. 2nd line will consist an integer k. 8 | 9 | Output Format 10 | In the first line print the lexicographically minimum substring. In the second line print the lexicographically maximum substring. 11 | */ 12 | 13 | import java.io.*; 14 | import java.util.*; 15 | 16 | public class Compare { 17 | 18 | public static void main(String[] args) { 19 | Scanner sc=new Scanner(System.in); 20 | String A=sc.next(); 21 | int B=sc.nextInt(); 22 | ArrayList ar = new ArrayList(); 23 | for (int i = 0; i <= A.length()-B; i++) { 24 | String a = A.substring(i, i+B); 25 | ar.add(a); 26 | } 27 | ar.sort(null); 28 | System.out.println(ar.get(0)); 29 | System.out.println(ar.get(ar.size()-1)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Java/Introduction/FormattedOutput.java: -------------------------------------------------------------------------------- 1 | /* 2 | Input Format 3 | Every line of input will contain a String followed by an integer. 4 | Each String will have a maximum of 1010 alphabetic characters, and each integer will be in the inclusive range from 00 to 999999. 5 | 6 | Output Format 7 | In each line of output there should be two columns: 8 | The first column contains the String and is left justified using exactly 1515 characters. 9 | The second column contains the integer, expressed in exactly 33 digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes. 10 | 11 | Sample Input 12 | java 100 13 | cpp 65 14 | python 50 15 | 16 | Sample Output 17 | ================================ 18 | java 100 19 | cpp 065 20 | python 050 21 | ================================ 22 | */ 23 | import java.util.Scanner; 24 | 25 | public class Solution { 26 | 27 | public static void main(String[] args) { 28 | Scanner sc=new Scanner(System.in); 29 | System.out.println("================================"); 30 | for(int i=0;i<3;i++){ 31 | String s1=sc.next(); 32 | int x=sc.nextInt(); 33 | System.out.printf("%-14s %03d\n", s1, x); 34 | } 35 | System.out.println("================================"); 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Java/Algorithms/aVeryBigSum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the aVeryBigSum function below. 12 | static long aVeryBigSum(long[] ar) { 13 | long sum =0; 14 | for (int l=0; lHello World is not valid, because the text starts with an h1 tag and ends with a non-matching h2 tag. 5 | 6 | Tags can be nested, but content between nested tags is considered not valid. For example, in

contentsinvalid

, contents is valid but invalid is not valid. 7 | 8 | Tags can consist of any printable characters. 9 | */ 10 | 11 | import java.io.*; 12 | import java.util.*; 13 | import java.text.*; 14 | import java.math.*; 15 | import java.util.regex.*; 16 | 17 | public class TagContent{ 18 | public static void main(String[] args){ 19 | 20 | Scanner in = new Scanner(System.in); 21 | int testCases = Integer.parseInt(in.nextLine()); 22 | while(testCases>0){ 23 | String line = in.nextLine(); 24 | String pattern= "(<)(.+)(>)([-\\w!@\\$%^&#\\*()_+|~=`\"\\{\\}\\[\\]:'\\, ]+)()"; 25 | Pattern r = Pattern.compile(pattern, Pattern.MULTILINE); 26 | Matcher m = r.matcher(line); 27 | if (m.find()) { 28 | do { 29 | System.out.println(m.group(4)); 30 | } 31 | while (m.find()); 32 | } 33 | else { 34 | System.out.println("None"); 35 | } 36 | testCases--; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Java/Strings/Regex.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a class called myRegex which will contain a string pattern. You need to write a regular expression and assign it to the pattern such that it can be used to validate an IP address. Use the following definition of an IP address: 3 | 4 | IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3. 5 | 6 | Some valid IP address: 7 | 000.12.12.034 8 | 121.234.12.12 9 | 23.45.12.56 10 | 11 | Some invalid IP address: 12 | 000.12.234.23.23 13 | 666.666.23.23 14 | .213.123.23.32 15 | 23.45.22.32. 16 | I.Am.not.an.ip 17 | */ 18 | import java.util.regex.Matcher; 19 | import java.util.regex.Pattern; 20 | import java.util.Scanner; 21 | 22 | class Regex { 23 | 24 | public static void main(String []args) 25 | { 26 | Scanner in = new Scanner(System.in); 27 | while(in.hasNext()) 28 | { 29 | String IP = in.next(); 30 | System.out.println(IP.matches(new myRegex().pattern)); 31 | } 32 | 33 | } 34 | 35 | class myRegex { 36 | public String pattern="([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])." 37 | + "([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])." 38 | + "([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])." 39 | + "([1][\\d][\\d]|[0][0][0]|([0][0]|)[\\d]|([0]|)[\\d][\\d]|[2][0-4][\\d]|[2][5][0-5])"; 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Java/Introduction/IfElse.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer NN as input, check the following: 3 | 4 | If NN is odd, print "Weird". 5 | If NN is even and, in between the range of 2 and 5(inclusive), print "Not Weird". 6 | If NN is even and, in between the range of 6 and 20(inclusive), print "Weird". 7 | If NN is even and N>20N>20, print "Not Weird". 8 | We given you partially completed code in the editor, complete it to solve the problem. 9 | 10 | Input Format 11 | There is a single line of input: integer NN. 12 | 13 | Constraints 14 | 1≤N≤1001≤N≤100 15 | 16 | Output Format 17 | Print "Weird" if the number is weird. Otherwise, print "Not Weird". Do not print the quotation marks. 18 | */ 19 | 20 | import java.io.*; 21 | import java.util.*; 22 | import java.text.*; 23 | import java.math.*; 24 | import java.util.regex.*; 25 | 26 | public class IfElse { 27 | 28 | public static void main(String[] args) { 29 | 30 | Scanner sc=new Scanner(System.in); 31 | int n=sc.nextInt(); 32 | String ans=""; 33 | if(n%2==1){ 34 | ans = "Weird"; 35 | } 36 | else{ 37 | if (n>=2 && n<=5) { 38 | ans = "Not Weird"; 39 | } 40 | if (n>=6 && n<=20) { 41 | ans = "Weird"; 42 | } 43 | if (n>20) { 44 | ans = "Not Weird"; 45 | } 46 | } 47 | System.out.println(ans); 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Java/ExceptionHandling/ExceptionHandling.java: -------------------------------------------------------------------------------- 1 | /* 2 | Create a class myCalculator which consists of a single method power(int,int). This method takes two integers, nn and pp, as parameters and finds npnp. If either nn or pp is negative, then the method must throw an exception which says "n and p should be non-negative". 3 | */ 4 | import java.util.*; 5 | import java.util.Scanner; 6 | 7 | class myCalculator { 8 | public String power (int n, int p) throws NegativeException { 9 | int power = 0; 10 | try { 11 | if (n<0|p<0) { 12 | throw new NegativeException(); 13 | } 14 | else { 15 | power = (int) Math.pow(n, p); 16 | return power +""; 17 | } 18 | } 19 | catch (NegativeException e) { 20 | return "java.lang.Exception: n and p should be non-negative"; 21 | } 22 | } 23 | } 24 | 25 | class NegativeException extends Exception 26 | { 27 | private static final long serialVersionUID = 1L; 28 | 29 | public NegativeException() 30 | { 31 | super(); 32 | } 33 | } 34 | 35 | class ExceptionHandling{ 36 | 37 | public static void main(String []argh) 38 | { 39 | Scanner in = new Scanner(System.in); 40 | 41 | while(in.hasNextInt()) 42 | { 43 | int n = in.nextInt(); 44 | int p = in.nextInt(); 45 | myCalculator M = new myCalculator(); 46 | try 47 | { 48 | System.out.println(M.power(n,p)); 49 | } 50 | catch(Exception e) 51 | { 52 | System.out.println(e); 53 | } 54 | } 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Java/Data-Structure/Generics.java: -------------------------------------------------------------------------------- 1 | /* 2 | Generic methods are a very efficient way to handle multiple datatypes using a single method. This problem will test your knowledge on Java Generic methods. 3 | 4 | Let's say you have an integer array and a string array. You have to write a single method printArray that can print all the elements of both arrays. The method should be able to accept both integer arrays or string arrays. 5 | 6 | You are given code in the editor. Complete the code so that it prints the following lines: 7 | 8 | 1 9 | 2 10 | 3 11 | Hello 12 | World 13 | */ 14 | import java.io.IOException; 15 | import java.lang.reflect.Method; 16 | class Printer 17 | { 18 | //Write your code here 19 | public static < E > void printArray( E[] inputArray ) 20 | { 21 | // Display array elements 22 | for ( E element : inputArray ){ 23 | System.out.println(element); 24 | } 25 | } 26 | } 27 | 28 | public class Solution { 29 | 30 | public static void main( String args[] ) { 31 | Printer myPrinter = new Printer(); 32 | Integer[] intArray = { 1, 2, 3 }; 33 | String[] stringArray = {"Hello", "World"}; 34 | myPrinter.printArray(intArray); 35 | myPrinter.printArray(stringArray); 36 | int count = 0; 37 | 38 | for (Method method : Printer.class.getDeclaredMethods()) { 39 | String name = method.getName(); 40 | 41 | if(name.equals("printArray")) 42 | count++; 43 | } 44 | 45 | if(count > 1)System.out.println("Method overloading is not allowed!"); 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Java/Data-Structure/JavaComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of Player objects, write a comparator sorts them in order of decreasing score; if or more players have the same score, sort those players alphabetically by name. To do this, you must create a Checker class that implements the Comparator interface, then write an int compare(Player a, Player b) method implementing the Comparator.compare(T o1, T o2) method. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | class Checker implements Comparator { 8 | public int compare(Object o1, Object o2) { 9 | Player p1 = (Player) o1; 10 | Player p2 = (Player) o2; 11 | 12 | if (p1.score == p2.score) { 13 | return p1.name.compareTo(p2.name); 14 | } 15 | else if (p1.score < p2.score) { 16 | return 1; 17 | } 18 | else { 19 | return -1; 20 | } 21 | } 22 | 23 | } 24 | class Player{ 25 | String name; 26 | int score; 27 | 28 | Player(String name, int score){ 29 | this.name = name; 30 | this.score = score; 31 | } 32 | } 33 | 34 | class Solution { 35 | 36 | public static void main(String[] args) { 37 | Scanner scan = new Scanner(System.in); 38 | int n = scan.nextInt(); 39 | 40 | Player player[] = new Player[n]; 41 | Checker checker = new Checker(); 42 | 43 | for(int i=0; i < n; i++){ 44 | player[i] = new Player(scan.next(), scan.nextInt()); 45 | } 46 | scan.close(); 47 | 48 | Arrays.sort(player, checker); 49 | for(int i = 0; i < player.length; i++){ 50 | System.out.printf("%s %s\n", player[i].name, player[i].score); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Java/BigNumber/BigDecimal.java: -------------------------------------------------------------------------------- 1 | /* 2 | Java BigDecimal class can handle arbitrary-precision signed decimal numbers. Lets test your knowledge on them! 3 | 4 | You are given nn real numbers, sort them in descending order! Read data from System.in. 5 | 6 | Note: To make the problem easier, we provided you the input/output part in the editor. Print the numbers as they appeared in the input, don't change anything. If two numbers represent numerically equivalent values, the output must list them in original order of the input 7 | 8 | Input Format 9 | The first line will consist an integer nn, each of the next n lines will contain a real number. nn will be at most 200. The numbers can have at most 300 digits! 10 | 11 | Output Format 12 | Print the numbers in descending orders, one number in each line. 13 | */ 14 | 15 | import java.math.BigDecimal; 16 | import java.util.*; 17 | 18 | class BigDecimal{ 19 | 20 | public static void main(String []argh) 21 | { 22 | Scanner sc= new Scanner(System.in); 23 | int n=sc.nextInt(); 24 | String []s=new String[n]; 25 | 26 | for(int i=0;i() { 32 | 33 | public int compare(Object a1, Object a2) { 34 | BigDecimal bigDec1 = new BigDecimal((String)a1); 35 | BigDecimal bigDec2 = new BigDecimal((String)a2); 36 | return bigDec2.compareTo(bigDec1); 37 | } 38 | }); 39 | 40 | for(int i=0;i0) { 34 | System.out.println("No"); 35 | } 36 | else if(B.compareTo(A)==0) { 37 | System.out.println("No"); 38 | } 39 | String a = "" + A.charAt(0); 40 | String b = "" + B.charAt(0); 41 | System.out.println(a.toUpperCase() + A.substring(1) +" " + b.toUpperCase() + B.substring(1)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Java/Introduction/IntToString.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an integer nn, you have to convert it into a string. 3 | 4 | Please complete the partially completed code in the editor. If your code successfully converts nn into a string ss the code will print "Good job". Otherwise it will print "Wrong answer". 5 | 6 | nn can range between −100−100 to 100100 inclusive. 7 | */ 8 | 9 | import java.util.*; 10 | import java.security.*; 11 | public class IntToString { 12 | public static void main(String[] args) { 13 | 14 | Do_Not_Terminate.forbidExit(); 15 | 16 | try{ 17 | Scanner in = new Scanner(System.in); 18 | int n=in.nextInt(); 19 | String s = Integer.toString(n); 20 | if(n==Integer.parseInt(s)) 21 | { 22 | System.out.println("Good job"); 23 | } 24 | else 25 | { 26 | System.out.println("Wrong answer."); 27 | } 28 | } 29 | catch (Do_Not_Terminate.ExitTrappedException e) { 30 | System.out.println("Unsuccessful Termination!!"); 31 | } 32 | } 33 | } 34 | 35 | //The following class will prevent you from terminating the code using exit(0)! 36 | class Do_Not_Terminate { 37 | 38 | public static class ExitTrappedException extends SecurityException { 39 | 40 | private static final long serialVersionUID = 1L; 41 | } 42 | 43 | public static void forbidExit() { 44 | final SecurityManager securityManager = new SecurityManager() { 45 | @Override 46 | public void checkPermission(Permission permission) { 47 | if (permission.getName().contains("exitVM")) { 48 | throw new ExitTrappedException(); 49 | } 50 | } 51 | }; 52 | System.setSecurityManager(securityManager); 53 | } 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /Java/Strings/UserName.java: -------------------------------------------------------------------------------- 1 | /* 2 | Regular expressions (regexpregexp) help us match or search for patterns in strings. In this problem, you will be given a username. Your task is to check whether that username is valid. A valid username will have the following properties: 3 | 1. The username can contain alphanumeric characters and/or underscores(_). 4 | 2. The username must start with an alphabetic character. 5 | 8 ≤≤ |Username| ≤≤ 30. 6 | To simplify your task, we have provided a portion of the code in the editor. You just need to write down the regexregex pattern which will be used to validate the username input. 7 | 8 | Input Format 9 | The first line of input contains an integer NN, representing the number of testcases. Each of the next NN lines contains a string that represents a username. 10 | Constraints 11 | The username consists of any printable characters. 12 | 1≤N≤1001≤N≤100 13 | Output Format 14 | For each username, output Valid if the username is valid; otherwise, output Invalid. 15 | */ 16 | 17 | import java.io.*; 18 | import java.util.*; 19 | import java.text.*; 20 | import java.math.*; 21 | import java.util.regex.*; 22 | 23 | public class UserName { 24 | public static void main(String[] args){ 25 | Scanner in = new Scanner(System.in); 26 | int testCases = Integer.parseInt(in.nextLine()); 27 | while(testCases>0){ 28 | String username = in.nextLine(); 29 | String pattern = "^[a-zA-Z]{1,1}[a-zA-Z0-9_]{7,29}$"; 30 | Pattern r = Pattern.compile(pattern); 31 | Matcher m = r.matcher(username); 32 | if (m.find( )) { 33 | System.out.println("Valid"); 34 | } else { 35 | System.out.println("Invalid"); 36 | } 37 | testCases--; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Java/Introduction/DataTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an input integer, you must determine which primitive data types are capable of properly storing that input. 3 | 4 | Input Format 5 | 6 | The first line contains an integer, TT, denoting the number of test cases. 7 | Each test case, TT, is comprised of a single line with an integer, nn, which can be arbitrarily large or small. 8 | 9 | Output Format 10 | 11 | For each input variable nn and appropriate primitive dataTypedataType, you must determine if the given primitives are capable of storing it. If yes, then print: 12 | 13 | n can be fitted in: 14 | * dataType 15 | If there is more than one appropriate data type, print each one on its own line and order them by size (i.e.: byte=-128 && x<=127) {System.out.println("* byte");} 40 | if(x>=Short.MIN_VALUE && x<=Short.MAX_VALUE){System.out.println("* short");} 41 | if(x>=Integer.MIN_VALUE && x<=Integer.MAX_VALUE){System.out.println("* int");} 42 | if(x>=Long.MIN_VALUE && x<=Long.MAX_VALUE){System.out.println("* long");} 43 | } 44 | catch(Exception e) 45 | { 46 | System.out.println(sc.next()+" can't be fitted anywhere."); 47 | } 48 | 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Java/Strings/DuplicateWords.java: -------------------------------------------------------------------------------- 1 | /* 2 | Using Regex, we can easily identify the repeated pattern in a given text. In this problem, you will be given a text. Your task is to identify the consecutively repeated words and delete them after the first occurrence of the word. 3 | 4 | Complete the code in the editor to solve this problem. Don't modify any extra lines. You will get the wrong answer if you modify more than 33 lines. 5 | 6 | To restore the original code in the editor, create a new buffer by clicking on the top-left button in the editor. 7 | 8 | Input Format 9 | The first line of input contains an integer NN, representing the number of testcases. The next NN lines contain a string of English letters and whitespaces. 10 | Constraints 11 | In each line, there will be at most 104104 English letters and whitespaces. 12 | 1≤N≤1001≤N≤100 13 | Output Format 14 | Print the input string after deleting the consecutive words after the first occurrence of the word. 15 | */ 16 | 17 | 18 | import java.util.Scanner; 19 | import java.util.regex.Matcher; 20 | import java.util.regex.Pattern; 21 | 22 | public class DuplicateWords 23 | { 24 | public static void main(String[] args){ 25 | 26 | String pattern = "(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+"; 27 | Pattern r = Pattern.compile(pattern, Pattern.MULTILINE + Pattern.CASE_INSENSITIVE); 28 | 29 | Scanner in = new Scanner(System.in); 30 | int testCases = Integer.parseInt(in.nextLine()); 31 | while(testCases>0){ 32 | String input = in.nextLine(); 33 | Matcher m = r.matcher(input); 34 | boolean findMatch = true; 35 | while(m.find( )){ 36 | input = input.replaceAll(input,input.replaceFirst("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+","$1")); 37 | findMatch = false; 38 | } 39 | System.out.println(input); 40 | testCases--; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Java/Advanced/Varargs.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given a class Solution and its main method in the editor. 3 | Your task is to create the class Add and the required methods so that the code prints the sum of the numbers passed to the function add. 4 | 5 | Note: Your add method in the Add class must print the sum as given in the Sample Output 6 | 7 | Input Format 8 | There are six lines of input, each containing an integer. 9 | 10 | Output Format 11 | There will be only four lines of output. Each line contains the sum of the integers passed as the parameters to add in the main method. 12 | */ 13 | 14 | import java.io.BufferedReader; 15 | import java.io.InputStreamReader; 16 | import java.lang.reflect.Method; 17 | import java.util.*; 18 | class Add { 19 | public void add (int ... n) { 20 | int r = n[0]; 21 | int sum = 0; 22 | for (int i = 0; i < n.length; i++) { 23 | sum += n[i]; 24 | System.out.print(n[i]); 25 | if (i a = new ArrayList(); 21 | ArrayList b = new ArrayList(); 22 | for (int i = 0; i < t; i++) { 23 | int x = s.nextInt(); 24 | a.add(x); 25 | for (int j = 0; j < x; j++) { 26 | b.add(s.nextInt()); 27 | } 28 | } 29 | int d = s.nextInt(); 30 | ArrayList e = new ArrayList(); 31 | for (int i = 0; i < d; i++) { 32 | e.add(s.nextInt()); 33 | e.add(s.nextInt()); 34 | } 35 | s.close(); 36 | for (int y = 0; y < d+d; y=y+2) { 37 | int pos = e.get(y+1); 38 | int line = e.get(y); 39 | int len = a.get(line-1); 40 | int sum2 = 0; 41 | if (line - 1 > 0) { 42 | for (int u = 0; u < line-1; u++) { 43 | sum2 += a.get(u); 44 | } 45 | } 46 | if (len < pos) { 47 | System.out.println("ERROR!"); 48 | } 49 | else { 50 | System.out.println (b.get(sum2 + (pos-1))); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Java/Data-Structure/JavaSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given a list of student information: ID, FirstName, and CGPA. Your task is to rearrange them according to their CGPA in decreasing order. If two student have the same CGPA, then arrange them according to their first name in alphabetical order. If those two students also have the same first name, then order them according to their ID. No two students have the same ID. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | class Student{ 8 | private int id; 9 | private String fname; 10 | private double cgpa; 11 | public Student(int id, String fname, double cgpa) { 12 | super(); 13 | this.id = id; 14 | this.fname = fname; 15 | this.cgpa = cgpa; 16 | } 17 | public int getId() { 18 | return id; 19 | } 20 | public String getFname() { 21 | return fname; 22 | } 23 | public double getCgpa() { 24 | return cgpa; 25 | } 26 | } 27 | 28 | class Compare implements Comparator { 29 | public int compare(Object o1, Object o2) { 30 | Student s1 = (Student) o1; 31 | Student s2 = (Student) o2; 32 | 33 | if (s1.getCgpa()==s2.getCgpa()) { 34 | if (s1.getFname()==s2.getFname()) { 35 | if (s1.getId() < s2.getId()) { 36 | return 1; 37 | } 38 | else { 39 | return -1; 40 | } 41 | } 42 | else { 43 | return s1.getFname().compareTo(s2.getFname()); 44 | } 45 | } 46 | else if (s1.getCgpa() < s2.getCgpa()) { 47 | return 1; 48 | } 49 | else { 50 | return -1; 51 | } 52 | } 53 | 54 | } 55 | 56 | public class Solution 57 | { 58 | public static void main(String[] args){ 59 | Scanner in = new Scanner(System.in); 60 | int testCases = Integer.parseInt(in.nextLine()); 61 | Compare c = new Compare(); 62 | List studentList = new ArrayList(); 63 | while(testCases>0){ 64 | int id = in.nextInt(); 65 | String fname = in.next(); 66 | double cgpa = in.nextDouble(); 67 | 68 | Student st = new Student(id, fname, cgpa); 69 | studentList.add(st); 70 | 71 | testCases--; 72 | } 73 | Collections.sort(studentList, c); 74 | for(Student st: studentList){ 75 | System.out.println(st.getFname()); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Java/Advanced/PrimeChecker.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given a class Solution and its main method in the editor. Your task is to create a class Prime which contains a single method checkPrime so that the code prints only prime numbers as the output. 3 | Please do not use method overloading! 4 | 5 | Input Format 6 | There are only five lines of input, each containing one integer. 7 | 8 | Output Format 9 | There will be only four lines of output. Each line contains only prime numbers depending upon the parameters passed to checkPrime in the main method of the class Solution. In case there is no prime number, then a blank line should be printed. 10 | */ 11 | 12 | import java.io.*; 13 | import java.util.*; 14 | import java.text.*; 15 | import java.math.*; 16 | import java.util.regex.*; 17 | import java.lang.reflect.*; 18 | import static java.lang.System.in; 19 | 20 | class Prime { 21 | public void checkPrime(int ... n) { 22 | for (int j=0;j set=new HashSet<>(); 55 | boolean overload=false; 56 | for(int i=0;i