Find goal difference for maximum wins and recent team with that goal difference
Last Updated :
30 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void solve(vector<string>& HT, vector< int >& HG,
vector<string>& AT, vector< int >& AG)
{
int N = HG.size();
unordered_map< int , int > gd_freq;
unordered_map< int , string> gd_team;
for ( int i = 0; i < N; i++) {
int gd = HG[i] - AG[i];
if (gd >= 0) {
gd_freq[gd]++;
gd_team[gd] = HT[i];
}
}
int max_index = 0;
int max_freq = -1;
for ( auto itr = gd_freq.begin();
itr != gd_freq.end();
itr++) {
if (max_freq <= itr->second) {
max_freq = itr->second;
max_index = itr->first;
}
}
if (max_freq == -1) {
cout << "-1" ;
}
else {
cout << max_index << " " << gd_team[max_index];
}
}
int main()
{
int N = 4;
vector<string> HT
= { "mumbai" , "bengaluru" , "goa" , "bengal" };
vector< int > HG = { 3, 1, 4, 3 };
vector<string> AT
= { "goa" , "kerala" , "hyderabad" , "goa" };
vector< int > AG = { 2, 0, 5, 1 };
solve(HT, HG, AT, AG);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void solve(String HT[], int HG[],
String AT[], int AG[])
{
int N = 4 ;
Map<Integer, Integer> gd_freq
= new HashMap<Integer, Integer>();
Map<Integer, String> gd_team
= new HashMap<Integer, String>();
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]);
}
}
int max_index = 0 ;
int max_freq = - 1 ;
for (Map.Entry<Integer, Integer> entry :
gd_freq.entrySet()) {
if (max_freq <= entry.getValue()) {
max_freq = entry.getValue();
max_index = entry.getKey();
}
}
if (max_freq == - 1 ) {
System.out.println( "-1" );
}
else {
System.out.println(max_index + " "
+ gd_team.get(max_index));
}
}
public static void main(String[] args)
{
int N = 4 ;
String HT[]
= { "mumbai" , "bengaluru" , "goa" , "bengal" };
int HG[] = { 3 , 1 , 4 , 3 };
String AT[]
= { "goa" , "kerala" , "hyderabad" , "goa" };
int AG[] = { 2 , 0 , 5 , 1 };
solve(HT, HG, AT, AG);
}
}
|
Python3
def solve(HT, HG, AT, AG) :
N = len (HG);
gd_freq = {};
gd_team = {};
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];
max_index = 0 ;
max_freq = - 1 ;
for itr in gd_freq :
if (max_freq < = gd_freq[itr]) :
max_freq = gd_freq[itr];
max_index = itr;
if (max_freq = = - 1 ) :
print ( "-1" );
else :
print (max_index, " " ,gd_team[max_index]);
if __name__ = = "__main__" :
N = 4 ;
HT = [ "mumbai" , "bengaluru" , "goa" , "bengal" ];
HG = [ 3 , 1 , 4 , 3 ];
AT = [ "goa" , "kerala" , "hyderabad" , "goa" ];
AG = [ 2 , 0 , 5 , 1 ];
solve(HT, HG, AT, AG);
|
C#
using System;
class GFG
{
public class goalDiff
{
public int frequency;
public String team_name;
}
static void solve(String[] HT, int [] HG,
String[] AT, int [] AG)
{
int MAX = 100;
int N = 4;
goalDiff[] arr = new goalDiff[MAX];
for ( int i = 0; i < MAX; i++)
{
arr[i] = new goalDiff();
arr[i].frequency = 0;
arr[i].team_name = "" ;
}
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++)
{
if (max_freq <= arr[i].frequency)
{
max_freq = arr[i].frequency;
max_index = i;
}
}
if (max_freq == -1)
{
Console.WriteLine( "-1" );
}
else
{
Console.WriteLine(max_index + " "
+ arr[max_index].team_name);
}
}
public static void Main()
{
int N = 4;
String[] HT = { "mumbai" , "bengaluru" , "goa" , "bengal" };
int [] HG = { 3, 1, 4, 3 };
String[] AT = { "goa" , "kerala" , "hyderabad" , "goa" };
int [] AG = { 2, 0, 5, 1 };
solve(HT, HG, AT, AG);
}
}
|
Javascript
function solve(HT, HG, AT, AG)
{
var N = HG.length;
var gd_freq = {};
var gd_team = {};
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];
}
}
var max_index = 0;
var max_freq = -1;
for (const itr in gd_freq)
{
if (max_freq <= gd_freq[itr])
{
max_freq = gd_freq[itr];
max_index = itr;
}
}
if (max_freq == -1)
console.log( "-1" );
else
console.log(max_index, gd_team[max_index]);
}
var N = 4;
var HT = [ "mumbai" , "bengaluru" , "goa" , "bengal" ];
var HG = [ 3, 1, 4, 3 ];
var AT = [ "goa" , "kerala" , "hyderabad" , "goa" ];
var AG = [ 2, 0, 5, 1 ];
solve(HT, HG, AT, AG);
|
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
struct goalDiff {
int frequency;
string team_name;
};
void solve(vector<string>& HT, vector< int >& HG,
vector<string>& AT, vector< int >& AG)
{
int N = 4;
goalDiff arr[MAX];
for ( int i = 0; i < MAX; i++) {
arr[i].frequency = 0;
arr[i].team_name = "" ;
}
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++) {
if (max_freq <= arr[i].frequency) {
max_freq = arr[i].frequency;
max_index = i;
}
}
if (max_freq == 0) {
cout << "-1" ;
}
else {
cout << max_index << " "
<< arr[max_index].team_name;
}
}
int main()
{
int N = 4;
vector<string> HT
= { "mumbai" , "bengaluru" , "goa" , "bengal" };
vector< int > HG = { 3, 1, 4, 3 };
vector<string> AT
= { "goa" , "kerala" , "hyderabad" , "goa" };
vector< int > AG = { 2, 0, 5, 1 };
solve(HT, HG, AT, AG);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class goalDiff {
int frequency;
String team_name;
}
class Main {
static void solve(String HT[], int HG[],
String AT[], int AG[])
{
int MAX = 100 ;
int N = 4 ;
goalDiff arr[] = new goalDiff[MAX];
for ( int i = 0 ; i < MAX; i++) {
arr[i] = new goalDiff();
arr[i].frequency = 0 ;
arr[i].team_name = "" ;
}
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++) {
if (max_freq <= arr[i].frequency) {
max_freq = arr[i].frequency;
max_index = i;
}
}
if (max_freq == - 1 ) {
System.out.println( "-1" );
}
else {
System.out.println(max_index + " "
+ arr[max_index].team_name);
}
}
public static void main(String[] args)
{
int N = 4 ;
String HT[]
= { "mumbai" , "bengaluru" , "goa" , "bengal" };
int HG[] = { 3 , 1 , 4 , 3 };
String AT[]
= { "goa" , "kerala" , "hyderabad" , "goa" };
int AG[] = { 2 , 0 , 5 , 1 };
solve(HT, HG, AT, AG);
}
}
|
Python3
MAX = 100
class goalDiff:
def __init__( self ,frequency,team_name):
self .frequency = frequency
self .team_name = team_name
def solve(HT, HG, AT, AG):
global MAX
N = 4
arr = [ 0 for i in range ( MAX )]
for i in range ( MAX ):
arr[i] = goalDiff( 0 ,"")
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 ):
if (max_freq < = arr[i].frequency):
max_freq = arr[i].frequency
max_index = i
if (max_freq = = 0 ):
print ( "-1" )
else :
print (f "{max_index} {arr[max_index].team_name}" )
N = 4
HT = [ "mumbai" , "bengaluru" , "goa" , "bengal" ]
HG = [ 3 , 1 , 4 , 3 ]
AT = [ "goa" , "kerala" , "hyderabad" , "goa" ]
AG = [ 2 , 0 , 5 , 1 ]
solve(HT, HG, AT, AG)
|
C#
using System;
class goalDiff {
public int frequency;
public string team_name;
}
class GFG {
static void solve( string [] HT, int [] HG, string [] AT,
int [] AG)
{
int MAX = 100;
int N = 4;
goalDiff[] arr = new goalDiff[MAX];
for ( int i = 0; i < MAX; i++) {
arr[i] = new goalDiff();
arr[i].frequency = 0;
arr[i].team_name = "" ;
}
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++) {
if (max_freq <= arr[i].frequency) {
max_freq = arr[i].frequency;
max_index = i;
}
}
if (max_freq == -1) {
Console.WriteLine( "-1" );
}
else {
Console.WriteLine(max_index + " "
+ arr[max_index].team_name);
}
}
public static void Main( string [] args)
{
string [] HT
= { "mumbai" , "bengaluru" , "goa" , "bengal" };
int [] HG = { 3, 1, 4, 3 };
string [] AT
= { "goa" , "kerala" , "hyderabad" , "goa" };
int [] AG = { 2, 0, 5, 1 };
solve(HT, HG, AT, AG);
}
}
|
Javascript
<script>
const MAX = 100
class goalDiff {
constructor(frequency,team_name){
this .frequency = frequency;
this .team_name = team_name;
}
}
function solve(HT, HG, AT, AG)
{
let N = 4;
let arr = new Array(MAX);
for (let i = 0; i < MAX; i++) {
arr[i] = new goalDiff(0, "" )
}
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++) {
if (max_freq <= arr[i].frequency) {
max_freq = arr[i].frequency;
max_index = i;
}
}
if (max_freq == 0) {
document.write( "-1" , "</br>" );
}
else {
document.write(max_index, " " ,arr[max_index].team_name, "</br>" );
}
}
let N = 4;
let HT = [ "mumbai" , "bengaluru" , "goa" , "bengal" ];
let HG = [ 3, 1, 4, 3 ];
let AT = [ "goa" , "kerala" , "hyderabad" , "goa" ];
let AG = [ 2, 0, 5, 1 ];
solve(HT, HG, AT, AG);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...