import java.util.*; public class Solution {      public static int maxmatchupbyHamiltonia = 0;      public static int maxmatchupbyBurrgadia = 0;     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int a = sc.nextInt();         int b = sc.nextInt();         int[] r_i = new int[a];         int[] s_j = new int[b];         for (int i = 0; i < a; i++) {             r_i[i] = sc.nextInt();         }         for (int i = 0; i < b; i++) {             s_j[i] = sc.nextInt();         }                  int K = Math.min(a, b);         Arrays.sort(r_i);         Arrays.sort(s_j);                  int i = 0, j = 0;                          while (i < a && j < b && i < K && j < K) {             if (r_i[i] >= s_j[j]) {                 maxmatchupbyHamiltonia++;                 i++;             } else {                 maxmatchupbyBurrgadia++;                 j++;             }         }                  System.out.printf("%d %d\n",maxmatchupbyHamiltonia,maxmatchupbyBurrgadia);     } }

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter19: Probabilistic Dynamic Programming
Section19.3: How To Maximize The Probability Of A Favorable Event Occurring
Problem 1P
icon
Related questions
Question

Information and about the problem is given in the screenshot. To Solve the problem, I have coded a solution in java which while is able to pass few test cases, but seems to fail to pass the other. Logically it seems there is an error. I need help in fixing the code (by adding conditions or modifying it, such that it is able to pass the three test cases mentioned in the screenshot ,(currently it is able to pass 2/3. It should pass all 3).

The code-

import java.util.*;

public class Solution {
     public static int maxmatchupbyHamiltonia = 0;
     public static int maxmatchupbyBurrgadia = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int[] r_i = new int[a];
        int[] s_j = new int[b];
        for (int i = 0; i < a; i++) {
            r_i[i] = sc.nextInt();
        }
        for (int i = 0; i < b; i++) {
            s_j[i] = sc.nextInt();
        }
        
        int K = Math.min(a, b);
        Arrays.sort(r_i);
        Arrays.sort(s_j);
        
        int i = 0, j = 0;
       
        
        while (i < a && j < b && i < K && j < K) {
            if (r_i[i] >= s_j[j]) {
                maxmatchupbyHamiltonia++;
                i++;
            } else {
                maxmatchupbyBurrgadia++;
                j++;
            }
        }
        
        System.out.printf("%d %d\n",maxmatchupbyHamiltonia,maxmatchupbyBurrgadia);
    }
}

 

Sample Input 0
35
5
4
ANO A
1
0
Sample Output 0
30
Sample Input 1
in c0 mm 0 in 0 m
54
8
3
3
4
8
5
1
8
3
Sample Output 1
22
Sample Input 2
TANGO in H
78
12
Sample Output 2
70
Transcribed Image Text:Sample Input 0 35 5 4 ANO A 1 0 Sample Output 0 30 Sample Input 1 in c0 mm 0 in 0 m 54 8 3 3 4 8 5 1 8 3 Sample Output 1 22 Sample Input 2 TANGO in H 78 12 Sample Output 2 70
Decisions are happening over battle. Two armies walk onto the battlefield, diametric'ly opposed, foes. They
emerge with a compromise, having opened doors that were previously closed.
They decide that their nations' fate must be addressed with diplomacy. Specifically, the sovereignty of their
nations will be decided with a rap battle between the two nations' government leaders. May the raddest
nation win.
The first nation of Hamiltonia has A leaders, each having a rap proficiency of R₂. The second nation of
Burrgadia has B leaders, each having a rap proficiency of Sj.
If one of the nations has more or fewer leaders than the other, only K rap battles will occur, where
K = min (A, B). The nation with fewer leaders can decide whom among its ranks will be matched against
each of the opposing team's leaders.
In a rap battle, the leader with the greater rap proficiency wins. Let us assume that two representatives are
currently matched up. Representative 1 has a rap proficiency of 5, while Representative 2 has a rap proficiency
of 6. Representative 2 will win in this case. If a tie occurs between two leaders' rap proficiencies, the winner is
the team with more total leaders, i.e., the team who did NOT choose the matchups.
Given the lineups of Hamiltonia and Burrgadia, can you determine the maximum matches each side can win,
assuming that the team with fewer leaders chooses their matchups optimally?
Input Format
Input begins with a line containing two space-separated integers: A and B, the number of leaders of
Hamiltonia and Burrgadia, respectively.
A lines follow, each containing a single integer R, the rap proficiency of Hamiltonia's ith leader.
Blines follow, each containing a single integer S;, the rap proficiency of Burrgadia's jth leader.
Constraints
1 ≤ A, B ≤ 2.105
A & B
0 ≤ Ri, Sj ≤ 10⁹
Output Format
Output contains a line with two space-separated integers W., and W₁.
Wo is the maximum matchups won by Hamiltonia
W, is the maximum matchups won by Burrgadia.
Transcribed Image Text:Decisions are happening over battle. Two armies walk onto the battlefield, diametric'ly opposed, foes. They emerge with a compromise, having opened doors that were previously closed. They decide that their nations' fate must be addressed with diplomacy. Specifically, the sovereignty of their nations will be decided with a rap battle between the two nations' government leaders. May the raddest nation win. The first nation of Hamiltonia has A leaders, each having a rap proficiency of R₂. The second nation of Burrgadia has B leaders, each having a rap proficiency of Sj. If one of the nations has more or fewer leaders than the other, only K rap battles will occur, where K = min (A, B). The nation with fewer leaders can decide whom among its ranks will be matched against each of the opposing team's leaders. In a rap battle, the leader with the greater rap proficiency wins. Let us assume that two representatives are currently matched up. Representative 1 has a rap proficiency of 5, while Representative 2 has a rap proficiency of 6. Representative 2 will win in this case. If a tie occurs between two leaders' rap proficiencies, the winner is the team with more total leaders, i.e., the team who did NOT choose the matchups. Given the lineups of Hamiltonia and Burrgadia, can you determine the maximum matches each side can win, assuming that the team with fewer leaders chooses their matchups optimally? Input Format Input begins with a line containing two space-separated integers: A and B, the number of leaders of Hamiltonia and Burrgadia, respectively. A lines follow, each containing a single integer R, the rap proficiency of Hamiltonia's ith leader. Blines follow, each containing a single integer S;, the rap proficiency of Burrgadia's jth leader. Constraints 1 ≤ A, B ≤ 2.105 A & B 0 ≤ Ri, Sj ≤ 10⁹ Output Format Output contains a line with two space-separated integers W., and W₁. Wo is the maximum matchups won by Hamiltonia W, is the maximum matchups won by Burrgadia.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole