Open In App

Find goal difference for maximum wins and recent team with that goal difference

Last Updated : 30 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Geek is a big fan of the Soccer League. Recently he has got a document containing the scores of all the matches played in the League till date. Since the number of matches that have been played is too large, he has randomly selected N matches whose scores he is going to analyze. Each match is given in the format of { HT HG AT AG} (where HT is Home team, HG is Home goal, AT is Away team, AG Away goal). Geek wants to find the Goal Difference (GD) [i.e., (goals scored by winning team – goals scored by losing team)] by which maximum number of times a Home Team has won a match. Next Geek also wants to find the name of the team which has most recently won a home match by this GD. However, Geek is very lazy and asks for your help. Can you help Geek with his analysis?
In case no home team has won in any of the N games, then output just -1.

Note: A Draw, ie. Both the teams have scored the same number of goals is considered as a win for the Home Team with GD = 0.

Example:

Input: N = 4
mumbai 3 goa 2
bengaluru 1 kerala 0
goa 4 hyderabad 5
bengal 3 goa 1
Output: 1 bengaluru
Explanation: The goal difference by which a home team is winning maximum time is 1.
Mumbai and Bengaluru wins with a goal difference 1, Bengal wins with goal difference 1.
The most recent home team to win with GD = 1, is bengaluru.

Input:
N = 3
mumbai 2 Kerala 3
mumbai 6 goa 3
goa 2 bengal 4
Output: 3 mumbai

 

Approach: The problem can be solved with the help of hashing using the following idea:

Take two hash maps, first one will store the non-negative GD (Goal Difference) i.e., HG (Home Goal) – AG (Arrival Goal), and the most recent team to win by that GD. And the second hash map will store the GD and the corresponding frequency. If there is no non-negative GD, then the output will be -1.

Follow the steps mentioned below to implement the approach:

  • Create two maps to store the frequency and goal difference as shown above.
  • Iterate the result of N matches:
    • Get the goal difference (i.e., GD = HG – AG).
    • If the value is not negative then the home team wins.
    • If the GD is already present in the map, update the team corresponding to it to store the most recent team with that much goal difference.
    • Increment the frequency of GD.
  • Return the GD with the highest frequency and the team associated with it. 

Below is the implementation of the above approach:

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the name of the team
// which has most recently won a home match
// by this GD
void solve(vector<string>& HT, vector<int>& HG,
           vector<string>& AT, vector<int>& AG)
{
    int N = HG.size();
 
    // Hash map to store gd and its frequency
    unordered_map<int, int> gd_freq;
 
    // Hash map to store gd and
    // the recent home team to win
    // with that gd
    unordered_map<int, string> gd_team;
 
    // Inserting league data in hash maps
    for (int i = 0; i < N; i++) {
        int gd = HG[i] - AG[i];
        if (gd >= 0) {
            gd_freq[gd]++;
            gd_team[gd] = HT[i];
        }
    }
 
    // Stores gd with maximum frequency
    int max_index = 0;
 
    // Stores maximum frequency
    int max_freq = -1;
    for (auto itr = gd_freq.begin();
         itr != gd_freq.end();
         itr++) {
 
        // To find maximum frequency
        // and the corresponding gd
        if (max_freq <= itr->second) {
            max_freq = itr->second;
            max_index = itr->first;
        }
    }
 
    // If no home team has won
    if (max_freq == -1) {
        cout << "-1";
    }
    else {
        cout << max_index << " " << gd_team[max_index];
    }
}
 
// Driver Code
int main()
{
    int N = 4;
 
    // Store home teams
    vector<string> HT
        = { "mumbai", "bengaluru", "goa", "bengal" };
 
    // Store goals by home teams
    vector<int> HG = { 3, 1, 4, 3 };
 
    // Store arrival teams
    vector<string> AT
        = { "goa", "kerala", "hyderabad", "goa" };
 
    // Store goals by home teams
    vector<int> AG = { 2, 0, 5, 1 };
 
    // Function call
    solve(HT, HG, AT, AG);
    return 0;
}


Java




// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the name of the team
    // which has most recently won a home match
    // by this GD
    public static void solve(String HT[], int HG[],
                             String AT[], int AG[])
    {
        int N = 4;
 
        // Hash map to store gd and its frequency
        Map<Integer, Integer> gd_freq
            = new HashMap<Integer, Integer>();
 
        // Hash map to store gd and
        // the recent home team to win
        // with that gd
        Map<Integer, String> gd_team
            = new HashMap<Integer, String>();
 
        // Inserting league data in hash maps
        for (int i = 0; i < N; i++) {
            int gd = HG[i] - AG[i];
            if (gd >= 0) {
                int prev_freq = 0;
                if (gd_freq.get(gd) != null) {
                    prev_freq = gd_freq.get(gd);
                }
                gd_freq.put(gd, prev_freq + 1);
                gd_team.put(gd, HT[i]);
            }
        }
 
        // Stores gd with maximum frequency
        int max_index = 0;
 
        // Stores maximum frequency
        int max_freq = -1;
        for (Map.Entry<Integer, Integer> entry :
             gd_freq.entrySet()) {
 
            // To find maximum frequency and the
            // corresponding gd
            if (max_freq <= entry.getValue()) {
                max_freq = entry.getValue();
                max_index = entry.getKey();
            }
        }
 
        // If no home team has won
        if (max_freq == -1) {
            System.out.println("-1");
        }
        else {
            System.out.println(max_index + " "
                               + gd_team.get(max_index));
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4;
 
        // Store home teams
        String HT[]
            = { "mumbai", "bengaluru", "goa", "bengal" };
 
        // Store goals by home teams
        int HG[] = { 3, 1, 4, 3 };
 
        // Store arrival teams
        String AT[]
            = { "goa", "kerala", "hyderabad", "goa" };
 
        // Store goals by home teams
        int AG[] = { 2, 0, 5, 1 };
 
        // Function call
        solve(HT, HG, AT, AG);
    }
}


Python3




# Python3 code to implement the approach
 
# Function to find the name of the team
# which has most recently won a home match
# by this GD
def solve(HT, HG, AT, AG) :
 
    N = len(HG);
 
    # Hash map to store gd and its frequency
    gd_freq = {};
 
    # Hash map to store gd and
    # the recent home team to win
    # with that gd
    gd_team = {};
 
    # Inserting league data in hash maps
    for i in range(N) :
        gd = HG[i] - AG[i];
         
        if (gd >= 0) :
            gd_freq[gd] = gd_freq.get(gd,0) + 1;
            gd_team[gd] = HT[i];
 
    # Stores gd with maximum frequency
    max_index = 0;
 
    # Stores maximum frequency
    max_freq = -1;
    for itr in gd_freq :
 
        # To find maximum frequency
        # and the corresponding gd
        if (max_freq <= gd_freq[itr]) :
            max_freq = gd_freq[itr];
            max_index = itr;
 
    # If no home team has won
    if (max_freq == -1) :
        print("-1");
     
    else :
        print(max_index," ",gd_team[max_index]);
 
 
# Driver Code
if __name__ == "__main__" :
 
    N = 4;
 
    # Store home teams
    HT = [ "mumbai", "bengaluru", "goa", "bengal" ];
 
    # Store goals by home teams
    HG = [ 3, 1, 4, 3 ];
 
    # Store arrival teams
    AT = [ "goa", "kerala", "hyderabad", "goa" ];
 
    # Store goals by home teams
    AG = [ 2, 0, 5, 1 ];
 
    # Function call
    solve(HT, HG, AT, AG);
     
    # This code is contributed by AnkThon


C#




// C# code to implement the approach
using System;
 
// class to store gd frequency and
// the recent home team
class GFG
{
    public class goalDiff
    {
        public int frequency;
        public String team_name;
    }
 
