Given a permutation P of first N natural numbers, the task is to count the index pairs (i, j) such that P[i] + P[j] = max(P[x]) where i ≤ x ≤ j.
Examples:
Input: P[] = {3, 4, 1, 5, 2}
Output: 2
Only valid index pairs are (0, 4) and (0, 2)Input: P[] = {1, 3, 2}
Output: 1
Naive approach: We can solve this problem by iterating for all possible pairs (i, j) and each time get maximum between them. The time complexity of this approach will be O(n3).
Efficient Approach: Fix the maximum element on a segment and iterate on either the elements to the left of it or to the right of it. If the current maximum is x, and element we found is y then check whether element x-y can form a subsegment with y (i.e. x is maximum value on segment
between y and x-y). This works in O(n*n)
But if we can precompute the borders of the segments where x is the maximum element and always choose to iterate on the smaller part of the segment then time complexity will reduce to O(nlogn).
Because every element will be processed no more than logn times, if we process it in a segment of size m, the smaller part of it contains no more than m/2 elements ( which we will process later, and the smaller part of this segment contains no more than m/4 elements, and so on..).
Below is the implementation of the above approach:
C++
// CPP implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return the count of // required index pairs int Count_Segment(int p[], int n) { // To store the required count int count = 0; // Array to store the left elements // upto which current element is maximum int upto[n + 1]; for(int i = 0; i < n + 1; i++) upto[i] = 0; // Iterating through the whole permutation // except first and last element int j = 0,curr = 0; for (int i = 1; i < n + 1; i++) { // If current element can be maximum // in a subsegment if (p[i] > p[i - 1] and p[i] > p[i + 1]) { // Current maximum curr = p[i]; // Iterating for smaller values then // current maximum on left of it j = i - 1; while (j >= 0 and p[j] < curr) { // Storing left borders // of the current maximum upto[p[j]]= curr; j -= 1; } // Iterating for smaller values then // current maximum on right of it j = i + 1; while (j < n and p[j] < curr) { // Condition satisfies if (upto[curr-p[j]] == curr) count += 1; j+= 1; } } } // Return count of subsegments return count; } // Driver Code int main() { int p[] = {3, 4, 1, 5, 2}; int n = sizeof(p)/sizeof(p[0]); cout << (Count_Segment(p, n)); return 0; } // This code is contributed by // Surendra_Gangwar |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the count of // required index pairs static int Count_Segment(int p[], int n) { // To store the required count int count = 0; // Array to store the left elements // upto which current element is maximum int []upto = new int[n + 1]; for(int i = 0; i < n + 1; i++) upto[i] = 0; // Iterating through the whole permutation // except first and last element int j = 0,curr = 0; for (int i = 1; i < n ; i++) { // If current element can be maximum // in a subsegment if (p[i] > p[i - 1] && p[i] > p[i + 1]) { // Current maximum curr = p[i]; // Iterating for smaller values then // current maximum on left of it j = i - 1; while (j >= 0 && p[j] < curr) { // Storing left borders // of the current maximum upto[p[j]]= curr; j -= 1; } // Iterating for smaller values then // current maximum on right of it j = i + 1; while (j < n && p[j] < curr) { // Condition satisfies if (upto[curr-p[j]] == curr) count += 1; j+= 1; } } } // Return count of subsegments return count; } // Driver Code public static void main(String[] args) { int p[] = {3, 4, 1, 5, 2}; int n = p.length; System.out.println(Count_Segment(p, n)); } } /* This code contributed by PrinciRaj1992 */ |
Python
# Python3 implementation of the approach # Function to return the count of # required index pairs def Count_Segment(p, n): # To store the required count count = 0 # Array to store the left elements # upto which current element is maximum upto = [False]*(n + 1) # Iterating through the whole permutation # except first and last element for i in range(1, n-1): # If current element can be maximum # in a subsegment if p[i]>p[i-1] and p[i]>p[i + 1]: # Current maximum curr = p[i] # Iterating for smaller values then # current maximum on left of it j = i-1 while j>= 0 and p[j]<curr: # Storing left borders # of the current maximum upto[p[j]]= curr j-= 1 # Iterating for smaller values then # current maximum on right of it j = i + 1 while j<n and p[j]<curr: # Condition satisfies if upto[curr-p[j]]== curr: count+= 1 j+= 1 # Return count of subsegments return count # Driver Code if __name__=="__main__": p =[3, 4, 1, 5, 2] n = len(p) print(Count_Segment(p, n)) |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count of // required index pairs static int Count_Segment(int []p, int n) { // To store the required count int count = 0; // Array to store the left elements // upto which current element is maximum int []upto = new int[n + 1]; for(int i = 0; i < n + 1; i++) upto[i] = 0; // Iterating through the whole permutation // except first and last element int j = 0,curr = 0; for (int i = 1; i < n ; i++) { // If current element can be maximum // in a subsegment if (p[i] > p[i - 1] && p[i] > p[i + 1]) { // Current maximum curr = p[i]; // Iterating for smaller values then // current maximum on left of it j = i - 1; while (j >= 0 && p[j] < curr) { // Storing left borders // of the current maximum upto[p[j]]= curr; j= j - 1; } // Iterating for smaller values then // current maximum on right of it j = i + 1; while (j < n && p[j] < curr) { // Condition satisfies if (upto[curr-p[j]] == curr) count += 1; j= j+ 1; } } } // Return count of subsegments return count; } // Driver Code static public void Main () { int []p = {3, 4, 1, 5, 2}; int n = p.Length; Console.WriteLine(Count_Segment(p, n)); } } /* This code contributed by ajit*/ |
2
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:
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Count of sub-sequences which satisfy the given condition
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Pairs from an array that satisfy the given condition
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Count of Fibonacci pairs which satisfy the given equation
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Count sub-sets that satisfy the given condition
- Count all possible N digit numbers that satisfy the given condition
- Count of indices in an array that satisfy the given condition
- Append two elements to make the array satisfy the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
- Count of N digit numbers possible which satisfy the given conditions
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Smallest index in the given array that satisfies the given condition
- Count of pairs satisfying the given condition
- Pair of integers (a, b) which satisfy the given equations
- Count of triplets that satisfy the given equation
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.

