给定范围内第 K 个最大的奇数
给定两个变量L和R ,表示从L到R的整数范围,以及一个数字K ,任务是找到第 K个最大的奇数。如果K > L到R范围内的奇数个数,则返回 0。
例子:
Input: L = -10, R = 10, K = 8
Output: -5
Explanation: The odd Numbers in the range are -9, -7, -5, -3, -1, 1, 3, 5, 7, 9 and the 8th Largest odd number is -5
Input: L = -3, R = 3, K = 1
Output: 3
方法:给定的问题可以用数学来解决。这个想法是检查R是奇数还是偶数,并相应地计算第 K 个最大的奇数。以下步骤可用于解决问题:
- 如果K<=0则返回 0
- 初始化count计算范围内奇数个数
- 如果R是奇数:
- 计数= ceil((浮点数)(R-L+1)/2)
- 如果K > count返回 0
- 否则返回 (R – 2*K + 2)
- 如果R是偶数
- 计数= 楼层((R-L+1)/2)
- 如果K > count返回 0
- 否则返回 (R – 2*K + 1)
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
#include
using namespace std;
// Function to return Kth largest
// odd number if it exists
int kthOdd(pair range, int K)
{
// Base Case
if (K <= 0)
return 0;
int L = range.first;
int R = range.second;
if (R & 1) {
// Calculate count of odd
// numbers within the range
int Count = ceil((float)(R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
}
else {
// Calculate count of odd
// numbers within the range
int Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
int main()
{
// Initialize the range
pair p = { -10, 10 };
// Initialize k
int k = 8;
// print the kth odd number
cout << kthOdd(p, k);
return 0;
}
Java
// Java implementation for the above approach
class GFG {
// Function to return Kth largest
// odd number if it exists
public static int kthOdd(int[] range, int K) {
// Base Case
if (K <= 0)
return 0;
int L = range[0];
int R = range[1];
if ((R & 1) > 0) {
// Calculate count of odd
// numbers within the range
int Count = (int) Math.ceil((R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
} else {
// Calculate count of odd
// numbers within the range
int Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
public static void main(String args[])
{
// Initialize the range
int[] p = { -10, 10 };
// Initialize k
int k = 8;
// print the kth odd number
System.out.println(kthOdd(p, k));
}
}
// This code is contributed by gfgking.
Python3
# python implementation for the above approach
import math
# Function to return Kth largest
# odd number if it exists
def kthOdd(range, K):
# Base Case
if (K <= 0):
return 0
L = range[0]
R = range[1]
if (R & 1):
# Calculate count of odd
# numbers within the range
Count = math.ceil((R - L + 1) / 2)
# if k > range then kth largest
# odd number is not in the range
if (K > Count):
return 0
else:
return (R - 2 * K + 2)
else:
# Calculate count of odd
# numbers within the range
Count = (R - L + 1) // 2
# If k > range then kth largest
# odd number is not in this range
if (K > Count):
return 0
else:
return (R - 2 * K + 1)
# Driver Code
if __name__ == "__main__":
# Initialize the range
p = [-10, 10]
# Initialize k
k = 8
# print the kth odd number
print(kthOdd(p, k))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
public class GFG
{
// Function to return Kth largest
// odd number if it exists
public static int kthOdd(int[] range, int K) {
// Base Case
if (K <= 0)
return 0;
int L = range[0];
int R = range[1];
if ((R & 1) > 0) {
// Calculate count of odd
// numbers within the range
int Count = ((R - L + 1) / 2);
// if k > range then kth largest
// odd number is not in the range
if (K > Count)
return 0;
else
return (R - 2 * K + 2);
} else {
// Calculate count of odd
// numbers within the range
int Count = (R - L + 1) / 2;
// If k > range then kth largest
// odd number is not in this range
if (K > Count)
return 0;
else
return (R - 2 * K + 1);
}
}
// Driver Code
public static void Main(string[] args)
{
// Initialize the range
int[] p = { -10, 10 };
// Initialize k
int k = 8;
// print the kth odd number
Console.WriteLine(kthOdd(p, k));
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
-5
时间复杂度:O(1)
辅助空间:O(1)