Reduce the array by replacing 1st and middle element with sum and difference alternatively
Last Updated :
24 Feb, 2022
Given an array arr[] of size N, the task is to find the last remaining element of the array after consecutively removing the 1st and the middle element of the array and alternatively appending their sum and difference at the end of the array.
Examples:
Input: A = {2, 4, 1, 5, 7}
Output: 5
Explanation: During the 1st iteration, remove arr[0] and arr[2] from the array
and append their sum (2 + 1 = 3) to the end of the array.
Hence, arr[] = {4, 5, 7, 3}.
Again repeat the same process and remove arr[0] and arr[2] from the array
and now append their difference (7 – 4) = 3.
Hence, arr[] = {5, 3, 3}.
After the next iteration, array will become arr[] = {3, 8}.
And finally after the last iteration, arr[] = {5} which is the last required value.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Output: 15
Approach: The given problem is an implementation-based problem and can be solved by repetitively following the given steps:
- Maintain a variable op, which stores the count of operation.
- Remove the first and middle element of the array till its size is more than 1 and append the array with their addition or subtraction according to the op (i., e if op is even, perform addition, otherwise perform subtraction).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lastElement(vector< int >& arr)
{
int op = 0, x = 0;
while (arr.size() != 1) {
int mid = arr.size() / 2;
if (op % 2 == 0) {
x = arr[mid] + arr[0];
arr.erase(arr.begin() + mid);
arr.erase(arr.begin());
}
else {
x = arr[mid] - arr[0];
arr.erase(arr.begin() + mid);
arr.erase(arr.begin());
}
arr.push_back(x);
op += 1;
}
return arr[0];
}
int main()
{
vector< int > arr = { 2, 4, 1, 5, 7 };
cout << lastElement(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int lastElement(Vector<Integer> arr)
{
int op = 0 , x = 0 ;
while (arr.size() != 1 ) {
int mid = arr.size() / 2 ;
if (op % 2 == 0 ) {
x = arr.get(mid) + arr.get( 0 );
arr.remove(mid);
arr.remove( 0 );
}
else {
x = arr.get(mid) - arr.get( 0 );
arr.remove(mid);
arr.remove( 0 );
}
arr.add(x);
op += 1 ;
}
return arr.get( 0 );
}
public static void main(String[] args)
{
Integer []arr = { 2 , 4 , 1 , 5 , 7 };
Vector<Integer> v = new Vector<Integer>();
Collections.addAll(v, arr);
System.out.print(lastElement(v));
}
}
|
Python3
def lastElement(arr):
op = 0
while len (arr) ! = 1 :
mid = len (arr) / / 2
if op % 2 = = 0 :
x = arr.pop(mid) + arr.pop( 0 )
else :
x = arr.pop(mid) - arr.pop( 0 )
arr.append(x)
op + = 1
return arr[ 0 ]
if __name__ = = "__main__" :
arr = [ 2 , 4 , 1 , 5 , 7 ]
print (lastElement(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int lastElement(List< int > arr)
{
int op = 0, x = 0;
while (arr.Count != 1) {
int mid = arr.Count / 2;
if (op % 2 == 0) {
x = arr[mid] + arr[0];
arr.RemoveAt(mid);
arr.RemoveAt(0);
}
else {
x = arr[mid] - arr[0];
arr.RemoveAt(mid);
arr.RemoveAt(0);
}
arr.Add(x);
op += 1;
}
return arr[0];
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 1, 5, 7 };
List< int > v = new List< int >(arr);
Console.Write(lastElement(v));
}
}
|
Javascript
<script>
function lastElement(arr) {
let op = 0, x = 0;
while (arr.length != 1) {
let mid = Math.floor(arr.length / 2);
if (op % 2 == 0) {
x = arr[mid] + arr[0];
let p1 = arr.slice(1, mid)
let p2 = arr.slice(mid + 1)
arr = [...p1.concat(p2)];
}
else {
x = arr[mid] - arr[0];
let p1 = arr.slice(1, mid)
let p2 = arr.slice(mid + 1)
arr = [...p1.concat(p2)];
}
arr.push(x);
op = op + 1;
}
return arr[0];
}
let arr = [2, 4, 1, 5, 7];
document.write(lastElement(arr));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...