Nice Pairs solution codechef
-
For Solution
Click Here!
Given a string SS of length NN containing only numeric characters, find the number of Nice Pairs.
A Nice Pair is a pair of indices – (i,j)(i,j) such that 1≤i<j≤N1≤i<j≤N and j−i=|Sj−Si|j−i=|Sj−Si|.
Input Format Nice Pairs solution codechef
- First line will contain TT, number of testcases. Then the testcases follow.
- The first line of each testcase contains a single integer, NN, the length of the string
- The second line of each testcase contains a string SS of length NN. The string contains only numeric characters [0−9][0−9]
Output Format Nice Pairs solution codechef
For each testcase, output a single integer – the number of nice pairs in the given string.
Constraints Nice Pairs solution codechef
- 1≤T≤10001≤T≤1000
- 2≤N≤1052≤N≤105
- String SS contains only numeric characters
- The sum of NN over all test cases does not exceed 2⋅1052⋅105
Sample Input 1 Nice Pairs solution codechef
3
3
123
5
13492
8
94241234
Sample Output 1 Nice Pairs solution codechef
3
2
9
Explanation
Test Case 11: There are 33 nice pairs in the given string – (1,2),(1,3),(2,3)(1,2),(1,3),(2,3)
Test Case 22: There are 22 nice pairs in the given string – (2,3),(3,5)
-
For Solution
Click Here!