Help me in solving CHRISCANDY problem

My issue

include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–){
int n;
int a[n];
cin>>n;

    for(int i=0;i < n;i++){
        cin>>a[i];
    }
    int currMax=a[0];
    int count=0;
    for(int i=1;i<n;i++){
        if(a[i]<currMax){
            count++;
        }
        else{
            currMax=a[i];
        }
    }
    cout<<count<<endl;
    
    
    
    
}

}
Question: In this code for the problem, can anyone say why I am getting runtime error despite of having correct logic. What corrections I need to make in this code?

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n;
	    int a[n];
	    cin>>n;
	    
	    for(int i=0;i < n;i++){
	        cin>>a[i];
	    }
	    int currMax=a[0];
	    int count=0;
	    for(int i=1;i<n;i++){
	        if(a[i]<currMax){
	            count++;
	        }
	        else{
	            currMax=a[i];
	        }
	    }
	    cout<<count<<endl;
	    
	    
	    
	    
	}

}

Problem Link: Christmas Candy Practice Coding Problem - CodeChef

You must assign the value to ‘n’, before initializing the array ‘a’ of size ‘n’.
Instead of :

It should be :

int n;
cin >> n;
int a[n];
2 Likes