I want to know the algo for this question

This is the 3rd auestion of googe kickstart.I tried the hashmap pproach with apprx. operation needed as 10^8 but still it got TLE…I used a hasmap approach…

THis is the question statement…

I want to know the optimization for it…

Problem
Cristobal has an array of N (possibly negative) integers. The i-th integer in his array is Ai. A contiguous non-empty subarray of Cristobal’s array is perfect if its total sum is a perfect square. A perfect square is a number that is the product of a non-negative integer with itself. For example, the first five perfect squares are 0, 1, 4, 9 and 16.

How many subarrays are perfect? Two subarrays are different if they start or end at different indices in the array, even if the subarrays contain the same values in the same order.

Input
The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case contains the integer N. The second line contains N integers describing Cristobal’s array. The i-th integer is Ai.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of perfect subarrays.

Limits
Memory limit: 1GB.
1 ≤ T ≤ 100.
-100 ≤ Ai ≤ 100, for all i.

Test set 1
Time limit: 20 seconds.
1 ≤ N ≤ 1000.

Test set 2
Time limit: 30 seconds.
For up to 5 cases, 1 ≤ N ≤ 105.
For the remaining cases, 1 ≤ N ≤ 1000.

Sample

Input

Output

3
3
2 2 6
5
30 30 9 1 30
4
4 0 0 16

Case #1: 1
Case #2: 3
Case #3: 9

In sample case #1, there is one perfect subarray: [2 2] whose sum is 22.

In sample case #2, there are three perfect subarrays:
[9], whose total sum is 32.
[1], whose total sum is 12.
[30 30 9 1 30], whose total sum is 102.

In sample case #3, there are nine perfect subarrays:
[4], whose total sum is 22.
[4 0], whose total sum is 22.
[4 0 0], whose total sum is 22.
[0], whose total sum is 02.
[0 0], whose total sum is 02.
[0 0 16], whose total sum is 42.
[0], whose total sum is 02.
[0 16], whose total sum is 42.
[16], whose total sum is 42.

Note: We do not recommend using interpreted/slower languages for the test set 2 of this problem.

Hey @kaushikjatin
This is my code for it
Its a simple cumulative sum based question


Rest the analysis has also been uploaded you can use it to understand better

Bro…as much I could understand from your code I can see that you made a prefix sum array…and for each element of prefix sum array you r finding the possible square it can make or not by using the no of squares possible till 100*n…and If it your algo then I also did the in the contest bro…but got TLE…here is link to my code…
https://ide.geeksforgeeks.org/Tsnukkuqx0
But if you have a different approach bro then please tell me or if you have did any optimization…And bro one more thing I need to ask that in most of the correct code I see that there r so many #define and # include…as I can see in your code…could you tell me the reason for it…
LIke these much r in your code.
#include<bits/stdc++.h>

#define int long long

#define pb push_back

#define pii pair<int,int>

#define ff first

#define ss second

#define MOD 998244353

#define MAX INT_MAX*200000ll

#define PI 3.14159265

#define f(i,s,n) for(int i=s;i<n;i++)

#define rf(i,e,n) for(int i=n-1;i>=e;i–)

using namespace std;

#define TRACE

#ifdef TRACE

@kaushikjatin
The #define are just to skip writing complete code
They are just custom defined short hand notations
You’ll get acquainted with them once you participate in contests regularly and you’ll end up making some for yourself

@kaushikjatin
I think the issue is with calculation of squares
You’re assuming max will be 100*n
But max might be very less compared to it
So see my code
max should be taken as max possible value in cumulative sum array
Rest everything seems perfect

Ya I thought of it but just thought that the max sum can be 100*n in worst case…but I will try with max_sum of array…

I tried with this updation too…but got TLE still…I used fast io and also got TLE…

Here is the updated code link…
https://ide.geeksforgeeks.org/dITJOrvvoh

@kaushikjatin
Even I can’t figure out the error now buddy
Might be due to Java don’t know
Your logic is the same only

I tried the fastest IO…but still TLE…

@kaushikjatin
It is infeasible but maybe you can see a few solutions on Kickstart result page and see if you get a Java one to see where you went wrong. Many people code in C++ so might take a lot of time though.

Ya…they have all used array of 10^8 length instead of the HashMap…because hashMap is almost O(1) but array is structly O(1)…as google provides us 1GB of memory …they took bebefit of that…

And do provide me the link for it’s resolution as it is tough to go back to course and search for this question and then resolve the doubt…

@kaushikjatin
No issues I’ll close it from my side

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.