Find the value at kth position in the generated array
Last Updated :
25 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findValueAtK( int n, int m, int k)
{
int positionOfRightmostSetbit = __builtin_ffs(k);
return ((m - 1) + positionOfRightmostSetbit);
}
int main()
{
int k = 100, n = 9, m = 74;
cout << findValueAtK(n, m, k);
return 0;
}
|
Java
class GFG
{
static int INT_SIZE = 32 ;
static int Right_most_setbit( int num)
{
int pos = 1 ;
for ( int i = 0 ; i < INT_SIZE; i++)
{
if ((num & ( 1 << i))== 0 )
pos++;
else
break ;
}
return pos;
}
static int findValueAtK( int n, int m, int k)
{
int positionOfRightmostSetbit = Right_most_setbit(k);
return ((m - 1 ) + positionOfRightmostSetbit);
}
public static void main (String[] args)
{
int k = 100 , n = 9 , m = 74 ;
System.out.println(findValueAtK(n, m, k));
}
}
|
Python3
import math
def findValueAtK(n, m, k):
positionOfRightmostSetbit = math.log2(k & - k) + 1
return ((m - 1 ) + positionOfRightmostSetbit)
k = 100
n = 9
m = 74
print (findValueAtK(n, m, k))
|
C#
using System;
class GFG
{
static int INT_SIZE = 32;
static int Right_most_setbit( int num)
{
int pos = 1;
for ( int i = 0; i < INT_SIZE; i++)
{
if ((num & (1 << i)) == 0)
pos++;
else
break ;
}
return pos;
}
static int findValueAtK( int n, int m, int k)
{
int positionOfRightmostSetbit = Right_most_setbit(k);
return ((m - 1) + positionOfRightmostSetbit);
}
public static void Main ()
{
int k = 100, n = 9, m = 74;
Console.WriteLine(findValueAtK(n, m, k));
}
}
|
PHP
<?php
function findValueAtK( $n , $m , $k )
{
$temp = $k & - $k ;
$positionOfRightmostSetbit = log( $temp , 2) + 1;
return (( $m - 1) + $positionOfRightmostSetbit );
}
$k = 100;
$n = 9;
$m = 74;
echo findValueAtK( $n , $m , $k );
?>
|
Javascript
function findValueAtK(n, m, k) {
let positionOfRightmostSetbit = Math.log2(k & -k) + 1;
return m + positionOfRightmostSetbit - 1;
}
let k = 100, n = 9, m = 74;
console.log(findValueAtK(n, m, k));
|
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...