This is the Java solution for the Hackerrank problem - Bitwise AND - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Bitwise AND](https://www.hackerrank.com/challenges/30-bitwise-and/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Day29BitwiseAND {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int a0 = 0; a0 < t; a0++) {
int n = in.nextInt();
int k = in.nextInt();
in.close();
if (((k - 1) | k) <= n) {
System.out.println(k - 1);
} else {
System.out.println(k - 2);
}
}
}
}
This is the Java solution for the Hackerrank problem - Let's Review - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Let's Review](https://www.hackerrank.com/challenges/30-review-loop/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Day6LetsReview {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int noOfTest = sc.nextInt();
sc.nextLine();
String str1 = "", str2 = "";
for (int i = 0; i < noOfTest; i++) {
String input = sc.next();
char[] char1 = input.toCharArray();
for (int j = 0; j < char1.length; j++) {
if (j % 2 == 0) {
str1 = str1 + char1[j];
} else {
str2 = str2 + char1[j];
}
}
System.out.println(str1 + " " + str2);
str1 = str2 = "";
}
sc.close();
}
}
This is the Java solution for the Hackerrank problem - Maximizing XOR - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Maximizing XOR](https://www.hackerrank.com/challenges/maximizing-xor/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.bitmanipulation;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class MaximizingXOR {
/*
* Complete the function below.
*/
static int maxXor(int l, int r) {
int maxXor = 0;
for (int i = l; i < r; i++) {
int temp;
temp = i ^ (i + 1);
if (temp > maxXor) {
maxXor = temp;
}
}
return maxXor;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res;
int _l;
_l = Integer.parseInt(in.nextLine());
int _r;
_r = Integer.parseInt(in.nextLine());
res = maxXor(_l, _r);
System.out.println(res);
in.close();
}
}
This is the Java solution for the Hackerrank problem - Valid Username Regular Expression - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.strings;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
class UsernameValidator {
public static final String regularExpression = "^[A-Za-z]\\w{7,29}$";;
}
public class ValidUsernameRegularExpression {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = Integer.parseInt(scan.nextLine());
while (n-- != 0) {
String userName = scan.nextLine();
if (userName.matches(UsernameValidator.regularExpression)) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
}
}
}
This is the Java solution for the Hackerrank problem - Java Anagrams - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.strings;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class JavaAnagrams {
static boolean isAnagram(String a, String b) {
if (a.length() != b.length()) {
return false;
} else {
for (int i = 0; i < a.length(); i++) {
char ch = a.toLowerCase().charAt(i);
b = b.toLowerCase();
if (b.indexOf(ch) != -1) {
b = b.replaceFirst(ch + "", "");
} else {
return false;
}
}
return b.length() == 0;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println((ret) ? "Anagrams" : "Not Anagrams");
}
}
This is the Java solution for the Hackerrank problem - Day 27: Testing - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Day 27: Testing](https://www.hackerrank.com/challenges/30-testing/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
import java.util.Random;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Day27Testing {
public static void main(String[] args) {
System.out.println(5);
System.out.println("4 3");
System.out.println("0 -3 4 2");
System.out.println("5 2");
System.out.println("0 -3 4 2 2");
System.out.println("3 3");
System.out.println("0 -3 4");
System.out.println("7 2");
System.out.println("0 -3 1 1 1 1 1");
System.out.println("6 3");
System.out.println("0 -3 4 2 1 1");
/**
* uncomment below method if you want to generate automated Testcase.
* Its generating proper test case and you can pass it to Angry professer program
* which will give you proper output. But hackerrank platform is validating based on hardcode test case, so commented
* the below logic. feel free to uncomment & see its working.
*/
// testCaseGeneration();
}
/**
* optional , written for learning purpose
*/
private static void testCaseGeneration() {
String []lectures={"YES","NO","YES","NO","YES"};
int t = 5,m=0;
System.out.println(t);
while (t-- > 0) {
String classCancel = lectures[m++];
Random gen = new Random();
int n = gen.nextInt(197) + 3;
int k = gen.nextInt(n - 1) + 1;
System.out.println(n + " " + k);
if (classCancel.equalsIgnoreCase("YES")) {
generateTcForFailure(n, k, gen);
} else {
generateTcForSuccess(n, k, gen);
}
System.out.println();
}
}
/**
* @param n
* @param k
* @param gen
*/
private static void generateTcForFailure(int n, int k, Random random) {
int studentCount = 0;
String tc="";
while (n > 0) {
int a = random.nextInt(2000) - 1000;
if (a < k && studentCount < k-1) {
tc+=a+" ";
studentCount++;
n--;
} else if (a > k) {
tc+=a+" ";
n--;
}
}
System.out.print(tc.trim());
}
/**
* @param n
* @param k
*/
private static void generateTcForSuccess(int n, int k, Random random) {
int studentCount = 0;
String tc="";
while (n > 0) {
int a = random.nextInt(2000) - 1000;
if ( studentCount >= k) {
tc+=a+" ";
n--;
} else if (a<=k && studentCount < k) {
studentCount++;
tc+=a+" ";
n--;
}
}
System.out.print(tc.trim());
}
}
This is the Java solution for the Hackerrank problem - Jim and the Orders - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Jim and the Orders](https://www.hackerrank.com/challenges/jim-and-the-orders/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.sorting;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
/**
* @author Kanahaiya Gupta
*
*/
public class JimAndTheOrders {
public static void main(String[] args) {
Map hmap = new LinkedHashMap();
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i = 1; i <= N; i++) {
int key = i;
int value = sc.nextInt() + sc.nextInt();
hmap.put(key, value);
}
Set> set = hmap.entrySet();
List> list = new ArrayList>(set);
Collections.sort(list, new Comparator>() {
public int compare(Entry e1, Entry e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
for (Entry i : list) {
System.out.print(i.getKey() + " ");
}
sc.close();
}
}
This is the Java solution for the Hackerrank problem - Operators - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Operators](https://www.hackerrank.com/challenges/30-operators/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Day2Operators {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price
int tipPercent = scan.nextInt(); // tip percentage
int taxPercent = scan.nextInt(); // tax percentage
scan.close();
// Write your calculation code here.
double tC = mealCost + (mealCost * tipPercent) / 100 + (mealCost * taxPercent) / 100;
// cast the result of the rounding operation to an int and save it as totalCost
int totalCost = (int) Math.round(tC);
System.out.println("The total meal cost is " + totalCost + " dollars.");
// Print your result
}
}
This is the Java solution for the Hackerrank problem - Java End-of-file - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.introduction;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class JavaEndOfFile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = 1;
while (sc.hasNext()) {
System.out.println(i++ + " " + sc.nextLine());
}
sc.close();
}
}
This is the Java solution for the Hackerrank problem - Hello, World - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Hello, World](https://www.hackerrank.com/challenges/30-hello-world)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Day0HelloWorld {
public static void main(String[] args) {
// Create a Scanner object to read input from stdin.
Scanner scan = new Scanner(System.in);
// Read a full line of input from stdin and save it to our variable,
// inputString.
String inputString = scan.nextLine();
// Close the scanner object, because we've finished reading
// all of the input from stdin needed for this challenge.
scan.close();
// Print a string literal saying "Hello, World." to stdout.
System.out.println("Hello, World.");
System.out.println(inputString);
}
}
This is the Java solution for the Hackerrank problem - BinarySearchTrees - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [BinarySearchTrees](https://www.hackerrank.com/challenges/30-binary-search-trees/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class Day22BinarySearchTrees {
static class Node {
Node left, right;
int data;
Node(int data) {
this.data = data;
left = right = null;
}
}
public static int getHeight(Node root) {
return (root == null) ? -1 : Math.max(getHeight(root.left) + 1, getHeight(root.right) + 1);
}
public static Node insert(Node root, int data) {
if (root == null) {
return new Node(data);
} else {
Node cur;
if (data <= root.data) {
cur = insert(root.left, data);
root.left = cur;
} else {
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
Node root = null;
while (T-- > 0) {
int data = sc.nextInt();
root = insert(root, data);
}
sc.close();
int height = getHeight(root);
System.out.println(height);
}
}
This is the Java solution for the Hackerrank problem - Linked List - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Linked List](https://www.hackerrank.com/challenges/30-linked-list/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
/**
* @author Kanahaiya Gupta
*
*/
public class Day15LinkedList {
static class Node{
int data;
Node next;
Node(int data){
this.data=data;
}
}
public static Node insert(Node head,int data) {
//Complete this method
Node newNode=new Node(data);
if(head==null) return newNode;
Node node=head;
while(node.next!=null){
node=node.next;
}
node.next=newNode;
return head;
}
}
This is the Java solution for the Hackerrank problem - Java Datatypes - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.introduction;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class JavaDatatypes {
public static void main(String[] argh) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
try {
long x = sc.nextLong();
System.out.println(x + " can be fitted in:");
if (x >= -128 && x <= 127)
System.out.println("* byte");
if (x >= -(Math.pow(2, 16 - 1)) && x <= (Math.pow(2, 16 - 1) - 1))
System.out.println("* short");
if (x >= -(Math.pow(2, 32 - 1)) && x <= (Math.pow(2, 32 - 1) - 1))
System.out.println("* int");
if (x >= -(Math.pow(2, 64 - 1)) && x <= (Math.pow(2, 64 - 1) - 1))
System.out.println("* long");
} catch (Exception e) {
System.out.println(sc.next() + " can't be fitted anywhere.");
}
}
sc.close();
}
}
This is the Java solution for the Hackerrank problem - Class vs. Instance - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
* Problem Statement-
* [Class vs. Instance](https://www.hackerrank.com/challenges/30-class-vs-instance/problem)
*
*/
package com.javaaid.hackerrank.solutions.thirtydaysofcode;
/**
* @author Kanahaiya Gupta
*
*/
class Person {
private int age;
public Person(int initialAge) {
if (initialAge < 0) {
System.out.println("Age is not valid, setting age to 0.");
}
this.age = initialAge;
// Add some more code to run some checks on initialAge
}
public void amIOld() {
// Write code determining if this person's age is old and print the correct
// statement:
if (age < 13) {
System.out.println("You are young.");
} else if (age >= 13 && age < 18) {
System.out.println("You are a teenager.");
} else {
System.out.println("You are old.");
}
}
public void yearPasses() {
// Increment this person's age.
this.age++;
}
}
This is the Java solution for the Hackerrank problem - Java Int to String - Hackerrank Challenge - Java Solution.
Source - Java-aid's repository.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.introduction;
/**
* @author Kanahaiya Gupta
*
*/
public class JavaIntToString {
int n; //already decalared
String s=n+"";
}