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 as {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 pair to {-1, 1}.
{2, 1}: Operation 2 modifies pair to {2, 3}.
{5, 10}: Operation 3 modifies pair to {5, 5}
{10, 9}: Operation 3 modifies pair to {10, 10}
{19, 1}: Operation 2 modifies 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 minimum for the first pair.
- Always choose Operation 2 for the last pair, since X is 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 <bits/stdc++.h> using namespace std; // Function to find maximum count of operations int find_max(vector<pair<int, int> > 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<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
// 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<pair> 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<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)); } } // 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 = 3v = [] 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<pair> 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<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)); } } // This code is contributed by 29AjayKumar |
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find if two rectangles overlap
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Count of operations required to update the array such that it satisfies the given conditions
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Count the number of operations required to reduce the given number
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Ways to form n/2 pairs such that difference of pairs is minimum
- Find the number of operations required to make all array elements Equal
- Count the number of carry operations required to add two numbers
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to delete all elements of the array
- Minimum number operations required to convert n to m | Set-2
- Minimum number of given operations required to convert a permutation into an identity permutation
- Find the minimum number of operations required to make all array elements equal
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations required to obtain a given Binary String
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum number of operations required to reduce N to 0
- Minimum number of operations required to maximize the Binary String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

