Skip to content

Commit cf1bf37

Browse files
committed
Optimal reordering of array such that maximum battles can be won
1 parent 5c64064 commit cf1bf37

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
Tyson is all prepared for the Beyblade World Championship.
3+
The tournament is team-based and each team can have N
4+
members. A player can fight against a single player only. Team G-Revolution
5+
is all excited and pumped up as they have practised a
6+
lot. Kenny, the mind of team G-Revolution, has created a
7+
database where he has the data about the power of other
8+
teams’ members and his own team members. The tournament
9+
is going to start in some time and Kenny moves to the cafeteria to have a
10+
snack before the competition.
11+
12+
The team G-Revolution is to fight in some time and they are tensed up as someone has kidnapped
13+
Kenny from the cafeteria. They have made a police complaint and the police are searching for
14+
Kenny. Luckily, they have found his device with all the data. The problem is, the data is present
15+
randomly and not in the order they have to fight the opponent. Team G-Revolution wants to win at
16+
any cost and for that, they need the order in which they have to fight optimally to win the
17+
maximum number of battles.
18+
19+
20+
21+
A player can win only when his/her beyblade power is strictly greater than the opponents beyblade
22+
power.
23+
24+
25+
26+
Example:
27+
28+
Consider the team size is 3, N = 3
29+
30+
31+
The 3 players of both the teams are shown with their beyblade powers.
32+
33+
Team G-Revolution is presented in the order: Tyson(20), Max(30), Ray(50)
34+
35+
Team All Starz is presented in the order: Michael(60), Eddy(40), Steve(25)
36+
37+
38+
39+
With the given arrangement, Team G-Revolution would be able to win only 1 fight. Team G-Revolution
40+
should be shuffled in an optimal manner as below:
41+
42+
The maximum number of fights Team G-Revolution can win is 2 when they are arranged optimally or
43+
fight in an optimal order.
44+
45+
46+
47+
Team G-Revolution needs help with the device. Tyson has heard about your skills and called you
48+
up to help them shuffle their positions in an order such that they would be able to win the
49+
maximum number of fights. Can you help Tyson and Team G-Revolution?
50+
51+
52+
53+
Input Format
54+
The first line of input consists of the number of test cases, T
55+
56+
The first line of each test case consists of the number of members each team can have, N.
57+
58+
The second line of each test case consists of N space-separated integers representing the power
59+
of beyblades of Team G-Revolution members.
60+
61+
62+
The third line of each test case consists of N space-separated integers representing the power
63+
of beyblades of opponent team members.
64+
65+
Constraints
66+
1<= T <=100000
67+
1<= N <=100000
68+
69+
0<= Power of Beyblade <= LLONG_MAX
70+
71+
72+
73+
Output Format
74+
For each test case, print the maximum number of fights Team G-Revolution can win if they go to
75+
fight in an optimal manner.
76+
77+
Sample TestCase 1
78+
Input
79+
1
80+
10
81+
3 6 7 5 3 5 6 2 9 1
82+
2 7 0 9 3 6 0 6 2 6
83+
Output
84+
7
85+
"""
86+
87+
#Greedy approach may be right/generic solution
88+
def optimalReOrder(g_rev,oppo,N):
89+
arr1 = g_rev.copy()
90+
arr2 = oppo.copy()
91+
i = 0
92+
j = 0
93+
k = i
94+
while ((i < N) and (j < N)):
95+
if (arr1[k] <= arr2[j]):
96+
if k == N-1:
97+
pass
98+
else:
99+
k += 1
100+
continue
101+
else:
102+
temp = arr1[i]
103+
arr1[i] = arr1[k]
104+
arr1[k] = temp
105+
i += 1
106+
j += 1
107+
k = i
108+
print (arr1)
109+
print (arr2)
110+
111+
if __name__ == '__main__':
112+
T = int(input())
113+
for tcs in range(T):
114+
N = int(input())
115+
g_rev = list(map(int,input().strip().split()))
116+
oppo = list(map(int,input().strip().split()))
117+
optimalReOrder(g_rev,oppo,N)

0 commit comments

Comments
 (0)