Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions CodeChef/NPAIRS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner scan=new Scanner(System.in);
int T=scan.nextInt();
while(T-->0)
{
int N=scan.nextInt();
String s=scan.next();
int count=0;
HashMap<Integer, Integer> mp = new HashMap<>();
HashMap<Integer, Integer> mpl = new HashMap<>();
for(int i = 0; i < N; i++)
{
int val=Integer.parseInt(String.valueOf(s.charAt(i)));

// Stores count of distinct
// values of arr[i] - x * i
mp.put(val-i,
mp.getOrDefault(val-i, 0) + 1);

}
for(int i = 0; i < N; i++)
{
int val=Integer.parseInt(String.valueOf(s.charAt(i)));


mpl.put(val+i,
mpl.getOrDefault(val+i, 0) + 1);
}

// Iterate over the Map
for(int v : mp.values())
{

// Increase count of pairs
// System.out.print(v);

if(v>1)
count +=(v*(v-1)/2);
}
//System.out.println();
for(int v : mpl.values())
{

// Increase count of pairs

// System.out.print(v);
if(v>1)
count +=(v*(v-1)/2);
}
//System.out.println();
// Print the count of such pairs
System.out.println(count);
}
}
}