Ishaan and Sticks | Practice | GeeksforGeeks

Ishaan has a craving for sticks. He has N sticks. He observes that some of his sticks are of the same length, and thus he can make squares out of those.
He wants to know how big a square he can make using those sticks as sides. Since the number of sticks is large, he can't do that manually. Can you tell him the maximum area of the biggest square that can be formed?
Also, calculate how many such squares can be made using the sticks.
 
Example 1:
â€‹Input : arr[ ] = {2, 2, 2, 9, 2, 2, 2, 2, 2}
Output : 4 2
Explanation:
2 squares of side 2 are formed.
return maximum area and number of square.


â€‹Example 2:
Input : arr[ ] = {5, 3, 2, 3, 6, 3, 3} 
Output :  9 1
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function square() that takes an array (arr), sizeOfArray (n) and return the array of the maximum area of the largest square that can be formed and the number of squares that can be formed if there is no possible square return -1. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(M), where M is the maximum value of an array.

 
Constraints : 
1 ≤ N ≤ 105
1 ≤ arri ≤ 103
0
rohitsingh0000654 months ago
JAVA SOLUTION USING HASHMAP EASY
 
 public static ArrayList<Integer> square (int arr[], int n) {
      HashMap<Integer,Integer> map = new HashMap<>();
      for(int el : arr){
          map.put(el ,  map.getOrDefault(el,0) + 1);
      }
      ArrayList<Integer> ans = new ArrayList<>();
      int max = 0, side = 0;
      for(int key : map.keySet()){
          if(map.get(key) >= 4){
              int carea = key;
              if(max < carea * carea){
                  max = carea * carea;
                  side = map.get(key)/4;
              }
          }
      }
      if(max==0){
          ans.add(-1);
          return ans;
      }
      ans.add(max);
      ans.add(side);
      return ans;
   }
0
nmnishant5 months ago
C++ Solution : Clean Codevector<int> square(int arr[], int n){   unordered_map<int, int> map;      for(int i=0; i<n; i++){       map[arr[i]] += 1;   }      int squares = 0;   int maxArea = -1;   for(auto i : map){       int side = i.first;       int count = i.second;          if(count >=4 && side * side > maxArea) {           squares = count/4;           maxArea = side*side;       }   }      vector<int> ans = {maxArea};   if(squares) ans.push_back(squares);      return ans;} 
+1
rohanpandey7496 months ago
C++ solution:
vector<int> square(int arr[], int n)
{
    // Complete the function
    map<int,int>mp;
    int fre=0;
    int ele=0;
    for(int i=0;i<n;i++){
        mp[arr[i]]++;
    }
    for(auto it:mp){
        if(it.second>=4){
            fre=it.second/4;
            ele=it.first;
        }
    }
    vector<int>answer;
    if(fre==0 && ele==0){
        answer.push_back(-1);
    }
    else{
        answer.push_back((ele*ele));
        answer.push_back((fre));
    }
    return answer;
}

+1
hanumanmanyam8376 months ago
vector<int> square(int arr[], int n){   // Complete the function   vector<int>res;   int max_len=0;   int squares=0;   unordered_map<int,int>map;   for(int i=0;i<n;i++)   {       map[arr[i]]++;   }   for(int i=0;i<n;i++)   {       if(map[arr[i]]>=4)       {           if(arr[i]>max_len)           {               max_len=arr[i];               squares=map[arr[i]]/4;           }       }   }   if(squares==0)   {       res.push_back(-1);       return res;   }   res.push_back(max_len*max_len);   res.push_back(squares);   return res;}
0
shreechandan_96 months ago
vector<int> square(int arr[], int n)
{
    // Complete the function
    sort(arr,arr+n);
    int area=0,count=0;
    for(int i=0;i<n;i++)
    {
        if(i+3<n&&arr[i]==arr[i+3])
        {
            if(area==arr[i]*arr[i+3])
                count++;
            else
            {
                area=arr[i]*arr[i+3];
                count=1;
            }
            i=i+3;
        }
    }
    if(count==0)
        return {-1};
    return {area,count};
}
+1
badgujarsachin837 months ago
vector<int> square(int arr[], int n)
{
    vector<int> v;
    if(n<4){
        v.push_back(-1);
        return v;
    }
    unordered_map<int,int> mp;
    for(int i=0;i<n;i++){
        mp[arr[i]]++;
    }
    int count=0;
    int area=0;
    for(auto it:mp){
        if(it.second/4>0 && it.first*it.first>area){
            count=it.second/4;
            area=max(area,it.first*it.first);
        }
    }
    
    if(count==0){
        v.push_back(-1);
        return v;
    }else{
        v.push_back(area);
        v.push_back(count);
    }
    return v;
}

-1
HARSH MEGHANI11 months agohttps://uploads.disquscdn.c...Reply Open ExternallyShow 0 RepliesLoading...
HARSH MEGHANI
https://uploads.disquscdn.c...
0
Ashman Jagdev1 year agohttps://uploads.disquscdn.c...Reply Open ExternallyShow 0 RepliesLoading...
Ashman Jagdev
https://uploads.disquscdn.c...
-1
Aniket patel1 year agohttps://practice.geeksforge...Reply Open ExternallyShow 0 RepliesLoading...
Aniket patel
https://practice.geeksforge...
-1
Drigger1 year agosol in java: https://ide.geeksforgeeks.o...Reply Open ExternallyShow 0 RepliesLoading...
Drigger
sol in java: https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.