|
| 1 | +--- |
| 2 | +id: count-integers-even-digit-sum |
| 3 | +title: Count Integers With Even Digit Sum |
| 4 | +sidebar_label: 2180-Count-Integers-With-Even-Digit-Sum |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - Simulation |
| 8 | + |
| 9 | +description: The problem no. is 2180. The Problem is to Count Integers With Even Digit Sum |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | +Given a positive integer `num`, return the number of positive integers less than or equal to `num` whose digit sums are even. |
| 14 | + |
| 15 | +The digit sum of a positive integer is the sum of all its digits. |
| 16 | + |
| 17 | + |
| 18 | +### Example |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | + |
| 23 | +``` |
| 24 | +Input: num = 4 |
| 25 | +Output: 2 |
| 26 | +Explanation: |
| 27 | +The only integers less than or equal to 4 whose digit sums are even are 2 and 4. |
| 28 | +``` |
| 29 | +**Example 2:** |
| 30 | +``` |
| 31 | +Input: num = 30 |
| 32 | +Output: 14 |
| 33 | +Explanation: |
| 34 | +The 14 integers less than or equal to 30 whose digit sums are even are |
| 35 | +2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28. |
| 36 | +``` |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- `1 <= num <= 1000` |
| 40 | + |
| 41 | +## Solution Approach |
| 42 | + |
| 43 | +### Intuition: |
| 44 | + |
| 45 | +To efficiently Count Integers With Even Digit Sum |
| 46 | + |
| 47 | + |
| 48 | +## Solution Implementation |
| 49 | + |
| 50 | +### Code In Different Languages: |
| 51 | + |
| 52 | +<Tabs> |
| 53 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 54 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 55 | + ```javascript |
| 56 | + |
| 57 | +class Solution { |
| 58 | + countEven(num) { |
| 59 | + let cnt = 0; |
| 60 | + for(let i = 1; i <= num; i++){ |
| 61 | + let sum = 0; |
| 62 | + if(i < 10){ |
| 63 | + if(i % 2 == 0) cnt++; |
| 64 | + } else{ |
| 65 | + let temp = i; |
| 66 | + while(temp > 0){ |
| 67 | + let rev = temp % 10; |
| 68 | + sum += rev; |
| 69 | + temp = Math.floor(temp / 10); |
| 70 | + } |
| 71 | + if(sum % 2 == 0) cnt++; |
| 72 | + } |
| 73 | + } |
| 74 | + return cnt; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | + ``` |
| 80 | + |
| 81 | + </TabItem> |
| 82 | + <TabItem value="TypeScript" label="TypeScript"> |
| 83 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 84 | + ```typescript |
| 85 | + |
| 86 | +class Solution { |
| 87 | + countEven(num: number): number { |
| 88 | + let cnt = 0; |
| 89 | + for(let i = 1; i <= num; i++){ |
| 90 | + let sum = 0; |
| 91 | + if(i < 10){ |
| 92 | + if(i % 2 == 0) cnt++; |
| 93 | + } else{ |
| 94 | + let temp = i; |
| 95 | + while(temp > 0){ |
| 96 | + let rev = temp % 10; |
| 97 | + sum += rev; |
| 98 | + temp = Math.floor(temp / 10); |
| 99 | + } |
| 100 | + if(sum % 2 == 0) cnt++; |
| 101 | + } |
| 102 | + } |
| 103 | + return cnt; |
| 104 | + } |
| 105 | +} |
| 106 | +
|
| 107 | +
|
| 108 | + ``` |
| 109 | + |
| 110 | + </TabItem> |
| 111 | + <TabItem value="Python" label="Python"> |
| 112 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 113 | + ```python |
| 114 | + |
| 115 | +class Solution: |
| 116 | + def countEven(self, num): |
| 117 | + cnt = 0 |
| 118 | + for i in range(1, num + 1): |
| 119 | + sum = 0 |
| 120 | + if i < 10: |
| 121 | + if i % 2 == 0: |
| 122 | + cnt += 1 |
| 123 | + else: |
| 124 | + temp = i |
| 125 | + while temp > 0: |
| 126 | + rev = temp % 10 |
| 127 | + sum += rev |
| 128 | + temp = temp // 10 |
| 129 | + if sum % 2 == 0: |
| 130 | + cnt += 1 |
| 131 | + return cnt |
| 132 | +
|
| 133 | +
|
| 134 | + ``` |
| 135 | + |
| 136 | + </TabItem> |
| 137 | + <TabItem value="Java" label="Java"> |
| 138 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 139 | + ```java |
| 140 | + public class Solution { |
| 141 | + public int countEven(int num) { |
| 142 | + int cnt = 0; |
| 143 | + for(int i = 1; i <= num; i++){ |
| 144 | + int sum = 0; |
| 145 | + if(i < 10){ |
| 146 | + if(i % 2 == 0) cnt++; |
| 147 | + } else{ |
| 148 | + int temp = i; |
| 149 | + while(temp > 0){ |
| 150 | + int rev = temp % 10; |
| 151 | + sum += rev; |
| 152 | + temp = temp / 10; |
| 153 | + } |
| 154 | + if(sum % 2 == 0) cnt++; |
| 155 | + } |
| 156 | + } |
| 157 | + return cnt; |
| 158 | + } |
| 159 | +} |
| 160 | +
|
| 161 | + ``` |
| 162 | + |
| 163 | + </TabItem> |
| 164 | + <TabItem value="C++" label="C++"> |
| 165 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 166 | + ```cpp |
| 167 | +class Solution { |
| 168 | +public: |
| 169 | + int countEven(int num) { |
| 170 | + int cnt = 0; |
| 171 | + for(int i=1; i<=num; i++){ |
| 172 | + int sum = 0; |
| 173 | + if(i<10){ |
| 174 | + if(i%2==0) cnt++; |
| 175 | + } |
| 176 | + else{ |
| 177 | + int temp = i; |
| 178 | + while(temp>0){ |
| 179 | + int rev = temp%10; |
| 180 | + sum+=rev; |
| 181 | + temp/=10; |
| 182 | + } |
| 183 | + if(sum%2==0) cnt++; |
| 184 | +
|
| 185 | + } |
| 186 | + } |
| 187 | + return cnt; |
| 188 | + } |
| 189 | +}; |
| 190 | +
|
| 191 | +``` |
| 192 | +</TabItem> |
| 193 | +</Tabs> |
| 194 | + |
| 195 | +#### Complexity Analysis |
| 196 | + |
| 197 | +- Time Complexity: $$O(n)$$ |
| 198 | +- Space Complexity: $$O(1)$$ |
| 199 | +- The time complexity is $$O(n)$$,where n is the input number num. This is because the algorithm uses a single loop that iterates from 1 to num. |
| 200 | +- The space complexity is $$O(1)$$ because we are not using any extra space. |
0 commit comments