# Value of k-th index of a series formed by append and insert MEX in middle

Given two integers, n and k. Initially we have a sequence consisting of a single number 1. We need to consider series formed after n steps. In each step, we append the sequence to itself and insert the **MEX**(minimum excluded)(>0) value of the sequence in the middle. Perform (n-1) steps. Finally, find the value of the k-th index of the resulting sequence.

Example:

Input : n = 3, k = 2. Output: Initially, we have {1}, we have to perform 2 steps. 1st step : {1, 2, 1}, since MEX of {1} is 2. 2nd step: {1, 2, 1, 3, 1, 2, 1}, since MEX of {1, 2, 1} is 3. Value of 2nd Index = 2. Input : n = 4, k = 8. Output: Perform 3 steps. After second step, we have {1, 2, 1, 3, 1, 2, 1} 3rd step: {1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1}, since MEX = 4. Value of 8th index = 4.

A **simple solution** is to generate the series using given steps and store in an array. Finally, we return k-th element of the array.

An **efficient solution **is to use Binary Search. Observe that the middle element of our resulting sequence is n itself. Length of the sequence is 2^{n} – 1, because lengths will be like (1, 3, 7, 15….2^{n} -1). We use Binary search to solve this problem. As we know that the middle element of a sequence is the number of the step(from 1) performed on it.

In fact, every element in the sequence is a middle element at one or other step.

We start searching the element from step n and compare the middle index with ‘k’ if found we return n, else we decrease n by 1 and update our range of our search.

we repeat this step until the index is reached.

## CPP

