# Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times

• Last Updated : 24 Dec, 2021

Given an array arr[] consisting of N integers and a positive integer K, the task is to find the last element present in the array obtained by repeatedly removing the first element of the array and appending it twice to the end of the array K times.

Examples:

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 modifies 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

Approach: The given problem can be solved by observing the following pattern:

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.

Follow the steps below to solve the problem:

• Iterate a loop from the value 0 using the variable j and update the value of K as (K – N * 2j) until K is greater than N * 2j.
• After completing the above steps, initialize a variable, say r as 2j, where each value repeats itself R number of times.
• Initialize a variable, say M as 1, that stores the index of the character in the array arr[] which is equal to the last character after performing K operations.
• Iterate over the range [1, N] using the variable i and if the value of K is greater than R * i, then increment M by 1.
• After completing the above steps, print the value of arr[M – 1] as the resultant last element of the array.

Below is the implementation of the above approach:

## 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<``int``> 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<``int``> 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

 ``

Output:

`2`

Time Complexity: O(log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up