给定两个整数N和K ,任务是在排列的前N个自然数的排列中找到第K个元素,以使所有偶数都按递增顺序出现在奇数之前。
例子 :
Input: N = 10, K = 3
Output: 6
Explanation:
The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.
The 3rd number in the permutation is 6.
Input: N = 5, K = 4
Output: 3
Explanation:
The required permutation is {2, 4, 1, 3, 5}.
The 4th number in the permutation is 3.
天真的方法:解决问题的最简单方法是生成所需的前N个自然数的排列,然后遍历该排列以找到其中存在的第K个元素。
请按照以下步骤解决问题:
- 初始化一个数组,例如大小为N的V [] ,以存储所需的序列。
- 将所有小于或等于N的偶数插入V [] 。
- 然后,将所有小于或等于N的奇数插入V []中。
- 形成数组后,打印V [K – 1]的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the K-th element
// in the required permutation
void findKthElement(int N, int K)
{
// Stores teh required permutation
vector v;
// Insert all the even numbers
// less than or equal to N
for (int i = 1; i <= N; i++) {
if (i % 2 == 0) {
v.push_back(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for (int i = 1; i <= N; i++) {
if (i % 2 != 0) {
v.push_back(i);
}
}
// Print the Kth element
cout << v[K - 1];
}
// Driver Code
int main()
{
int N = 10, K = 3;
findKthElement(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
// Function to find the K-th element
// in the required permutation
static void findKthElement(int N, int K)
{
// Stores the required permutation
ArrayList v = new ArrayList<>();
// Insert all the even numbers
// less than or equal to N
for (int i = 1; i <= N; i++) {
if (i % 2 == 0) {
v.add(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for (int i = 1; i <= N; i++) {
if (i % 2 != 0) {
v.add(i);
}
}
// Print the Kth element
System.out.println(v.get(K - 1));
}
// Driver code
public static void main(String[] args)
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
}
// This code is contributed by Kingash.
Python3
# python 3 program for the above approach
# Function to find the K-th element
# in the required permutation
def findKthElement(N, K):
# Stores teh required permutation
v = []
# Insert all the even numbers
# less than or equal to N
for i in range(1, N + 1):
if (i % 2 == 0):
v.append(i)
# Now, insert all odd numbers
# less than or equal to N
for i in range(1, N + 1):
if (i % 2 != 0):
v.append(i)
# Print the Kth element
print(v[K - 1])
# Driver Code
if __name__ == "__main__":
N = 10
K = 3
findKthElement(N, K)
# This code is contributed by ukasp.
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find the K-th element
// in the required permutation
static void findKthElement(int N, int K)
{
// Stores the required permutation
List v = new List();
// Insert all the even numbers
// less than or equal to N
for (int i = 1; i <= N; i++) {
if (i % 2 == 0) {
v.Add(i);
}
}
// Now, insert all odd numbers
// less than or equal to N
for (int i = 1; i <= N; i++) {
if (i % 2 != 0) {
v.Add(i);
}
}
// Print the Kth element
Console.WriteLine(v[K - 1]);
}
// Driver code
public static void Main(String[] args)
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
}
// This code is contributed by susmitakundugoaldanga.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Kth element
// in the required permutation
void findKthElement(int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
cout << ans;
}
// Driver Code
int main()
{
int N = 10, K = 3;
findKthElement(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the Kth element
// in the required permutation
static void findKthElement(int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
System.out.println(ans);
}
// Driver code
public static void main(String[] args)
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
}
// This code is contributed by Kingash.
Python3
# Python 3 program for the above approach
# Function to find the Kth element
# in the required permutation
def findKthElement(N, K):
# Store the required result
ans = 0
# If K is in the first
# N / 2 elements, print K * 2
if (K <= N / 2):
ans = K * 2
# Otherwise, K is greater than N/2
else:
# If N is even
if (N % 2 == 0):
ans = (K * 2) - N - 1
# If N is odd
else:
ans = (K * 2) - N
# Print the required result
print(ans)
# Driver Code
if __name__ == '__main__':
N = 10
K = 3
findKthElement(N, K)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the Kth element
// in the required permutation
static void findKthElement(int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
Console.Write(ans);
}
// Driver code
static void Main()
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出
6
时间复杂度: O(N)
辅助空间: O(N)
高效方法:为了优化上述方法,该思想基于以下观察结果:前N / 2个元素为偶数,并且前一半中的第K个元素的值等于K * 2 。如果K> N / 2 ,则第K个元素的值取决于N是奇数还是偶数。
请按照以下步骤解决问题:
- 初始化一个变量,例如ans,以存储第K个元素。
- 检查K的值是否≤N / 2 。如果确定为true,则将ans更新为K * 2 。
- 否则, K位于下半部分。在这种情况下, ans取决于N的值。
- 如果N的值是偶数,则将ans更新为(K * 2)-N-1 。
- 否则,将ans更新为(K * 2)-N 。
- 打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Kth element
// in the required permutation
void findKthElement(int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
cout << ans;
}
// Driver Code
int main()
{
int N = 10, K = 3;
findKthElement(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the Kth element
// in the required permutation
static void findKthElement(int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
System.out.println(ans);
}
// Driver code
public static void main(String[] args)
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
}
// This code is contributed by Kingash.
Python3
# Python 3 program for the above approach
# Function to find the Kth element
# in the required permutation
def findKthElement(N, K):
# Store the required result
ans = 0
# If K is in the first
# N / 2 elements, print K * 2
if (K <= N / 2):
ans = K * 2
# Otherwise, K is greater than N/2
else:
# If N is even
if (N % 2 == 0):
ans = (K * 2) - N - 1
# If N is odd
else:
ans = (K * 2) - N
# Print the required result
print(ans)
# Driver Code
if __name__ == '__main__':
N = 10
K = 3
findKthElement(N, K)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the Kth element
// in the required permutation
static void findKthElement(int N, int K)
{
// Store the required result
int ans = 0;
// If K is in the first
// N / 2 elements, print K * 2
if (K <= N / 2) {
ans = K * 2;
}
// Otherwise, K is greater than N/2
else {
// If N is even
if (N % 2 == 0) {
ans = (K * 2) - N - 1;
}
// If N is odd
else {
ans = (K * 2) - N;
}
}
// Print the required result
Console.Write(ans);
}
// Driver code
static void Main()
{
int N = 10, K = 3;
// functions call
findKthElement(N, K);
}
}
// This code is contributed by sanjoy_62.
Java脚本
输出
6
时间复杂度: O(1)
辅助空间: O(1)