`// CPP program to fin k-th element after append ` `// and insert middle operations ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `void` `findElement(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `ans = n; ` `// Middle element of the sequence ` ` ` `int` `left = 1; ` ` ` ` ` `// length of the resulting sequence. ` ` ` `int` `right = ` `pow` `(2, n) - 1; ` ` ` `while` `(1) { ` ` ` `int` `mid = (left + right) / 2; ` ` ` `if` `(k == mid) { ` ` ` `cout << ans << endl; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Updating the middle element of next sequence ` ` ` `ans--; ` ` ` ` ` `// Moving to the left side of the middle element. ` ` ` `if` `(k < mid) ` ` ` `right = mid - 1; ` ` ` ` ` `// Moving to the right side of the middle element. ` ` ` `else` ` ` `left = mid + 1; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 8; ` ` ` `findElement(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to fin k-th element after append ` `// and insert middle operations ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `void` `findElement(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// Middle element of the sequence ` ` ` `int` `ans = n; ` ` ` `int` `left = ` `1` `; ` ` ` ` ` `// length of the resulting sequence. ` ` ` `int` `right = (` `int` `) (Math.pow(` `2` `, n) - ` `1` `); ` ` ` `while` `(` `true` `) ` ` ` `{ ` ` ` `int` `mid = (left + right) / ` `2` `; ` ` ` `if` `(k == mid) ` ` ` `{ ` ` ` `System.out.println(ans); ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Updating the middle element ` ` ` `// of next sequence ` ` ` `ans--; ` ` ` ` ` `// Moving to the left side of ` ` ` `// the middle element. ` ` ` `if` `(k < mid) ` ` ` `{ ` ` ` `right = mid - ` `1` `; ` ` ` `} ` ` ` ` ` `// Moving to the right side of ` ` ` `// the middle element. ` ` ` `else` ` ` `{ ` ` ` `left = mid + ` `1` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `, k = ` `8` `; ` ` ` `findElement(n, k); ` ` ` `} ` ` ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find k-th element after append ` `# and insert middle operations ` `import` `math ` ` ` `def` `findElement(n , k): ` ` ` `ans ` `=` `n ` `# Middle element of the sequence ` ` ` `left ` `=` `1` ` ` ` ` `# length of the resulting sequence. ` ` ` `right ` `=` `math.` `pow` `(` `2` `, n) ` `-` `1` ` ` `while` `1` `: ` ` ` `mid ` `=` `int` `((left ` `+` `right) ` `/` `2` `) ` ` ` `if` `k ` `=` `=` `mid: ` ` ` `print` `(ans) ` ` ` `break` ` ` ` ` `# Updating the middle element of next sequence ` ` ` `ans` `-` `=` `1` ` ` ` ` `# Moving to the left side of the middle element. ` ` ` `if` `k < mid: ` ` ` `right ` `=` `mid ` `-` `1` ` ` ` ` `# Moving to the right side of the middle element. ` ` ` `else` `: ` ` ` `left ` `=` `mid ` `+` `1` ` ` `# Driver code ` `n ` `=` `4` `k ` `=` `8` `findElement(n, k) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to fin k-th element after append ` `// and insert middle operations ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `void` `findElement(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// Middle element of the sequence ` ` ` `int` `ans = n; ` ` ` `int` `left = 1; ` ` ` ` ` `// length of the resulting sequence. ` ` ` `int` `right = (` `int` `) (Math.Pow(2, n) - 1); ` ` ` `while` `(` `true` `) ` ` ` `{ ` ` ` `int` `mid = (left + right) / 2; ` ` ` `if` `(k == mid) ` ` ` `{ ` ` ` `Console.WriteLine(ans); ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Updating the middle element ` ` ` `// of next sequence ` ` ` `ans--; ` ` ` ` ` `// Moving to the left side of ` ` ` `// the middle element. ` ` ` `if` `(k < mid) ` ` ` `{ ` ` ` `right = mid - 1; ` ` ` `} ` ` ` ` ` `// Moving to the right side of ` ` ` `// the middle element. ` ` ` `else` ` ` `{ ` ` ` `left = mid + 1; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4, k = 8; ` ` ` `findElement(n, k); ` ` ` `} ` ` ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to fin k-th element ` `// after append and insert middle ` `// operations ` ` ` `function` `findElement(` `$n` `, ` `$k` `) ` `{ ` ` ` ` ` `// Middle element of the sequence ` ` ` `$ans` `= ` `$n` `; ` ` ` `$left` `= 1; ` ` ` ` ` `// length of the resulting sequence. ` ` ` `$right` `= pow(2, ` `$n` `) - 1; ` ` ` `while` `(1) ` ` ` `{ ` ` ` `$mid` `= (` `$left` `+ ` `$right` `) / 2; ` ` ` `if` `(` `$k` `==` `$mid` `) ` ` ` `{ ` ` ` `echo` `$ans` `,` `"\n"` `; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Updating the middle element ` ` ` `// of next sequence ` ` ` `$ans` `--; ` ` ` ` ` `// Moving to the left side of ` ` ` `// the middle element. ` ` ` `if` `(` `$k` `< ` `$id` `) ` ` ` `$right` `= ` `$mid` `- 1; ` ` ` ` ` `// Moving to the right side ` ` ` `// of the middle element. ` ` ` `else` ` ` `$left` `= ` `$mid` `+ 1; ` ` ` `} ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 4; ` ` ` `$k` `= 8; ` ` ` `findElement(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:**O(log n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum index i such that all the elements from index i to given index are equal
- Queries for number of distinct elements from a given index till last index in an array
- Count of contiguous subarrays possible for every index by including the element at that index
- Number of permutations such that sum of elements at odd index and even index are equal
- Check if every index i has an index j such that sum of elements in both directions are equal
- Check if it is possible to reach to the index with value K when start index is given
- Middle of three using minimum comparisons
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Sorting array with reverse around middle
- C program to append content of one text file to another
- Append two elements to make the array satisfy the given condition
- Maximum possible middle element of the array after deleting exactly k elements
- Find kth node from Middle towards Head of a Linked List
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Count of triplets of numbers 1 to N such that middle element is always largest
- Perform append, update, delete and range sum queries on the given array
- Find Equal (or Middle) Point in a sorted array with duplicates
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Search, insert and delete in a sorted array
- Search, insert and delete in an unsorted array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.