دروس من الحياة

Advertisements

Java Primality Test

Java Primality Test

import java.util.*;
import java.math.*;

public class Solution {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		BigInteger n = in.nextBigInteger();
		in.close();

		if (!n.equals(new BigInteger("1")) && n.isProbablePrime(1))
			System.out.println("prime");
		else
			System.out.println("not prime");

	}
}

496. Next Greater Element I

496. Next Greater Element I

class Solution {
  	public int[] nextGreaterElement(int[] nums1, int[] nums2) {
		int[] out = new int[nums1.length];

		for (int i = 0; i < nums1.length; i++) {
			// search
			int startSearch = 0;
			for (int k = 0; k < nums2.length; k++) {
				if (nums1[i] == nums2[k]) {
					startSearch = k;
					break;
				}
			}
			int outElement = -1;
			for (int j = startSearch + 1; j < nums2.length; j++) {
				if (nums1[i] < nums2[j]) {
					outElement = nums2[j];
					break;
				}
			}
			out[i] = outElement;
		}
		return out;
	}
}