# Reduce the array to a single element with the given operation

Given an integer **N** and an array **arr** containing integers from **1** to **N** in sorted fashion. The task is to reduce the array to a single element by performing the following operation:

All the elements at the odd positions will be removed after a single operation, this operation will be performed until only a single element is left int the array and print that element in the end.

**Examples:**

Input:N = 3

Output:2

Initially the array will be arr[] = {1, 2, 3}

After 1st operation, ‘1’ and ‘3’ will be removed and the array becomes arr[] = {2}

So 2 is the only element left at the end.

Input:N = 6

Output:4

arr[] = {1, 2, 3, 4, 5, 6}

After first iteration, array becomes {2, 4, 6}

After second iteration, array becomes {4}

So 4 is the last element.

**Approach:** For this kind of problem:

- Write multiple test cases and the respective output.
- Analyze the output for the given input and the relation between them.
- Once we find the relation we will try to express it in the form of mathematical expression if possible.
- Write the code/algorithm for the above expression.

So let’s create a table for the given input **N** and its respective output

Input(N) | Output |
---|---|

3 | 2 |

4 | 4 |

6 | 4 |

8 | 8 |

12 | 8 |

20 | 16 |

**Analyzed Relation:** The output is in **2 ^{i}**. Using the above table we can create the output table for the range of inputs

Input(N) | Output |
---|---|

2-3 | 2 |

4-7 | 4 |

8-15 | 8 |

16-31 | 16 |

32-63 | 32 |

2^{i} – 2^{i + 1} – 1 |
2^{i} |

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` ` ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the final element ` `long` `getFinalElement(` `long` `n) ` `{ ` ` ` `long` `finalNum; ` ` ` `for` `(finalNum = 2; finalNum * 2 <= n; finalNum *= 2) ` ` ` `; ` ` ` `return` `finalNum; ` `} ` ` ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 12; ` ` ` `cout << getFinalElement(N) ; ` ` ` ` ` `return` `0; ` `} ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `OddPosition { ` ` ` ` ` `// Function to return the final element ` ` ` `public` `static` `long` `getFinalElement(` `long` `n) ` ` ` `{ ` ` ` `long` `finalNum; ` ` ` `for` `(finalNum = ` `2` `; finalNum * ` `2` `<= n; finalNum *= ` `2` `) ` ` ` `; ` ` ` `return` `finalNum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `12` `; ` ` ` `System.out.println(getFinalElement(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the final element ` `def` `getFinalElement(n): ` ` ` ` ` `finalNum ` `=` `2` ` ` `while` `finalNum ` `*` `2` `<` `=` `n: ` ` ` `finalNum ` `*` `=` `2` ` ` `return` `finalNum ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `12` ` ` `print` `( getFinalElement(N)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to return the final element ` ` ` `public` `static` `long` `getFinalElement(` `long` `n) ` ` ` `{ ` ` ` `long` `finalNum; ` ` ` `for` `(finalNum = 2; finalNum * 2 <= n; finalNum *= 2) ` ` ` `; ` ` ` `return` `finalNum; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `N = 12; ` ` ` `Console.WriteLine(getFinalElement(N)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP implementation of the approach ` ` ` `// Function to return the final element ` `function` `getFinalElement(` `$n` `) ` `{ ` ` ` `$finalNum` `=0; ` ` ` `for` `(` `$finalNum` `= 2; (` `$finalNum` `* 2) <= ` `$n` `; ` `$finalNum` `*= 2) ; ` ` ` ` ` `return` `$finalNum` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$N` `= 12; ` ` ` `echo` `getFinalElement(` `$N` `) ; ` ` ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

8

## Recommended Posts:

- Check if the last element of array is even or odd after performing a operation p times
- Make all elements of an array equal with the given operation
- Find element in array that divides all array elements
- Divide every element of one array by other array elements
- Floor of every element in same array
- Finding 'k' such that its modulus with each array element is same
- Find an integer X which is divisor of all except exactly one element in an array
- Count pairs in an array such that at least one element is prime
- Replace every element of the array by product of all other elements
- Largest element in the array that is repeated exactly k times
- Find the position of the last removed element from the array
- Sum of element whose prime factors are present in array
- Find maximum operations to reduce N to 1
- Replace the maximum element in the array by coefficient of range
- Check if the array has an element which is equal to sum of all the remaining elements

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.