Open In App

# Maximum number of operations required such that no pairs from a Matrix overlap

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:
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++

 `// C++ Program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function to find maximum count of operations``int` `find_max(vector > v, ``int` `n)``{``    ``// Initialize count by 0``    ``int` `count = 0;` `    ``if` `(n >= 2)``        ``count = 2;` `    ``else``        ``count = 1;` `    ``// Iterate over remaining pairs``    ``for` `(``int` `i = 1; i < n - 1; i++) {` `        ``// Check if first operation``        ``// is applicable``        ``if` `(v[i - 1].first``            ``< (v[i].first - v[i].second))``            ``count++;` `        ``// Check if 2nd operation is applicable``        ``else` `if` `(v[i + 1].first``                 ``> (v[i].first + v[i].second)) {``            ``count++;``            ``v[i].first = v[i].first + v[i].second;``        ``}` `        ``// Otherwise``        ``else``            ``continue``;``    ``}` `    ``// Return  the count of operations``    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``int` `n = 3;``    ``vector > v;` `    ``v.push_back({ 10, 20 });``    ``v.push_back({ 15, 10 });``    ``v.push_back({ 20, 16 });` `    ``cout << find_max(v, n);` `    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;` `class` `GFG{``    ` `static` `class` `pair``{``    ``int` `first, second;``    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}``}` `// Function to find maximum count of operations``static` `int` `find_max(Vector v, ``int` `n)``{``    ``// Initialize count by 0``    ``int` `count = ``0``;` `    ``if` `(n >= ``2``)``        ``count = ``2``;``    ``else``        ``count = ``1``;` `    ``// Iterate over remaining pairs``    ``for``(``int` `i = ``1``; i < n - ``1``; i++)``    ``{``        ` `        ``// Check if first operation``        ``// is applicable``        ``if` `(v.get(i - ``1``).first <``           ``(v.get(i).first - v.get(i).second))``            ``count++;` `        ``// Check if 2nd operation is applicable``        ``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;``        ``}` `        ``// Otherwise``        ``else``            ``continue``;``    ``}` `    ``// Return the count of operations``    ``return` `count;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``3``;``    ``Vector 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));``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to find maximum count of``# operations``def` `find_max(v, n):` `    ``# Initialize count by 0``    ``count ``=` `0` `    ``if``(n >``=` `2``):``        ``count ``=` `2``    ``else``:``        ``count ``=` `1` `    ``# Iterate over remaining pairs``    ``for` `i ``in` `range``(``1``, n ``-` `1``):` `        ``# Check if first operation``        ``# is applicable``        ``if``(v[i ``-` `1``][``0``] > (v[i][``0``] ``+``                          ``v[i][``1``])):``            ``count ``+``=` `1` `        ``# Check if 2nd operation is applicable``        ``elif``(v[i ``+` `1``][``0``] > (v[i][``0``] ``+``                            ``v[i][``1``])):``            ``count ``+``=` `1``            ``v[i][``0``] ``=` `v[i][``0``] ``+` `v[i][``1``]` `        ``# Otherwise``        ``else``:``            ``continue` `    ``# Return the count of operations``    ``return` `count` `# Driver Code``n ``=` `3``v ``=` `[]` `v.append([ ``10``, ``20` `])``v.append([ ``15``, ``10` `])``v.append([ ``20``, ``16` `])` `print``(find_max(v, n))` `# This code is contributed by Shivam Singh`

## C#

 `// C# program to implement``// the above approach``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;``    ``}``}` `// Function to find maximum count of operations``static` `int` `find_max(List v, ``int` `n)``{``    ` `    ``// Initialize count by 0``    ``int` `count = 0;` `    ``if` `(n >= 2)``        ``count = 2;``    ``else``        ``count = 1;` `    ``// Iterate over remaining pairs``    ``for``(``int` `i = 1; i < n - 1; i++)``    ``{``        ` `        ``// Check if first operation``        ``// is applicable``        ``if` `(v[i - 1].first <``           ``(v[i].first - v[i].second))``            ``count++;` `        ``// Check if 2nd operation is applicable``        ``else` `if` `(v[i + 1].first >``                ``(v[i].first + v[i].second))``        ``{``            ``count++;``            ``v[i].first = v[i].first +``                         ``v[i].second;``        ``}` `        ``// Otherwise``        ``else``            ``continue``;``    ``}` `    ``// Return the count of operations``    ``return` `count;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `n = 3;``    ``List v = ``new` `List();` `    ``v.Add(``new` `pair(10, 20));``    ``v.Add(``new` `pair(15, 10));``    ``v.Add(``new` `pair(20, 16));` `    ``Console.Write(find_max(v, n));``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`2`

Time Complexity: O(N)
Auxiliary Space: O(1)