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 position int 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 code int main() { int k = 100, n = 9, m = 74; cout << findValueAtK(n, m, k); return 0; } |
Java
// Java implementation of the approach 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 (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 approach import math # Function to find value # at k-th position def 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 code k = 100n = 9m = 74print(findValueAtK(n, m, k)) # This code is contributed # by mohit kumar |
C#
// C# implementation of the approach using 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 position function 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 ?> |
76
Recommended Posts:
- Find the Kth smallest element in the sorted generated array
- Find determinant of matrix generated by array rotation
- Find the position of the last removed element from the array
- Find the element in the matrix generated by given rules
- Find the sum of elements of the Matrix generated by the given rules
- Find position of the only set bit
- Find the position of box which occupies the given ball
- Find letter's position in Alphabet using Bit operation
- Find position of left most dis-similar bit for two numbers
- Find element position in given monotonic sequence
- Find position of the given number among the numbers made of 4 and 7
- Complete the sequence generated by a polynomial
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Find minimum value to assign all array elements so that array product becomes greater
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



