Minimize deletions such that no even index Array element is same as next element
Last Updated :
06 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int minOperations(vector< int >& arr)
{
int n = arr.size();
stack<pair< int , int > > st;
int k = 1;
st.push({ arr[0], 0 });
for ( int i = 1; i < n; i++) {
if (st.top().second % 2 == 0
&& st.top().first == arr[i]) {
continue ;
}
else {
st.push({ arr[i], k });
k++;
}
}
int s = st.size();
if (s & 1 == 1) {
return n - s + 1;
}
return n - s;
}
int main()
{
vector< int > arr = { 1, 1, 2, 3, 5 };
cout << minOperations(arr);
return 0;
}
|
Java
import java.util.Stack;
class GFG
{
static int minOperations( int [] arr)
{
int n = arr.length;
Stack< int []> st = new Stack<>();
int k = 1 ;
int [] stFirstElem = { arr[ 0 ], 0 };
st.push(stFirstElem);
for ( int i = 1 ; i < n; i++) {
if (st.peek()[ 1 ] % 2 == 0
&& st.peek()[ 1 ] == arr[i]) {
continue ;
}
else {
int [] stElem = { arr[i], k };
st.push(stElem);
k++;
}
}
int s = st.size();
if ((s & 1 ) == 1 ) {
return n - s + 1 ;
}
return n - s;
}
public static void main(String[] args)
{
int [] arr = { 1 , 1 , 2 , 3 , 5 };
System.out.print(minOperations(arr));
}
}
|
Python3
def minOperations(arr):
n = len (arr)
st = []
k = 1
st.append([arr[ 0 ], 0 ])
for i in range ( 1 , n):
if st[ len (st) - 1 ][ 1 ] % 2 = = 0 and st[ len (st) - 1 ][ 0 ] = = arr[i]:
continue
else :
st.append([arr[i], k])
k + = 1
s = len (st)
if s & 1 = = 1 :
return n - s + 1
return n - s
arr = [ 1 , 1 , 2 , 3 , 5 ]
print (minOperations(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int minOperations( int [] arr)
{
int n = arr.Length;
Stack< int []> st = new Stack< int []>();
int k = 1;
int [] stFirstElem = { arr[0], 0 };
st.Push(stFirstElem);
for ( int i = 1; i < n; i++) {
if (st.Peek()[1] % 2 == 0
&& st.Peek()[1] == arr[i]) {
continue ;
}
else {
int [] stElem = { arr[i], k };
st.Push(stElem);
k++;
}
}
int s = st.Count;
if ((s & 1) == 1) {
return n - s + 1;
}
return n - s;
}
public static void Main()
{
int [] arr = { 1, 1, 2, 3, 5 };
Console.Write(minOperations(arr));
}
}
|
Javascript
<script>
const minOperations = (arr) => {
let n = arr.length;
let st = [];
let k = 1;
st.push([arr[0], 0]);
for (let i = 1; i < n; i++) {
if (st[st.length - 1][1] % 2 == 0
&& st[st.length - 1][0] == arr[i]) {
continue ;
}
else {
st.push([arr[i], k]);
k++;
}
}
let s = st.length;
if (s & 1 == 1) {
return n - s + 1;
}
return n - s;
}
let arr = [1, 1, 2, 3, 5];
document.write(minOperations(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...