View difference between Paste ID: YGst2Hmc and v31w9RAq
SHOW: | | - or go back to the newest paste.
1
// Author : Saurav Kalsoor
2-
// Sort By Indices - JAVA
2+
// Sort By Primes - 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-
        int m = sc.nextInt();
11+
12
        ArrayList<Integer> arr = new ArrayList<>();
13-
        ArrayList<Integer> indices = 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);
20-
        for(int i=0; i < m; i++) {
20+
21
        for(int i=0; i < n; i++)
22-
            indices.add(input);
22+
23
        
24
        System.out.println();
25-
        ArrayList<Integer> result = sortByIndices(arr, indices, n, m);
25+
26
27
    public static ArrayList<Integer> sortPrimeNonPrime(ArrayList<Integer> arr, int n){
28
29
        int maxLimit = 10001;
30
        HashSet<Integer> primesSet = new HashSet<>();
31
        boolean[] isPrime = new boolean[maxLimit];
32
33-
    public static ArrayList<Integer> sortByIndices(ArrayList<Integer> arr, ArrayList<Integer> indices, int n, int m){
33+
        for(int i=2; i < maxLimit; i++) isPrime[i] = true;
34-
        ArrayList<Integer> sortDesc = new ArrayList<Integer>();
34+
35-
        ArrayList<Integer> sortAsc = new ArrayList<Integer>();
35+
        for(int i=2; i < maxLimit; i++){
36
            if(isPrime[i]){
37-
        HashSet<Integer> indicesSet = new HashSet<Integer>();
37+
                primesSet.add(i);
38
                for(int j = i*i ; j < maxLimit; j += i)
39-
        for(int i=0; i < m; i++){
39+
                    isPrime[j] = false;
40-
            indicesSet.add(indices.get(i));
40+
41
        }
42
43
        ArrayList<Integer> primes = new ArrayList<Integer>();
44-
            if(indicesSet.contains(i)){
44+
        ArrayList<Integer> nonPrimes = new ArrayList<Integer>();
45-
                sortDesc.add(arr.get(i));
45+
46
        for(int i=0; i < n; i++){
47-
                sortAsc.add(arr.get(i));
47+
            if(primesSet.contains(arr.get(i))){
48
                primes.add(arr.get(i));
49
            }else{
50
                nonPrimes.add(arr.get(i));
51-
        Collections.sort(sortDesc, Collections.reverseOrder());
51+
52-
        Collections.sort(sortAsc);
52+
53
    
54
        Collections.sort(primes);
55
        Collections.sort(nonPrimes, Collections.reverseOrder());
56
        
57
        ArrayList<Integer> result = new ArrayList<Integer>();
58-
            if(indicesSet.contains(i)){
58+
59-
                result.add(sortDesc.get(j));
59+
60
        for(int i=0; i < n; i++){
61
            if(primesSet.contains(arr.get(i))){
62-
                result.add(sortAsc.get(k));
62+
                result.add(primes.get(j));
63
                j++;
64
            }else{
65
                result.add(nonPrimes.get(k));
66
                k++;
67
            }
68
        }
69
70
        return result;
71
    }
72
}
73
74