- Related Questions & Answers
- Count all possible N digit numbers that satisfy the given condition in C++
- Find numbers a and b that satisfy the given condition in C++
- Count all pairs with given XOR in C++
- Maximum count of pairs which generate the same sum in C++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Count all pairs of an array which differ in K bits in C++
- Count digits in given number N which divide N in C++
- C# Program to check whether the elements of a sequence satisfy a condition or not
- Print index of columns sorted by count of zeroes in the Given Matrix in C Program.
- Index Pairs of a String in Python
- Count divisible pairs in an array in C++
- Count the number of key/value pairs in the Hashtable in C#
- Print all pairs with given sum in C++
- Count the number of key/value pairs in HybridDictionary in C#

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an array of permutation of first N natural numbers. The goal here is to find the index pairs of elements that satisfy the condition mentioned below −

If an array is Arr[], then i,j are indexes, count element pairs such that Arr[i]+Arr[j]=max(Arr[x]) such that i<=x<=j.

That is, the sum of Arr[i] and A[j] is equal to the maximum element occurring between these two segments.

Arr[]= { 2,4,1,3,6,5 }

Count of index pairs which satisfy the given condition:1

**Explanation** − The sum of pairs is given −

2+4=6, 6 is maximum but not between 2 and 4.

2+1=3, 3 is not in between 2 and 1 and maximum between them is 4.

2+3=5, 5 is not in between 2 and 3 and maximum between them is 4.

2+6=8, 8 is not in between 2 and 6 and maximum between them is 4.

Similarly

1+5=6, 6 is in between 1 and 5 and maximum between them is 6.

Out of all only 1 pair is present that satisfies the condition.

Arr[]= { 1,2,5,4,3 }

Count of index pairs which satisfy the given condition:2

**Explanation** − The sum of pairs is given −

1+5=6, 6 is maximum but not between 1 and 5.

1+4=5, 5 is in between 1 and 4 and the maximum between them is 5.

2+3=5, 5 is in between 2 and 3 and the maximum between them is 5.

1+3=4, 4 is in between 1 and 3 but the maximum between them is 5.

Out of all 2 pairs are present that satisfy the condition.

Integer array Arr[] stores the numbers and size its length.

Function countPairs(int A[],int n) takes an array and its size n as input and returns the count of pairs which satisfy the above condition..

Variable count is used to store the initial value 0 for such pairs.

Initialize max1 with the first element and its index in maxindex as 0 to store the value and index of maximum value found so far.

Start traversing the array using for loop.

Inside nested for loop if given A[j]>=max1 then update max1 and its index with j.

For each pair of A[i] and A[j] if sum is equal to max1 and index maxindex is in between i and j, then increment count as condition is satisfied.

After the end both loops return the result present in count.

// CPP implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return the count of // required index pairs int countPairs(int A[], int n){ // To store the required count int count = 0; int i,j,k; int max1=A[0]; int maxindex=0; for ( i = 0; i<n-1; i++){ for(j=i+1;j<n;j++){ if(A[j]>=max1){ max1=A[j]; maxindex=j; } if(A[i]+A[j]==max1 && maxindex>=i && maxindex<=j) count++; } } // Return count of subsegments return count; } int main(){ int Arr[] = {3, 4, 6, 1, 5, 2}; int size =6; cout <<endl<<"Count of index pairs which satisfy the given condition:" <<countPairs(Arr,size); return 0; }

Count of index pairs which satisfy the given condition: 1

Advertisements