Thread: |C| Question about finding equal values in array

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    28

    |C| Question about finding equal values in array

    hello,

    how i can find two equal int in array with O(n) time complexityand O(1) place complexity?

    thanks,

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    You're gonna afta give a bit more information then O(n), that name alone means nothing to everyone but you in this context. Try reading your question without context whatsoever and you'll understand what everyone else is reading when they read your question.

  3. #3
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by awsdert View Post
    You're gonna afta give a bit more information then O(n), that name alone means nothing to everyone but you in this context. Try reading your question without context whatsoever and you'll understand what everyone else is reading when they read your question.
    What are you talking about, O(n) means linear and O(1) means constant.
    Last edited by Jimmy; 02-03-2015 at 05:36 PM.

  4. #4
    Registered User
    Join Date
    Nov 2014
    Posts
    28
    Quote Originally Posted by awsdert View Post
    You're gonna afta give a bit more information then O(n), that name alone means nothing to everyone but you in this context. Try reading your question without context whatsoever and you'll understand what everyone else is reading when they read your question.
    Quote Originally Posted by Jimmy View Post
    What are you talking about, O(n) means linear and O(1) means constant.
    the general question is to build a function that get array and size, and i need to check if there two variable that their %10 are equal.
    its not a homework question.
    i have one answer that they use a counter array but i try to find a diffrent way.

    ***
    yeah i meant O(n) linear and O(1) means constant.
    Last edited by vibex; 02-03-2015 at 05:38 PM.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    That might be clear to you Jimmy but wasn't to me but thank you for clearing it up for me.
    Well here's my crack at it:
    Code:
    int func( int *linearv, int linearc, int *constv, int constc )
    {
      int end = (linearc > constc) ? constc : linearc;
      for( int i = 0; i < end; ++i )
      {
        if ( (linearv[i] % 10) == (constv[i] % 10) )
          return i;
      }
      return -1;
    }

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    By the way does the index have to match or was that an incorrect assumption of mine?

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by vibex View Post
    find two equal int in array with O(n) time complexity and O(1) place complexity?
    If the array (of n ints) is sorted, then the two are consecutive.

    Are there are limitations or requirements or specifications you forgot to mention about the array or the values?

    Edit: Ah, so you're not looking for equal integers, but integers with equal least significant decimal digits.

    Use the counting phase of a bucket sort to count the number of values in the array with each possible least significant decimal digit (of which there are 10). If you wish to return the identities of the first matching pair, just use another ten buckets to store the index to the first value bucketed, and abort as soon as you find a duplicate (so you have the index to the second one, too).
    Last edited by Nominal Animal; 02-03-2015 at 06:35 PM.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by vibex View Post
    the general question is to build a function that get array and size, and i need to check if there two variable that their %10 are equal.
    its not a homework question.
    i have one answer that they use a counter array but i try to find a diffrent way.

    ***
    yeah i meant O(n) linear and O(1) means constant.
    Is it perhaps valid to say "that you need a function which takes an array and size, in which, if its two elements within the array has its %10 equal return turn else false?"

    Not sure what the function should return

    1. Do you want to just boolean verdict?
    2. Do you want the index of those two elemenst if found?
    3. Or Do you want the element values itself if found?

    or

    return nothing if not found (false or null (if 2/3 is choosen).
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't think this is possible: if you do an in-place sort, you will have O(1) space but O(n log n) time complexity. If you count, you could achieve O(n) time complexity by using a lookup table as in counting sort, but your space complexity would depend on the range of the input, unless you accept O(n) average but not worst case complexity with a hash table.

    ... except that you mentioned "check if there two variable that their %10 are equal". Since there are 10 possibilities, you only need a lookup table of 10 elements, hence you end up with O(1) space complexity (excluding the input itself) and O(n) time complexity in the worst case.

    Quote Originally Posted by vibex
    its not a homework question.
    An interview question? It doesn't really matter: it is effectively a homework question.

    Quote Originally Posted by vibex
    i have one answer that they use a counter array but i try to find a diffrent way.
    This would probably be what I described earlier about counting. Why do you want to find a different way? Are you sure a different way exists, or are you just tinkering with some alternative idea? If so, it seems rather strange that you neither mentioned this solution in your original post, nor do you mention your alternative idea now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding an array of unique values within a 2D array
    By saadashfaq in forum C Programming
    Replies: 6
    Last Post: 11-14-2011, 12:02 AM
  2. Question about array values
    By elliptic in forum C Programming
    Replies: 4
    Last Post: 06-15-2006, 12:55 PM
  3. quick question about storing values in an array
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-10-2004, 01:33 PM
  4. Pointers equal non--&-ed values
    By C++Child in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2004, 07:06 PM
  5. finding max values in an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 02:47 PM