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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:07:22+00:00 2026-06-04T13:07:22+00:00

I came across a post Find the minimum difference between two arrays and using

  • 0

I came across a post Find the minimum difference between two arrays
and using the concept I tried to solve a problem of SPOJ – http://www.spoj.pl/problems/ACPC11B but strangely I got WA (Wrong Answer) !!!!

I then tried the simple way..
using two for loops..
Calculating the difference between each and every element …
Then I got AC. !!!!

Can anyone please tell me why this method
if (|A[i+1] – B[j]| < |A[i] – B[j+1]|) then increment i, otherwise increment j fails in this case ????

EDIT: I forgot to mention that when I implemented the first method I have already performed qsort on both the arrays…
Also in the SPOJ question, indexes of the elements whose difference is minimum are not required. Only minimum difference is required !!!

  • 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-04T13:07:23+00:00Added an answer on June 4, 2026 at 1:07 pm

    Here’s what I think should do the trick (based on your code), basically your code didn’t check 2 cases:

    First when the minimum is between the 2 first values in the arrays.

    Second is when we’ve checked every value in one array but still have values in the second. (Consider the case a[0,1,2], b[-1,-2,-3,-5,-6,2])

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    
    int comp(const void * a,const void * b)
    {
        if (*(int*)a==*(int*)b)
            return 0;
        else if (*(int*)a < *(int*)b)
            return -1;
        else
            return 1;
    }
    
    int main()
    {
        int x,y;
        printf("x: ");
        scanf ("%d", &x);
        int a[x];
        srand(time(NULL));
        for (int i = 0; i < x; i++)
        {
            a[i] = rand() % 30 - 15;
            //scanf ("%d", &a[i]);
            printf ("%d\n",a[i]);
        }
    
        printf("y: ");
        scanf ("%d", &y);
    
        int b[y];
        for (int i = 0; i < y; i++)
        {
            b[i] = rand() % 30 - 15;
            //scanf ("%d", &b[i]);
            printf ("%d\n",b[i]);
        }
    
    
        qsort(a,x,sizeof(int),comp) ;
        qsort(b,y,sizeof(int),comp) ;
        int i = 0, j = 0;
        int * inc; // using a pointer to select which var to increment. Avoid repeating code, optional and subjective approach.
        int minimum = abs(a[0]-b[0]); // Set min to be a[0] - b[0] which is never checked in the loop
    
        /* Added the min > 0 condition to avoid looping unnecessarily and avoid the break statement
        Changed the condition from && to || in order to handle the second case
        Changed the != to < which is more restrictive */
        while (minimum > 0 && (i < (x - 1) || j < (y - 1)))
        {
    
            int z;
            if ( i == x-1) // we've reached the end of a, inc j automatically
            {
                inc = &j;
                z = abs(a[i]-b[j+1]);
            }
            else if ( j == y -1 ) // we've reached the end of b, inc i automatically
            {
                inc = &i;
                z = abs(a[i+1]-b[j]);
            }
            else // here's the original loop, rewritten to shorten the code
            {
                int zi = abs(a[i+1]-b[j]);
                int zj = abs(a[i]-b[j+1]);
                if ( zi < zj)
                {
                    inc = &i;
                    z = zi;
                }
                else
                {
                    inc = &j;
                    z = zj;
                }
            }
            if ( z < minimum)
            {
                minimum = z;
            }
    
            (*inc)++;
        }
        printf("min: ");
        printf ("%d\n", minimum);
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While browsing I came across this blog post about using the Wikipedia API from
I came across this post , which reports the following interview question: Given two
I came across this post: Upload files with FTP using PowerShell But I'm looking
I came across a post How to find a duplicate element in an array
I came across this post in which someone wants to find out the name
I came across a post in SO where the algorithm is implemented in python
I came across this post pass array to method Java on how to pass
I recently came across a forum post in the android developers group. (link below)
Came across a problem whereby I wanted the last time data was imported to
While stumbling through the chromium code documentation, I came across this post: http://code.google.com/p/chromium/wiki/UsingGit#Windows If

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.