从 L 到 R 范围内的第 K 个最小的偶数
给定两个变量L和R ,表示从L到R的整数范围,以及一个数字K ,任务是找到第 K个最小的偶数。如果K大于L到R范围内的多个偶数,则返回 -1。 LLONG_MIN <= L <= R <= LLONG_MAX 。
例子:
Input: L = 3, R = 9, K = 3
Output: 8
Explanation: The even numbers in the range are 4, 6, 8 and the 3rd smallest even number is 8
Input: L = -3, R = 3, K = 2
Output: 0
Naive Approach:基本思想是从L到R遍历数字,然后打印第K个偶数。
时间复杂度:O(RL)
辅助空间:O(1)
方法:给定的问题可以使用基础数学和使用 cmath 库中的 ceil 和 floor 来解决。这个想法是检查L是奇数还是偶数,并相应地计算第 K 个最小的偶数。以下步骤可用于解决问题:
- 如果K<=0则返回-1
- 初始化count计算范围内偶数个数
- 如果L是奇数
- 计数 = 楼层((浮点数)(R-L+1)/2)
- 如果K > 计数返回-1
- 否则返回(L + 2*K – 1)
- 如果L是偶数
- 计数 = ceil((浮点数)(R-L+1)/2)
- 如果K > 计数返回-1
- 否则返回(L + 2*K – 2)
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
#define ll long long
using namespace std;
// Function to return Kth smallest
// even number if it exists
ll findEven(ll L, ll R, ll K)
{
// Base Case
if (K <= 0)
return -1;
if (L % 2) {
// Calculate count of even numbers
// within the range
ll Count = floor((float)(R - L + 1) / 2);
// if k > range then kth smallest
// even number is not in this range
// then return -1
return (K > Count) ? -1 : (L + 2 * K - 1);
}
else {
// Calculate count of even numbers
// within the range
ll Count = ceil((float)(R - L + 1) / 2);
// if k > range then kth smallest
// even number is not in this range
// then return -1
return (K > Count) ? -1 : (L + 2 * K - 2);
}
}
// Driver Code
int main()
{
ll L = 3, R = 9, K = 3;
cout << findEven(L, R, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to return Kth smallest
// even number if it exists
static long findEven(long L, long R, long K)
{
// Base Case
if (K <= 0)
return -1;
if (L % 2 == 1) {
// Calculate count of even numbers
// within the range
long Count = (int)Math.floor((float)(R - L + 1) / 2);
// if k > range then kth smallest
// even number is not in this range
// then return -1
return (K > Count) ? -1 : (L + 2 * K - 1);
}
else {
// Calculate count of even numbers
// within the range
long Count = (int)Math.ceil((float)(R - L + 1) / 2);
// if k > range then kth smallest
// even number is not in this range
// then return -1
return (K > Count) ? -1 : (L + 2 * K - 2);
}
}
// Driver Code
public static void main(String args[])
{
long L = 3, R = 9, K = 3;
System.out.println(findEven(L, R, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to return Kth smallest
# even number if it exists
def findEven(L, R, K):
# Base Case
if (K <= 0):
return -1
if (L % 2):
# Calculate count of even numbers
# within the range
Count = (R - L + 1) // 2
# if k > range then kth smallest
# even number is not in this range
# then return -1
return -1 if (K > Count) else (L + 2 * K - 1)
else:
# Calculate count of even numbers
# within the range
Count = (R - L + 1) // 2
# if k > range then kth smallest
# even number is not in this range
# then return -1
return -1 if (K > Count) else (L + 2 * K - 2)
# Driver Code
L = 3
R = 9
K = 3
print(findEven(L, R, K))
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to return Kth smallest
// even number if it exists
static long findEven(long L, long R, long K)
{
// Base Case
if (K <= 0)
return -1;
if (L % 2 == 1) {
// Calculate count of even numbers
// within the range
long Count = (int)Math.Floor((float)(R - L + 1) / 2);
// if k > range then kth smallest
// even number is not in this range
// then return -1
return (K > Count) ? -1 : (L + 2 * K - 1);
}
else {
// Calculate count of even numbers
// within the range
long Count = (int)Math.Ceiling((float)(R - L + 1) / 2);
// if k > range then kth smallest
// even number is not in this range
// then return -1
return (K > Count) ? -1 : (L + 2 * K - 2);
}
}
// Driver Code
public static void Main()
{
long L = 3, R = 9, K = 3;
Console.Write(findEven(L, R, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
8
时间复杂度:O(1)
辅助空间:O(1)