Find the value at kth position in the generated array

Given three integer n, m and k. Find the element at kth position after repeating the given operation n number of times. In a single operation, an integer one greater than the maximum element from the array is appended to the array and the original array gets appended after that. For example, arr[] = {3, 4, 1} after a single operation will be arr[] = {3, 4, 1, 5, 3, 4, 1}.
Note that the array contains a single element in the beginning which is m.
Examples:
Input: n = 3, m = 3, k = 3
Output: 3
Array after each steps:
Operation 1: arr[] = {3, 4, 3}
Operation 2: arr[] = {3, 4, 3, 5, 3, 4, 3}
Operation 3: arr[] = {3, 4, 3, 5, 3, 4, 3, 6, 3, 4, 3, 5, 3, 4, 3}Input: n = 9, m = 74, k = 100
Output: 76
Approach: For the brute force approach, generate the resultant array and then after find the element at position k. But the time consumption as well as memory consumption will be quite high. So, let’s perform some analysis of problem statement before proceeding to actual solution.
- First element will always be m and no matter how many times above mentioned step got repeated m will occur alternatively.
- Similarly second element will be m+1 and it got repeated after each 4th element. Means its position will be 2, 6, 10..
- Third element will again be m.
- Fourth element will be m+2 and it got repeated after each 8th element.Means its position will be 4, 12, 20…
From the above analysis after applying the reverse approach there is a conclusion that element at position k depends upon binary representation of k and i.e. The element at position is equal to (m-1) + position of right most set bit in k.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to find value at k-th positionint findValueAtK(int n, int m, int k){ // __builtin_ffsll return the position // of rightmost setbit int positionOfRightmostSetbit = __builtin_ffs(k); // Return the required element return ((m - 1) + positionOfRightmostSetbit);}// Driver codeint main(){ int k = 100, n = 9, m = 74; cout << findValueAtK(n, m, k); return 0;} |
Java
// Java implementation of the approachclass GFG{ static int INT_SIZE = 32; // function returns the position // of rightmost setbit static int Right_most_setbit(int num) { int pos = 1; // counting the position of first set bit for (int i = 0; i < INT_SIZE; i++) { if ((num & (1 << i))== 0) pos++; else break; } return pos; } // Function to find value at k-th position static int findValueAtK(int n, int m, int k) { int positionOfRightmostSetbit = Right_most_setbit(k); // Return the required element return ((m - 1) + positionOfRightmostSetbit); } // Driver code public static void main (String[] args) { int k = 100, n = 9, m = 74; System.out.println(findValueAtK(n, m, k)); }}// This code is contributed by ihritik |
Python3
# Python3 implementation of the approachimport math # Function to find value # at k-th positiondef findValueAtK(n, m, k): # __builtin_ffsll return the position # of rightmost setbit positionOfRightmostSetbit = math.log2(k & -k) + 1 # Return the required element return ((m - 1) + positionOfRightmostSetbit)# Driver codek = 100n = 9m = 74print(findValueAtK(n, m, k))# This code is contributed # by mohit kumar |
C#
// C# implementation of the approachusing System;class GFG{ static int INT_SIZE = 32; // function returns the position // of rightmost setbit static int Right_most_setbit(int num) { int pos = 1; // counting the position of first set bit for (int i = 0; i < INT_SIZE; i++) { if ((num & (1 << i)) == 0) pos++; else break; } return pos; } // Function to find value at k-th position static int findValueAtK(int n, int m, int k) { int positionOfRightmostSetbit = Right_most_setbit(k); // Return the required element return ((m - 1) + positionOfRightmostSetbit); } // Driver code public static void Main () { int k = 100, n = 9, m = 74; Console.WriteLine(findValueAtK(n, m, k)); }}// This code is contributed by ihritik |
PHP
<?php// PHP implementation of the approach // Function to find value // at k-th positionfunction findValueAtK($n, $m, $k){ // __builtin_ffsll return the position // of rightmost setbit $temp = $k & -$k ; $positionOfRightmostSetbit = log($temp, 2) + 1; // Return the required element return (($m - 1) + $positionOfRightmostSetbit);}// Driver code$k = 100;$n = 9;$m = 74;echo findValueAtK($n, $m, $k);// This code is contributed by Ryuga?> |
Javascript
// JavaScript implementation of the approachfunction findValueAtK(n, m, k) {// __builtin_ffsll return the position// of rightmost setbitlet positionOfRightmostSetbit = Math.log2(k & -k) + 1;// Return the required elementreturn m + positionOfRightmostSetbit - 1;}// Driver codelet k = 100, n = 9, m = 74;console.log(findValueAtK(n, m, k)); |
76
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



