View difference between Paste ID: 0FWfSxUB and YGst2Hmc
SHOW: | | - or go back to the newest paste.
1
// Author : Saurav Kalsoor
2-
// Sort By Primes - JAVA
2+
// Game with Thor and Loki - JAVA
3
4
import java.util.*;
5
6
public class Test {
7
    
8
    static Scanner sc = new Scanner(System.in);
9
    public static void main(String[] args) {
10
        int n = sc.nextInt();
11
12
        ArrayList<Integer> arr = new ArrayList<>();
13
14
        for(int i=0; i < n; i++) {
15
            int input = sc.nextInt();
16
            arr.add(input);
17
        }
18
19-
        ArrayList<Integer> result = sortPrimeNonPrime(arr, n);
19+
		gameWithThorAndLoki(arr, n);
20-
        
20+
21-
        for(int i=0; i < n; i++)
21+
22-
            System.out.print(result.get(i) + " ");
22+
23-
        
23+
    public static void gameWithThorAndLoki(ArrayList<Integer> arr, int n){
24-
        System.out.println();
24+
		int start = 0, end = n - 1;
25
		boolean isThorsTurn = true;
26
		
27-
    public static ArrayList<Integer> sortPrimeNonPrime(ArrayList<Integer> arr, int n){
27+
		ArrayList<Integer> res = recurr(arr, start, end, isThorsTurn);
28
29-
        int maxLimit = 10001;
29+
		System.out.println(res.get(0) + " " + res.get(1));
30-
        HashSet<Integer> primesSet = new HashSet<>();
30+
31-
        boolean[] isPrime = new boolean[maxLimit];
31+
32
	public static ArrayList<Integer> recurr(ArrayList<Integer> arr, int start, int end, boolean isThorsTurn){
33-
        for(int i=2; i < maxLimit; i++) isPrime[i] = true;
33+
		if(start > end){
34
			ArrayList<Integer> res = new ArrayList<Integer>();
35-
        for(int i=2; i < maxLimit; i++){
35+
			res.add(0);
36-
            if(isPrime[i]){
36+
			res.add(0);
37-
                primesSet.add(i);
37+
			return res;
38-
                for(int j = i*i ; j < maxLimit; j += i)
38+
		}
39-
                    isPrime[j] = false;
39+
40-
            }
40+
		int left = arr.get(start);
41
		int right = arr.get(end);
42
		int currThor = 0, currLoki = 0;
43-
        ArrayList<Integer> primes = new ArrayList<Integer>();
43+
44-
        ArrayList<Integer> nonPrimes = new ArrayList<Integer>();
44+
		if(isThorsTurn){
45
			currThor = Math.max(left, right);
46-
        for(int i=0; i < n; i++){
46+
			currLoki = Math.min(left, right);
47-
            if(primesSet.contains(arr.get(i))){
47+
		}else{
48-
                primes.add(arr.get(i));
48+
			currLoki = Math.max(left, right);
49-
            }else{
49+
			currThor = Math.min(left, right);
50-
                nonPrimes.add(arr.get(i));
50+
		}
51-
            }
51+
52
		start++;
53
		end--;
54-
        Collections.sort(primes);
54+
		ArrayList<Integer> res = recurr(arr, start, end, !isThorsTurn);
55-
        Collections.sort(nonPrimes, Collections.reverseOrder());
55+
56-
        
56+
		res.set(0, res.get(0) + currThor);
57-
        ArrayList<Integer> result = new ArrayList<Integer>();
57+
		res.set(1, res.get(1) + currLoki);
58
		return res;
59-
        int j = 0, k = 0;
59+
	}
60-
        for(int i=0; i < n; i++){
60+
61-
            if(primesSet.contains(arr.get(i))){
61+
62-
                result.add(primes.get(j));
62+
63-
                j++;
63+
64-
            }else{
64+
65-
                result.add(nonPrimes.get(k));
65+