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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:15:24+00:00 2026-05-19T12:15:24+00:00

I have millions of fixed-size (100) int arrays. Each array is sorted and has

  • 0

I have millions of fixed-size (100) int arrays. Each array is sorted and has unique elements. For each array, I want to find all arrays which have 70% common elements. Right now I am getting around 1 million comparisons (using Arrays.binarySearch()) per second, which is too slow for us.

Can anyone recommend a better searching algorithm?

  • 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-19T12:15:25+00:00Added an answer on May 19, 2026 at 12:15 pm

    Something like this should do the job (provided that the arrays are sorted and contain unique elements):

    public static boolean atLeastNMatchingElements(final int n,
        final int[] arr1,
        final int[] arr2){
    
        /* check assumptions */
        assert (arr1.length == arr2.length);
    
        final int arrLength = arr2.length;
    
        { /* optimization */
            final int maxOffset = Math.max(arrLength - n, 0);
            if(arr1[maxOffset] < arr2[0] || arr2[maxOffset] < arr1[0]){
                return false;
            }
        }
    
        int arr2Offset = 0;
        int matches = 0;
    
        /* declare variables only once, outside loop */
        int compResult; int candidate;
    
        for(int i = 0; i < arrLength; i++){
            candidate = arr1[i];
            while(arr2Offset < arrLength - 1){
                compResult = arr2[arr2Offset] - candidate;
                if(compResult > 0){
                    break;
                } else{
                    arr2Offset++;
                    if(compResult == 0){
                        matches++;
                        break;
                    }
                }
            }
            if(matches == n){
                return true;
            }
            /* optimization */
            else if(matches < n - (arrLength - arr2Offset)){
                return false;
            }
        }
        return false;
    }
    

    Sample usage:

    public static void main(final String[] args){
        final int[] arr1 = new int[100];
        final int[] arr2 = new int[100];
        int x = 0, y = 0;
        for(int i = 0; i < 100; i++){
            if(i % 10 == 0){
                x++;
            }
            if(i % 12 == 0){
                y++;
            }
            arr1[i] = x;
            arr2[i] = y;
            x++;
            y++;
        }
        System.out.println(atLeastNMatchingElements(70, arr1, arr2));
        System.out.println(atLeastNMatchingElements(95, arr1, arr2));
    }
    

    Output:

    true
    false

    Premature Optimizations™

    I have now tried to optimize the above code. Please check whether the code blocks marked as

    /* optimization */
    

    make a noticeable difference. After optimization, I would refactor the code to get it down to one or two return statements.

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

Sidebar

Related Questions

I have an int and float array each of length 220 million (fixed). Now,
I have millions of songs, each song has its unique Song ID. Corresponding to
I have an int and float array of length 220 million (fixed). Now, I
I have a large set of names (millions in number). Each of them has
I have a millions of pairs of string of same length which I want
I have to store millions of entries in a database. Each entry is identified
I have a large hashmap containing millions of entries, and I want to persist
I have some very large (>4 GB) files containing (millions of) fixed-length binary records.
Actually I have millions of vector objects in my program. In default for each
I have millions of Keywords in a column labeled Keyword.text. Each factor or Keyword

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.