Lambda Expressions HackerRank Solution

The given Java code is a solution to a problem where the user needs to determine whether a given number is odd, prime, or a palindrome. The code uses lambda expressions, which are a feature of Java 8, to define the logic for these checks. The code is organized into a class 'MyMath' that contains methods for each of these checks, and a main method in the 'JavaLambdaExpressions' class that uses these methods.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Lambda Expressions HackerRank Solution HackerRank solution using CodeRankGPT:

The solution approach is to define three methods in the 'MyMath' class, each returning a lambda expression that performs a specific check. The 'isOdd' method checks if a number is odd by using bitwise AND operation with 1. The 'isPrime' method checks if a number is prime by checking divisibility from 2 to the square root of the number. The 'isPalindrome' method checks if a number is a palindrome by reversing the string representation of the number and comparing it with the original. In the main method, based on user input, the appropriate method is called and the result is printed.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.advanced;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 *
 */
interface PerformOperation {
	boolean check(int a);
}

class MyMath {
	public static boolean checker(PerformOperation p, int num) {
		return p.check(num);
	}

	// Write your code here
	public static PerformOperation isOdd() {
		return n -> ((n & 1) == 1);
	}

	public static PerformOperation isPrime() {
		return n -> {
			if (n < 2) {
				return false;
			} else {
				int k = (int) Math.sqrt(n);
				for (int i = 2; i <= k; i++) {
					if (n % i == 0)
						return false;
				}
				return true;
			}
		};
	}

	public static PerformOperation isPalindrome() {
		return n -> {
			String org = n + "";
			String newString = new StringBuffer(org).reverse().toString();
			return org.equals(newString);
		};
	}
}

public class JavaLambdaExpressions {
	public static void main(String[] args) throws IOException {
		MyMath ob = new MyMath();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		PerformOperation op;
		boolean ret = false;
		String ans = null;
		while (T-- > 0) {
			String s = br.readLine().trim();
			StringTokenizer st = new StringTokenizer(s);
			int ch = Integer.parseInt(st.nextToken());
			int num = Integer.parseInt(st.nextToken());
			if (ch == 1) {
				op = ob.isOdd();
				ret = ob.checker(op, num);
				ans = (ret) ? "ODD" : "EVEN";
			} else if (ch == 2) {
				op = ob.isPrime();
				ret = ob.checker(op, num);
				ans = (ret) ? "PRIME" : "COMPOSITE";
			} else if (ch == 3) {
				op = ob.isPalindrome();
				ret = ob.checker(op, num);
				ans = (ret) ? "PALINDROME" : "NOT PALINDROME";

			}
			System.out.println(ans);
		}
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform