Hackerearth-Implementation

  1. Brute Force: Day of the Programmer
package org.javaaid.hackerrank.solutions.implementation.bruteforce;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class DayOfTheProgrammer {
	public static void main(String[] args) throws ParseException {
		Scanner sc = new Scanner(System.in);
		int y = sc.nextInt();
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		int d = 243;
		if (y >= 1700 && y <= 1917) {
			if (y % 4 == 0) {
				d = 244;
			}
		} else if (y >= 1919 && y <= 2700) {
			if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
				d = 244;
			}
		} else if (y == 1918) {
			d = 230;
		}
		int r = 256 - d;
		String date = r + "." + 9 + "." + y;
		System.out.println(sdf.format(sdf.parse(date)));
		sc.close();
	}
}

2. Brute Force: Pangrams

package org.javaaid.hackerrank.solutions.implementation.bruteforce;
import java.util.Scanner;
public class Pangrams {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String line = sc.nextLine().toLowerCase();
		boolean found = true;
		for (int i = 97; i <= 122; i++) {
			char ch = (char) i;
			if (line.indexOf(ch) == -1) {
				found = false;
				break;
			}
		}
		System.out.println(found ? "pangram" : "not pangram");
		sc.close();
	}
}

3. Brute Force: Climbing the Leaderboard

package org.javaaid.hackerrank.solutions.implementation.bruteforce;
import java.util.Scanner;
public class ClimbingTheLeaderboard {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] scores = new int[n];
		scores[0] = in.nextInt();
		int k = 1, counter = 0;
		for (int scores_i = 1; scores_i < n; scores_i++) {
			int temp = in.nextInt();
			if (temp != scores[k - 1]) {
				scores[k++] = temp;
			} else {
				counter++;
			}
		}
		for (int i = scores.length - 1; i >= 0 && counter > 0; i--) {
			counter--;
			scores[i] = Integer.MIN_VALUE;
		}
		int m = in.nextInt();
		for (int alice_i = 0; alice_i < m; alice_i++) {
			int tmp = in.nextInt();
			if (tmp > scores[0]) {
				System.out.println(1);
			} else if (tmp < scores[scores.length - 1]) {
				System.out.println(scores.length + 1);
			} else {
				System.out.println(binarySearch(scores, tmp) + 1);
			}
		}
		in.close();
	}
	private static int binarySearch(int[] a, int key) {
		int lo = 0;
		int hi = a.length - 1;
		while (lo <= hi) {
			int mid = lo + (hi - lo) / 2;
			if (a[mid] == key) {
				return mid;
			} else if (a[mid] < key && key < a[mid - 1]) {
				return mid;
			} else if (a[mid] > key && key >= a[mid + 1]) {
				return mid + 1;
			} else if (a[mid] < key) {
				hi = mid - 1;
			} else if (a[mid] > key) {
				lo = mid + 1;
			}
		}
		return -1;
	}
}

4. Brute Force: Strings: Making Anagrams

package org.javaaid.hackerrank.solutions.implementation.bruteforce;
import java.util.HashMap;
import java.util.Scanner;
public class StringsMakingAnagrams {
	public static int numberNeeded(String first, String second) {
		HashMap<Character, Integer> hmap = new HashMap<Character, Integer>();
		int length = 0;
		for (int i = 0; i < first.length(); i++) {
			char c = first.charAt(i);
			if (hmap.containsKey(c)) {
				hmap.put(c, hmap.get(c) + 1);
			} else {
				hmap.put(c, 1);
			}
		}
		for (int i = 0; i < second.length(); i++) {
			char c = second.charAt(i);
			if (hmap.containsKey(c) && hmap.get(c) != 0) {
				hmap.put(c, hmap.get(c) - 1);
				length += 2;
			}
		}
		return (first.length() + second.length() - length);
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String a = in.next();
		String b = in.next();
		in.close();
		System.out.println(numberNeeded(a, b));
	}
}

5. Brute Force: Encryption

package org.javaaid.hackerrank.solutions.implementation.bruteforce;
import java.util.Scanner;
public class Encryption {
	 static String encryption(String s) {
	        int length=s.length();
	        double rows=Math.floor(Math.sqrt(length));
	        double cols=Math.ceil(Math.sqrt(length));
	        int len=(int) (rows>cols?rows:cols);
	        String result="";
	        for(int i=0;i<len;i++) {
	        	for(int j=i;j<s.length();j=(int) (j+cols)) {
	        		char ch=s.charAt(j);
	        		result+=ch;
	        	}
	        	result+=" ";
	        }
			return result;
	    }
	    public static void main(String[] args) {
	        Scanner in = new Scanner(System.in);
	        String s = in.next();
	        String result = encryption(s);
	        System.out.println(result);
	        in.close();
	    }
	}

6. Brute Force: Making Anagrams

package org.javaaid.hackerrank.solutions.implementation.bruteforce;
import java.util.HashMap;
import java.util.Scanner;
public class MakingAnagrams {
	public static int numberNeeded(String first, String second) {
		HashMap<Character, Integer> hmap = new HashMap<Character, Integer>();
		int length = 0;
		for (int i = 0; i < first.length(); i++) {
			char c = first.charAt(i);
			if (hmap.containsKey(c)) {
				hmap.put(c, hmap.get(c) + 1);
			} else {
				hmap.put(c, 1);
			}
		}
		for (int i = 0; i < second.length(); i++) {
			char c = second.charAt(i);
			if (hmap.containsKey(c) && hmap.get(c) != 0) {
				hmap.put(c, hmap.get(c) - 1);
				length += 2;
			}
		}
		return (first.length() + second.length() - length);
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String a = in.next();
		String b = in.next();
		System.out.println(numberNeeded(a, b));
		in.close();
	}
}

Related Articles

Core Java

1. What are primitive types in Java ? byte, short, int, long, float, double, char, boolean… 2. What are tokens? Name the 5 types of tokens available in Java with an example…

Hackerearth-Java

     1. Challenge : Welcome to Java! Welcome to the world of Java! In this challenge, we practice printing to stdout. The code stubs…

Hackerearth-Algorithm

      1. Challenge: Quicksort 1 – Partition The previous challenges covered Insertion Sort, which is a simple and intuitive sorting algorithm with a running…

Responses

Your email address will not be published. Required fields are marked *

KodNest Training New Batch is starting on 5th October 2020. Attend one week free demo classes.Register Now

New Report

Close