only half of the test cases pass when i traverse through unordered_map, but when i use vector all of them pass, is there any mistake in my code?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n; cin>>n;
unordered_map<int,int> mp;
vector<int> v; v.reserve(n);
while(n--)
{
int x,y; cin>>x>>y;
int h=y+x;
if(mp[h]==0){ v.push_back(h); }
mp[h]++;
}
int s; cin>>s; int sa=0;
for(auto i:mp)
{
int j=i.first+s;
long long u=mp[j]*(i.second);
sa=sa+u;
}
cout<<sa;
}
In this loop:
for(auto i:mp)
{
int j=i.first+s;
long long u=mp[j]*(i.second);
sa=sa+u;
}
you are potentially modifying mp when you do mp[j], in the case that j is not a key in the unordered_map. Modifying a range that you are iterating over in a range-for loop invokes undefined behavior.
You can use find to check if the key j exists and do something else in that case (but not modify the unordered_map).
Related
I am writing a function in c++ which takes a vector array as a parameter and then finds the number of occurrences of the largest value in it.
I have written function birthdaycakecandles and main function as below.
#include <iostream>
using namespace std;
int birthdaycakecandles(vector<int> a)
{
int largest=a[0];
int pos;
for(int i=1;i<a.size();++i)
{
if(largest<a[i])
{
largest=a[i];
pos=i;
}
}
int count=1;
for(int i=0;i<a.size();++i)
{
if(a[i]==largest&&i!=pos)
count++;
}
return count;
}
int main()
{
double n;
cin>>n;
vector<int> a;
for(double i=0;i<n;++i)
cin>>a[i];
int val=birthdaycakecandles(a);
cout<<val;
return 0;
}
Please review the code and suggest me the reason(s) for the error.
The compiler is giving me segmentation error.
Thanks.
Given the code
vector<int> a;
for(double i=0;i<n;++i)
cin>>a[i];
a is empty; access to nonexistent elements as a[i] leads to UB, anything is possible.
You can initialize a containing n elements from the beginning.
size_t n;
cin>>n;
vector<int> a(n);
for(size_t i=0;i<n;++i)
cin>>a[i];
Or use push_back to insert elements into a.
size_t n;
cin>>n;
vector<int> a;
for(size_t i=0;i<n;++i) {
int x;
cin>>x;
a.push_back(x);
}
PS: Using double as the index of vector seems meaningless.
When using vector, we have to add elements using push_back or emplace_back. In order to traverse the vector, make use of vector iterators for exception safety due to out of bound access.
for (vector<int>::iterator it = a.begin() ; it != a.end(); ++it)
{
//your implementation goes here.
cout << *it << endl; //access each element like this
}
Also note that a.end() is in fact the last_element + 1
vector<int> a;
for(double i=0;i<n;++i)
cin>>a[i];
This is not the way to input in vector use a.push_back(i); instead;
and use auto to traverse till a.begin() till a.end()
My code is:
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
struct Interval
{
int init,last;
};
bool compare(Interval a,Interval b)
{
return (a.init < b.init);
}
int main()
{
int t,i,j;
int a[1000];
cin >> t;
for( j=0;j<t;++j){
for(i=0;i<t;i++)
{
cin >> a[i];
}
}
sort(a,a+t,compare);
for( j=0;j<t;++j)
for(i=0;i<t;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
return 0;
}
What is the solution of the below line?
sort(a,a+t,compare);
The problem is here
bool compare(Interval a,Interval b)
{
return (a.init < b.init);
}
compare compares Interval objects
But
int a[1000];
sort(a,a+t,compare);
you are trying to sort an int array.
Either sort an int array or an Interval array, but be consistent. The compare function must match the array that you are sorting.
You are attempting to sort int a[1000]; which is an int array, not an Interval array. If this is really your intention, then you do not need the predicate (compare function) for sort. You can simply use the default operator< that is provided for int. That means you code could just be:
std::sort(std::begin(a), std::begin(a) + t);
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{ vector <int> a,b;
int i,n,m,k;
bool cond=true;
cin>>n;
for(i=0;i<n;i++)
{
cin>>m;
a.push_back(m);
}
for(i=1;i<n-1;i++)
{
if(a.at(i)==a.at(i+1))
a.erase(a.begin()+i);
}
n=a.size();
for(i=0;i<n;i++)
cout<<a[i];
}
return 0;
}
I want to delete all repetitions of consecutive numbers.
This program throws an "out of bounds" error.
Is there any logical error, or could you suggest some better algorithm?
You are accessing out of bounds since after erasing an element your container is now one element shorter than the number of times you are iterating.
In any case; you can solve your problem in two lines if you just use what's already available in the standard library:
auto last = std::unique(a.begin(), a.end());
a.erase(last, a.end());
See also: http://en.cppreference.com/w/cpp/algorithm/unique
Hi I started doing code chef beginner problems and got stuck at this one.I tried reducing the stack memory by declaring the arrays globally but that doesn't work too.Here is my code
#include<iostream>
using namespace std;
#define max 101
int main()
{
int t,n;
cin>>t;
while(t--)
{
int a[max];
int c[max]={0};
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
c[a[j]]++;
}
int temp=0;
int x=0;
for(int k=0;k<n;k++)
{
if(c[k]>temp)
{
temp=c[k];
x=k;
}
}
cout<<x<<" "<<temp<<endl;
}
}
You may need a data structure that does not limit the input value to be bounded, or just change your algorithm.
Either use std::map<int, int> in place of c to count occurence of each number, or just sort a to aggregate same values and count.
I was trying to get first and second element of pair when i am using pair as key in map.For better clarification please see code below.This is what i have tried
#include <bits/stdc++.h>
using namespace std;
int main()
{
// your code goes here
map<pair<int,int>,int>mp;
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i)cin>>a[i];
int y=0;
for(int i=0;i<n;++i)
{
mp.insert(make_pair(y,a[i]));
y=a[i]+1;
}
int m;
cin>>m;
int q[m];
for(int i=0;i<m;++i)cin>>q[i];
for(int i=0;i<m;i++)
{
int temp=q[i];
for(map<pair<int,int>,int>::iterator it=mp.begin();it!=mp.end();++it)
{
if(((it->first)<=temp)&&((it->second)>=temp))
cout<<mp->second<<endl;
}
}
return 0;
}
I want to get first and second element of key here.How can i do that ?
When you iterate over your map, you can get the following items
std::pair<int, int> key = it->first;
int value = it->second;
Therefore the first and second value of the key would be
it->first.first;
it->first.second;