Differing pairs

Given an array of unique randomly sorted integers and a number k k it is required to find the count of the unordered pairs in the array that have a difference of k k . In the text file, how many unorederd pairs of numbers have a difference of 70.

Details and Assumptions

As an example in the array [2, 8, 4, 3, 1] with k = 2 k=2 is 2 2 .... (2,4) and (3,1) .


The answer is 659.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

6 solutions

Thaddeus Abiy
Aug 12, 2015

Here is a Θ ( n ) \Theta(n) solution:

  • Put the numbers in the list in a Hash-table

  • Go through each number i i in the list

  • Check if i + 70 i+70 is in the Hashtable and increment a counter variable if it is. We know that despite collisions this takes O ( 1 ) O(1) via amortized analysis.

Below is a python implementation via a set object which is a hash-table.

1
2
3
4
with open('L1JKzVE9.txt','rb') as numbers:
    exec('List = set( '+numbers.read() + ')' ) #Extracting the numbers from the file

print len([ i for i in List if (i+70) in List])


Moderator note:

Good approach!

well im new to this but i was wondering whats "unorederd pairs "? well it also says in the" text file" but it doesnt show this text file isnt text file needed to solven the equation ?

Rainbow Hard - 5 years, 7 months ago

Excellent solution!

Márcio Gomes - 5 years, 5 months ago
Abdeslem Smahi
Aug 11, 2015
1
2
3
4
5
6
7
8
9
s= text
def different(x):
    count = 0
    for i in x:
        for k in x:
            if i - k == 70:
                count = count + 1
    return count
print different(s)

This is my first try solving computer science problem any suggest please :)

Well the solution is O ( n 2 ) O(n^2) and could be made more efficient...

Beakal Tiliksew - 5 years, 10 months ago

Log in to reply

i didn't get what you mean , sorry this the second day learning python.

Abdeslem Smahi - 5 years, 10 months ago

Log in to reply

I am basically talking about the run time of the algorithm, you can learn more about it here . For large amount of items your algorithm will take longer to yield an answer.

Beakal Tiliksew - 5 years, 10 months ago

Log in to reply

@Beakal Tiliksew Ah , it is clear now , thank you :)

Abdeslem Smahi - 5 years, 10 months ago
Hossain Nahdi
Jun 6, 2021

Brute Force Approach: O ( n 2 ) O(n^{2})

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int dups(vector<int>& a, int k) {
    int cnt = 0;
    for(int i = 0; i < a.size()-1;i ++) {
        for(int j = i+1; j < a.size(); j++) {
            if(abs(a[j]-a[i]) == 70)
                cnt++;
        }
    }
    return cnt;
}

Two Pointers Approach: O ( n ) O(n)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
int dups(vector<int>& a, int k) {
    sort(a.begin(), a.end());

    int i = 0, j = 1;
    int cnt = 0;
    while(j < a.size()) {
        int n = abs(a[j] - a[i]);
        if(n == k) {
            cnt++;
            if(j < a.size()-1 && a[j] == a[j+1])
                j++;
            else if(i < a.size()-1 && a[i] == a[i+1])
                i++;
            else 
                i++;
        } else if(n > k) 
            i++;
        else 
            j++;
    }
    return cnt;
}

Arulx Z
Aug 22, 2015

Here's a trivial Θ ( n 2 ) \Theta \left( { n }^{ 2 } \right) solution. I am working on improving my solution.

1
2
3
4
5
6
>>> n = [that array]
>>> count = 0
>>> for x in n:
        for y in n:
            count += x - y == 70
>>> print count

Surprising thing is that my solution still runs in a reasonable amount of time on my machine (about 6.92 secs).

By the way, isn't the question overrated? (400 points! whoa!)

7 seconds isn't actually reasonable considering it can be solved in a tenth of a second, and i agree the problem is overrated. Surprisingly this problem solving rate is 2%

Beakal Tiliksew - 5 years, 9 months ago

Log in to reply

Yes. You're right. I'll try to improve my solution.

Arulx Z - 5 years, 9 months ago
Bill Bell
Aug 13, 2015

I haven't done any timings or measurements of memory use; it's worth remembering though that building actual lists and sets (rather than using generators) takes time and space too.

And eval is naughty. ;)

1
2
3
4
5
6
numbers=[82048, ..., 85637]
s=(1 for i in numbers for j in numbers if i-j ==70)
count=0
for k in s:
    count+=k
print count

Brock Brown
Aug 11, 2015

Python 3.4:

1
2
3
4
5
6
7
8
9
# convert text to array of integers
numbers = eval(open("numbers.txt", "r").read())
# count pairs with difference of 70
count = 0
for a in numbers:
    for b in numbers:
        if a - b == 70:
            count += 1
print ("Answer:", count)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...