Skip to main content
Code Review

Return to Question

missing closing bracket
Source Link
RobH
  • 17.1k
  • 6
  • 38
  • 73

Sherlock is given an array of N integers (\$A_0, A_1 \ldots A_{N-1}\$) by Watson. Now Watson asks Sherlock how many different pairs of indices \$i\$ and \$j\$ exist such that \$i\$ is not equal to \$j\$ but \$A_i\$ is equal to \$A_j\$.

Sherlock is given an array of N integers (\$A_0, A_1 \ldots A_{N-1}\$ by Watson. Now Watson asks Sherlock how many different pairs of indices \$i\$ and \$j\$ exist such that \$i\$ is not equal to \$j\$ but \$A_i\$ is equal to \$A_j\$.

Sherlock is given an array of N integers (\$A_0, A_1 \ldots A_{N-1}\$) by Watson. Now Watson asks Sherlock how many different pairs of indices \$i\$ and \$j\$ exist such that \$i\$ is not equal to \$j\$ but \$A_i\$ is equal to \$A_j\$.

Tweeted twitter.com/#!/StackCodeReview/status/626717083510927360
added 140 characters in body; edited tags; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Calculate pairs in a Set ("Sherlock and Pairs" HackerRank challenge)

I have started reading clean code and want to improve my coding practices. Here is my attempt at solving an online puzzleonline puzzle. Please review the code and let me know how could I have written it better in terms of logic, coding practices or any other advice you might have.

Sherlock is given an array of N integers (A0, A1 ... AN−1\$A_0, A_1 \ldots A_{N-1}\$ by Watson. Now Watson asks Sherlock how many different pairs of indices i\$i\$ and j\$j\$ exist such that i\$i\$ is not equal to j\$j\$ but Ai\$A_i\$ is equal to Aj\$A_j\$.

That is, Sherlock has to count the total number of pairs of indices (i,j)\$(i,j)\$ where Ai =Aj\$A_i =Aj\$ AND i≠j\$i≠j\$.

The first line contains T\$T\$, the number of test cases. T\$T\$ test cases follow. Each test case consists of two lines; the first line contains an integer N\$N\$, the size of array, while the next line contains N space separated integers.

1≤T≤10\1ドル≤T≤10\$

1≤N≤105\1ドル≤N≤10^5\$

1≤A[i]≤\1ドル≤A[i]≤10^6\$

Calculate pairs in a Set

I have started reading clean code and want to improve my coding practices. Here is my attempt at solving an online puzzle. Please review the code and let me know how could I have written it better in terms of logic, coding practices or any other advice you might have.

Sherlock is given an array of N integers (A0, A1 ... AN−1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.

That is, Sherlock has to count the total number of pairs of indices (i,j) where Ai =Aj AND i≠j.

The first line contains T, the number of test cases. T test cases follow. Each test case consists of two lines; the first line contains an integer N, the size of array, while the next line contains N space separated integers.

1≤T≤10

1≤N≤105

1≤A[i]≤

Calculate pairs in a Set ("Sherlock and Pairs" HackerRank challenge)

I have started reading clean code and want to improve my coding practices. Here is my attempt at solving an online puzzle. Please review the code and let me know how could I have written it better in terms of logic, coding practices or any other advice you might have.

Sherlock is given an array of N integers (\$A_0, A_1 \ldots A_{N-1}\$ by Watson. Now Watson asks Sherlock how many different pairs of indices \$i\$ and \$j\$ exist such that \$i\$ is not equal to \$j\$ but \$A_i\$ is equal to \$A_j\$.

That is, Sherlock has to count the total number of pairs of indices \$(i,j)\$ where \$A_i =Aj\$ AND \$i≠j\$.

The first line contains \$T\$, the number of test cases. \$T\$ test cases follow. Each test case consists of two lines; the first line contains an integer \$N\$, the size of array, while the next line contains N space separated integers.

\1ドル≤T≤10\$

\1ドル≤N≤10^5\$

\1ドル≤A[i]≤10^6\$

deleted 47 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Calculate pairs in a Set - Is my code Readable and efficient?

Sherlock is given an array of N integers (A0, A1 ... AN−1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.

Sherlock is given an array of N integers (A0, A1 ... AN−1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.

That is, Sherlock has to count the total number of pairs of indices (i,j) where Ai =Aj AND i≠j.

That is, Sherlock has to count the total number of pairs of indices (i,j) where Ai =Aj AND i≠j.

Input Format

Input Format

The first line contains T, the number of test cases. T test cases follow. Each test case consists of two lines; the first line contains an integer N, the size of array, while the next line contains N space separated integers.

The first line contains T, the number of test cases. T test cases follow. Each test case consists of two lines; the first line contains an integer N, the size of array, while the next line contains N space separated integers.

Output Format

Output Format

For each test case, print the required answer on a different line.

For each test case, print the required answer on a different line.

Constraints

Constraints

1≤T≤10

1≤T≤10

1≤N≤105

1≤N≤105

1≤A[i]≤

1≤A[i]≤

I have kept the test cases still commented. Is that okay? Also, I have still kept the factorial function as it can be used in the future. Should I remove the factorial function or can I keep it commented?

Thanks in advance for you valuable inputs.

Calculate pairs in a Set - Is my code Readable and efficient?

Sherlock is given an array of N integers (A0, A1 ... AN−1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.

That is, Sherlock has to count the total number of pairs of indices (i,j) where Ai =Aj AND i≠j.

Input Format

The first line contains T, the number of test cases. T test cases follow. Each test case consists of two lines; the first line contains an integer N, the size of array, while the next line contains N space separated integers.

Output Format

For each test case, print the required answer on a different line.

Constraints

1≤T≤10

1≤N≤105

1≤A[i]≤

I have kept the test cases still commented. Is that okay? Also I have still kept the factorial function as it can be used in the future. Should I remove the factorial function or can I keep it commented?

Thanks in advance for you valuable inputs.

Calculate pairs in a Set

Sherlock is given an array of N integers (A0, A1 ... AN−1 by Watson. Now Watson asks Sherlock how many different pairs of indices i and j exist such that i is not equal to j but Ai is equal to Aj.

That is, Sherlock has to count the total number of pairs of indices (i,j) where Ai =Aj AND i≠j.

Input Format

The first line contains T, the number of test cases. T test cases follow. Each test case consists of two lines; the first line contains an integer N, the size of array, while the next line contains N space separated integers.

Output Format

For each test case, print the required answer on a different line.

Constraints

1≤T≤10

1≤N≤105

1≤A[i]≤

I have kept the test cases still commented. Is that okay? Also, I have still kept the factorial function as it can be used in the future. Should I remove the factorial function or can I keep it commented?

Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /