We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 676ec43 commit 2e60a28Copy full SHA for 2e60a28
scripts/algorithms/F/Find Closest Number to Zero/Find Closest Number to Zero.cpp
@@ -1,17 +1,18 @@
1
+// Runtime: 62 ms (Top 15.48%) | Memory: 19.7 MB (Top 56.49%)
2
class Solution {
3
public:
4
int findClosestNumber(vector<int>& nums) {
-// setting the ans to maximum value of int
5
+// setting the ans to maximum value of int
6
int ans = INT_MAX ;
- for(int i : nums){
7
+ for(int i : nums){
8
// checking if each value of nums is less than the max value
9
if(abs(i) < abs(ans)){
- ans = i ; //check for the lesser value
10
+ ans = i ; //check for the lesser value
11
}
12
else if(abs(i) == abs(ans)){
13
ans = max (ans,i) ; // return the maximum in cases there are multiple answers
14
15
16
return ans ;
17
-};
18
+};
0 commit comments