Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:26:00+00:00 2026-05-27T15:26:00+00:00

int n, k; int count = 0, diff; BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  • 0
int n, k;
int count = 0, diff;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input;
input = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
int[] a = new int[n];
k = Integer.parseInt(input[1]);
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
  a[i] = Integer.parseInt(input[i]);
     for (int j = 0; j < i; j++) {
        diff = a[j] - a[i];
        if (diff == k || -diff == k) {
           count++;
        }
     }
}
System.out.print(count);

This is a sample program where I am printing particular difference count, where n range is <=100000
Now problem is to decrease execution for this program. How can I make it better to reduce running time.

Thanks in advance for suggestions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:26:00+00:00Added an answer on May 27, 2026 at 3:26 pm

    Here is a comparison of @Socha23’s solution using HashSet, TIntIntHashSet and the original solution.

    For 100,000 numbers I got the following (without the reading and parsing)

    For 100 unique values, k=10

    Set: 89,699,743 took 0.036 ms
    Trove Set: 89,699,743 took 0.017 ms
    Loops: 89,699,743 took 3623.2 ms
    

    For 1000 unique values, k=10

    Set: 9,896,049 took 0.187 ms
    Trove Set: 9,896,049 took 0.193 ms
    Loops: 9,896,049 took 2855.7 ms
    

    The code

    import gnu.trove.TIntIntHashMap;
    import gnu.trove.TIntIntProcedure;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    
    class Main {
        public static void main(String... args) throws Exception {
            Random random = new Random(1);
            int[] a = new int[100 * 1000];
            int k = 10;
            for (int i = 0; i < a.length; i++)
                a[i] = random.nextInt(100);
    
            for (int i = 0; i < 5; i++) {
                testSet(a, k);
                testTroveSet(a, k);
                testLoops(a, k);
            }
        }
    
        private static void testSet(int[] a, int k) {
            Map<Integer, Integer> readNumbers = new HashMap<Integer, Integer>();
            for (int num : a) {
                Integer freq = readNumbers.get(num);
                readNumbers.put(num, freq == null ? 1 : freq + 1);
            }
    
            long start = System.nanoTime();
            int count = 0;
            for (Integer aNumber : readNumbers.keySet()) {
                if (readNumbers.containsKey(aNumber + k)) {
                    count += (readNumbers.get(aNumber) * readNumbers.get(aNumber + k));
                }
            }
            long time = System.nanoTime() - start;
            System.out.printf("Set: %,d took %.3f ms%n", count, time / 1e6);
        }
    
        private static void testTroveSet(int[] a, final int k) {
            final TIntIntHashMap readNumbers = new TIntIntHashMap();
            for (int num : a)
                readNumbers.adjustOrPutValue(num, 1,1);
    
            long start = System.nanoTime();
            final int[] count = { 0 };
            readNumbers.forEachEntry(new TIntIntProcedure() {
                @Override
                public boolean execute(int key, int keyCount) {
                    count[0] += readNumbers.get(key + k) * keyCount;
                    return true;
                }
            });
            long time = System.nanoTime() - start;
            System.out.printf("Trove Set: %,d took %.3f ms%n", count[0], time / 1e6);
        }
    
        private static void testLoops(int[] a, int k) {
            long start = System.nanoTime();
            int count = 0;
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < i; j++) {
                    int diff = a[j] - a[i];
                    if (diff == k || -diff == k) {
                        count++;
                    }
                }
            }
            long time = System.nanoTime() - start;
            System.out.printf("Loops: %,d took %.1f ms%n", count, time / 1e6);
        }
    
        private static long free() {
            return Runtime.getRuntime().freeMemory();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void main(String[] args) { int count, innerCount; for(count=0;count<=3;count++) { System.out.println(Count is +
I have this code: public static String SelectRandomFromTemplate(String template,int count) { String[] split =
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I'm using System.IO.Stream.Read(byte[] buffer, int offset, int count) . Is there an alternative to
I can't figure out the problem in this: #include<stdio.h> int main() { int a,b,count
Say I have a function: void someFunc(int *x,int count); which is out of my
I am using System.Threading.Tasks.Parallel.For to do some heavyweight processing. My code is: int count
void permute(string elems, int mid, int end) { static int count; if (mid ==
When I use new[] to create an array of my classes: int count =
class Category { public string Name { get; set; } public int Count {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.