Content-Length: 272714 | pFad | http://github.com/Saikat2407/DSA_Hacktoberfest/pull/93/files

AC Create NPAIRS.java by Himalaya001 · Pull Request #93 · Saikat2407/DSA_Hacktoberfest · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create NPAIRS.java #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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);
}
}
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Saikat2407/DSA_Hacktoberfest/pull/93/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy