checking if 2 numbers of array add up to I

后端 未结 15 2612
日久生厌
日久生厌 2020-12-15 01:13

I saw a interview question as follows: Give an unsorted array of integers A and and an integer I, find out if any two members of A add up to I.

any clues?

ti

相关标签:
15条回答
  • 2020-12-15 01:41

    An implementation in python

    def func(list,k):
    temp={} ## temporary dictionary
    for i in range(len(list)):
        if(list[i] in temp): ## if temp already has the key just increment its value
            temp[list[i]] +=1
        else:  ## else initialize the key in temp with count as 0
            temp[list[i]]=0 
    
        if(k-list[i] in temp and ((k/2 != list[i]) or temp[list[i]]>=1)): ## if the corresponding other value to make the sum k is in the dictionary and its either not k/2 or the count for that number is more than 1
            return True
    
    return False
    

    Input: list is a list of numbers (A in the question above)...
    k is the sum (I in the question above)....

    The function outputs True if there exist a pair in the list whose sum is equal to k and False otherwise...

    I am using a dictionary whose key is the element in the array(list) and value is the count of that element(number of times that element is present in that list). Average running time complexity is O(n).

    This implementation also takes care of two important edge cases:

    • repeated numbers in the list and
    • not adding the same number twice.
    0 讨论(0)
  • 2020-12-15 01:44

    for nlogn : Sort the array and for each element [0<=j<len A] , subtract i-A[j] and do a binary search for this element in sorted array.

    hashmap (frequency of no, number) should work in O(n).

    0 讨论(0)
  • 2020-12-15 01:45
    for each ele in the array
      if (sum - ele) is hashed and hashed value is not equal to index of ele
        print ele, sum-ele
      end-if
      Hash ele as key and index as value
    end-for
    
    0 讨论(0)
  • 2020-12-15 01:51

    Here is a solution witch takes into account duplicate entries. It is written in javascript and assumes array is sorted. The solution runs in O(n) time and does not use any extra memory aside from variable. Choose a sorting algorithm of choice. (radix O(kn)!) and then run the array through this baby.

    var count_pairs = function(_arr,x) {
      if(!x) x = 0;
      var pairs = 0;
      var i = 0;
      var k = _arr.length-1;
      if((k+1)<2) return pairs;
      var halfX = x/2; 
      while(i<k) {
        var curK = _arr[k];
        var curI = _arr[i];
        var pairsThisLoop = 0;
        if(curK+curI==x) {
          // if midpoint and equal find combinations
          if(curK==curI) {
            var comb = 1;
            while(--k>=i) pairs+=(comb++);
            break;
          }
          // count pair and k duplicates
          pairsThisLoop++;
          while(_arr[--k]==curK) pairsThisLoop++;
          // add k side pairs to running total for every i side pair found
          pairs+=pairsThisLoop;
          while(_arr[++i]==curI) pairs+=pairsThisLoop;
        } else {
          // if we are at a mid point
          if(curK==curI) break;
          var distK = Math.abs(halfX-curK);
          var distI = Math.abs(halfX-curI);
          if(distI > distK) while(_arr[++i]==curI);
          else while(_arr[--k]==curK);
        }
      }
      return pairs;
    }
    

    I solved this during an interview for a large corporation. They took it but not me. So here it is for everyone.

    Start at both side of the array and slowly work your way inwards making sure to count duplicates if they exist.

    It only counts pairs but can be reworked to

    • find the pairs
    • find pairs < x
    • find pairs > x

    Enjoy and don't forget to bump if its the best solution!

    0 讨论(0)
  • 2020-12-15 01:53

    Split the array into two groups <= I/2 and > I/2. Then split those into <= I/4,>I/4 and <= 3I/4,>3I/4 And repeat for log(I) steps and check the pairs joining from the outside e.g 1I/8<= and >7I/8 and if they both contain at least one element then they add to I. This will take n.Log(I) + n/2 steps and for I

    0 讨论(0)
  • 2020-12-15 01:54

    If you have the range which the integers are within, you can use a counting sort-like solution where you scan over the array and count an array up. Ex you have the integers

    input = [0,1,5,2,6,4,2]
    

    And you create an array like this:

    count = int[7]
    

    which (in Java,C# etc.) are suited for counting integers between 0 and 6.

    foreach integer in input
        count[i] = count[i] + 1
    

    This will give you the array [1,1,2,0,1,1,1]. Now you can scan over this array (half of it) and check whether there are integers which adds up to i like

    for j = 0 to count.length - 1
        if count[j] != 0 and count[i - j] != 0 then // Check for array out-of-bounds here
             WUHUU! the integers j and i - j adds up
    

    Overall this algorithm gives you O(n + k) where n is from the scan over the input of length n and k is the scan over the count array of length k (integers between 0 and k - 1). This means that if n > k then you have a guaranteed O(n) solution.

    0 讨论(0)
提交回复
热议问题