-1
 Given N 2D Points, Calculate no. of distinct points among them.

 Ex: x[5] = {2, 1, 3, 2, 2}
 y[5] = {3, 1, 2, 3, 4}

 The first array represents the x co-ordinates, the second array represents the y co-ordinate.       Acoording to above examples the points are 
(x[0],y[0])->(2,3)
(x[1],y[1])->(1,1)
(x[2],y[2])->(3,2)
(x[3],y[3])->(2,3)
(x[4],y[4])->(2,4)

Total number of distinct points are 4.

I wrote the implementation for the above problem by creating two HashMaps and storing the index as keys and elements as values. However, the code gave wrong output for ex: for the above test case the output of my code was "5 " as against "4". The implementation is as below:

      HashMap<Integer,Integer> mpp1 = new HashMap<>();
        HashMap<Integer,Integer> mpp2 = new HashMap<>();
        for(int i =0;i<n;i++){
            mpp1.put(i,x[i]);
        }
        for(int i =0;i<n;i++){
            mpp2.put(i,y[i]);
        }
        int count=0;
        boolean b=true;
        for(int i=0;i<n;i++){
            if(mpp1.get(i).equals(mpp2.get(i))==false){
                    b=false;
                   count++;
            }
          //  System.out.print(mpp1.get(i)+" ");
        }
       // if(b==true){
        //System.out.print("-1");
        //}
        
        System.out.print(count);
7
  • Don't compare objects using == or !=, but using equals(). Commented Oct 29, 2023 at 10:00
  • can you please make those changes in the code ? Commented Oct 29, 2023 at 10:26
  • user_program: can you please make those changes in the code? Seriously? You can't make those yourself? Commented Oct 29, 2023 at 19:16
  • i tried to make those changes but they didnt work. Commented Oct 30, 2023 at 4:02
  • for(int i=0;i<n;i++){ if(mpp1.get(i).equals(mpp2.get(i))==false){ count++; } } System.out.print(count); Commented Oct 30, 2023 at 4:04

2 Answers 2

0
    Set<Integer> set = new HashSet();
    int count=0;
    
    for(int i=0;i<n;i++){
        int x = arr1[i]-arr2[i];
        
        if(!set.contains(x)){
            count++;
        }
        set.add(x);
    }
    
    System.out.println(count);
Sign up to request clarification or add additional context in comments.

Comments

-1

Your implementation gave me the answer 4 for the stated problem.

    int n=5;
    int[] x = new int[] {2, 1, 3, 2, 2};
    int[] y = new int[] {3, 1, 2, 3, 4};

So it works, since it gives the expected answer. But I think it is the wrong approach, since you are comparing the x, y coordinates of a point, where x=1, y=1 is the only point that matches x with y and as such is not counted, which results in 4.

But you are looking for distinct 2D points, a 2D point is defined by x, y coordinates. So point-0 is x[0] and y[0], point-1 x[1] and y[1] etc.. So going by this you would need to first find your points, then compare their respective coordinates.

(not optimised) An approach would be to first find matching x coordinates, then check if y also matches if so, then this is your none distinct point. So in your example it would be point-0 (x=2, y=3) and point-3 (x=2, y=3).

The code could look like this:

    int[] x = new int[] {2, 1, 3, 2, 2};
    int[] y = new int[] {3, 1, 2, 3, 4};
    int noneUnique = 0;

    for(int i = x.length - 1; i > 0; i--) {
        for(int j = i - 1; j > -1; j--) {
            if (x[i] == x[j] && y[i] == y[j]) {
                noneUnique++;
            }
        }
    }

    System.out.format("found %d distinct points", x.length - noneUnique);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.