    // Function to find the team and
    // the goal difference
    static void solve(String[] HT, int[] HG,
                      String[] AT, int[] AG)
    {
        int MAX = 100;
        int N = 4;
 
        // Hash map to store gd, its frequency and
        // the recent home team to win with that gd
        goalDiff[] arr = new goalDiff[MAX];
        for (int i = 0; i < MAX; i++)
        {
            arr[i] = new goalDiff();
            arr[i].frequency = 0;
            arr[i].team_name = "";
        }
 
        // Inserting league data in hash maps
        for (int i = 0; i < N; i++)
        {
            int gd = HG[i] - AG[i];
            if (gd >= 0)
            {
                arr[gd].frequency++;
                arr[gd].team_name = HT[i];
            }
        }
 
        // Stores gd with maximum frequency
        int max_index = 0;
 
        // Stores maximum frequency
        int max_freq = -1;
        for (int i = 0; i < MAX; i++)
        {
 
            // To find maximum frequency and
            // the corresponding gd
            if (max_freq <= arr[i].frequency)
            {
                max_freq = arr[i].frequency;
                max_index = i;
            }
        }
 
        // If no home team has won
        if (max_freq == -1)
        {
            Console.WriteLine("-1");
        }
        else
        {
            Console.WriteLine(max_index + " "
                               + arr[max_index].team_name);
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 4;
 
        // Store home teams
        String[] HT = { "mumbai", "bengaluru", "goa", "bengal" };
 
        // Store goals by home teams
        int[] HG = { 3, 1, 4, 3 };
 
        // Store arrival teams
        String[] AT = { "goa", "kerala", "hyderabad", "goa" };
 
        // Store goals by home teams
        int[] AG = { 2, 0, 5, 1 };
 
        // Function call
        solve(HT, HG, AT, AG);
    }
}
 
// This code is contributed by saurabh_jaiswal.


Javascript




//JS code to implement the approach
 
// Function to find the name of the team
// which has most recently won a home match
// by this GD
function solve(HT, HG, AT, AG)
{
    var N = HG.length;
 
    // Hash map to store gd and its frequency
    var gd_freq = {};
 
    // Hash map to store gd and
    // the recent home team to win
    // with that gd
    var gd_team = {};
 
    // Inserting league data in hash maps
    for (var i = 0; i < N; i++)
    {
        var gd = HG[i] - AG[i];
         
        if (gd >= 0)
        {
            if (!gd_freq.hasOwnProperty(gd))
                gd_freq[gd] = 1;
            else
                gd_freq[gd] = gd_freq[gd] + 1;
            gd_team[gd] = HT[i];
        }
    }
 
    // Stores gd with maximum frequency
    var max_index = 0;
 
    // Stores maximum frequency
    var max_freq = -1;
     
    for (const itr in gd_freq)
    {
        // To find maximum frequency
        // and the corresponding gd
        if (max_freq <= gd_freq[itr])
        {
            max_freq = gd_freq[itr];
            max_index = itr;
        }
    }
    // If no home team has won
    if (max_freq == -1)
        console.log("-1");
     
    else
        console.log(max_index, gd_team[max_index]);
}
 
 
// Driver Code
var N = 4;
 
// Store home teams
var HT = [ "mumbai", "bengaluru", "goa", "bengal" ];
 
// Store goals by home teams
var HG = [ 3, 1, 4, 3 ];
 
// Store arrival teams
var AT = [ "goa", "kerala", "hyderabad", "goa" ];
 
// Store goals by home teams
var AG = [ 2, 0, 5, 1 ];
 
// Function call
solve(HT, HG, AT, AG);
     
// This code is contributed by phasing17


Output

1 bengaluru

Time Complexity: O(N)
Auxiliary Space: O(N)

Alternative Approach: The problem can also be solved based on the concept of class and structure as per the following idea:

Create a structure or class with objects as frequency and team_name (which will store the most recent team who won with that Goal difference). Create an array of the declared structure or class with the maximum possible goal difference as its size which will work as a hash table where goal difference is the key.

Follow the steps mentioned below to implement the idea:

