Minimize deletions such that no even index Array element is same as next element
Given an array arr[], the task is to find the minimum number of deletion operations required such that:
- The newly created array should have an even length.
- arr[i] ≠ arr[i+1] for every i where i%2==0.
Examples:
Input: arr[] = {1, 1, 2, 3, 5}
Output: 1
Explanation: Delete the first or second element of the array to satisfy the conditions.Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: 2
Explanation: Delete first element as the next element is a duplicate and the current index is even.
Need to delete another element from the newly created array because
the size of the newly created array is odd. arr = {1, 2, 2, 3, 3}
Delete the last element to make its length even. So the total number of operations is 2.
Approach: The general idea to solve this problem is to:
Maximize the number of elements in the newly created array and keep on checking if any even index element has the same value as the one just next to it.
The above idea can be implemented using a stack to generate the new array. Follow the steps mentioned below to implement the above observation:
- Create a stack of pairs to store the elements and the index of the element in the new array.
- Iterate over the array arr[] from i = 0 to N-1:
- If the index of the topmost element of the stack is odd, then push the current element along with its index in the new array in the stack.
- Otherwise, check if the value of arr[i] and the topmost element are the same.
- If they are same, continue to the next element of arr[].
- Else add the current element in the new array. index pointer to the stack.
- At last, if the size of the stack is odd then delete one element more from the stack.
- Return the value of N – stack size as the answer, because this is the minimum number of deletions required.
Below is the implementation of the above approach:
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum deletions int minOperations(vector< int >& arr) { int n = arr.size(); // Stack that will maintain the newly // created array stack<pair< int , int > > st; int k = 1; // Pushed the first element to the stack. st.push({ arr[0], 0 }); for ( int i = 1; i < n; i++) { // If the top most element in the // stack is at even index and the // element is same as the current // array element then continue. if (st.top().second % 2 == 0 && st.top().first == arr[i]) { continue ; } // If the top most element in the stack // is at odd index or the two elements // are not same then push the current // element to the stack. else { st.push({ arr[i], k }); k++; } } // Find the stack size int s = st.size(); // If stack size is odd then // delete further one element // from stack, hence return // array size - stack size +1. if (s & 1 == 1) { return n - s + 1; } // Return (array size - stack size). return n - s; } // Driver code int main() { vector< int > arr = { 1, 1, 2, 3, 5 }; // Function call cout << minOperations(arr); return 0; } |
Java
// Java program for above approach import java.util.Stack; class GFG { // Function to find the minimum deletions static int minOperations( int [] arr) { int n = arr.length; // Stack that will maintain the newly // created array Stack< int []> st = new Stack<>(); int k = 1 ; // Pushed the first element to the stack. int [] stFirstElem = { arr[ 0 ], 0 }; st.push(stFirstElem); for ( int i = 1 ; i < n; i++) { // If the top most element in the // stack is at even index and the // element is same as the current // array element then continue. if (st.peek()[ 1 ] % 2 == 0 && st.peek()[ 1 ] == arr[i]) { continue ; } // If the top most element in the stack // is at odd index or the two elements // are not same then push the current // element to the stack. else { int [] stElem = { arr[i], k }; st.push(stElem); k++; } } // Find the stack size int s = st.size(); // If stack size is odd then // delete further one element // from stack, hence return // array size - stack size +1. if ((s & 1 ) == 1 ) { return n - s + 1 ; } // Return (array size - stack size). return n - s; } // driver code public static void main(String[] args) { int [] arr = { 1 , 1 , 2 , 3 , 5 }; // Function call System.out.print(minOperations(arr)); } } // This code is contributed by phasing17 |
Python3
# Python3 program for above approach # Function to find the minimum deletions def minOperations(arr): n = len (arr) # stack that will maintain # the newly created array st = [] k = 1 # Pushed the first element to the stack st.append([arr[ 0 ], 0 ]) for i in range ( 1 , n): # If the top most element in the # stack is at even index and the # element is same as the current # array element then continue if st[ len (st) - 1 ][ 1 ] % 2 = = 0 and st[ len (st) - 1 ][ 0 ] = = arr[i]: continue # If the top most element in the stack # is at odd index or the two elements # are not same then push the current # element to the stack. else : st.append([arr[i], k]) k + = 1 # Find the stack size s = len (st) # If stack size is odd then # delete further one element # from stack, hence return # array size - stack size +1. if s & 1 = = 1 : return n - s + 1 # Return (array size - stack size). return n - s # Driver code arr = [ 1 , 1 , 2 , 3 , 5 ] # Function call print (minOperations(arr)) # This code is contributed by phasing17 |
C#
// C# program for above approach using System; using System.Collections.Generic; public class GFG { // Function to find the minimum deletions static int minOperations( int [] arr) { int n = arr.Length; // Stack that will maintain the newly // created array Stack< int []> st = new Stack< int []>(); int k = 1; // Pushed the first element to the stack. int [] stFirstElem = { arr[0], 0 }; st.Push(stFirstElem); for ( int i = 1; i < n; i++) { // If the top most element in the // stack is at even index and the // element is same as the current // array element then continue. if (st.Peek()[1] % 2 == 0 && st.Peek()[1] == arr[i]) { continue ; } // If the top most element in the stack // is at odd index or the two elements // are not same then push the current // element to the stack. else { int [] stElem = { arr[i], k }; st.Push(stElem); k++; } } // Find the stack size int s = st.Count; // If stack size is odd then // delete further one element // from stack, hence return // array size - stack size +1. if ((s & 1) == 1) { return n - s + 1; } // Return (array size - stack size). return n - s; } // driver code public static void Main() { int [] arr = { 1, 1, 2, 3, 5 }; // Function call Console.Write(minOperations(arr)); } } // This code is contributed by jana_sayantan. |
Javascript
<script> // JavaScript program for above approach // Function to find the minimum deletions const minOperations = (arr) => { let n = arr.length; // Stack that will maintain the newly // created array let st = []; let k = 1; // Pushed the first element to the stack. st.push([arr[0], 0]); for (let i = 1; i < n; i++) { // If the top most element in the // stack is at even index and the // element is same as the current // array element then continue. if (st[st.length - 1][1] % 2 == 0 && st[st.length - 1][0] == arr[i]) { continue ; } // If the top most element in the stack // is at odd index or the two elements // are not same then push the current // element to the stack. else { st.push([arr[i], k]); k++; } } // Find the stack size let s = st.length; // If stack size is odd then // delete further one element // from stack, hence return // array size - stack size +1. if (s & 1 == 1) { return n - s + 1; } // Return (array size - stack size). return n - s; } // Driver code let arr = [1, 1, 2, 3, 5]; // Function call document.write(minOperations(arr)); // This code is contributed by rakeshsahni </script> |
1
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...