📌  相关文章
📜  在首先包含奇数元素然后包含偶数元素的数组中查找第K个元素

📅  最后修改于: 2021-04-21 22:20:10             🧑  作者: Mango

给定整数N和整数K的数组的长度。任务是修改阵列,所述阵列的第一包含升序从1到N的所有奇数这样的方式,再从1中的所有偶数整数N的升序,然后打印K元件修改后的数组英寸

例子:

天真的方法:一种简单的方法是先存储奇数,一个到N ,然后存储一个偶数,直到N ,然后打印第k个元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the kth element
// in the modified array
int getNumber(int n, int k)
{
    int arr[n];
  
    int i = 0;
  
    // First odd number
    int odd = 1;
    while (odd <= n) {
  
        // Insert the odd number
        arr[i++] = odd;
  
        // Next odd number
        odd += 2;
    }
  
    // First even number
    int even = 2;
    while (even <= n) {
  
        // Insert the even number
        arr[i++] = even;
  
        // Next even number
        even += 2;
    }
  
    // Return the kth element
    return arr[k - 1];
}
  
// Driver code
int main()
{
    int n = 8, k = 5;
    cout << getNumber(n, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int []arr = new int[n];
  
    int i = 0;
  
    // First odd number
    int odd = 1;
    while (odd <= n)
    {
  
        // Insert the odd number
        arr[i++] = odd;
  
        // Next odd number
        odd += 2;
    }
  
    // First even number
    int even = 2;
    while (even <= n)
    {
  
        // Insert the even number
        arr[i++] = even;
  
        // Next even number
        even += 2;
    }
  
    // Return the kth element
    return arr[k - 1];
}
  
// Driver code
public static void main(String[] args)
{
    int n = 8, k = 5;
    System.out.println(getNumber(n, k));
}
} 
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return the kth element
# in the modified array
def getNumber(n, k):
    arr = [0] * n;
  
    i = 0;
  
    # First odd number
    odd = 1;
    while (odd <= n):
          
        # Insert the odd number
        arr[i] = odd;
        i += 1;
  
        # Next odd number
        odd += 2;
  
    # First even number
    even = 2;
    while (even <= n):
        # Insert the even number
        arr[i] = even;
        i += 1;
  
        # Next even number
        even += 2;
  
    # Return the kth element
    return arr[k - 1];
  
# Driver code
if __name__ == '__main__':
    n = 8;
    k = 5;
    print(getNumber(n, k));
  
# This code is contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int []arr = new int[n];
  
    int i = 0;
  
    // First odd number
    int odd = 1;
    while (odd <= n)
    {
  
        // Insert the odd number
        arr[i++] = odd;
  
        // Next odd number
        odd += 2;
    }
  
    // First even number
    int even = 2;
    while (even <= n)
    {
  
        // Insert the even number
        arr[i++] = even;
  
        // Next even number
        even += 2;
    }
  
    // Return the kth element
    return arr[k - 1];
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 8, k = 5;
    Console.WriteLine(getNumber(n, k));
}
} 
  
// This code is contributed by PrinciRaj1992


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the kth element
// in the modified array
int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if (n % 2 == 0) {
        pos = n / 2;
    }
    else {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos) {
        return (k * 2 - 1);
    }
    else
  
        return ((k - pos) * 2);
}
  
// Driver code
int main()
{
    int n = 8, k = 5;
  
    cout << getNumber(n, k);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG
{
      
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if ((n % 2) == 0) 
    {
        pos = n / 2;
    }
    else 
    {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos)
    {
        return (k * 2 - 1);
    }
    else
        return ((k - pos) * 2);
}
  
// Driver code
public static void main (String[] args)
{
    int n = 8, k = 5;
    System.out.println (getNumber(n, k));
}
}
  
// This code is contributed by @tushil.


Python3
# Python3 implementation of the approach 
  
# Function to return the kth element 
# in the modified array 
def getNumber(n, k) : 
  
    # Finding the index from where the 
    # even numbers will be stored 
    if (n % 2 == 0) : 
        pos = n // 2; 
      
    else :
        pos = (n // 2) + 1; 
  
    # Return the kth element 
    if (k <= pos) :
        return (k * 2 - 1); 
          
    else :
        return ((k - pos) * 2); 
  
# Driver code 
if __name__ == "__main__" :
    n = 8; k = 5;
      
    print(getNumber(n, k)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if ((n % 2) == 0) 
    {
        pos = n / 2;
    }
    else
    {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos)
    {
        return (k * 2 - 1);
    }
    else
        return ((k - pos) * 2);
}
  
// Driver code
static public void Main ()
{
    int n = 8, k = 5;
    Console.Write(getNumber(n, k));
}
}
  
// This code is contributed by @ajit.


输出:
2

高效的方法:找到将第一个偶数元素存储在生成的数组中的索引。现在,如果k的值小于或等于index,则所需的数字将为k * 2-1 –否则,所需的数字将为(k-索引)* 2

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the kth element
// in the modified array
int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if (n % 2 == 0) {
        pos = n / 2;
    }
    else {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos) {
        return (k * 2 - 1);
    }
    else
  
        return ((k - pos) * 2);
}
  
// Driver code
int main()
{
    int n = 8, k = 5;
  
    cout << getNumber(n, k);
  
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
  
class GFG
{
      
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if ((n % 2) == 0) 
    {
        pos = n / 2;
    }
    else 
    {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos)
    {
        return (k * 2 - 1);
    }
    else
        return ((k - pos) * 2);
}
  
// Driver code
public static void main (String[] args)
{
    int n = 8, k = 5;
    System.out.println (getNumber(n, k));
}
}
  
// This code is contributed by @tushil. 

Python3

# Python3 implementation of the approach 
  
# Function to return the kth element 
# in the modified array 
def getNumber(n, k) : 
  
    # Finding the index from where the 
    # even numbers will be stored 
    if (n % 2 == 0) : 
        pos = n // 2; 
      
    else :
        pos = (n // 2) + 1; 
  
    # Return the kth element 
    if (k <= pos) :
        return (k * 2 - 1); 
          
    else :
        return ((k - pos) * 2); 
  
# Driver code 
if __name__ == "__main__" :
    n = 8; k = 5;
      
    print(getNumber(n, k)); 
  
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if ((n % 2) == 0) 
    {
        pos = n / 2;
    }
    else
    {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos)
    {
        return (k * 2 - 1);
    }
    else
        return ((k - pos) * 2);
}
  
// Driver code
static public void Main ()
{
    int n = 8, k = 5;
    Console.Write(getNumber(n, k));
}
}
  
// This code is contributed by @ajit.
输出:
2