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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Utility Function void solve( int & N, vector<pair< int , int > > v) { // Initializing pair for input and // one for starting position {0, 0} 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]; // Reduce if found a draw situation if (p1.first == p1.second) { draws--; } // Calculation for possible // values for x would be // min(r, s) - max(p, q) + 1 diff = min(p2.first, p2.second) - max(p1.first, p1.second) + 1; // If possible value found, // add to our answer if (diff > 0) { draws += diff; } // In end, update previous pair // to new pair p1 = p2; } cout << draws; } // Driver Code int main() { int N = 1; vector<pair< int , int > > v = { { 5, 4 } }; solve(N, v); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; // User defined Pair class class Pair { int x; int y; // Constructor public Pair( int x, int y) { this .x = x; this .y = y; } } class GFG { // Utility Function static void solve( int N, Pair v[]) { // Initializing pair for input and // one for starting position {0, 0} 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); // Reduce if found a draw situation if (p1.x == p1.y) { draws--; } // Calculation for possible // values for x would be // min(r, s) - max(p, q) + 1 diff = Math.min(p2.x, p2.y) - Math.max(p1.x, p1.y) + 1 ; // If possible value found, // add to our answer if (diff > 0 ) { draws += diff; } // In end, update previous pair // to new pair p1 = p2; } System.out.print(draws); } // Driver Code public static void main (String[] args) { int n = 1 ; Pair arr[] = new Pair[n]; arr[ 0 ] = new Pair( 5 , 4 ); solve(n, arr); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python code for the above approach # Utility Function def solve(N, v): # Initializing pair for input and # one for starting position {0, 0} draws = 1 diff = 0 ; p1 = { "first" : 0 , "second" : 0 }; for i in range (N): p2 = v[i]; # Reduce if found a draw situation if (p1[ "first" ] = = p1[ "second" ]): draws - = 1 # Calculation for possible # values for x would be # min(r, s) - max(p, q) + 1 diff = min (p2[ "first" ], p2[ "second" ]) - max (p1[ "first" ], p1[ "second" ]) + 1 ; # If possible value found, # add to our answer if (diff > 0 ): draws + = diff; # In end, update previous pair # to new pair p1 = p2; print (draws); # Driver Code N = 1 ; v = [{ "first" : 5 , "second" : 4 }]; solve(N, v); # This code is contributed by Saurabh Jaiswal |
C#
// C# program for the above approach using System; // User defined Pair class class Pair { public int x; public int y; // Constructor public Pair( int x, int y) { this .x = x; this .y = y; } } public class GFG { // Utility Function static void solve( int N, Pair []v) { // Initializing pair for input and // one for starting position {0, 0} 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); // Reduce if found a draw situation if (p1.x == p1.y) { draws--; } // Calculation for possible // values for x would be // min(r, s) - max(p, q) + 1 diff = Math.Min(p2.x, p2.y) - Math.Max(p1.x, p1.y) + 1; // If possible value found, // add to our answer if (diff > 0) { draws += diff; } // In end, update previous pair // to new pair p1 = p2; } Console.Write(draws); } // Driver Code public static void Main(String[] args) { int n = 1; Pair []arr = new Pair[n]; arr[0] = new Pair(5, 4); solve(n, arr); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // JavaScript code for the above approach // Utility Function function solve(N, v) { // Initializing pair for input and // one for starting position {0, 0} let p1, p2; let draws = 1, diff = 0; p1 = { first: 0, second: 0 }; for (let i = 0; i < N; i++) { p2 = v[i]; // Reduce if found a draw situation if (p1.first == p1.second) { draws--; } // Calculation for possible // values for x would be // min(r, s) - max(p, q) + 1 diff = Math.min(p2.first, p2.second) - Math.max(p1.first, p1.second) + 1; // If possible value found, // add to our answer if (diff > 0) { draws += diff; } // In end, update previous pair // to new pair p1 = p2; } document.write(draws); } // Driver Code let N = 1; let v = [{ first: 5, second: 4 }]; solve(N, v); // This code is contributed by Potta Lokesh </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...