Ayush-Tibrewal / LEETCODE---Java-Solutions-

0 stars 0 forks source link

genernal ways to remeber #1

Open Ayush-Tibrewal opened 1 month ago

Ayush-Tibrewal commented 1 month ago

First, Arrays.sort does not return the sorted array; it sorts the array in place. This means you should call Arrays.sort directly on the array without reassigning it

int[] ayush = new int[n];
 ayush = Arrays.sort(heights);
// this is not possibkle as it is in place 

the assignment ayush = heights makes ayush just another reference to the heights array, so sorting heights also sorts ayush. This leads to incorrect results.

  int[] ayush = new int[n];
        ayush = heights;
        Arrays.sort(heights);

correct way

int[] ayush = Arrays.copyOf(heights, n); 
        Arrays.sort(heights);
Ayush-Tibrewal commented 2 weeks ago

when we maximise someowm from minimum or minimus someone from maximum -- must be in brain binary search