How to write a O(n^2) method that finds the largest distance between two points

前端 未结 2 1795
自闭症患者
自闭症患者 2021-01-25 19:13

I have an array int [] nums = {5, 1, 6, 10, 4, 7, 3, 9, 2}

I want to find the distance between the smallest and largest number in that array in O(n^2) time.

2条回答
  •  感情败类
    2021-01-25 19:59

    For numbers (as opposed to points on a plane) You can do that in linear time. Find maximum, find minimum, then subtract. So, no nested loops required.

提交回复
热议问题