📌  相关文章
📜  重复删除第一个元素并将其添加到数组末尾两次恰好 K 次后数组的最后一个元素

📅  最后修改于: 2021-10-26 06:53:54             🧑  作者: Mango

给定一个由N 个整数和一个正整数K组成的数组arr[] ,任务是通过重复删除数组的第一个元素并将其两次附加到数组末尾K次,找到数组中存在的最后一个元素.

例子:

方法:可以通过观察以下模式来解决给定的问题:

请按照以下步骤解决问题:

  • 使用变量j从值0迭代一个循环,并将K的值更新为(K – N * 2 j ),直到K大于N * 2 j
  • 完成上述步骤后,初始化一个变量,比如r2 j ,其中每个值重复R次。
  • 初始化一个变量,比如M1 ,它存储数组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 现场工作专业课程学生竞争性编程现场课程