title: Code running fine on my system but segmentation fault on HackerRank submission
body: <p>I'm doing a coding problem on hackerrank and my code is running successfully on my system but giving segmentation fault while submitting the solution. Please help me out. Have stuck for hours in it. But not able to find the issue.</p>
  
  <blockquote>
    <p>HackerRank Question:
    <a href="https://www.hackerrank.com/challenges/torque-and-development" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/torque-and-development</a></p>
  </blockquote>
  
  <p>This is my code:</p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  
  using namespace std;
  
  int n,m,cRoad,cLib;
  
  void initialize(bool visited[])
  {
      int i ;
      for(i=0;i&lt;=n;i++)
      {
          visited[i] = false ;
      }
  }
  
  void  dfs(vector &lt;int&gt; arr[],bool visited[],int node,int &amp;numOfNodes)
  {
      int i,j;
      for(i=0;i&lt;arr[node].size();i++)
      {
          if(visited[arr[node][i] ] == false )
          {
              visited[arr[node][i] ] = true ;
              dfs(arr,visited,arr[node][i],numOfNodes);
          }
  
      }
      numOfNodes ++ ;
  }
  
  int minCost(vector &lt;int&gt; arr[],bool visited[])
  {
      int cost = 0;
      int i , connectedComponents =0;
      if(cLib &lt; cRoad)
          return (n * cLib);
      else
      {
          for(i=1;i&lt;=n;i++)
          {
            int numOfNodes = 0 ;
  
            if(visited[i]==false)
            {
                dfs(arr,visited,i,numOfNodes);
                connectedComponents++;
                cost += (numOfNodes - 1 ) * cRoad + cLib ;
            }
          }
          return cost ;
      }
  }
  
  int main()
  {
    int q,u,v,i,j;
    scanf("%d",&amp;q);
  
    while(q--)
    {
        scanf("%d %d %d %d",&amp;n,&amp;m,&amp;cLib ,&amp;cRoad);
        vector &lt;int&gt; arr[n];
        bool visited[n];
        initialize(visited);
  
        for(i=0;i&lt;m;i++)
        {
            scanf("%d %d",&amp;u,&amp;v);
            arr[u].push_back(v);
            arr[v].push_back(u);
        }
        cout&lt;&lt;minCost(arr,visited);
    }
  }
  </code></pre>
  
  <p>Sample Input: </p>
  
  <pre><code>2
  3 3 2 1
  1 2
  3 1
  2 3
  6 6 2 5
  1 3
  3 4
  2 4
  1 2
  2 3
  5 6
  </code></pre>
  
  <p>Sample Output:</p>
  
  <pre><code>4
  12
  </code></pre>
  
  <p>Error on Hackerrank:</p>
  
  <blockquote>
    <p>GDB trace:</p>
    
    <p>Reading symbols from solution...done.</p>
    
    <p>[New LWP 14235]</p>
    
    <p>Core was generated by `solution'.</p>
    
    <p>Program terminated with signal SIGSEGV, <strong>Segmentation fault</strong>.</p>
    
    <p>/#0  0x00000000004009d9 in
    __gnu_cxx::new_allocator::construct (this=0x7ffdbd2b9738, __p=0x1)</p>
    
    <p>at /usr/include/c++/6/ext/new_allocator.h:120</p>
    
    <p>120       { ::new((void *)__p) _Up(std::forward&lt;_Args>(__args)...); }</p>
    
    <p>/#0  0x00000000004009d9 in
    __gnu_cxx::new_allocator::construct (this=0x7ffdbd2b9738, __p=0x1)</p>
    
    <p>at /usr/include/c++/6/ext/new_allocator.h:120</p>
    
    <p>/#1  std::allocator_traits >::construct</p>
    
    <p>(</p>
    
    <p>__a=..., __p=0x1) at /usr/include/c++/6/bits/alloc_traits.h:455</p>
    
    <p>/#2  std::vector >::push_back (</p>
    
    <p>__x=@0x7ffdbd2b9754: 1, this=0x7ffdbd2b9738)</p>
    
    <p>at /usr/include/c++/6/bits/stl_vector.h:918</p>
    
    <p>/#3  main () at solution.cc:76</p>
  </blockquote>