-2

I want to optimize this code which prints all subarrays of given array: Or, is there any better algorithm to print all subarrays of an array?

#include<bits/stdc++.h>
using namespace std;
int main()
{
 ios_base::sync_with_stdio (false);
 cin.tie(NULL);
 cout.tie(NULL);
 vector<int> v;
 int n;
 cin>>n;   // the size of the array
 for(int i=0;i<n;i++)
 {
  int x;
  cin>>x;
  v.push_back(x);
 }
 for(int i=0;i<n;i++)
 {
 for(int j=i;j<n;j++)
 {
 for(int k=i;k<=j;k++)
  cout<<v[k]<<" ";
  cout<<endl;
 }
 }
 return 0;
}
  • 2
    This reads like it's from some contest/challenge/competitive coding/hacking site. Is it? Those web sites are meant for people who are already skilled in C++. If your goal is to learn C++, you won't learn anything there. In nearly all cases, like this one, the correct solution is based on a mathematical or a programming trick. Other than that, there's nothing here that actually improves one's knowledge of C++. If you're trying to learn C++, you won't learn anything from meaningless online contest sites [but only from a good C++ textbook](https://stackoverflow.com/questions/388242). – Sam Varshavchik May 26 '22 at 11:08
  • 1
    Wherever you learned `#include `, avoid that. – Eljay May 26 '22 at 11:11
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/995714), [Why should I not `#include `?](https://stackoverflow.com/q/31816095/995714) And learn to format your code properly – phuclv May 31 '22 at 10:34

0 Answers0