Find goal difference for maximum wins and recent team with that goal difference
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 GDvoid 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 Codeint 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 approachimport 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 GDdef 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 Codeif __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 approachusing System;// class to store gd frequency and// the recent home teamclass 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 GDfunction 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 Codevar N = 4;// Store home teamsvar HT = [ "mumbai", "bengaluru", "goa", "bengal" ];// Store goals by home teamsvar HG = [ 3, 1, 4, 3 ];// Store arrival teamsvar AT = [ "goa", "kerala", "hyderabad", "goa" ];// Store goals by home teamsvar AG = [ 2, 0, 5, 1 ];// Function callsolve(HT, HG, AT, AG); // This code is contributed by phasing17 |
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 namestruct goalDiff { int frequency; string team_name;};// Function to find the team// and the goal differencevoid 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 Codeint 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 approachimport java.io.*;import java.util.*;// class to store gd frequency and// the recent home teamclass 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 differenceMAX = 100# class with frequency# and recent team nameclass goalDiff: def __init__(self,frequency,team_name): self.frequency = frequency self.team_name = team_name # Function to find the team# and the goal differencedef 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 CodeN = 4# Store home teamsHT = [ "mumbai", "bengaluru", "goa", "bengal" ]# Store goals by home teamsHG = [ 3, 1, 4, 3 ]# Store arrival teamsAT = [ "goa", "kerala", "hyderabad", "goa" ]# store goals by home teamsAG = [ 2, 0, 5, 1 ]# Function callsolve(HT, HG, AT, AG)# This code is contributed by shinjanpatra |
C#
// C# code to implement the approachusing System;// class to store gd frequency and// the recent home teamclass 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 differenceconst MAX = 100// class with frequency// and recent team nameclass goalDiff { constructor(frequency,team_name){ this.frequency = frequency; this.team_name = team_name; }}// Function to find the team// and the goal differencefunction 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 Codelet N = 4;// Store home teamslet HT = [ "mumbai", "bengaluru", "goa", "bengal" ];// Store goals by home teamslet HG = [ 3, 1, 4, 3 ];// Store arrival teamslet AT = [ "goa", "kerala", "hyderabad", "goa" ];// store goals by home teamslet AG = [ 2, 0, 5, 1 ];// Function callsolve(HT, HG, AT, AG);// This code is contributed by shinjanpatra</script> |
1 bengaluru
Time Complexity: O(N)
Auxiliary Space: O(N)


Please Login to comment...