How we can make pair of array without using 2 for loop(which has not O(n2) time complexity
for e.g- if array=[1,2,3]
Ans=[12,13,21,23,31,32]
1 Like
Here is one way!
arr = [1, 2, 3]
arr2 = []
for x in range(len(arr)):
for y in range(len(arr)):
if arr[x] != arr[y]:
arr2.append(int(str(arr[x]) + str(arr[y])))
arr2.append(int(str(arr[x]) + str(arr[y])))
print(sorted(set(arr2)))
I would really appreciate any feedback and karma (if you could upvote this). I need karma to ask a few of my own questions.
Thanks!
2 Likes
It is not possible to do this in less than \mathcal{O}(n^2) since the number of elements in the new array will be 2 {{n}\choose{2}} which is \mathcal{O}(n^2).
4 Likes