Given an integer N followed by a matrix V[][] consisting of pairs {X, Y} in ascending order of X, the task is to for each pair given in ascending order of X, the following operations can be performed:
- Convert the pair {X, Y} to {X – Y, X}
- Convert the pair {X, Y} to {X, X+Y}
- Change the pair to {X, X}
The task is to find the count of addition and subtraction operations required such that no two pairs overlap.
Examples:
Input: N = 5, V[] = {{1, 2} {2, 1} {5, 10} {10, 9} {19, 1}}
Output: 3
Explanation:
{1, 2}: Operation 1 modifies the pair to {-1, 1}.
{2, 1}: Operation 2 modifies the pair to {2, 3}.
{5, 10}: Operation 3 modifies the pair to {5, 5}
{10, 9}: Operation 3 modifies the pair to {10, 10}
{19, 1}: Operation 2 modifies the pair to {19, 20}.
Therefore, none of the pairs overlap. Hence, the count of addition and subtraction operations required is 3.
Input: N = 3, V[][] = {{10, 20} {15, 10} {20, 16}}
Output: 2
Approach:
The main idea is to observe that the answer, in any case, will not exceed N, since any of the three operations cannot be applied twice on a pair. Follow the steps below to solve the problem:
- Always choose Operation 1 for the first pair, since X is the minimum for the first pair.
- Always choose Operation 2 for the last pair, since X is the maximum for the last pair.
- For the remaining pairs, check if applying Operation 1 violates rules or not. If it does not violate the rules, then it will always maximize the result. Otherwise, check for Operation 2. Increase count if any of the two operations is applicable.
- If both the rules are not applicable, perform operation 3.
- Finally, print count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_max(vector<pair< int , int > > v, int n)
{
int count = 0;
if (n >= 2)
count = 2;
else
count = 1;
for ( int i = 1; i < n - 1; i++) {
if (v[i - 1].first
< (v[i].first - v[i].second))
count++;
else if (v[i + 1].first
> (v[i].first + v[i].second)) {
count++;
v[i].first = v[i].first + v[i].second;
}
else
continue ;
}
return count;
}
int main()
{
int n = 3;
vector<pair< int , int > > v;
v.push_back({ 10, 20 });
v.push_back({ 15, 10 });
v.push_back({ 20, 16 });
cout << find_max(v, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int find_max(Vector<pair> v, int n)
{
int count = 0 ;
if (n >= 2 )
count = 2 ;
else
count = 1 ;
for ( int i = 1 ; i < n - 1 ; i++)
{
if (v.get(i - 1 ).first <
(v.get(i).first - v.get(i).second))
count++;
else if (v.get(i + 1 ).first >
(v.get(i).first + v.get(i).second))
{
count++;
v.get(i).first = v.get(i).first +
v.get(i).second;
}
else
continue ;
}
return count;
}
public static void main(String[] args)
{
int n = 3 ;
Vector<pair> v = new Vector<>();
v.add( new pair( 10 , 20 ));
v.add( new pair( 15 , 10 ));
v.add( new pair( 20 , 16 ));
System.out.print(find_max(v, n));
}
}
|
Python3
def find_max(v, n):
count = 0
if (n > = 2 ):
count = 2
else :
count = 1
for i in range ( 1 , n - 1 ):
if (v[i - 1 ][ 0 ] > (v[i][ 0 ] +
v[i][ 1 ])):
count + = 1
elif (v[i + 1 ][ 0 ] > (v[i][ 0 ] +
v[i][ 1 ])):
count + = 1
v[i][ 0 ] = v[i][ 0 ] + v[i][ 1 ]
else :
continue
return count
n = 3
v = []
v.append([ 10 , 20 ])
v.append([ 15 , 10 ])
v.append([ 20 , 16 ])
print (find_max(v, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int find_max(List<pair> v, int n)
{
int count = 0;
if (n >= 2)
count = 2;
else
count = 1;
for ( int i = 1; i < n - 1; i++)
{
if (v[i - 1].first <
(v[i].first - v[i].second))
count++;
else if (v[i + 1].first >
(v[i].first + v[i].second))
{
count++;
v[i].first = v[i].first +
v[i].second;
}
else
continue ;
}
return count;
}
public static void Main(String[] args)
{
int n = 3;
List<pair> v = new List<pair>();
v.Add( new pair(10, 20));
v.Add( new pair(15, 10));
v.Add( new pair(20, 16));
Console.Write(find_max(v, n));
}
}
|
Javascript
<script>
function find_max(v, n)
{
let count = 0;
if (n >= 2)
count = 2;
else
count = 1;
for (let i = 1; i < n - 1; i++) {
if (v[i - 1][0]
< (v[i][0] - v[i][1]))
count++;
else if (v[i + 1][0]
> (v[i][0] + v[i][1])) {
count++;
v[i][0] = v[i][0] + v[i][1];
}
else
continue ;
}
return count;
}
let n = 3;
let v = [ [10, 20], [15, 10], [20, 16] ];
document.write(find_max(v, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)