Twin Pairs
Bob always bragged about his smartness. To test this, Alice gave him an
array ‘A’ of size ‘N’ and asked him to find the number of twin pairs in that array.
A twin pair can be defined as a pair of indexes ‘x’, ‘y’ such that ‘x’ is less than ‘y’ and ‘A[y]’ - ‘A[x]’ = ‘y’ - ‘x’.
Bob was having a hard time doing so, so he asked you to help him find the total number of twin pairs.
Input Format:
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two spaced integers, ‘N’ denoting the size of the array.
The following line contains an array ‘A’ of ‘N’ spaced integers.
Output Format:
For each test case, print a single integer denoting the number of twin pairs in the array.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 5
1 <= N <= 10^5
1 <= A[i] <= N
Time Limit: 1 sec
CodingNinjas
author
2y
Brute Force
We can check all the pairs of indexes using two nested loops and if they satisfy the given constraints, then increase the count of twin pairs by 1.
Algorithm:
- Initialise ‘ans’ to 0.
- Run a l...read more
CodingNinjas
author
2y
Maths
We are given that two indexes, ‘x’ and ‘y’ are twin pairs when:
- ‘x’ < ‘y’
- ‘A[y]’ - ‘A[x]’ = ‘y’ - ‘x’
If we rearrange the second condition we get:
‘A[y]’ - y = ‘A[x]’ - ‘x’
Now let ‘B[i]’ be equal t...read more
Help your peers!
Add answer anonymously...
Top TCS System Engineer interview questions & answers
Popular interview questions of System Engineer
Top HR questions asked in TCS System Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app