Dec 25, 2018 at 3:03pm UTC  
 
Question- 
https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/perfect-pair-df920e90/
Summary of question:  Find the number of pairs which add up to a perfect square or 
a perfect cube.
Issue- 
My answer > correct answer
My answer is correct for test cases but not for the other big cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 
  #include<iostream> 
#include<vector> 
using  namespace  std;
int  main()
{
    vector<int >sqcb;
    sqcb.push_back(0);
    sqcb.push_back(1);
    for (int  i=2;i<=10;i++)
    {
        sqcb.push_back(i*i);
        sqcb.push_back(i*i*i);
    }
    for (int  i=11;i<=31;i++)
        sqcb.push_back(i*i);
    int  t;
    cin>>t;
    while (t>0)
    {
        int  freq[1001];
        for (int  i=0;i<=1000;i++)
        {
            freq[i]=0;
        }
        int  n,temp;
        cin>>n;
        for (int  i=0;i<n;i++)
        {
            cin>>temp;
            freq[temp]++;
        }
        long  long  int  ans=0;
        for (auto  x:sqcb)
        {
            for (int  i=0;i<=x/2;i++)
            {
                if (x-i==i)
                {
                    ans+=(freq[i]*(freq[i]-1))/2;
                }
                else 
                {
                    ans+=freq[x-i]*freq[i];
                }
            }
        }
        cout<<ans<<endl;
        t--;
    }
    return  0;
}
 
 
Last edited on Dec 25, 2018 at 3:04pm UTC  
 
 
 
 
  Dec 25, 2018 at 3:19pm UTC  
 
My answer is correct for test cases but not for the other big cases. 
 
well, does anything overflow?  What is the largest value you have for i ?  What is the largest value of int on your system?  Is i*i*i bigger than this?
Last edited on Dec 25, 2018 at 3:20pm UTC  
 
 
 
 
  Dec 26, 2018 at 6:30am UTC  
 
i*i*i in code is maximum 1000
i*i in code is maximum 961
these are within int
 
Last edited on Dec 26, 2018 at 6:31am UTC  
 
 
 
 
  Dec 26, 2018 at 2:44pm UTC  
 
ok, what value gives an incorrect answer? 
 
 
 
 
  Dec 27, 2018 at 2:39pm UTC  
 
I am having trouble understanding your code...
1 2 3 4 5 6 7 8 9 10 
vector<int >sqcb;
    sqcb.push_back(0);
    sqcb.push_back(1);
    for (int  i=2;i<=10;i++)
    {
        sqcb.push_back(i*i);
        sqcb.push_back(i*i*i);
    }
    for (int  i=11;i<=31;i++)
        sqcb.push_back(i*i);
 
can you please explain use of this ??
Also please explain your code or at least add comments to it for others to understand it.
 
Last edited on Dec 27, 2018 at 7:35pm UTC  
 
 
 
 
  Dec 27, 2018 at 5:03pm UTC  
 
From the constraints on the problem page: 1 ≤  Ai  ≤ 103 . So the largest sum is 2000, not 1000.  So the loop at line 12 should go to 12 and the loop at line 14 should go to 44. 
 
Line 39 is wrong. The problem states "a pair where (Ai  + Aj ) is a perfect square or a perfect cube and i ≠ j ." 
 
 
 
Last edited on Dec 27, 2018 at 5:03pm UTC