Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include<iostream>
  2. #include<unordered_map>
  3. #include<forward_list>
  4. #include<utility>
  5. const std::forward_list<unsigned>& input(unsigned N)
  6. {
  7. static std::forward_list<unsigned> ls;
  8. unsigned temp=0;
  9. while(N--){
  10. std::cin>>temp;
  11. ls.push_front(std::move(temp));
  12. }
  13. return ls;
  14. }
  15. std::unordered_map<unsigned,unsigned>& squared(unsigned N)
  16. {
  17. static std::unordered_map<unsigned,unsigned> ht;
  18. unsigned temp=0;
  19. while(N--){
  20. std::cin>>temp;
  21. bool found=ht.find(temp)!=ht.end();
  22. if(!found)
  23. ht.insert(std::pair<unsigned,unsigned>(temp,1));
  24. else
  25. ht[temp]++;
  26. }
  27. return ht;
  28. }
  29. bool same(const std::forward_list<unsigned>& ls,std::unordered_map<unsigned,unsigned>& ht)
  30. {
  31. for(const auto& i: ls){
  32. unsigned sq=i*i;
  33. bool found=ht.find(sq)!=ht.end();
  34. if(!found)
  35. return 0;
  36. else{
  37. ht[sq]--;
  38. if(!ht[sq])
  39. ht.erase(sq);
  40. }
  41. }
  42. return 1;
  43. }
  44. int main()
  45. {
  46. std::cout<<"Enter the limit: ";
  47. unsigned N=0;
  48. std::cin>>N;
  49. std::cout<<same(input(N),squared(N))<<std::endl;
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement