Thread: A faster algorithm

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    23

    A faster algorithm

    Hi,
    I was told to write an algorithm as following
    There are three arrays A[],B[],C[] of same size N.Find out all possible (i,j,k) such that A[i]+B[j]=C[k].The maximum allowed time complexity is O(N2).Following is the algorithm I wrtten for O(N2)
    Code:
    #include<stdio.h>
    
    #define MAX 1000
    
    struct sum
    {
            int result;
            int i;
            int j;
    };
    
    int main()
    {
            struct sum sum_array[MAX];
            int n=4;
            int a[] = {4,1,2,5};
            int b[] = {1,7,6,0};
            int c[] = {11,3,8,2};
            int k;
            int i,j;
            for(i=0;i<MAX;i++)
                    sum_array[i].result=-1;
            for(i=0;i<n;i++)
            {
                    for(j=0;j<n;j++)
                    {
                            sum_array[a[i]+b[j]].result=a[i]+b[j];
                            sum_array[a[i]+b[j]].i=i;
                            sum_array[a[i]+b[j]].j=j;
                    }
            }
            for(k=0;k<n;k++)
            {
                    if(sum_array[c[k]].result==c[k])
                    {
                            printf("<i,j,k> = <%d,%d,%d>\n",sum_array[c[k]].i,sum_array[c[k]].j,k);
                    }
            }
            return 0;
    }
    My question is how to do it faster ? Any O(N*logN) or better algorithm for this ?

    Regards,
    Arka

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Hint: sorting.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    23
    I have already tried with sorting.Can you elaborate please ?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Find out all possible (i,j,k) such that A[i]+B[j]=C[k].
    You haven't succeeded in this at all.

    You algorithm overrides values so that only one possible match for each `C[K]' is logged.

    Any O(N*logN) or better algorithm for this ?
    You can get better than "O(n*log(n))" without assuming weird constraints which are probably not intended.

    I have already tried with sorting.Can you elaborate please ?
    Show us your version that is based on sorting and searching.

    Soma

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    As phantomotap has indicated, you've put a rather bizarre restriction on the maximum sum. Is that allowed in the specs? Moreover, you aren't even getting the right answer. Implement the naive O(N cubed) algorithm so that you at least know what the correct answer is.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    23
    Thanks all of you for replying.

    Quote Originally Posted by oogabooga View Post
    You haven't succeeded in this at all.

    You algorithm overrides values so that only one possible match for each `C[K]' is logged.
    I can use chaining to overcome this.Can any one please give me some insight for doing it faster than O(N^2).

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As stated above, your ORIGINAL IMPLEMENTATION IS WRONG. Fix it, and provide a less efficient but CORRECT solution and then we can improve the complexity. A merge-sort for example, would only take O(n*log(n)).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    23
    Ok fine.Let me take back my solution as if I have not written the above code.Now can anyone please suggest me how to do the same faster than O(N2) ?

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You've already been given a hint about doing it with less complexity which you've said you already tried and then denied my request for that solution.

    The thing you aren't getting: we aren't here to do your job.

    We all have our own problems. We will be happy to help, but that's pretty much the extent of what we will offer.

    If you want to do this "faster" post a solution that is correct so that we can appreciate that you understand the problem correctly and knowing that can guide you into improving it by sorting the data.

    You may instead post your broken attempt at a sorting based solution and we can guide you to understanding what you did wrong.

    I guess you might catch someone in an extremely good mood to post a complete solution for you, but that is unlikely so actually doing one of the two things above is basically a requirement.

    Soma

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by arka.sharma View Post
    Let me take back my solution
    Quote Originally Posted by arka.sharma View Post
    I have not written the above code.
    I lolled.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    23
    Peoples are upset over here it seems.I didn't mean that and sorry for that.So let's get back to the topic if everything is cool

    @phantomotap
    Show us your version that is based on sorting and searching.
    First I tried to store A[i]+B[j] for all 0<=(i,j)<=n the in an array and then radix sort it and screwed up cause that array itself become of size O(N2).

    For the first mistake that for scenario like
    Code:
    A[]={1,2,0},B[]={3,2,7}
    where different (i,j) combination producing same sum for this case A[0]+B[0] and A[1]+B[1] I will use chaining or probing with some handy hash function.

    Now can anyone suggest me something from this point ??

    Looking forward.....................
    Seriously.............

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Lame bump to a lame thread which contains lame code which is not even yours. What are we talking about? Before you produce any code that is your own, there won't be much help here.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    23
    Were you there when I written the code and posted ????
    Trust me pal that is mine.....
    And I got to tell you, you are not at all helping and it thanks for your kind information that Merge Sort takes O(n*log(n)) ................

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You said it yourself!!! You said your self that you have not written the code, I think due to the age of this thread you are tripping yourself in your own myriad of lies. Bottom line is: no code, no help.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by claudiu View Post
    You said your self that you have not written the code
    I don't think he did (my emphasis):
    Quote Originally Posted by arka.sharma View Post
    Let me take back my solution as if I have not written the above code.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "meeting point" - help with faster algorithm
    By cuber in forum Tech Board
    Replies: 1
    Last Post: 11-01-2011, 08:24 PM
  2. Which is faster?
    By Yarin in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2008, 05:20 PM
  3. What is faster....?
    By Unimatrix_001 in forum C++ Programming
    Replies: 12
    Last Post: 06-15-2003, 09:34 AM
  4. which one is faster....
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 11-10-2002, 01:36 AM
  5. Faster Way?
    By SavesTheDay in forum C Programming
    Replies: 5
    Last Post: 01-23-2002, 06:19 PM