what is the reason for the segmentation fault?
There are many, many things that can cause a segmentation fault; see e.g. here.
what is max array size in 2 d and 3 d which will not lead to this error
i think product of sizes of all dimension should be less than 10^6 or 2 * 10^6 so 10^3 * 10^3 and 10^2 * 10^2 * 10^2 will work…i think.
why declaring that big array or vector is better outside main?
Declaring inside main will allocate space on stack section and that is often small ( upto 10^6 work fine) .While declaring globally allocate space on heap section ( so you can have bigger range 10^7 to 10^8).
so I can create array of 10^8 in global and 10^6 in main??
do declaring a vector of n inside main takes O(n) time??