  • Create a class or structure as mentioned above and an array (say arr[])of that class or structure.
  • Iterate over all the records of the match:
    • Calculate the goal difference (i.e., GD = HG – AG).
    • If GD is not negative then the home team wins.
    • Fill arr[GD] with the updated frequency and the current team name as this is the team to win the match with a goal difference of GD.
  • Now iterate arr[]:
    • If frequency at arr[i] is the maximum then update the maximum frequency and the associated team with the data of arr[i].
  • Return the maximum frequency and the team associated with that as the answer.

Below is the implementation of the above approach:

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Maximum possible goal difference
#define MAX 100
 
// Structure with frequency
// and recent team name
struct goalDiff {
    int frequency;
    string team_name;
};
 
// Function to find the team
// and the goal difference
void solve(vector<string>& HT, vector<int>& HG,
           vector<string>& AT, vector<int>& AG)
{
    int N = 4;
 
    // Array to store gd, its frequency and
    // the recent home team to win with that gd
    goalDiff arr[MAX];
    for (int i = 0; i < MAX; i++) {
        arr[i].frequency = 0;
        arr[i].team_name = "";
    }
 
    // Inserting league data in hash maps
    for (int i = 0; i < N; i++) {
        int gd = HG[i] - AG[i];
        if (gd >= 0) {
            arr[gd].frequency++;
            arr[gd].team_name = HT[i];
        }
    }
 
    int max_index = 0;
    int max_freq = -1;
    for (int i = 0; i < MAX; i++) {
 
        // To find maximum frequency and
        // the corresponding gd
        if (max_freq <= arr[i].frequency) {
            max_freq = arr[i].frequency;
            max_index = i;
        }
    }
 
    // If no home team has won
    if (max_freq == 0) {
        cout << "-1";
    }
    else {
        cout << max_index << " "
             << arr[max_index].team_name;
    }
}
 
// Driver Code
int main()
{
    int N = 4;
 
    // Store home teams
    vector<string> HT
        = { "mumbai", "bengaluru", "goa", "bengal" };
 
    // Store goals by home teams
    vector<int> HG = { 3, 1, 4, 3 };
 
    // Store arrival teams
    vector<string> AT
        = { "goa", "kerala", "hyderabad", "goa" };
 
    // store goals by home teams
    vector<int> AG = { 2, 0, 5, 1 };
 
    // Function call
    solve(HT, HG, AT, AG);
    return 0;
}


Java




// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
// class to store gd frequency and
// the recent home team
class goalDiff {
    int frequency;
    String team_name;
}
 
class Main {
 
    // Function to find the team and
    // the goal difference
    static void solve(String HT[], int HG[],
                      String AT[], int AG[])
    {
        int MAX = 100;
        int N = 4;
 
        // Hash map to store gd, its frequency and
        // the recent home team to win with that gd
        goalDiff arr[] = new goalDiff[MAX];
        for (int i = 0; i < MAX; i++) {
            arr[i] = new goalDiff();
            arr[i].frequency = 0;
            arr[i].team_name = "";
        }
 
        // Inserting league data in hash maps
        for (int i = 0; i < N; i++) {
            int gd = HG[i] - AG[i];
            if (gd >= 0) {
                arr[gd].frequency++;
                arr[gd].team_name = HT[i];
            }
        }
 
        // Stores gd with maximum frequency
        int max_index = 0;
 
        // Stores maximum frequency
        int max_freq = -1;
        for (int i = 0; i < MAX; i++) {
 
            // To find maximum frequency and
            // the corresponding gd
            if (max_freq <= arr[i].frequency) {
                max_freq = arr[i].frequency;
                max_index = i;
            }
        }
 
        // If no home team has won
        if (max_freq == -1) {
            System.out.println("-1");
        }
        else {
            System.out.println(max_index + " "
                               + arr[max_index].team_name);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4;
 
        // Store home teams
        String HT[]
            = { "mumbai", "bengaluru", "goa", "bengal" };
 
        // Store goals by home teams
        int HG[] = { 3, 1, 4, 3 };
 
        // Store arrival teams
        String AT[]
            = { "goa", "kerala", "hyderabad", "goa" };
 
        // Store goals by home teams
        int AG[] = { 2, 0, 5, 1 };
 
        // Function call
        solve(HT, HG, AT, AG);
    }
}


Python3




# Python3 code to implement the approach
 
# Maximum possible goal difference
MAX = 100
 
# class with frequency
# and recent team name
class goalDiff:
    def __init__(self,frequency,team_name):
        self.frequency = frequency
        self.team_name = team_name
     
# Function to find the team
# and the goal difference
def solve(HT, HG, AT, AG):
    global MAX
    N = 4
 
    # Array to store gd, its frequency and
    # the recent home team to win with that gd
    arr = [0 for i in range(MAX)]
    for i in range(MAX):
        arr[i] = goalDiff(0,"")
 
    # Inserting league data in hash maps
    for i in range(N):
        gd = HG[i] - AG[i]
        if (gd >= 0):
            arr[gd].frequency += 1
            arr[gd].team_name = HT[i]
 
    max_index = 0
    max_freq = -1
    for i in range(MAX):
 
        # To find maximum frequency and
        # the corresponding gd
        if (max_freq <= arr[i].frequency):
            max_freq = arr[i].frequency
            max_index = i
 
