-1
class Solution {
public:
    vector<int> getConcatenation(vector<int>& nums) {
        
      int n=nums.size();
        vector<int> ans(2*n);
        for(int i=0;i<2*n;i++)
        {
            if(i<n)
            {
                ans.push_back(nums[i]);
            }
            else
            {
                ans.push_back(nums[i-n]);
            }  
        }
        return ans;
        
    }
};

This above code is not giving appropriate ans.

while below code is working fine.

class Solution {
public:
    vector<int> getConcatenation(vector<int>& nums) {
        
      int n=nums.size();
        vector<int> ans(2*n);
        for(int i=0;i<2*n;i++)
        {
            if(i<n)
            {
                ans[i]=nums[i];
            }
            else
            {
                ans[i]=nums[i-n];
            }  
        }
        return ans;
        
    }
};
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    The first one creates a vector with 2 * n zeroes in it and then pushes back 2 * n more values in the loop. The second just fills the initial allocated space with the values from the loop. – Retired Ninja Apr 16 '22 at 07:01
  • 3
    `push_back` adds a new element, the `[]` operator sets an existing one – Alan Birtles Apr 16 '22 at 07:01
  • May wanna just take a more direct route without all the math: [example here](https://pastebin.com/5JY4Kt46). – WhozCraig Apr 16 '22 at 07:34
  • If you're free to modify the signature for the function, you'd be able to apply it in more scenarios, if you were using `vector getConcatenation(vector const& nums) const`. (This change doesn't require an update of the function body...) – fabian Apr 16 '22 at 08:29

1 Answers1

0

When you create the vector you have specified a size already (usually not done with vectors) so your vector already filled with ints that have been set to 0. So when you use ans[i] = nums[i]; you are simply editing one of the already existing elements stored in the vector.

Usually when you create a vector you create it as an empty one by doing vector<int> ans; or vector<int>* ans = new vector<int>(); (if you want to create it on the heap) and then add a new element to it using ans.push_back(value); which will add the element to the back/end of the vector.

  • 1
    `vector ans = new vector();` doesn't compile. If you fix the typo, it's still mostly useless. – HolyBlackCat Apr 16 '22 at 09:46
  • @HolyBlackCat what do you mean by fixing the typo? Like I have in the edited version of my answer? If so then how is it mostly useless? – GrimStarGaming Apr 16 '22 at 09:54
  • Yes, like this. Why would you want to create it on the heap? (remember that its elements are on the heap regardless). Even if you have a reason, a smart pointer should be used instead. – HolyBlackCat Apr 16 '22 at 10:04
  • @HolyBlackCat That depends on the scenario and even just the coders preference. Smart pointer or not is also dependent on scenario and preference.. some people don't forget to delete what they create on the heap and don't mind typing delete. – GrimStarGaming Apr 16 '22 at 10:21
  • 1
    This is not a preference thing. Manual `delete` can be very tricky when exceptions are involved, and even without them, there's no reason to spend your mental capacity trying to use it correctly, unless you're writing a *custom* smart pointer or container. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c149-use-unique_ptr-or-shared_ptr-to-avoid-forgetting-to-delete-objects-created-using-new – HolyBlackCat Apr 16 '22 at 10:30
  • @HolyBlackCat I don't think you are understanding why using delete in that example from cppcore is a bad use of delete. It is only dangerous because there is a return possibility before the delete is called not just because delete is being used on a local variable. So like i said, it depends on the scenario and if someone is confident with how to code in c++ and is familiar with using the heap then it might be their preference to trust their coding skills. – GrimStarGaming Apr 16 '22 at 10:43
  • *"only dangerous because there is a return possibility before the delete"* Not only because of that. Also because the second allocation can throw, causing a leak on the first allocation. Think of the amount of `try`/`catch` you'd need to correctly handle that, if you do multiple heap allocations in the same function. – HolyBlackCat Apr 16 '22 at 11:09