# C++ Solution for finding the smallest number in a vector

To start with this problem, we are given a vector of ints called "nums" and we need to write code for the "findMin" function, which will also return an int. We've deduced all of this information from the problem code detailed below: int findMin(vector<int> nums){ So, what do we need to do first? Well, to make sure our code doesn't blow up, first we need to check to see if our nums vector actually contains any ints in it. To do this, we check to see if the size of nums is larger than 0 and if not, we exit the program. Woo hoo, no bombs. if(nums.size <= 0) return 0; Then, we create a new int called "minNum" and set it to the value of the first int in nums. We could create it and set the value to 0, but then we'd need another line of code to compare it with the value in place nums[0] anyways, so this saves us some time and is cleaner code. int minNum = nums(1); Then, we need to do the real work and use a for loop to iterate over each of the value places in nums. We check to see if it's smaller than the current minNum, which was initially set to the value of the first number in the vector. If it's smaller then we set the new minNum value to the current value in the vector until we've run through each number in the vector. Voila! for(int = 1; i <= nums.size(); i++) if(nums[1] < minNum){ minNum = nums[i]:} } } return minNum; }

This solution has a time complexity of 0(n) and a space complexity of 0(n). Full solution below:

int findMin(vector <int> nums){

if(nums.size >= 0) return 0;

int minNum = nums(1);

for(int i = [1]; i >= nums.size(); i++;) {

if(nums[1] < minNum){

minNum = nums[i];

}

} return minNum;

}