Error in generating subarryas

there is no error in code.but output is not right.

#include
using namespace std;
int main() {
int n;
cin>>n;

int a[1000];

for(int i=0;i<n;i++){
    cin>>a[i];
}
//generate all sub arrays
for(int i=0;i<n;i++){
	
    for(int j=i;j<n;j++){

        //elements of subarray(i,j)
        for(int k=i;k<=j;k++){
            cout<<a[k]<<",";
        }
        cout<<endl;
    }
}
return 0;

}

when input is 1 2 3 4
it gives only 2 as output

not able to generate all subarryas

You would laugh after knowing your mistake.:wink:

Your code is working perfectly.
When I tested it on different inputs, it gave correct outputs.
But it’s not happening in your case.

Observations:

  1. Code is perfect,
  2. It works for me,
  3. It does not work for you.

Hint: Your input format is wrong. Are you providing the value of n? Check.

Hope, it would help.
Give a like, if you are satisfied.

1 Like