In your 1st solution array B is of type int while in 2nd solution A is long long int.
Declaring B as int will cause overflow when numbers will be of order 10^5
int can accept the size of 10^5 there is no issue with that. Besides I have tested it using long too and getting the runtime error in that case too
You probably missed the case where N is 0, which would lead to n infinite loop.
In your code, variable
B array should be long long because when you are precomputing the array
B, it can go upto
10^5 * 10^5 which will cause overflow for INT datatype. Changing the datatypes of
B array, variable
s and variable
i (in last loop) gives correct results.
for n=0 loop will not run even a single time.