Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Saurav Kalsoor
- // Beautiful Array - JAVA
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int n = sc.nextInt();
- int k = sc.nextInt();
- ArrayList<Integer> arr = new ArrayList<>();
- for(int i=0; i < n ;i++){
- arr.add(sc.nextInt());
- }
- System.out.println(beautifulArray(n, k, arr));
- }
- public static int beautifulArray(int n, int k, ArrayList<Integer> arr){
- HashMap<Integer, Integer> freq = new HashMap<>();
- for(int x : arr){
- freq.put(x, freq.getOrDefault(x, 0) + 1);
- }
- Collections.sort(arr);
- int res = 0;
- for(int i = 0; i < n; i++){
- if(freq.containsKey(arr.get(i))){
- int y = k * arr.get(i);
- freq.put(arr.get(i), freq.get(arr.get(i)) - 1);
- if(freq.get(arr.get(i)) == 0){
- freq.remove(arr.get(i));
- }
- if(freq.containsKey(y)){
- freq.put(y, freq.get(y) - 1);
- if(freq.get(y) == 0){
- freq.remove(y);
- }
- }else{
- res++;
- }
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement