-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_number_even.cpp
50 lines (46 loc) · 1.68 KB
/
Find_number_even.cpp
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
// Attempt #1
// This Program Uses builtin function to convert integer into string and then compares its length.
// Mainly We have used to_string function to convert integer into string.
// Complexity Analysis
// Space Complexity -> O(elements inside nums length)
// Time Complexity -> O(N^2) or O(N*size of elements inside nums)
class Solution {
public:
int findNumbers(vector<int>& nums) {
int even_count = 0;
for(int i=0;i<nums.size();i++){
if(to_string(nums[i]).length()%2==0){
even_count++;
}
}
return even_count;
}
};
// Attempt #2
// In This Approach I have used simple arithematics to Solve the problem by dividing number by 10 again and again untill it becomes 0.
// No of times the loop is running is depicting that how many digits are there in the number and return True and False (booleans) appropriately.
// Complexity Analysis
// Space Complexity -> O(1)
// Time Complexity -> O(N log(N))
// Actually it is O(N (log(N)/log(10))) but actually it doesn't matter as log to base 10 will lesser than log to the base 2 , so for upper bound it doesn't matter.
// As N is very large we can ignore constants.
// class Solution {
// public:
// bool even_number_digit(int number){
// int count = 0;
// while(number>0){
// count++;
// number = number/10;
// }
// return (count&1)==0?1:0;
// }
// int findNumbers(vector<int>& nums) {
// int even_count = 0;
// for(int i=0;i<nums.size();i++){
// if(even_number_digit(nums[i])){
// even_count++;
// }
// }
// return even_count;
// }
// };