File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int longestSquareStreak(vector<int>& nums) {
4
+ int longestStreak = -1,n;
5
+ n = nums.size();
6
+ vector<int> presenceChecker(100001,0);
7
+
8
+ // Keeping track of presence of each element of nums array in presenceChecker array.
9
+ for(int i=0;i<n;i++)
10
+ presenceChecker[nums[i]] = 1;
11
+
12
+ // Applying our algorithm for each element in nums array.
13
+ for(int i=0;i<n;i++){
14
+ long long int curr = nums[i];
15
+ int currLongestStreak = 1;
16
+ while(true){
17
+ if(curr*curr<100001 && presenceChecker[curr*curr] == 1){
18
+ currLongestStreak++;
19
+ curr = curr*curr;
20
+ }
21
+ else
22
+ break;
23
+ }
24
+ // if there is atleat 1 next square present in the array then currLongestStreak will
25
+ // be always greater than 1.
26
+ if(currLongestStreak != 1)
27
+ longestStreak = max(longestStreak,currLongestStreak);
28
+ }
29
+
30
+ return longestStreak;
31
+ }
32
+ };
You can’t perform that action at this time.
0 commit comments