Given an array **arr[]** of size **N** and an integer **K**. The task is to find the last remaining element in the array after reducing the array. The rules for reducing the array are:

- The first and last element say X and Y are chosen and removed from the array arr[].
- The values X and Y are added. Z = X + Y.
- Insert the value of
**Z % K**into the array arr[] at the position**((N/2) + 1)**position, where N denotes the current length of the array.^{th}

Examples:

Input:N = 5, arr[] = {1, 2, 3, 4, 5}, K = 7

Output:1

Explanation:

The given array arr[] reduces as follows:

{1, 2, 3, 4, 5} -> {2, 6, 3, 4}

{2, 6, 3, 4} -> {6, 6, 3}

{6, 6, 3} -> {2, 6}

{2, 6} -> {1}

The last element of A is 1.

Input:N = 5, arr[] = {2, 4, 7, 11, 3}, K = 12

Output:3

Explanation:

The given array arr[] reduces as follows:

{2, 4, 7, 11, 3} -> {4, 5, 7, 11}

{4, 5, 7, 11} -> {5, 3, 7}

{5, 3, 7} -> {0, 3}

{0, 3} -> {3}

The last element of A is 3.

**Naive approach:** The naive approach for this problem is that at every step, find the first element and last element in the array and compute **(X + Y) % K** where X is the first element and Y is the last element of the array at every step. After computing this value, insert this value at the given position.

**Time Complexity:** O(N^{2})

**Efficient Approach:**

- On observing carefully, it can be said that the sum of the elements of the array modulo K is never changed in the array throughout.
- This is because we are basically inserting the value
**X + Y % K**back into the array. - Therefore, this problem can be solved in linear time by directly finding the sum of the array and finding the value
**sum % K**.

Below is the implementation of the above approach:

## C++

`// C++ program to find the value of the ` `// reduced Array by reducing the array ` `// based on the given conditions ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find the value of the ` `// reduced Array by reducing the array ` `// based on the given conditions ` `int` `find_value(` `int` `a[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// For loop to iterate through the ` ` ` `// given array and find the sum ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Return the required value ` ` ` `return` `sum % k; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 3; ` ` ` `int` `a[] = { 12, 4, 13, 0, 5 }; ` ` ` `cout << find_value(a, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the value of the ` `// reduced Array by reducing the array ` `// based on the given conditions ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to find the value of the ` ` ` `// reduced Array by reducing the array ` ` ` `// based on the given conditions ` ` ` `public` `static` `int` `find_value(` `int` `a[], ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// For loop to iterate through the ` ` ` `// given array and find the sum ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Return the required value ` ` ` `return` `sum % k; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, k = ` `3` `; ` ` ` `int` `a[] = { ` `12` `, ` `4` `, ` `13` `, ` `0` `, ` `5` `}; ` ` ` `System.out.println(find_value(a, n, k)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the value of the ` `# reduced Array by reducing the array ` `# based on the given conditions ` ` ` `# Function to find the value of the ` `# reduced Array by reducing the array ` `# based on the given conditions ` `def` `find_value(a, n, k): ` ` ` ` ` `# Variable to store the sum ` ` ` `sum` `=` `0` ` ` ` ` `# For loop to iterate through the ` ` ` `# given array and find the sum ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `sum` `+` `=` `a[i] ` ` ` ` ` `# Return the required value ` ` ` `return` `sum` `%` `k ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n, k ` `=` `5` `, ` `3` `; ` ` ` `a ` `=` `[` `12` `, ` `4` `, ` `13` `, ` `0` `, ` `5` `]; ` ` ` `print` `(find_value(a, n, k)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the value of the ` `// reduced Array by reducing the array ` `// based on the given conditions ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the value of the ` ` ` `// reduced Array by reducing the array ` ` ` `// based on the given conditions ` ` ` `public` `static` `int` `find_value(` `int` `[]a, ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// For loop to iterate through the ` ` ` `// given array and find the sum ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += a[i]; ` ` ` `} ` ` ` ` ` `// Return the required value ` ` ` `return` `sum % k; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `n = 5, k = 3; ` ` ` `int` `[]a = { 12, 4, 13, 0, 5 }; ` ` ` `Console.WriteLine(find_value(a, n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(N)

## Recommended Posts:

- Find maximum value of the last element after reducing the array with given operations
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Check if sum of array can be reduced to zero by repetitively reducing array element by their index value
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Find last element after deleting every second element in array of n integers
- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- Find the last remaining Character in the Binary String according to the given conditions
- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Maximize the cost of reducing array elements
- Find the sum of remaining sticks after each iterations
- Average of remaining elements after removing K largest and K smallest elements from array
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Last seen array element (last appearance is earliest)
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Check if the array has an element which is equal to XOR of remaining elements
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of 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 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.