Given a circular array of size n containing integers from 1 to n. Find the last element that would remain in the list after erasing every second element starting from the first element.
Input: 5 Output: 3 Explanation Element in circular array are: 1 2 3 4 5 Starting from first element i.e, '1' delete every second element like this, 1 0 3 4 5 1 0 3 0 5 0 0 3 0 5 0 0 3 0 0 For demonstration purpose erased element would be treated as '0'. Thus at the end of list, the last element remains is 3. Input: 10 Output: 5
The Naive approach is to remove every second element from the array until the size of array becomes equals to ‘1’. Time complexity of this approach is O(n2) which would not be feasible for large value of ‘n’.
The Efficient approach is to use recursion. Let’s consider n to be even. In one traversal, numbers 2, 4, 6 … N will be removed and we start again from 1. Thus exactly n/2 numbers are removed, and we start as if from 1 in an array of N/2 containing only odd digits 1, 3, 5, … n/2.
Thus by this intuation, their recursive formula can be written as,
If n is even: solve(n) = 2 * solve(n/2) - 1 else solve(n) = 2 * solve((n-1) / 2) + 1 Base condition would occur when n = 1, then answer would be 1.
Time complexity: O(log(n))
Auxiliary space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Maximum possible middle element of the array after deleting exactly k elements
- Find the largest after deleting the given elements
- Find the smallest after deleting given elements
- Find the k largest numbers after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Change in Median of given array after deleting given elements
- Count distinct elements after adding each element of First Array with Second Array
- Count of strings in the first array which are smaller than every string in the second array
- Find just strictly greater element from first array for each element in second array
- Find maximum value of the last element after reducing the array with given operations
- Find last remaining element after reducing the Array
- Last seen array element (last appearance is earliest)
- Maximum score of deleting an element from an Array based on given condition
- Find maximum points which can be obtained by deleting elements from array
- Find a number which give minimum sum when XOR with every number of array of integers
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Array obtained by repeatedly reversing array after every insertion from given array
- Check if the last element of array is even or odd after performing a operation p times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t