solving the code for this problem in Java. The time complexity has to be as less as possible (nlogn or n at best, no n^2).

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

URGENT- Information is present in the screenshot and below. Based on that need help in solving the code for this problem in Java. The time complexity has to be as less as possible (nlogn or n at best, no n^2). Apply divide-and-conquer algorithm or greedy algorithm in the problem. I have coded it partially and it gives partially correct answer, but seems to not to work for lot of  test cases. Can you modify the code so it passes the three test cases given  in the screenshot ASAP

The Code-

import java.util.*;

public class Solution {
    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();
        }
        
        Arrays.sort(r_i);
        Arrays.sort(s_j);
        
        int i = 0, j = 0;
        int maxmatchupbyHamiltonia = 0, maxmatchupbyBurrgadia = 0;
        
        while (i < a && j < b) {
            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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Time complexity
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education