[Solution] Nice Pairs solution codechef

CodeChef Starters 15 Division 3 (Rated) - CodeChef

Nice Pairs solution codechef

Given a string S of length N containing only numeric characters, find the number of Nice Pairs.

A Nice Pair is a pair of indices – (i,j) such that 1i<jN and ji=|SjSi|.

Nice Pairs solution codechef Input Format

  • First line will contain T, number of testcases. Then the testcases follow.
  • The first line of each testcase contains a single integer, N, the length of the string
  • The second line of each testcase contains a string S of length N. The string contains only numeric characters [09]

Nice Pairs solution codechef Output Format

For each testcase, output a single integer – the number of nice pairs in the given string.

Nice Pairs solution codechef Constraints

  • 1T1000
  • 2N105
  • String S contains only numeric characters
  • The sum of N over all test cases does not exceed 2105

Sample Input 1

3
3
123
5
13492
8
94241234

Sample Output 1

3
2
9

Nice Pairs solution codechef Explanation

Test Case 1: There are 3 nice pairs in the given string – (1,2),(1,3),(2,3)

Test Case 2: There are 2 nice pairs in the given string – (2,3),(3,5)


Updating Soon…


Nice Pairs solution codechef

Counting Tuples codechef solution

 

Leave a Comment