Queries to print rely of distinct array components after changing factor at index P by a given factor

by admin

 

import java.io.*;

import java.util.*;

 

class GFG {

 

    

    

    static void retailer(int arr[], int n,

                      HashMap<Integer,

                              Integer>

                          map)

    {

        for (int i = 0; i < n; i++) {

 

            

            

            if (!map.containsKey(arr[i]))

                map.put(arr[i], 1);

            else

                map.put(arr[i],

                        map.get(arr[i]) + 1);

        }

    }

 

    

    

    static void Distinct(int arr[], int n,

                         int p, int x,

                         HashMap<Integer,

                                 Integer>

                             map)

    {

 

        

        

        map.put(arr[p - 1],

                map.get(arr[p - 1]) - 1);

 

        if (map.get(arr[p - 1]) == 0)

            map.take away(arr[p - 1]);

 

        

        if (!map.containsKey(x)) {

            map.put(x, 1);

        }

        else {

            map.put(x, map.get(x) + 1);

        }

 

        

        arr[p - 1] = x;

 

        

        

        System.out.print(map.dimension() + " ");

    }

 

    

    

    static void updateQuery(

        int arr[], int n,

        int queries[][], int q)

    {

        

        HashMap<Integer, Integer> map

            = new HashMap<>();

 

        retailer(arr, n, map);

 

        for (int i = 0; i < q; i++) {

 

            

            Distinct(arr, n, queries[i][0],

                     queries[i][1], map);

        }

    }

 

    

    public static void major(String[] args)

    {

        

        int[] arr = { 2, 2, 5, 5, 4, 6, 3 };

 

        int N = arr.size;

        int Q = 3;

 

        

        int queries[][]

            = new int[][] { { 1, 7 },

                            { 6, 8 },

                            { 7, 2 } };

 

        

        updateQuery(arr, N, queries, Q);

    }

}

Related Posts

Leave a Comment