2523. Closest Prime Numbers in Range Medium

@problem@discussion
#Math#Number Theory



1/**
2 * [2523] Closest Prime Numbers in Range
3 *
4 * Given two positive integers left and right, find the two integers num1 and num2 such that:
5 * 
6 * 	left <= nums1 < nums2 <= right .
7 * 	nums1 and nums2 are both prime numbers.
8 * 	nums2 - nums1 is the minimum amongst all other pairs satisfying the above conditions.
9 * 
10 * Return the positive integer array ans = [nums1, nums2]. If there are multiple pairs satisfying these conditions, return the one with the minimum nums1 value or [-1, -1] if such numbers do not exist.
11 * A number greater than 1 is called prime if it is only divisible by 1 and itself.
12 *  
13 * <strong class="example">Example 1:
14 * 
15 * Input: left = 10, right = 19
16 * Output: [11,13]
17 * Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.
18 * The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].
19 * Since 11 is smaller than 17, we return the first pair.
20 * 
21 * <strong class="example">Example 2:
22 * 
23 * Input: left = 4, right = 6
24 * Output: [-1,-1]
25 * Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= left <= right <= 10^6
31 * 
32 *  
33 * <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; 
34 * }
35 * .spoiler {overflow:hidden;}
36 * .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
37 * .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
38 * .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
39 * </style>
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/closest-prime-numbers-in-range/
45// discuss: https://leetcode.com/problems/closest-prime-numbers-in-range/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn closest_primes(left: i32, right: i32) -> Vec<i32> {
51        vec![]
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2523() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.