Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.9 kB
2
Indexable
Never
Q1. Prime Sum

Problem Description
Given an even number A ( greater than 2 ), return two prime numbers whose sum will be equal to the given number.

If there is more than one solution possible, return the lexicographically smaller solution.

If [a, b] is one solution with a <= b, and [c,d] is another solution with c <= d, then 
[a, b] < [c, d], If a < c OR a==c AND b < d. 
NOTE: A solution will always exist. Read Goldbach's conjecture.

Problem Constraints
4 <= A <= 2*107

Input Format
First and only argument of input is an even number A.

Output Format
Return a integer array of size 2 containing primes whose sum will be equal to given number.

Example Input
 4

Example Output
 [2, 2]


Example Explanation
 There is only 1 solution for A = 4.


// This solution is working
public class Solution {
    public int[] sieve(int N){
        // Generate isPrime List less equal than N
        int[] isPrime = new int[N + 1];
        isPrime[0] = 0;
        isPrime[1] = 0;
        for(int i = 2; i <= N; i++){
            isPrime[i] = 1;
        }
        // Sieve of Erastothenes
        for(int i = 2; i <= N; i++) {
            if (isPrime[i] == 0)
                continue;
            if (i > N / i) 
                break;
            for (int j = i * i; j <= N; j += i) 
                isPrime[j] = 0;
        }
        return isPrime;
    }
    public int[] primesum(int A) {
        int[] isPrime = sieve(A);
        int[] ans = new int[2];
        for(int i = 2; i <= A; ++i) {
            if(isPrime[i] == 1 && isPrime[A - i] == 1) {
                ans[0] = i;
                ans[1] = A - i;
                return ans;
            }
        } 
        return ans;
    }
}


// This below solution is throwing error. Surprisingly Scaler Given Full score even though Its throwing error. 
// There is no logic changed Just used ArrayList<Integer> insted on int[]
// Error pasted below the solution
// It is throwing error at Line Number 99 below
public class Solution {
    public ArrayList<Integer> primesum(int A) {
        ArrayList<Integer> s = seive(A);

		ArrayList<Integer> output = new ArrayList<Integer>();

		for (int i = 2; i <= A; i++) {
			if (1 == s.get(i) && s.get(A - i) == 1) {

				output.add(i);
				output.add(A - i);

				return output;

			}
		}

		return output;
	}

	public static ArrayList<Integer> seive(int A) {
		ArrayList<Integer> s = new ArrayList<Integer>();

		s.add(0);
		s.add(0);

		for (int i = 2; i <= A; i++) {
			s.add(1);
		}

		for (int i = 2; i <= A; i++) {

			if (s.get(i) == 0) {
				continue;
			}

			if (i > A / i) {
				break;
			}

			for (int j = i * i; j <= A; j = j + i) {
				s.set(j, 0);
			}

		}
		return s;
	}

}



Compiling your Code...
> Success!
Running Test Cases...
> TestCase - Easy Failed
Runtime Error
Your submission stopped because of a runtime error,ex: division by zero, array index out of bounds, uncaught exception. You can try testing your code with custom input and try putting debug statements in your code.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:267)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:241)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:233)
	at java.util.ArrayList.add(ArrayList.java:464)
	at Solution.seive(Solution.java:28)
	at Solution.primesum(Solution.java:3)
	at Main.main(Main.java:329)
Your submission failed for the following input
Arg 1: A single Integer, For e.g 9
16777214
Arg 2: An Integer Array, For e.g [1,2,3]
[]
Test As Custom Input
The expected return value:
31 16777183 
Final Verdict
> Wrong Answer