Whats Wrong in it? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Problem: https://www.hackerrank.com/challenges/migratory-birds/problem
Test Case Link: https://hr-testcases-us-east-1.s3.amazonaws.com/33294/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1573162301&Signature=MgpSHa3lxX%2FwYwumjzAmF8uviZE%3D&response-content-type=text%2Fplain
for this test case i cant get any output why ?
Thanks in advance
#include<iostream>
using namespace std;
int main()
{
long long n,i=0,num=0,mx=0,r=0;
cin>>n;
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
for(int i=1;i<6;i++)
{
if(arr[i]>mx)
{
mx=arr[i];
r=i;
}
}
cout<<r;
}

This looks problematic:
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
If the input value read into num is greater than 5 (or less than zero), undefined behavior will exist when a value is written into an invalid memory location offset from arr.

There is nothing wrong with you code i got accepted all test cases. But the solution can be return in more optimized way

Related

Priority Queue not printing the values pushed into it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I just started Data Structures and i have stumbled upon Priority queues, I wrote a simple program to print the values that i have pushed in the Queues but it wont print anything.
#include<iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> maxi;
priority_queue<int, vector<int>, greater<int>> mini;
int m = maxi.size();
int n = mini.size();
maxi.push(1);
maxi.push(3);
maxi.push(2);
maxi.push(5);
maxi.push(0);
cout<<"Max size->"<<maxi.size()<<endl;
for(int i=0; i<m; i++){
cout<<maxi.top()<<" ";
maxi.pop();
}cout<<endl;
mini.push(1);
mini.push(3);
mini.push(2);
mini.push(5);
mini.push(0);
cout<<"Mini size->"<<mini.size()<<endl;
for(int j=0; j<n; j++){
cout<<mini.top()<<" ";
mini.pop();
}cout<<endl;
}
I have read so many articles i just dont seem to find any error. Even the Compiler doesnt give any error.
You are getting the size of maxi and mini before adding elements to them. Since you're copying the value of the size (explained here), it wouldn't be updated when adding elements to the containers. Therefore your loop is not executed at all.

Unordered_set not working on codechef ide [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
"unordered_set" is not working properly on codechef and giving wrong output on its online ide whereas I am getting right output on geeksforgeeks ide and codeblocks
for Input like 3 2 10 1 100 4 3 i am getting 4 rows as expected in codeblocks and geeksforgeeks because n+m-1 is 4 whereas I am getting only 2 rows in codechef what can be the reason behind and now how it will work on codechef?
#include<stdio.h>
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
int main()
{
int n,m,c=0,d,i,j,sum;
int a[10000];
int b[10000];
unordered_set <int> s;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
sum=a[i]+b[j];
if(s.find(sum)==s.end())
{
s.insert(sum);
printf("%d %d\n",i,j);
c++;
}
if(c>=(n+m-1))
{d=1;break;}
}
if(d==1)
break;
}
}
Your program exhibits undefined behavior, by way of accessing the value of an uninitialized variable d.

What is the difference between the two codes in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

What's the difference between the given two codes. One gives time limit exceeded when run on ideone and the other works fine [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
1st code: works fine gives success with time of 0sec
int main()
{
int n=100000;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{}
cout<<"ffdfdf";
}
2nd code: gives a time limit exceeded
int main()
{
int n=100000;
bool **a=new bool*[n];
for(int i=0;i<n;i++)
{
bool[i]=new bool[n];
for(int j=0;j<n;j++)
{
bool[i][j]=1;
}
}
cout<<"ffdfdf";
}
can anyone explain why the two code fragments have a vast time difference.I am not understanding it.
bool[i]=new bool[n]; is extremely expensive cf. the other statements.
A good compiler will optimise out your first program to cout << "ffdfdf";, since it will know that the loop doesn't do anything.
Once you've replaced your errant bools with as so the second snippet actually compiles, you'd be advised to pair your new[] calls with delete[].

Pseudo- andom number generation error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am the very beginner of the developing field, and I am facing a problem in Pseudo code random error in C++ language.
I think the Error in my program is:
{
srand(time (NULL));
}
Please help me how can I remove this error and the reason of error.
The program I developed is below,
#include<iostream>
using namespace std;
main()
{
int lowerRange;
int upperRange;
lowerRange=1;
upperRange=1;
int secretNumber;
int guess;
guess=10;
cout<<"My Student ID is BC130400789 "<<endl;
cout<<"Enter lower range : ";
cin>>lowerRange;
cout<<"Enter upper range : ";
cin>>upperRange;
cout<<"Computer is calculating a random secret number in the given range...Done!"<<endl;
cout<<"\nPlease guesss the secret number in the range ["<<lowerRange<<" - "<<upperRange<<"]: ";
cin>>guess;
if(guess<10)
{
cout<<"You won! You guess the correct number.. ";
}
else
{
cout<<"Oooppsss...Your entered number is too high...Computer won"<<endl<<endl;
}
{ srand(time (NULL));}
secretNumber = rand()%10+1;
cout<<"Secret number was: "<<secretNumber<<endl<<endl;
}
system("pause");
}
main HAS TO return int
rand() is defined in cstdlib which you don't include
You also need to include ctime for time function
As said by G. Samaras you shouldn't use system("pause") (also you don't include required header for it) and you have mismatched {}.
I would also recommend you reading something about code formatting.
You have an extra bracket at the end. Remove this and the system("pause"); and you will get no errors. Also main() usually returns an int, thus make it int main().
system(“pause”); - Why is it wrong?