Skip to content

Commit 3f6faa4

Browse files
committed
leetcode
1 parent fda8d6a commit 3f6faa4

4 files changed

+239
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
3+
4+
-* 1523. Count Odd Numbers in an Interval Range *-
5+
6+
Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).
7+
8+
9+
10+
Example 1:
11+
12+
Input: low = 3, high = 7
13+
Output: 3
14+
Explanation: The odd numbers between 3 and 7 are [3,5,7].
15+
Example 2:
16+
17+
Input: low = 8, high = 10
18+
Output: 1
19+
Explanation: The odd numbers between 8 and 10 are [9].
20+
21+
22+
Constraints:
23+
24+
0 <= low <= high <= 10^9
25+
26+
27+
*/
28+
29+
class A {
30+
int countOdds(int high, int low) {
31+
return ((high + 1) ~/ 2) - (low ~/ 2);
32+
}
33+
}
34+
35+
class B {
36+
int countOdds(int high, int low) {
37+
if ((high & 1) == 0) high--;
38+
return (low > high) ? 0 : (high + 1) ~/ 2 - (low ~/ 2);
39+
}
40+
}
41+
42+
class C {
43+
int countOdds(int high, int low) {
44+
int total = high - low + 1;
45+
if (total % 2 == 0)
46+
return total ~/ 2;
47+
else {
48+
if (low % 2 != 0)
49+
return total ~/ 2 + 1;
50+
else
51+
return total ~/ 2;
52+
}
53+
}
54+
}
55+
56+
class D {
57+
int countOdds(int low, int high) {
58+
if (low % 2 == 0 && high % 2 == 0)
59+
return (high - low) ~/ 2;
60+
else {
61+
int firstOddNumber = low % 2 == 0 ? low + 1 : low;
62+
return (high - firstOddNumber) ~/ 2 + 1;
63+
}
64+
}
65+
}
66+
67+
class F {
68+
int countOdds(int high, int low) {
69+
int count = 0;
70+
for (int i = low; i <= high; i++) {
71+
if (i % 2 != 0) count++;
72+
}
73+
return count;
74+
}
75+
}
76+
77+
class G {
78+
int countOdds(int low, int high) {
79+
int result = 0;
80+
if ((low & 1) == 0) {
81+
result++;
82+
low++;
83+
}
84+
if ((high & 1) == 0) {
85+
result++;
86+
high--;
87+
}
88+
result += (high - low) ~/ 2;
89+
90+
return result;
91+
}
92+
}
93+
94+
class H {
95+
int countOdds(int low, int high) {
96+
if (low % 2 == 0 && high % 2 == 0) {
97+
return (high - low) ~/ 2;
98+
} else if (low % 2 != 0 && high % 2 != 0) {
99+
return 2 + (high - low) ~/ 2 - 1;
100+
} else {
101+
return 1 + (high - low) ~/ 2;
102+
}
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
func countOdds1(low int, high int) int {
4+
5+
return ((high + 1) / 2) - (low / 2)
6+
}
7+
8+
func countOdds2(low int, high int) int {
9+
if (high & 1) == 0 {
10+
high--
11+
}
12+
if low > high {
13+
return 0
14+
}
15+
return ((high + 1) / 2) - (low / 2)
16+
}
17+
18+
func countOdds3(low int, high int) int {
19+
var total int = high - low + 1
20+
if total%2 == 0 {
21+
return total / 2
22+
} else {
23+
if low%2 != 0 {
24+
return (total / 2) + 1
25+
26+
}
27+
}
28+
return total / 2
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 🔥 6 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Intuition
4+
5+
we will calculate all odd number from 1 to low-1 and 1 to high;
6+
why low-1??
7+
because we want include our low value.
8+
9+
## Approach
10+
11+
Total odd number between 1 and low - 1 is low/2.
12+
Total odd number between 1 and high is (high + 1 ) / 2.
13+
For getting answer we will do
14+
Total odd number between 1 and high - Total odd number between 1 and low - 1
15+
16+
## Complexity
17+
18+
### - Time complexity:O(1)
19+
20+
### - Space complexity:O(1)
21+
22+
## Code -1
23+
24+
```dart
25+
class Solution {
26+
int countOdds(int high, int low) {
27+
return ((high + 1) ~/ 2) - (low ~/ 2);
28+
}
29+
}
30+
```
31+
32+
## Code -2
33+
34+
```dart
35+
class Solution {
36+
int countOdds(int high, int low) {
37+
if ((high & 1) == 0) high--;
38+
return (low > high) ? 0 : (high + 1) ~/ 2 - (low ~/ 2);
39+
}
40+
}
41+
```
42+
43+
## Code -3
44+
45+
```dart
46+
class Solution {
47+
int countOdds(int high, int low) {
48+
int total = high - low + 1;
49+
if (total % 2 == 0)
50+
return total ~/ 2;
51+
else {
52+
if (low % 2 != 0)
53+
return total ~/ 2 + 1;
54+
else
55+
return total ~/ 2;
56+
}
57+
}
58+
}
59+
```
60+
61+
## Code -4
62+
63+
```dart
64+
class Solution {
65+
int countOdds(int low, int high) {
66+
if (low % 2 == 0 && high % 2 == 0)
67+
return (high - low) ~/ 2;
68+
else {
69+
int firstOddNumber = low % 2 == 0 ? low + 1 : low;
70+
return (high - firstOddNumber) ~/ 2 + 1;
71+
}
72+
}
73+
}
74+
```
75+
76+
## Code -5
77+
78+
```dart
79+
class Solution {
80+
int countOdds(int high, int low) {
81+
int count = 0;
82+
for (int i = low; i <= high; i++) {
83+
if (i % 2 != 0) count++;
84+
}
85+
return count;
86+
}
87+
}
88+
```
89+
90+
## Code -6
91+
92+
```dart
93+
class Solution {
94+
int countOdds(int low, int high) {
95+
if (low % 2 == 0 && high % 2 == 0) {
96+
return (high - low) ~/ 2;
97+
} else if (low % 2 != 0 && high % 2 != 0) {
98+
return 2 + (high - low) ~/ 2 - 1;
99+
} else {
100+
return 1 + (high - low) ~/ 2;
101+
}
102+
}
103+
}
104+
```

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
209209
- [**2306.** Naming a Company](NamingACompany/naming_a_company.dart)
210210
- [**1162.** As Far from Land as Possible](AsFarFromLandAsPossible/as_far_from_land_as_possible.dart)
211211
- [**1129.** Shortest Path with Alternating Colors](ShortestPathWithAlternatingColors/shortest_path_with_alternating_colors.dart)
212-
213212
- [**2477.** Minimum Fuel Cost to Report to the Capital](MinimumFuelCostToReportToTheCapital/minimum_fuel_cost_to_report_to_the_capital.dart)
213+
- [**1523.** Count Odd Numbers in an Interval Range](CountOddNumbersInAnIntervalRange/count_odd_numbers_in_an_interval_range.dart)
214214

215215
## Reach me via
216216

0 commit comments

Comments
 (0)