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);
==or!=, but usingequals().can you please make those changes in the code?Seriously? You can't make those yourself?