Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Largest number from the longest set of anagrams possible from all perfect squares of length K

  • Last Updated : 23 Jul, 2021

Given an integer K such that there is a set of all possible perfect squares, each of length K. From this set of perfect squares, form a set of the longest possible length having those numbers which are anagram to each other. The task is to print the largest element present in the generated set of anagrams. 
Note: If there is more than one set of maximum length, then print the largest number among them.

The anagram of a string is another string that contains the same characters, only the order of characters are different. 


Input: K = 2 
Output: 81 
All possible squares of length K = 2 are {16, 25, 36, 49, 64, 81}. 
The possible anagram sets are [16], [25], [36], [49], [64], [81]. 
Therefore, each set is of the same size i.e., 1. 
In this case, print the set containing the largest element, which is 81.

Input: K = 5 
Output: 96100



Naive Approach: The simplest approach is to store all possible K length perfect squares and form a valid anagram sets using recursion. Then, find the maximum element present in the set of longest length.

Time Complexity: O(N2
Auxiliary Space: O(N)

Efficient Approach: The idea is based on the observation that on sorting the digits of the perfect squares, the sequence of anagram numbers becomes equal. Below are the steps: 

  1. Initialise a Map to store all the anagram numbers corresponding to the numbers whose digit are arranged in sorted order.
  2. Generate all the perfect squares of length K.
  3. For each perfect square generated, insert the number in the Map corresponding to the number whose digit is arranged in ascending order.
  4. Traverse the Map and print the largest number from the set of maximum length.

Below is the implementation of the above approach:


// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the largest set of
// perfect squares which are anagram
// to each other
void printResult(
    map<string, set<long long int> > m)
    auto max_itr = m.begin();
    long long maxi = -1;
    for (auto itr = m.begin();
         itr != m.end(); ++itr) {
        long long int siz1
            = (itr->second).size();
        // Check if size of maximum set
        // is less than the current set
        if (maxi < siz1) {
            // Update maximum set
            maxi = siz1;
            max_itr = itr;
        // If lengths are equal
        else if (maxi == siz1) {
            // Update maximum set to store
            // the set with maximum element
            if ((*(max_itr->second).rbegin())
                < *(itr->second.rbegin())) {
                maxi = siz1;
                max_itr = itr;
    // Stores the max element from the set
    long long int result
        = *(max_itr->second).rbegin();
    // Print final Result
    cout << result << endl;
// Function to find the
// perfect squares which are anagrams
void anagramicSquares(long long int k)
    // Stores the sequence
    map<string, set<long long int> > m;
    // Initialize start and end
    // of perfect squares
    long long int end;
    if (k % 2 == 0)
        end = k / 2;
        end = ((k + 1) / 2);
    long long int start = pow(10, end - 1);
    end = pow(10, end);
    // Iterate form start to end
    for (long long int i = start;
         i <= end; i++) {
        long long int x = i * i;
        // Converting int to string
        ostringstream str1;
        str1 << x;
        string str = str1.str();
        if (str.length() == k) {
            // Sort string for storing
            // number at exact map position
            sort(str.begin(), str.end());
            // Insert number at map
    // Print result
// Driver Code
int main()
    // Given K
    long long int K = 2;
    // Function Call
    return 0;


Time Complexity: O(N) 
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!