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 = 100 n = 9 m = 74 print (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 ?> |
Javascript
// JavaScript implementation of the approach function findValueAtK(n, m, k) { // __builtin_ffsll return the position // of rightmost setbit let positionOfRightmostSetbit = Math.log2(k & -k) + 1; // Return the required element return m + positionOfRightmostSetbit - 1; } // Driver code let 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.
Contact Us