Help to optimize the code

How to reaarange the second array to get that…maximum number of elemnets in the first array is greater than the element of second array
@everule1
My code is:

from itertools import permutations
for _ in range(int(input())):
    n=int(input())
    team_g=list(map(int,input().split()))
    other=list(map(int,input().split()))
    ma=0
    for prem in permutations(other):
        count=0
        for j in range(len(prem)):
            if prem[j]<team_g[j]:
                count+=1
        ma=max(ma,count)
        if(ma==n):
            break
    print(ma)

help to optimize this

sample input :
1
10
3 6 7 5 3 5 6 2 9 1
2 7 0 9 3 6 0 6 2 6

output

7

Please use ``` above and below your code to format it correctly.

Thanks @everule1 …Kindly suggest some alternate approach

What are the constraints on n? By rearrange does it mean any permutation, or a cyclic shift?

1<= T <=100000
1<= N <=100000

There’s definitely also a constraint on \sum n also then. What is the constraint on the sum of n over all testcases?

The problem is, the data is present randomly and not in the order they have to fight the opponent. Team G-Revolution wants to win at any cost and for that, they need the order in which they have to fight optimally to win the maximum number of battles.

A player can win only when his/her beyblade power is strictly greater than the opponents beyblade power.

0<= beyblade power <=INT_MAX

Oh okay. I’ll just give you a pseudocode as it’s a bit difficult to explain

Input arrays a and b
Sort a and b from largest to smallest
i,j=0
ans=0
For j =0 to n
       If  a[i]>b[j]
             ans++
             i++

Try it out on paper you’ll see what I mean.

1 Like

Thanks i’ll check it out…Kindly help me to improve my skills…I’m the beginner…suggest some tips,tutorials,concepts,algorithms…where can i start?

One of the comments has a link to the competetive programming handbook pdf. Read the full thing, and try questions again. Its only 300 pages. It’s okay if you don’t understand it. Just read the full thing. As you do questions, they’ll remind you of some topic in the book, then reread that part.

Thanks @everule1

   sort(S.begin(), S.end(), greater<int>());
    sort(G.begin(), G.end(), greater<int>());
    int k,j=0;
    for(k=0;k<N;k++){
        if(S[j]>G[k]){
            count++;
            j++;
        }
    }
    cout<<count<<endl;

I’m using the same technique but having problem for passing all the test cases.

Where’s the question link?

the open coding contest problem number two

Hi @abhay2 Link is broken.

https://www.techgig.com/codegladiators/opencontest
PROBLEM NUMBER-2

Bro, you can not discuss problem of ongoing contest


** No worries brother I have solved the problem**


which question is this