Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the Intersection between two Vectors

And I'm doing pretty good in fact I found the intersection and think I have the right code. Only problem is that it doesn't seem to print out the last value.

So if I have two sets:

9 12 7 8 1 19 11 2 14

15 10 8 2 5 16 14 7 19 0 11 3 13 18 9 17 1 12

My code will produce the following output:

1
2
7
8
9
11
12
14

But the right intersection of the sets should be:

1
2
7
8
9
11
12
14
19

So, my code doesn't print out the last value and I can't find out why.

void findIntersection(vector<int> A, vector<int> B)
{
    vector<int> intersection;
    int n1 = A.size();
    int n2 = B.size();
    int i = 0, j =0;
    while(i <= n1 && j <= n2)
    {
        if(A[i] > B[j])
        {
            j++;
        }
        else if( B[j] > A[i])
        {
            i++;
        }
        else
        {
            intersection.push_back(A[i]);
            i++;
            j++;
        }
    }

    for(unsigned int i = 0; i <= intersection.size(); i++)
    {
        cout << intersection[i] << endl;
    }
}

void slowintersect(vector<vector<int> > v)
{
    vector<int> vec;
    vector<int> c;
    int store_0;

    int row = 0;
    for(size_t j =0; j < v.at(row).size(); j++)
    {
        store_0 = v[row][j];
        c.push_back(store_0);
    }

    for(size_t i = 0; i < v.size(); i++)
    {
        for(size_t k = 0; k < v.at(i).size(); k++)
        {
            vec.push_back(k);
        }
    }

    findIntersection(c, vec);
}
like image 286
user2757849 Avatar asked Mar 25 '26 11:03

user2757849


2 Answers

#include <algorithm>
#include <set>

set<int> intersect;
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(), // sorted!
                  std::inserter(intersect,intersect.begin()));

example

if you don't want std::algorithm, there are several errors in your code. i.e:

for(unsigned int i = 0; i <= intersection.size(); i++)
                           ^
                        should be <

here is an important error:

int i = 0, j =0;
while(i <= n1 && j <= n2)
{        ^          ^
         <          <
    if(A[i] > B[j])
    {
        j++;

you will skip all n-1 A elements if A[0] is greater than each B element, and end the loop

like image 100
4pie0 Avatar answered Mar 27 '26 01:03

4pie0


I'm assuming you do not want to use the C++ standard algorithm.

Couple of things.
1. Your algorithm shall work only if both vectors are initially sorted. Are they?
2. You shouldn't access vector[vector.size()] element as it is out of bounds.

My second point means:

while(i <= n1 && j <= n2)

Change this to

while(i < n1 && j < n2)

And change the following

for(unsigned int i = 0; i <= intersection.size(); i++)

to

for(unsigned int i = 0; i < intersection.size(); i++)

Also, MOST IMPORTANT ERROR!!!!

for(size_t k = 0; k < v.at(i).size(); k++)
            {
                vec.push_back(k);

            }

Change it to:

for(size_t k = 0; k < v[i].size(); k++)
            {
                vec.push_back(v[i][k]);

            }
like image 37
Abhishek Bansal Avatar answered Mar 27 '26 02:03

Abhishek Bansal