Saturday, October 5, 2024
Google search engine
HomeData Modelling & AIFind the Kth position element of the given sequence

Find the Kth position element of the given sequence

Given two integers N and K, the task is to find the element at the Kth position if all odd numbers from 1 to N are written down in increasing order followed by all the even numbers from 1 to N in increasing order.
Examples: 
 

Input: N = 10, K = 3 
Output:
The required sequence is 1, 3, 5, 7, 9, 2, 4, 6, 8 and 10.
Input: N = 7, K = 7 
Output:
 

 

Approach: It is known that the Nth even number is given by 2 * K and the Nth odd number is given by 2 * K – 1. But since the even numbers are written after (N + 1) / 2 odd numbers here. Therefore, Kth even number is given by 2 * (K – (N + 1) / 2) and the odd numbers will remain the same as 2 * K – 1
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the kth number
// from the required sequence
int kthNum(int n, int k)
{
 
    // Count of odd integers
    // in the sequence
    int a = (n + 1) / 2;
 
    // kth number is even
    if (k > a)
        return (2 * (k - a));
 
    // It is odd
    return (2 * k - 1);
}
 
// Driver code
int main()
{
    int n = 7, k = 7;
 
    cout << kthNum(n, k);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
// Function to return the kth number
// from the required sequence
static int kthNum(int n, int k)
{
 
    // Count of odd integers
    // in the sequence
    int a = (n + 1) / 2;
 
    // kth number is even
    if (k > a)
        return (2 * (k - a));
 
    // It is odd
    return (2 * k - 1);
}
 
// Driver code
public static void main(String []args)
{
    int n = 7, k = 7;
 
    System.out.println(kthNum(n, k));
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 implementation of the approach
 
# Function to return the kth number
# from the required sequence
def kthNum(n, k) :
 
    # Count of odd integers
    # in the sequence
    a = (n + 1) // 2;
 
    # kth number is even
    if (k > a) :
        return (2 * (k - a));
 
    # It is odd
    return (2 * k - 1);
 
# Driver code
if __name__ == "__main__" :
 
    n = 7; k = 7;
 
    print(kthNum(n, k));
 
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to return the kth number
// from the required sequence
static int kthNum(int n, int k)
{
 
    // Count of odd integers
    // in the sequence
    int a = (n + 1) / 2;
 
    // kth number is even
    if (k > a)
        return (2 * (k - a));
 
    // It is odd
    return (2 * k - 1);
}
 
// Driver code
public static void Main(String []args)
{
    int n = 7, k = 7;
 
    Console.WriteLine(kthNum(n, k));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the kth number
// from the required sequence
function kthNum(n, k)
{
    // Count of odd integers
    // in the sequence
    var a = (n + 1) / 2;
 
    // kth number is even
    if (k > a)
        return (2 * (k - a));
 
    // It is odd
    return (2 * k - 1);
}
 
// Driver code
var n = 7, k = 7;
document.write(kthNum(n, k));
 
</script>


Output: 

6

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments