Click here to Skip to main content
14,329,474 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have to find min absolute difference between two elements of unsorted arrays. My approach is to first sort both the array, run a loop over one array and find lower bound of each element of this array in another array.

And then check whether it is minimum or not and store it for further comparisons

Test Case:

2
8 1 3 5 7 9 7 3 1

8 2 4 6 8 10 8 6 2

8 2 3 5 10 9 3 2 1

7 1 2 6 12 13 3 2
Output :

1
0
result : passed

Explanation:

1) min will be abs(a[7]-b[7])

2) min will be abs(a[0]-b[(1)])

But when i am submitting to spoj I am getting wrong answer .
Please help!

Problem Link : SPOJ.com - Problem ACPC11B[^]

What I have tried:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector <int> a;
vector <int> b;
int main(){
    int t;
    cin>>t;
    while(t--){
        int na;
        cin>>na;
        for(int i=0;i<na;i++){
            int temp;
            cin>>temp;
            a.push_back(temp);
        }
        int nb;
        cin>>nb;
        for(int i=0;i<nb;i++){
            int temp;
            cin>>temp;
            b.push_back(temp);
        }
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        int ans=abs(a[0]-b[0]);
        for(int i=0;i<a.size();i++){
            int bval = lower_bound(b.begin(),b.end(),a[i])-b.begin();
            ans = min(ans,abs(a[i]-b[bval]));
            if(bval>0)
            ans = min(ans,abs(a[i]-b[bval-1]));
        }
        cout<<ans<<endl;
        a.clear();
        b.clear();
    }
}
Posted
Updated 18-Mar-19 2:37am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Looking at requirements (above all the constraints), a brute force approach (two nested loops) would work smoothly.

Regarding your own code, this part
Quote:
int bval = lower_bound(b.begin(),b.end(),a[i])-b.begin();
ans = min(ans,abs(a[i]-b[bval]));
if(bval>0)
ans = min(ans,abs(a[i]-b[bval-1]));
is incorrect.

It should be
size_t bval = lower_bound(b.begin(),b.end(),a[i])-b.begin();
if ( bval < b.size() )
  ans = min(ans,abs(a[i]-b[bval]));
if(bval > 0)
  ans = min(ans,abs(a[i]-b[bval-1]));
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Start with new odd samples of data to see how your algorithm behave.
2
8 1 3 5 7 9 11 21 23
8 13 14 15 19 23 25 29 29
8 13 14 15 19 23 25 29 29
8 1 3 5 7 9 11 21 23
Do you find the correct answer ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

[UpDate]
You can make your code faster by removing this line
sort(a.begin(),a.end());

because your code don't take advantage of the sorted array.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100