Reduce given Array by replacing adjacent elements with their difference Last Updated : 21 Oct, 2022 Summarize Suggest changes Share Like Article Like Report Given an array arr[] consisting of N elements(such that N = 2k for some k ? 0), the task is to reduce the array and find the last remaining element after merging all elements into one. The array is reduced by performing the following operation: Merge the adjacent elements i.e merge elements at indices 0 and 1 into one, 2 and 3 into one and so on. Upon merging the newly formed element will become the absolute difference between the two elements merged. Examples: Input: N = 4, arr[] = [1, 2, 3, 4]Output: 0Explanation: First operation:On merging 1st and 2nd elements we will have a element with value1. On merging 3rd and 4th elements, we will have a element with value1. Therefore, we are left with two elements where each of them having cost 1.Second operation:On merging the 1st and 2nd elements we will get a new element with value 0.This is because both elements had the same value of 1. Input: N = 1, arr[] = [20]Output: 20Explanation: We can't perform any operation because performing an operation requires at least 2 elements. Hence, 20 is cost of the last remaining element Approach: This problem can be solved using the Divide and Conquer approach. Create a recursive function.The base condition for recursion will be if the size of the array is 1 then the answer will be the only array element in it.Return the absolute difference between the first half of the array and the second half of the array by calling the recursive function for both halves.Merge both halves and get the answer. Below is the implementation of the above approach: C++ // C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to get the last remaining element // by using divide and conquer int f(int l, int e, int a[]) { // Base condition if (l == e) return a[l]; return abs(f(l, l + (e - l) / 2, a) - f(l + (e - l) / 2 + 1, e, a)); } int find(int n, int a[]) { return f(0, n - 1, a); } // Driver code int main() { int arr[] = { 1, 2, 3, 4 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << find(N, arr); return 0; } Java // Java code to implement the approach import java.io.*; class GFG { // Function to get the last remaining element // by using divide and conquer static int f(int l, int e, int[] a) { // Base condition if (l == e) { return a[l]; } return Math.abs(f(l, l + (e - l) / 2, a) - f(l + (e - l) / 2 + 1, e, a)); } static int find(int n, int[] a) { return f(0, n - 1, a); } public static void main(String[] args) { int[] arr = { 1, 2, 3, 4 }; int N = arr.length; // Function call System.out.print(find(N, arr)); } } // This code is contributed by lokeshmvs21. Python3 # Python code to implement the approach # Function to get the last remaining element # by using divide and conquer import sys sys.setrecursionlimit(1500) def f(l, e, a): # Base condition if (l == e): return a[l] return abs(f(l, l + (e - l) // 2, a) - f(l + (e - l) // 2 + 1, e, a)) def find(n, a): return f(0, n - 1, a) # Driver code if __name__ == "__main__": arr = [1, 2, 3, 4] N = len(arr) # Function Call print(find(N, arr)) # This code is contributed by Rohit Pradhan C# // C# implementation using System; public class GFG{ // Function to get the last remaining element // by using divide and conquer public static int f(int l, int e, int []a) { // Base condition if (l == e) return a[l]; return Math.Abs(f(l, l + (e - l) / 2, a) - f(l + (e - l) / 2 + 1, e, a)); } public static int find(int n, int []a) { return f(0, n - 1, a); } static public void Main (){ int []arr = { 1, 2, 3, 4 }; int N = arr.Length; // Function Call Console.WriteLine(find(N, arr)); } } // This code is contributed by ksam24000 JavaScript // Javascript code to implement the approach // Function to get the last remaining element // by using divide and conquer function f(l, e, a) { // Base condition if (l == e) { return a[l]; } return Math.abs(f(l, l + Math.floor((e - l) / 2), a) - f(l + Math.floor((e - l) / 2) + 1, e, a)); } function find(n, a) { return f(0, n - 1, a); } let arr = [1, 2, 3, 4]; let N = arr.length; // Function call console.log(find(N, arr)); // This code is contributed by Saurabh Jaiswal Output0 Time Complexity: O(2log2N)Auxiliary Space: O(N) Advertise with us Next Article Rearrange array such that difference of adjacent elements is in descending order I ishankhandelwals Follow Similar Reads Reduce Array by replacing pair with their difference You are given an array of positive integers. You can perform the following operations on the array any number of times: Select the two largest elements in the array, suppose a and b.If a = b, remove both of them from the array.If a > b, remove b and the new value of a will be a-b or vice-versa.Th 9 min read Find minimum difference with adjacent elements in Array Given an array A[] of N integers, the task is to find min(A[0], A[1], ..., A[i-1]) - min(A[i+1], A[i+2], ..., A[n-1]) for each i (1 ⤠i ⤠N). Note: If there are no elements at the left or right of i then consider the minimum element towards that part zero. Examples: Input: N = 4, A = {8, 4, 2, 6}Out 12 min read Reduce the array by replacing 1st and middle element with sum and difference alternatively 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: 5Explanation: 5 min read Array value by repeatedly replacing max 2 elements with their absolute difference Given an array arr size N, the task is to print the final array value remaining in the array when the maximum and second maximum element of the array is replaced by their absolute difference in the array, repeatedly. Note: If the maximum two elements are same, then both are removed from the array, w 6 min read Rearrange array such that difference of adjacent elements is in descending order Given an array a[] with n integers the task is to rearrange the elements of the array in such a way that the differences of the adjacent elements are in descending order.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output : 6 1 5 2 4 3 Explanation: For first two elements the difference is abs(6-1)=5 6 min read Minimum elements to be inserted in Array to make adjacent differences equal Given an array of integers Arr[]. The elements of the array are sorted in increasing order. The task is to find the minimum number of elements to be inserted in the array so that the differences between any two consecutive elements are the same. Examples: Input: Arr[] = {1, 4, 13, 19, 25} Output: 4 8 min read Article Tags : Divide and Conquer DSA Arrays Practice Tags : ArraysDivide and Conquer Like