0

(1 ≤ N ≤ 1e+5) I have to find how many cases in this condition: i < j and array[i] > array[j] here I have tried:

k = int(input())
n = tuple(map(int, input().split()))
h=0
i=0
j=0
for j in range(k-1):
    for i in range(j+1,k):
        if n[j]>n[i]:
            h+=1
print(h)

but it gives time limit. is there better way to iterate it. Test case 1:

10
7 6 2 4 1 5 10 3 9 8
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • I used reduce but it has same effect and timelimit occured ```k = int(input()) n = list(map(int, input().split())) h=0 for i in range(k): h+=len(list(filter(lambda x:n[i]>x,n[i:]))) #print(list(filter(lambda x:n[i]>x,n[i:]))) print(h)``` – Avazbek Vaxobov May 09 '21 at 06:54
  • Don't put code like that in a comment, edit your question and add it as formatted text. In any case, why are you using `list(reduce(...))`? – juanpa.arrivillaga May 09 '21 at 06:57
  • Also, when you make a stack overflow question, please spare us the `input()` nonsense. Just give us example data. – juanpa.arrivillaga May 09 '21 at 07:08

0 Answers0