Count maximum possible number of draws in a match between two people
Given an array of pairs v[][] of size N which shows the score of the match at N instances where (v[i][0], v[i][1]) shows a score equivalent to (p, q) of 2 players at that moment. After a goal is made score would change to (p+1: q) or (p: q+1). The task is to find the maximum number of times a draw can occur during the whole match.
Note: The last score denotes the final result of the match. The score is provided in increasing order as time increases.
Examples:
Input: N = 3, v[][] = {{2, 0}, {3, 1}, {3, 4}}
Output: 2
Explanation: Possible scores in the match would be
0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4
Now, 0:0 and 3:3 are two scores leading to draw.
Thus, answer = 2.
Input: N = 1, v[][] = {{5, 4}}
Output: 5
Explanation: Possible scores in the match would be
0:0, 0:1, 1:1, 1:2, 2:2, 2:3, 3:3, 3:4, 4:4, 5:4
Now, 0:0, 1:1, 2:2, 3:3 and 4:4 are five scores leading to draw.
Thus, answer = 5.
Approach: Assuming a score (p, q) and (r, s). Put in between pairs (x, x) as much as possible. This x needs to be in such a way that :
p ≤ x ≤ r and q ≤ x ≤ s, thus max(p, q) ≤ x ≤ min(r, s). So, just count number of such x. Follow the steps below to solve the problem:
- Initialize pairs p1[] and p2[].
- Initialize the variables draws as 1 and diff as 0.
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- Set p2 as v[i].
- If p1.first is the same as p1.second then decrease the value of draws by 1.
- Set diff as min(p2.first, p2.second) – max(p1.first, p1.second) + 1.
- If diff is greater than 0 then increase the value of draws by diff.
- Set the value of p1 as p2.
- After performing the above steps, print the value of draws as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int & N,
vector<pair< int , int > > v)
{
pair< int , int > p1, p2;
int draws = 1, diff = 0;
p1 = make_pair(0, 0);
for ( int i = 0; i < N; i++) {
p2 = v[i];
if (p1.first == p1.second) {
draws--;
}
diff = min(p2.first, p2.second)
- max(p1.first, p1.second)
+ 1;
if (diff > 0) {
draws += diff;
}
p1 = p2;
}
cout << draws;
}
int main()
{
int N = 1;
vector<pair< int , int > > v = { { 5, 4 } };
solve(N, v);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Pair {
int x;
int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
class GFG {
static void solve( int N, Pair v[])
{
Pair p1 = new Pair( 0 , 0 );
int draws = 1 , diff = 0 ;
p1.x = 0 ;
p1.y = 0 ;
for ( int i = 0 ; i < N; i++) {
Pair p2 = new Pair(v[i].x, v[i].y);
if (p1.x == p1.y) {
draws--;
}
diff = Math.min(p2.x, p2.y)
- Math.max(p1.x, p1.y)
+ 1 ;
if (diff > 0 ) {
draws += diff;
}
p1 = p2;
}
System.out.print(draws);
}
public static void main (String[] args) {
int n = 1 ;
Pair arr[] = new Pair[n];
arr[ 0 ] = new Pair( 5 , 4 );
solve(n, arr);
}
}
|
Python3
def solve(N, v):
draws = 1
diff = 0 ;
p1 = { "first" : 0 , "second" : 0 };
for i in range (N):
p2 = v[i];
if (p1[ "first" ] = = p1[ "second" ]):
draws - = 1
diff = min (p2[ "first" ], p2[ "second" ]) - max (p1[ "first" ], p1[ "second" ]) + 1 ;
if (diff > 0 ):
draws + = diff;
p1 = p2;
print (draws);
N = 1 ;
v = [{ "first" : 5 , "second" : 4 }];
solve(N, v);
|
C#
using System;
class Pair {
public int x;
public int y;
public Pair( int x, int y)
{
this .x = x;
this .y = y;
}
}
public class GFG {
static void solve( int N, Pair []v)
{
Pair p1 = new Pair(0,0);
int draws = 1, diff = 0;
p1.x = 0;
p1.y = 0;
for ( int i = 0; i < N; i++) {
Pair p2 = new Pair(v[i].x, v[i].y);
if (p1.x == p1.y) {
draws--;
}
diff = Math.Min(p2.x, p2.y)
- Math.Max(p1.x, p1.y)
+ 1;
if (diff > 0) {
draws += diff;
}
p1 = p2;
}
Console.Write(draws);
}
public static void Main(String[] args) {
int n = 1;
Pair []arr = new Pair[n];
arr[0] = new Pair(5, 4);
solve(n, arr);
}
}
|
Javascript
<script>
function solve(N,
v) {
let p1, p2;
let draws = 1, diff = 0;
p1 = { first: 0, second: 0 };
for (let i = 0; i < N; i++) {
p2 = v[i];
if (p1.first == p1.second) {
draws--;
}
diff = Math.min(p2.first, p2.second)
- Math.max(p1.first, p1.second)
+ 1;
if (diff > 0) {
draws += diff;
}
p1 = p2;
}
document.write(draws);
}
let N = 1;
let v = [{ first: 5, second: 4 }];
solve(N, v);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...