    # If no home team has won
    if (max_freq == 0):
        print("-1")
    else:
        print(f"{max_index} {arr[max_index].team_name}")
     
# Driver Code
N = 4
 
# Store home teams
HT = [ "mumbai", "bengaluru", "goa", "bengal" ]
 
# Store goals by home teams
HG = [ 3, 1, 4, 3 ]
 
# Store arrival teams
AT = [ "goa", "kerala", "hyderabad", "goa" ]
 
# store goals by home teams
AG = [ 2, 0, 5, 1 ]
 
# Function call
solve(HT, HG, AT, AG)
 
# This code is contributed by shinjanpatra


C#




// C# code to implement the approach
 
using System;
 
// class to store gd frequency and
// the recent home team
class goalDiff {
    public int frequency;
    public string team_name;
}
 
class GFG {
 
    // Function to find the team and
    // the goal difference
    static void solve(string[] HT, int[] HG, string[] AT,
                      int[] AG)
    {
        int MAX = 100;
        int N = 4;
 
        // Hash map to store gd, its frequency and
        // the recent home team to win with that gd
        goalDiff[] arr = new goalDiff[MAX];
        for (int i = 0; i < MAX; i++) {
            arr[i] = new goalDiff();
            arr[i].frequency = 0;
            arr[i].team_name = "";
        }
 
        // Inserting league data in hash maps
        for (int i = 0; i < N; i++) {
            int gd = HG[i] - AG[i];
            if (gd >= 0) {
                arr[gd].frequency++;
                arr[gd].team_name = HT[i];
            }
        }
 
        // Stores gd with maximum frequency
        int max_index = 0;
 
        // Stores maximum frequency
        int max_freq = -1;
        for (int i = 0; i < MAX; i++) {
 
            // To find maximum frequency and
            // the corresponding gd
            if (max_freq <= arr[i].frequency) {
                max_freq = arr[i].frequency;
                max_index = i;
            }
        }
 
        // If no home team has won
        if (max_freq == -1) {
            Console.WriteLine("-1");
        }
        else {
            Console.WriteLine(max_index + " "
                              + arr[max_index].team_name);
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
 
        // Store home teams
        string[] HT
            = { "mumbai", "bengaluru", "goa", "bengal" };
 
        // Store goals by home teams
        int[] HG = { 3, 1, 4, 3 };
 
        // Store arrival teams
        string[] AT
            = { "goa", "kerala", "hyderabad", "goa" };
 
        // Store goals by home teams
        int[] AG = { 2, 0, 5, 1 };
 
        // Function call
        solve(HT, HG, AT, AG);
    }
}
 
// This code is contributed by phasing17


Javascript




<script>
 
// JavaScript code to implement the approach
 
 
// Maximum possible goal difference
const MAX = 100
 
// class with frequency
// and recent team name
class goalDiff {
    constructor(frequency,team_name){
    this.frequency = frequency;
    this.team_name = team_name;
    }
}
 
// Function to find the team
// and the goal difference
function solve(HT, HG, AT, AG)
{
    let N = 4;
 
    // Array to store gd, its frequency and
    // the recent home team to win with that gd
    let arr = new Array(MAX);
    for (let i = 0; i < MAX; i++) {
        arr[i] = new goalDiff(0,"")
    }
 
    // Inserting league data in hash maps
    for (let i = 0; i < N; i++) {
        let gd = HG[i] - AG[i];
        if (gd >= 0) {
            arr[gd].frequency++;
            arr[gd].team_name = HT[i];
        }
    }
 
    let max_index = 0;
    let max_freq = -1;
    for (let i = 0; i < MAX; i++) {
 
        // To find maximum frequency and
        // the corresponding gd
        if (max_freq <= arr[i].frequency) {
            max_freq = arr[i].frequency;
            max_index = i;
        }
    }
 
    // If no home team has won
    if (max_freq == 0) {
        document.write("-1","</br>");
    }
    else {
        document.write(max_index," ",arr[max_index].team_name,"</br>");
    }
}
 
// Driver Code
 
let N = 4;
 
// Store home teams
let HT = [ "mumbai", "bengaluru", "goa", "bengal" ];
 
// Store goals by home teams
let HG = [ 3, 1, 4, 3 ];
 
// Store arrival teams
let AT = [ "goa", "kerala", "hyderabad", "goa" ];
 
// store goals by home teams
let AG = [ 2, 0, 5, 1 ];
 
// Function call
solve(HT, HG, AT, AG);
 
// This code is contributed by shinjanpatra
 
</script>


Output

1 bengaluru

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads