- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find out the number of pairs in an array that satisfy a given condition
Suppose, we are given n numbers in array nums. We have to choose a pair of two numbers from the array, and there is a condition that the difference of their positions in the array is equal to the sum of the two numbers. There can be a total of n(n - 1)/2 number of total pairs from the given array of numbers. We have to find out the total number of such pairs from the array.
So, if the input is like n = 8, nums = {4, 2, 1, 0, 1, 2, 3, 3}, then the output will be 13.
There can be 13 such pairs in the array.
To solve this, we will follow these steps −
Define an array vals(n) for initialize i := 0, when i < n, update (increase i by 1), do: vals[i] := i + 1 - nums[i] sort the array vals res := 0 for initialize i := 0, when i < n, update (increase i by 1), do: k := nums[i] + i + 1 res := res + (position of first occurrence of a value not less than k in array vals - position of first occurrence of a value not greater than k in array vals) return res
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n, vector<int> nums){ vector<int> vals(n); for( int i = 0; i < n; i++) vals[i] = i + 1 - nums[i]; sort(vals.begin(), vals.end()); int res = 0; for( int i = 0; i < n; i++ ) { int k = nums[i] + i + 1; res += upper_bound(vals.begin(), vals.end(), k) - lower_bound(vals.begin(), vals.end(), k); } return res; } int main() { int n = 8; vector<int> nums = {4, 2, 1, 0, 1, 2, 3, 3}; cout<< solve(n, nums); return 0; }
Input
8, {4, 2, 1, 0, 1, 2, 3, 3}
Output
13
- Related Articles
- Program to find number of subsequences that satisfy the given sum condition using Python
- Count index pairs which satisfy the given condition in C++
- Find numbers a and b that satisfy the given condition in C++
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++
- Count subsets that satisfy the given condition in C++
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- How to count the number of values that satisfy a condition in an R vector?
- C++ program to find out the number of coordinate pairs that can be made
- Find the Number of Sextuplets that Satisfy an Equation using C++
- Count all possible N digit numbers that satisfy the given condition in C++
- C++ Program to find an array satisfying an condition
- Maximum size of sub-array that satisfies the given condition in C++ program
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- Program to find out the number of pairs of equal substrings in Python

Advertisements