给定三个整数n , m和k 。重复给定操作n次后,在第k个位置找到元素。在单个操作中,将比数组中的最大元素大的整数附加到数组,然后在原始数组之后附加原始数组。例如,单个操作后的arr [] = {3,4,1 }将是arr [] = {3,4,1,5,3,4,1} 。
请注意,数组在开头包含一个元素,即m 。
例子:
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
方法:对于蛮力方法,生成结果数组,然后在位置k找到元素。但是时间消耗和内存消耗都将很高。因此,在进行实际解决之前,让我们对问题陈述进行一些分析。
- 第一个元素将始终为m,并且无论上述步骤重复了多少次,m都会交替出现。
- 同样,第二个元素将是m + 1,并且在每个第四个元素之后重复执行。表示其位置将是2、6、10。
- 第三个元素将再次是m。
- 第四个元素是m + 2,在第8个元素之后重复出现。它的位置将是4、12、20…
通过应用反向方法后的上述分析得出结论,位置k的元素取决于k的二进制表示,即,位置k的元素等于(m-1)+ k中最右置位的位置。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
Javascript
输出:
76