Count index pairs which satisfy the given condition
Last Updated :
24 May, 2022
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 gets 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 the element we found is y then check whether element x-y can form a subsegment with y (i.e. x is the maximum value on the 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++
#include<bits/stdc++.h>
using namespace std;
int Count_Segment( int p[], int n)
{
int count = 0;
int upto[n + 1];
for ( int i = 0; i < n + 1; i++)
upto[i] = 0;
int j = 0,curr = 0;
for ( int i = 1; i < n + 1; i++)
{
if (p[i] > p[i - 1] and p[i] > p[i + 1])
{
curr = p[i];
j = i - 1;
while (j >= 0 and p[j] < curr)
{
upto[p[j]]= curr;
j -= 1;
}
j = i + 1;
while (j < n and p[j] < curr)
{
if (upto[curr-p[j]] == curr)
count += 1;
j+= 1;
}
}
}
return count;
}
int main()
{
int p[] = {3, 4, 1, 5, 2};
int n = sizeof (p)/ sizeof (p[0]);
cout << (Count_Segment(p, n));
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int Count_Segment( int p[], int n)
{
int count = 0 ;
int []upto = new int [n + 1 ];
for ( int i = 0 ; i < n + 1 ; i++)
upto[i] = 0 ;
int j = 0 ,curr = 0 ;
for ( int i = 1 ; i < n ; i++)
{
if (p[i] > p[i - 1 ] && p[i] > p[i + 1 ])
{
curr = p[i];
j = i - 1 ;
while (j >= 0 && p[j] < curr)
{
upto[p[j]]= curr;
j -= 1 ;
}
j = i + 1 ;
while (j < n && p[j] < curr)
{
if (upto[curr-p[j]] == curr)
count += 1 ;
j+= 1 ;
}
}
}
return count;
}
public static void main(String[] args)
{
int p[] = { 3 , 4 , 1 , 5 , 2 };
int n = p.length;
System.out.println(Count_Segment(p, n));
}
}
|
Python
def Count_Segment(p, n):
count = 0
upto = [ False ] * (n + 1 )
for i in range ( 1 , n - 1 ):
if p[i]>p[i - 1 ] and p[i]>p[i + 1 ]:
curr = p[i]
j = i - 1
while j> = 0 and p[j]<curr:
upto[p[j]] = curr
j - = 1
j = i + 1
while j<n and p[j]<curr:
if upto[curr - p[j]] = = curr:
count + = 1
j + = 1
return count
if __name__ = = "__main__" :
p = [ 3 , 4 , 1 , 5 , 2 ]
n = len (p)
print (Count_Segment(p, n))
|
C#
using System;
class GFG
{
static int Count_Segment( int []p, int n)
{
int count = 0;
int []upto = new int [n + 1];
for ( int i = 0; i < n + 1; i++)
upto[i] = 0;
int j = 0,curr = 0;
for ( int i = 1; i < n ; i++)
{
if (p[i] > p[i - 1] && p[i] > p[i + 1])
{
curr = p[i];
j = i - 1;
while (j >= 0 && p[j] < curr)
{
upto[p[j]]= curr;
j= j - 1;
}
j = i + 1;
while (j < n && p[j] < curr)
{
if (upto[curr-p[j]] == curr)
count += 1;
j= j+ 1;
}
}
}
return count;
}
static public void Main ()
{
int []p = {3, 4, 1, 5, 2};
int n = p.Length;
Console.WriteLine(Count_Segment(p, n));
}
}
|
Javascript
<script>
function Count_Segment(p , n) {
var count = 0;
var upto = Array(n + 1).fill(0);
for (i = 0; i < n + 1; i++)
upto[i] = 0;
var j = 0, curr = 0;
for (i = 1; i < n; i++) {
if (p[i] > p[i - 1] && p[i] > p[i + 1]) {
curr = p[i];
j = i - 1;
while (j >= 0 && p[j] < curr) {
upto[p[j]] = curr;
j -= 1;
}
j = i + 1;
while (j < n && p[j] < curr) {
if (upto[curr - p[j]] == curr)
count += 1;
j += 1;
}
}
}
return count;
}
var p = [ 3, 4, 1, 5, 2 ];
var n = p.length;
document.write(Count_Segment(p, n));
</script>
|
Time Complexity: O(N2), where N represents the size of the given array.
Auxiliary Space: O(N), where N represents the size of the given array.
Share your thoughts in the comments
Please Login to comment...