给定一个由N 个整数和一个正整数K组成的数组arr[] ,任务是通过重复删除数组的第一个元素并将其两次附加到数组末尾K次,找到数组中存在的最后一个元素.
例子:
Input: arr[] = {1, 2, 3, 4, 5}, K = 5
Output: 5
Explanation:
Initially, the array is {1, 2, 3, 4, 5}. Following operations are performed K(= 5) number of times:
After the 1st operation, the array modifies to {2, 3, 4, 5, 1, 1}.
After the 2nd operation, the array modifes to {3, 4, 5, 1, 1, 2, 2}.
After the 3rd operation, the array modifies to {4, 5, 1, 1, 2, 2, 3, 3}.
After the 4th operation, the array modifies to {5, 1, 1, 2, 2, 3, 3, 4, 4}.
After the 5th operation, the array modifies to {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}.
After completing the above operations, the last element is 5. Therefore, the answer is 5.
Input: arr[] = {1, 2, 3}, K = 7
Output: 2
方法:可以通过观察以下模式来解决给定的问题:
Consider an array arr[] = {1, 2, 3, 4, 5}
After first 5 operations, the array modifies to {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}.
After first 10 operations, the array modifies to {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}.
Therefore, the size of the array after N * 2K operations is 2 * N * 2K.
请按照以下步骤解决问题:
- 使用变量j从值0迭代一个循环,并将K的值更新为(K – N * 2 j ),直到K大于N * 2 j 。
- 完成上述步骤后,初始化一个变量,比如r为2 j ,其中每个值重复R次。
- 初始化一个变量,比如M为1 ,它存储数组arr[] 中字符的索引,该索引等于执行K 次操作后的最后一个字符。
- 使用变量i在范围[1, N] 上迭代,如果K的值大于R * i ,则将M增加1 。
- 完成上述步骤后,打印arr[M – 1] 的值作为结果数组的最后一个元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the last element
// after performing given operations
void findLastElement(int N, vector A)
{
// Length of the array
int l = A.size();
int j = 0;
// Increment j until
// condition is satisfied
while (N > l * (pow(2, j)))
{
N = N - l * pow(2, j);
j += 1;
}
int k = 1;
// In each pair every value is
// repeating r number of times
int r = pow(2, j);
for(int i = 1; i < l; i++)
{
if (N > r * i)
k += 1;
}
// Print the result according
// to the value of k
for(int i = 0; i < l; i++)
{
if (i + 1 == k)
{
cout << (A[i]);
return;
}
}
}
// Driver Code
int main()
{
// Given K
int K = 7;
// Given arr[]
vector A = { 1, 2, 3 };
// Function call
findLastElement(K, A);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the last element
// after performing given operations
static void findLastElement(int N, int[] A)
{
// Length of the array
int l = A.length;
int j = 0;
// Increment j until
// condition is satisfied
while (N > l * (int)(Math.pow(2, j))) {
N = N - l * (int)Math.pow(2, j);
j += 1;
}
int k = 1;
// In each pair every value is
// repeating r number of times
int r = (int)Math.pow(2, j);
for (int i = 1; i < l; i++) {
if (N > r * i)
k += 1;
}
// Print the result according
// to the value of k
for (int i = 0; i < l; i++) {
if (i + 1 == k) {
System.out.print(A[i]);
return;
}
}
}
// Driver Code
public static void main(String[] args)
{
// Given K
int K = 7;
// Given arr[]
int[] A = { 1, 2, 3 };
// Function call
findLastElement(K, A);
}
}
// This code is contributed by subham348.
Python3
# Python program for the above approach
# Function to find the last element
# after performing given operations
def findLastElement(N, A):
# Length of the array
l = len(A)
j = 0
# Increment j until
# condition is satisfied
while(N > l*(2**j)):
N = N - l * 2**j
j += 1
k = 1
# In each pair every value is
# repeating r number of times
r = 2**j
for i in range(1, l):
if N > r * i:
k += 1
# Print the result according
# to the value of k
for i in range(0, len(A)):
if(i + 1 == k):
print(A[i])
return
# Driver Code
if __name__ == '__main__':
# Given K
K = 7
# Given arr[]
A = [1, 2, 3]
# Function call
findLastElement(K, A)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the last element
// after performing given operations
static void findLastElement(int N, int[] A)
{
// Length of the array
int l = A.Length;
int j = 0;
// Increment j until
// condition is satisfied
while (N > l * (int)(Math.Pow(2, j))) {
N = N - l * (int)Math.Pow(2, j);
j += 1;
}
int k = 1;
// In each pair every value is
// repeating r number of times
int r = (int)Math.Pow(2, j);
for (int i = 1; i < l; i++) {
if (N > r * i)
k += 1;
}
// Print the result according
// to the value of k
for (int i = 0; i < l; i++) {
if (i + 1 == k) {
Console.WriteLine(A[i]);
return;
}
}
}
// Driver Code
public static void Main(String[] args)
{
// Given K
int K = 7;
// Given arr[]
int[] A = { 1, 2, 3 };
// Function call
findLastElement(K, A);
}
}
// This code is contributed by target_62.
Javascript
2
时间复杂度: O(log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。