Find the number of unordered pair in an array

前端 未结 5 1274
囚心锁ツ
囚心锁ツ 2021-01-05 10:18

I ran into an interesting algorithm problem:

Given an array of integer, find the number of un-ordered pairs in that array, say given {1, 3, 2}, the answer is 1 becau

5条回答
  •  心在旅途
    2021-01-05 10:37

    If you are just looking for the number of un-ordered pair and the array is sorted in ascending order. You can use this formula n * (n - 1) / 2. Suppose your array has n elements, for example 3 in your case. It will be 3 * 2 / 2 = 3. Assuming there are no duplicate elements.

提交回复
热议问题