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 4381368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:38:25+00:00 2026-05-21T12:38:25+00:00

so i took the codility interview test yesterday and was informed today that i

  • 0

so i took the codility interview test yesterday and was informed today that i failed, unfortunately i wasnt given any other information by either codility nor the employer as to where i screwed up so i would appreciate some help in knowing where i went wrong. i know codility pays alot of emphasis on how fast the program runs and how it behaves for large numbers. now i didnt copy paste the questions so this the approx of what i remember

  1. count the number of elements in an array a which are absolute distinct, what it means is if an array had -3 and 3 in it these numbers are not distinct because|-3|=|3|. i think an example would clear it up better

a={-5,-3,0,1,-3}
the result would be 4 because there are 4 absolute distinct elements in this array.

The question also stated that a.length would be <=10000, and most importantly it stated that assume that the array is sorted in ascending order but i didnt really understand why we would need it to be sorted

IF YOU THINK I MISSED SOMETHING ASK AND I WILL TRY TO CLEAR UP THE QUESTION FURTHER.

here is my code

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;


public class test2 {

    int test(int[] a){
        Set<Integer> s=new HashSet<Integer>();

        for(int i=0;i<a.length;i++){
            s.add(Math.abs(a[i]));

        }
        return s.size();

    }

    public static void main(String[] args) {
        test2 t=new test2();
        int[] a={1,1,1,2,-1};
        System.out.println(t.test(a));

    }

}
  • 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-21T12:38:26+00:00Added an answer on May 21, 2026 at 12:38 pm

    If the array is sorted you can find duplicates by looking a neighbours. To compare absolute values to need to start at both the start and the end. This avoid creating a new structure.

    EDIT: IMHO HashMap/HashSet is O(log(log(n)) due to collisions, it is only O(1) if there is a perfect hash function. I would have thought not creating object which be much much faster but appears to be only 4x fast on my machine.

    In summary, you can see that using a Set is simpler, clearer and easier to maintain. It is still very fast and would be the best solution in 98% of cases.

    public static void main(String[] args) throws Exception {
        for (int len : new int[]{100 * 1000 * 1000, 10 * 1000 * 1000, 1000 * 1000, 100 * 1000, 10 * 1000, 1000}) {
            int[] nums = new int[len];
            for (int i = 0; i < len; i++)
                nums[i] = (int) (Math.random() * (Math.random() * 2001 - 1000));
            Arrays.sort(nums);
    
            long timeArray = 0;
            long timeSet = 0;
            int runs = len > 1000 * 1000 ? 10 : len >= 100 * 1000 ? 100 : 1000;
            for (int i = 0; i < runs; i++) {
                long time1 = System.nanoTime();
                int count = countDistinct(nums);
                long time2 = System.nanoTime();
                int count2 = countDistinctUsingSet(nums);
                long time3 = System.nanoTime();
                timeArray += time2 - time1;
                timeSet += time3 - time2;
                assert count == count2;
            }
            System.out.printf("For %,d numbers, using an array took %,d us on average, using a Set took %,d us on average, ratio=%.1f%n",
                    len, timeArray / 1000 / runs, timeSet / 1000 / runs, 1.0 * timeSet / timeArray);
        }
    }
    
    private static int countDistinct(int[] nums) {
        int lastLeft = Math.abs(nums[0]);
        int lastRight = Math.abs(nums[nums.length - 1]);
        int count = 0;
        for (int a = 1, b = nums.length - 2; a <= b;) {
            int left = Math.abs(nums[a]);
            int right = Math.abs(nums[b]);
            if (left == lastLeft) {
                a++;
                lastLeft = left;
            } else if (right == lastRight) {
                b--;
                lastRight = right;
            } else if (lastLeft == lastRight) {
                a++;
                b--;
                lastLeft = left;
                lastRight = right;
                count++;
            } else if (lastLeft > lastRight) {
                count++;
                a++;
                lastLeft = left;
            } else {
                count++;
                b--;
                lastRight = right;
            }
        }
        count += (lastLeft == lastRight ? 1 : 2);
        return count;
    }
    
    private static int countDistinctUsingSet(int[] nums) {
        Set<Integer> s = new HashSet<Integer>();
        for (int n : nums)
            s.add(Math.abs(n));
        int count = s.size();
        return count;
    }
    

    prints

    For 100,000,000 numbers, using an array took 279,623 us on average, using a Set took 1,270,029 us on average, ratio=4.5

    For 10,000,000 numbers, using an array took 28,525 us on average, using a Set took 126,591 us on average, ratio=4.4

    For 1,000,000 numbers, using an array took 2,846 us on average, using a Set took 12,131 us on average, ratio=4.3

    For 100,000 numbers, using an array took 297 us on average, using a Set took 1,239 us on average, ratio=4.2

    For 10,000 numbers, using an array took 42 us on average, using a Set took 156 us on average, ratio=3.7

    For 1,000 numbers, using an array took 8 us on average, using a Set took 30 us on average, ratio=3.6


    On @Kevin K’s point, even Integer can have collision even through it’s hash values are unique, it can map to the same bucket as the capacity is limited.

    public static int hash(int h) {
        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
    
    public static void main(String[] args) throws Exception {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>(32, 2.0f);
        for (int i = 0; i < 10000 && map.size() < 32 * 2; i++) {
            if (hash(i) % 32 == 0)
                map.put(i, i);
        }
        System.out.println(map.keySet());
    }
    

    prints

    [2032, 2002, 1972, 1942, 1913, 1883, 1853, 1823, 1763, 1729, 1703, 1669, 1642, 1608, 1582, 1548, 1524, 1494, 1456, 1426, 1405, 1375, 1337, 1307, 1255, 1221, 1187, 1153, 1134, 1100, 1066, 1032, 1016, 986, 956, 926, 881, 851, 821, 791, 747, 713, 687, 653, 610, 576, 550, 516, 508, 478, 440, 410, 373, 343, 305, 275, 239, 205, 171, 137, 102, 68, 34, 0]

    The values are in reverse order because the HashMap has generated into a LinkedList.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a stored procedure in MySQL that took a list
I took a snapshot of the jquery.js file and jquery-ui files that I use
I took over an iPhone project recently that was developed prior iOS4. I'm wondering
I took my first 'fundamentals of programming' lab session at uni today. One thing
I took a data structures class in C++ last year, and consequently implemented all
I took the plunge this afternoon and began studying LINQ, so far just mucking
It took me forever to reduce the problem to this. I cannot express the
This took me a while to figure out and I found the answer on
I recently took my Db initiating code out of the __construct of my Page
I wish to calculate the time it took for an API to return a

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.