Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 2501. Longest Square Streak in an Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int longestSquareStreak(vector<int>& nums) {
int longestStreak = -1,n;
n = nums.size();
vector<int> presenceChecker(100001,0);

// Keeping track of presence of each element of nums array in presenceChecker array.
for(int i=0;i<n;i++)
presenceChecker[nums[i]] = 1;

// Applying our algorithm for each element in nums array.
for(int i=0;i<n;i++){
long long int curr = nums[i];
int currLongestStreak = 1;
while(true){
if(curr*curr<100001 && presenceChecker[curr*curr] == 1){
currLongestStreak++;
curr = curr*curr;
}
else
break;
}
// if there is atleat 1 next square present in the array then currLongestStreak will
// be always greater than 1.
if(currLongestStreak != 1)
longestStreak = max(longestStreak,currLongestStreak);
}

return longestStreak;
}
};
Loading