-1

Following is the code which is running as expected in local ide but when i am submitingh the code in codeforces it is displaying that the outputs are wrong.Question is enter link description heredense array

 #include<bits/stdc++.h>
using namespace std;
int main()
{   int t;
    cin>>t;
    while(t--)
    {
    std::vector<int> my;
    int x,i,a,j,bno,sno;
    cin>>x;//enter number of elements
    for(i=0;i<x;i++)
    {
        cin>>a;//enter element
        my.push_back(a);
    }
    for(i=1;i<my.size();i++)
    {
        for(j=i-1;j<my.size();j++)
        {
            bno=(my[j]>my[j+1])?my[j]:my[j+1];
            sno=(my[j]<my[j+1])?my[j]:my[j+1];
            if(bno>(2*sno))
            {
             auto it=my.begin()+j+1;
             my.insert(it,(2*sno));
             break;
            }
        }
    }
    for(i=0;i<my.size();i++)
    {
        if(my.back()==0)
        {
            my.pop_back();
        }
    }
    cout<<(my.size()-x)<<endl;
    }
}
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • 2
    This will access the vector out of bounds: `bno=(my[j]>my[j+1])?my[j]:my[j+1];` on the last iteration of the loop. There are a few other places in your code this happens too. If you use the `at()` function instead of `[]` you'll find the problems because an exception will be thrown. – Retired Ninja Mar 01 '21 at 19:45
  • This is a brute force solution. It may well work, I haven't looked at it particularly closely, but it's likely too slow to pass timing constraints on a competition site. – user4581301 Mar 01 '21 at 19:45
  • @user4581301 yes you are correct my loop was only creating issue and now its fixed than u – Md Ayyan Fahim Mar 01 '21 at 20:00

1 Answers1

-1

Your problem is the following:

#include<bits/stdc++.h>

This is a header-file created by Bjarne Stroustrup(the creator of C++) and is only meant for beginners to use(so that they don't have to pick the headers they need to include). Simply replace it with:

#include <iostream>
#include <vector>

On a related note: You seem to be a total beginner to c++(e.g. You are using using namespace std;, which is bad practice; google it). You really shouldn't be doing competitive programming. Better start with the basics.

Nikita Demodov
  • 553
  • 5
  • 17
  • I don't disagree with your points, but this doesn't answer the question asked. – Retired Ninja Mar 01 '21 at 19:41
  • if you can suggest me some books or course to begin with then that will be a great help to me – Md Ayyan Fahim Mar 01 '21 at 19:41
  • You have confused bits/stdc++.h with std_lib_facilities.h. bits/stdc++.h is a library implementation header from g++ intended to aid in pre-compiled headers. You are correct that it should not be used, but this is true of every header in the bits folder. – user4581301 Mar 01 '21 at 19:41
  • @MdAyyanFahim the C++ community on Stack Overflow maintains [a list of texts and references generally considered to be of high quality](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Mar 01 '21 at 19:43
  • Here's a link that explains how toxic `using namespace std;` can get: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Mar 01 '21 at 19:47