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);
}
#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
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]);
}
Your while co dition is broken. You must continue until both indexes are at the end. Currently you stop when one of the indexes has reached the end.
When you fix that, you also must ensure that you don't increase i beyond n1; same for j and n2.
While you are developing you should rather use vector.at(i) instead of vector[i] to enable safety checks.
Related
The description of a task goes like this:
We have n numbers, and we have to find quantity of unique sums of all the pairs in the array.
For example:
3 2 5 6 3
The sums of all the pairs(non-repeated) are 5 9 8 6 8 7 5 11 9 8
Unique are 5 9 8 6 7 11
Therefore output is 6
I have come up with this really primitive, and time-consuming (meaning complexity) solution:
int n = 0;
cin >> n;
vector<int> vec(n);
for (int i = 0; i < n; i++)
{
cin >> vec[i];
}
vector<int> sum;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
sum.push_back(vec[i] + vec[j]);
}
}
sort(sum.begin(), sum.end());
for (int i = 0; i < sum.size()-1;)
{
if (sum[i] == sum[i + 1]) sum.erase(sum.begin() + i);
else i++;
}
cout << endl << sum.size();
I feel like there could be a solution using Combinatorics or something easier. I have thought a lot and couldn't think of anything. So my request is if anyone can improve the solution.
As mentioned above what you need it is difficult to do this without computing the sum of all pairs, so I am not going to handle that, I am just going to advise about efficient data structures.
Analysis of your solution
Your code adds everything in advance O(n^2) then sorts O(n^2 log(n)), then remove duplicates. But since you are erasing from a vector, that ultimately has complexity linear with the number of elements to the end of the list. It means that the second loop will make the complexity of your algorithm O(n^4).
You can count the unique elements in a sorted array without removing
int count = 0;
for (int i = 0; i < sum.size()-1; ++i)
{
if (sum[i] != sum[i + 1]) ++count
}
This change alone makes your algorithm complexity O(n^2 log n).
Alternatives without sorting.
Here are alternatives that O(n^2) and storage depending on the range of the input values instead of the length of the vector (except for the last).
I am testing with 1000 elements smaller between 0 and 10000
vector<int> vec;
for(int i = 0; i < 1000; ++i){
vec.push_back(rand() % 10000);
}
Your implementation sum_pairs1(vec) (18 seconds)
int sum_pairs1(const vector<int> &vec){
vector<int> sum;
int n = vec.size();
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
sum.push_back(vec[i] + vec[j]);
}
}
sort(sum.begin(), sum.end());
for (int i = 0; i < sum.size()-1;)
{
if (sum[i] == sum[i + 1]) sum.erase(sum.begin() + i);
else i++;
}
return sum.size();
}
If you know the range for the sum of the values you can use a bitset, efficient use of memory sum_pairs2<20000>(vec) (0.016 second).
template<size_t N>
int sum_pairs2(const vector<int> &vec){
bitset<N> seen;
int n = vec.size();
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
seen[vec[i] + vec[j]] = true;
}
}
return seen.count();
}
If you know that the maximum sum is not so high (the vector is not very sparse), but you don't know at compilation time you can use a vector, you can keep track of minimum and maximum to allocate the minimum possible and also supporting negative values.
int sum_pairs2b(const vector<int> &vec){
int VMAX = vec[0];
int VMIN = vec[0]
for(auto v : vec){
if(VMAX < v) VMAX = v;
else if(VMIN > v) VMIN = v;
}
vector<bool> seen(2*(VMAX - VMIN) + 1);
int n = vec.size();
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
seen[vec[i] + vec[j] - 2*VMIN] = true;
}
}
int count = 0;
for(auto c : seen){
if(c) ++count;
}
return count;
}
And If you want a more general solution that works well with sparse data sum_pairs3<int>(vec) (0.097 second)
template<typename T>
int sum_pairs3(const vector<T> &vec){
unordered_set<T> seen;
int n = vec.size();
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
seen.insert(vec[i] + vec[j]);
}
}
return seen.size();
}
I'm trying to compare elements of the "coordinateList" vector with elements of "Buttons" vector and if four different if-statements are okay to pass them, increase 1 in "countList" vector. But, the problem is that whenever I input 2 numbers for "coordinateList" which can't pass the if-statement, it gets "vector subscript out of range" on the part;
if ((coordinateList[j][0] >= Buttons[i][0])
&& (coordinateList[j][0] <= Buttons[i][1])
&& (coordinateList[j][1] >= Buttons[i][2])
&& (coordinateList[j][1] <= Buttons[i][3]))
I don't know how to fix it.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numberOfButtons;
int numberOfClicks;
cin >> numberOfButtons;
cin >> numberOfClicks;
int buttonCoordinate;
vector <vector<int> > Buttons;
for (unsigned int i = 0; i < numberOfButtons; i++) {
vector<int> oneButtonCoordinate;
for (unsigned int j = 0; j < 4; j++) {
cin >> buttonCoordinate;
oneButtonCoordinate.push_back(buttonCoordinate);
}
Buttons.push_back(oneButtonCoordinate);
}
int XYCoordinate;
vector <vector<int> > coordinateList;
vector <int> clickCount;
for (unsigned int i = 0; i < numberOfClicks; i++) {
vector<int> oneClickCoordinate;
for (unsigned int j = 0; j < 2; j++) {
cin >> XYCoordinate;
oneClickCoordinate.push_back(XYCoordinate);
}
coordinateList.push_back(oneClickCoordinate);
}
for (unsigned int i = 0; i < numberOfButtons; i++) {
clickCount.push_back(0);
}
for (unsigned int j = 0; j < numberOfClicks; j++) {
for (unsigned int i = Buttons.size() - 1; i >= 0; i--) {
if ((coordinateList[j][0] >= Buttons[i][0]) && (coordinateList[j][0] <= Buttons[i][1]) && (coordinateList[j][1] >= Buttons[i][2]) && (coordinateList[j][1] <= Buttons[i][3])) {
clickCount.at(i) += 1;
break;
}
}
}
(skip)
return 0;
}
Example of correct Input and Output
Input
2 5
1 5 1 5
3 8 3 8
1 1
3 3
3 5
8 8
3 10
Output
Button: #1: 1
Button: #2: 3
so it is the line
for (unsigned int i = Buttons.size() - 1; i >= 0; i--)
which ends up in an invalid index (0xFFFFFFFF still >= 0!).
Use instead:
for (int i = (int) Buttons.size() - 1; i >= 0; i--)
Remove the rows where a value is encountered twice from the matrix. Where's the bug in my code that I've already written, so that I do it without using any function or anything that would make it easier?
My code:
#include <iostream>
using namespace std;
int main()
{
int v[99][99], m, n, i, j, k, vlinie[99], a=0;
cout<<"m="; cin>>m;
cout<<"n="; cin>>n;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
cin>>v[i][j];
//finding out each row that has doubles and remembering its index in an array called vlinie
for(i=1; i<=m; i++)
for(j=1; j<=n-1; j++)
for(k=j+1; k<=n; k++)
if(v[i][j]==v[i][k])
{
a++; //a is the number of numbers in vlinie
vlinie[a]=i;
}
//removing duplicates from vlinie, in case there are any
for(i=1; i<=a-1; i++)
for(j=i+1; j<=a; j++)
if(vlinie[i]==vlinie[j])
for(k=i; k<=m; k++)
{
vlinie[k]=vlinie[k+1];
a--;
}
//this is where we move the rows around, and supposedly the line where i got it wrong, so what do i change in this line?
for(i=1; i<=a; i++)
for(j=1; j<=n; j++)
for(k=vlinie[i]; k<=m; k++)
v[vlinie[i]+k][j]=v[vlinie[i]+k+1][j];
for(i=1; i<=m; i++)
{
cout<<endl;
for(j=1; j<=n; j++)
cout<<v[i][j]<<" ";
}
return 0;
}
Example:
1 2 2 3
4 5 6 7
7 7 8 9
7 6 5 4
What it should output:
4 5 6 7
7 6 5 4
0 0 0 0
0 0 0 0 (without the zeros theoretically but the way i wrote it leaves it like this, for now)
What it outputs instead:
1 2 2 3
7 7 8 9
5 4 3 2 (it didn't delete the lines that it should delete and instead deleted some line it should keep)
How do I fix this?
PS: yes, i know i shouldn't have started my indexing at 1 instead of 0, no need to remind me about it again
Your problem was at these lines :
for(i=1; i<=a; i++)
for(j=1; j<=n; j++)
for(k=vlinie[i]; k<=m; k++)
v[vlinie[i]+k][j]=v[vlinie[i]+k+1][j];
There is a first issue with the indices: we should copy v[k+1][] to v[k][].
The second issue is that once a first move has been performed, the table vlinie is no longer valid.
This last point is not so easy to correct.
Moreover, for each suppressed row, you are implementing a shift of several rows, which leads to a poor efficiency.
It is simpler and more efficient to memorize which rows are suppressed (suppress[]) and from this table, to determine which row of the input matrix will end at the ith row of the final matrix (table posi[]).
Hereafter is an example of implementation.
#include <iostream>
int main()
{
int v[99][99], m, n, i, j, k, posi[99];
bool suppress[99];
std::cout << "m = "; std::cin >> m;
std::cout << "n = "; std::cin >> n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
std::cin >> v[i][j];
//finding out each row that has doubles
for (i = 0; i < m; i++) {
suppress[i] = false;
for (j = 0; (j < n-1) && !suppress[i]; j++)
for (k = j+1; (k < n) && !suppress[i]; k++)
suppress[i] = (v[i][j] == v[i][k]);
}
// determination of the moves to be performed
int n_keep = 0;
for (i = 0; i < m; i++) {
if (!suppress[i])
posi[n_keep++] = i;
}
//this is where we move the rows around
for (i = 0; i < n_keep; i++)
for (j = 0; j < n; j++)
v[i][j] = v[posi[i]][j];
// Set to 0 remaining rows
for (i = n_keep; i < m; i++)
for (j = 0; j < n; j++)
v[i][j] = 0;
for(i = 0; i < m; i++)
{
std::cout << "\n";
for(j = 0; j < n; j++)
std::cout << v[i][j] << " ";
}
return 0;
}
I am trying to fill an array of 52 with the numbers 0 - 12. Once it hits 12, it needs to go back to 0 - 12 again. You might have already guessed it's a deck of cards. My code is below and doesn't work. It prints 0 - 12 one time, but then prints the address of the array I believe for the remainder of the iterations left.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int myArray[52];
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i] = i;
}
}
for (int k = 0; k < 52; k++)
{
cout << myArray[k] << endl;
}
//system("pause");
return 0;
}
Can someone please help me with this brain fart?
int myints[52];
for (int idx = 0; idx < 52; idx++)
{
myints[idx] = idx % 13;
}
Modulus of 13 will range from 0 to 12.
You're indexing the same first 12 elements of the array in the inner loop for every iteration of the outer loop.
Try changing it to something like this
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i + 13 * j] = i;
}
}
#include <iostream>
using namespace std;
void print_array(int array[], int size)
{
cout<< "insertion sort steps: ";
int j;
for (j=0; j<size;j++)
cout <<" "<< array[j];
cout << endl;
}
void insertion_sort(int a[], int n)
{
int i;
for(int j = 1; j < n; j++) {
i = 0;
while ((a[j] > a[i])) {
i = i+1;
}
int m = a[j];
for(int k = 0; k <= (j-i-1); k++) {
a[j-k] = a[j-k-1];
}
a[i] = m;
print_array(a,n);
}
}
int main() {
int array[6]= {3,2,4,5,1,6};
insertion_sort(array,6);
return 0;
}
I am trying to modify this the insertion sort that it uses a linear search technique that inserts the jth element in the correct place by first comparing it with the (j − 1)st element, then the (j − 2)th element if necessary, and so on.
So where it says i = 0; it should now be i = j-1;
My attempt:
void insertion_sort(int a[], int n)
{
int i;
for(int j = 1; j < n; j++) {
i = j-1;
while ((a[j] > a[i]) && (i > 0)) {
i = i-1;
}
int m = a[j];
for(int k = (j-i-1); k >= 0; k--) {
a[j-k] = a[j-k-1];
}
a[i] = m;
print_array(a,n);
}
}
Here is the output
insertion sort steps: 2 3 4 5 1 6
insertion sort steps: 4 2 2 5 1 6
insertion sort steps: 5 4 4 4 1 6
insertion sort steps: 5 4 4 1 4 6
insertion sort steps: 6 5 5 5 5 5
The first step is works correct by the 2nd step is when it all starts to fail.
The
while ((a[j] > a[i]) && (i > 0)) {
i = i-1;
}
is wrong, because it causes insertion of a[j] at index 0 if a[j] were not to be moved. Change that to
while (0 <= i && a[j] < a[i]) --i;
++i;
Also, the shifting loop is wrong and complicated - change it to
int m = a[j];
for (int k = j; k > i; k--) a[k] = a[k-1];
a[i] = m;