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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:12:30+00:00 2026-06-03T06:12:30+00:00

Is there a more efficient way to achieve this: Given an array A of

  • 0

Is there a more efficient way to achieve this:
Given an array A of size n and two positive integers a and b, find the sum floor(abs(A[i]-A[j])*a/b) taken over all pairs (i, j) where 0 <= i < j < n.

int A[n];
int a, b; // assigned some positive integer values
...
int total = 0;
for (int i = 0; i < n; i++) {
    for (int j = i+1; j < n; j++) {
        total += abs(A[i]-A[j])*a/b; // want integer division here
    }
}

To optimize this a little bit, I sorted the array (O(nlogn)) and then didn’t use an abs function. Also, I cached the value a[i] before the inner for loop, so I could just read stuff from A sequentially. I was considering precomputing a/b and storing that in a float, but the extra casting just makes it slower (especially since I want to take the floor of the result).

I couldn’t come up with a solution that was better than O(n^2).

  • 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-06-03T06:12:32+00:00Added an answer on June 3, 2026 at 6:12 am

    Yes, there is a more efficient algorithm. It can be done it in O(n*log n). I don’t expect there to be an asymptotically faster way, but I’m far from any idea of a proof.

    Algorithm

    First sort the array in O(n*log n) time.

    Now, let us look at the terms

    floor((A[j]-A[i])*a/b) = floor ((A[j]*a - A[i]*a)/b)
    

    for 0 <= i < j < n. For each 0 <= k < n, write A[k]*a = q[k]*b + r[k] with 0 <= r[k] < b.

    For A[k] >= 0, we have q[k] = (A[k]*a)/b and r[k] = (A[k]*a)%b with integer division, for A[k] < 0, we have q[k] = (A[k]*a)/b - 1 and r[k] = b + (A[k]*a)%b unless b divides A[k]*a, in which case we have q[k] = (A[k]*a)/b and r[k] = 0.

    Now we rewrite the terms:

    floor((A[j]*a - A[i]*a)/b) = floor(q[j] - q[i] + (r[j] - r[i])/b)
                               = q[j] - q[i] + floor((r[j] - r[i])/b)
    

    Each q[k] appears k times with positive sign (for i = 0, 1, .. , k-1) and n-1-k times with negative sign (for j = k+1, k+2, ..., n-1), so its total contribution to the sum is

    (k - (n-1-k))*q[k] = (2*k+1-n)*q[k]
    

    The remainders still have to be accounted for. Now, since 0 <= r[k] < b, we have

    -b < r[j] - r[i] < b
    

    and floor((r[j]-r[i])/b) is 0 when r[j] >= r[i] and -1 when r[j] < r[i]. So

                                n-1
     ∑ floor((A[j]-A[i])*a/b) =  ∑ (2*k+1-n)*q[k] - inversions(r)
    i<j                         k=0
    

    where an inversion is a pair (i,j) of indices with 0 <= i < j < n and r[j] < r[i].

    Calculating the q[k] and r[k] and summing the (2*k+1-n)*q[k] is done in O(n) time.

    It remains to efficiently count the inversions of the r[k] array.

    For each index 0 <= k < n, let c(k) be the number of i < k such that r[k] < r[i], i.e. the number of inversions in which k appears as the larger index.

    Then obviously the number of inversions is ∑ c(k).

    On the other hand, c(k) is the number of elements that are moved behind r[k] in a stable sort (stability is important here).

    Counting these moves, and hence the inversions of an array is easy to do while merge-sorting it.

    Thus the inversions can be counted in O(n*log n) too, giving an overall complexity of O(n*log n).

    Code

    A sample implementation with a simple unscientific benchmark (but the difference between the naive quadratic algorithm and the above is so large that an unscientific benchmark is conclusive enough).

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    long long mergesort(int *arr, unsigned elems);
    long long merge(int *arr, unsigned elems, int *scratch);
    long long nosort(int *arr, unsigned elems, long long a, long long b);
    long long withsort(int *arr, unsigned elems, long long a, long long b);
    
    int main(int argc, char *argv[]) {
        unsigned count = (argc > 1) ? strtoul(argv[1],NULL,0) : 1000;
        srand(time(NULL)+count);
        long long a, b;
        b = 1000 + 9000.0*rand()/(RAND_MAX+1.0);
        a = b/3 + (b-b/3)*1.0*rand()/(RAND_MAX + 1.0);
        int *arr1, *arr2;
        arr1 = malloc(count*sizeof *arr1);
        arr2 = malloc(count*sizeof *arr2);
        if (!arr1 || !arr2) {
            fprintf(stderr,"Allocation failed\n");
            exit(EXIT_FAILURE);
        }
        unsigned i;
        for(i = 0; i < count; ++i) {
            arr1[i] = 20000.0*rand()/(RAND_MAX + 1.0) - 2000;
        }
        for(i = 0; i < count; ++i) {
            arr2[i] = arr1[i];
        }
        long long res1, res2;
        double start = clock();
        res1 = nosort(arr1,count,a,b);
        double stop = clock();
        printf("Naive:   %lld in %.3fs\n",res1,(stop-start)/CLOCKS_PER_SEC);
        start = clock();
        res2 = withsort(arr2,count,a,b);
        stop = clock();
        printf("Sorting: %lld in %.3fs\n",res2,(stop-start)/CLOCKS_PER_SEC);
        return EXIT_SUCCESS;
    }
    
    long long nosort(int *arr, unsigned elems, long long a, long long b) {
        long long total = 0;
        unsigned i, j;
        long long m;
        for(i = 0; i < elems-1; ++i) {
            m = arr[i];
            for(j = i+1; j < elems; ++j) {
                long long d = (arr[j] < m) ? (m-arr[j]) : (arr[j]-m);
                total += (d*a)/b;
            }
        }
        return total;
    }
    
    long long withsort(int *arr, unsigned elems, long long a, long long b) {
        long long total = 0;
        unsigned i;
        mergesort(arr,elems);
        for(i = 0; i < elems; ++i) {
            long long q, r;
            q = (arr[i]*a)/b;
            r = (arr[i]*a)%b;
            if (r < 0) {
                r += b;
                q -= 1;
            }
            total += (2*i+1LL-elems)*q;
            arr[i] = (int)r;
        }
        total -= mergesort(arr,elems);
        return total;
    }
    
    long long mergesort(int *arr, unsigned elems) {
        if (elems < 2) return 0;
        int *scratch = malloc((elems + 1)/2*sizeof *scratch);
        if (!scratch) {
            fprintf(stderr,"Alloc failure\n");
            exit(EXIT_FAILURE);
        }
        return merge(arr, elems, scratch);
    }
    
    long long merge(int *arr, unsigned elems, int *scratch) {
        if (elems < 2) return 0;
        unsigned left = (elems + 1)/2, right = elems-left, i, j, k;
        long long inversions = 0;
        inversions += merge(arr, left, scratch);
        inversions += merge(arr+left,right,scratch);
        if (arr[left] < arr[left-1]) {
            for(i = 0; i < left; ++i) {
                scratch[i] = arr[i];
            }
            i = 0; j = 0; k = 0;
            int *lptr = scratch, *rptr = arr+left;
            while(i < left && j < right) {
                if (rptr[j] < lptr[i]) {
                    arr[k++] = rptr[j++];
                    inversions += (left-i);
                } else {
                    arr[k++] = lptr[i++];
                }
            }
            while(i < left) arr[k++] = lptr[i++];
        }
        return inversions;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more efficient way to convert an HTMLCollection to an Array, other
Is there a more efficient way to clamp real numbers than using if statements
Is there are more efficient way than the following for selecting the third parent?
If I have a standard for loop is there a more efficient way to
Possible Duplicate: More efficient way to remove elements from an array list I have
Is there a more efficient (in terms of memory foot print) binding to datagrid
I'm wondering if there is a more efficient method for replacing colors in a
I was wondering if there was a more efficient (efficient as in simpler/cleaner code)
Is there any time in which a reference type is more efficient than a
Is there an algorithm that is more time efficient than O(n^2) for detecting cycles

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.