Minimum deletion such that Xor of adjacent digits is atmost 1
Last Updated :
03 Jan, 2023
Given a string S consisting of N digits, the task is to find the minimum number of deletions such that the bitwise Xor of any two adjacent digits of the remaining string is at most 1.
Examples:
Input: S = “24244”
Output: 2
?Explanation: We can delete both the 2’s to make the string “444”, which satisfies the condition.
Input: S = “9999”
Output: 0
Approach: The problem can be solved based on the following observations:
It is known that bitwise XOR of same number is 0 and bitwise XOR of consecutive even and odd (odd greater than even) is always one i.e. 4 ? 5 = 1. So we have remove all the elements which separate the elements that can form such a pair.
Follow the steps mentioned below to implement the idea.
- Iterate a loop over the string to count the frequency of characters and check character at ith position is odd or even:
- If the character is even then increment the frequency count.
- Otherwise, perform bitwise XOR with 1 and count the frequency of the result.
- Set max = 0 and check whether the frequency count of the character is greater than max, if it is true then set max = freqCount.
- After that find (n – max) which is the minimum number of deletions performed on the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDeletion( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[arr[i]]++;
}
int ans = 0;
for ( auto i : mp) {
if (i.first % 2 == 0) {
if (mp.find(i.first + 1) != mp.end()) {
ans = max(ans,
(i.second + mp[i.first + 1]));
}
}
ans = max(ans, i.second);
}
return n - ans;
}
int main()
{
string S = "24244" ;
int A[S.length()];
for ( int i = 0; i < S.length(); i++) {
A[i] = S.at(i);
}
int N = sizeof (A) / sizeof (A[0]);
cout << minDeletion(A, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int minDeletion( int arr[], int n)
{
HashMap<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
map.put(arr[i],
map.getOrDefault(arr[i], 0 ) + 1 );
}
else {
map.put(arr[i] ^ 1 ,
map.getOrDefault(arr[i] ^ 1 , 0 )
+ 1 );
}
}
int max = 0 ;
for ( int freqCount : map.values()) {
max = ( int )Math.max(freqCount, max);
}
int res = n - max;
return res;
}
public static void main(String[] args)
{
String S = "24244" ;
int [] A = new int [S.length()];
for ( int i = 0 ; i < S.length(); i++) {
A[i] = S.charAt(i);
}
int N = A.length;
System.out.println(minDeletion(A, N));
}
}
|
Python3
def minDeletion(arr, n):
mp = {}
for i in range (n):
if (arr[i] in mp):
mp[arr[i]] = mp[arr[i]] + 1
else :
mp[arr[i]] = 1
ans = 0
for i,value in mp.items():
if (i % 2 = = 0 ):
if ((i + 1 ) in mp):
ans = max (ans, value + mp[i + 1 ])
ans = max (ans, value)
return n - ans
S = "24244"
A = [ 0 ] * len (S)
for i in range ( len (S)):
A[i] = ord (S[i])
N = len (A)
print (minDeletion(A,N))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int minDeletion( int [] arr, int n)
{
var mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (!mp.ContainsKey(arr[i])) {
mp.Add(arr[i], 0);
}
mp[arr[i]]++;
}
int ans = 0;
foreach ( var item in mp)
{
if (item.Key % 2 == 0) {
if (mp.ContainsKey(item.Key + 1)) {
ans = Math.Max(
ans,
(item.Value + mp[item.Key + 1]));
}
}
ans = Math.Max(ans, item.Value);
}
return n - ans;
}
static public void Main()
{
string S = "24244" ;
int [] A = new int [S.Length];
for ( int i = 0; i < S.Length; i++) {
A[i] = ( int )S[i];
}
int N = A.Length;
Console.WriteLine(minDeletion(A, N));
}
}
|
Javascript
function minDeletion(arr, n)
{
let mp = new Map();
for (let i=0;i<n;i++){
if (mp.has(arr[i])){
mp.set(arr[i],mp.get(arr[i])+1)
}
else {
mp.set(arr[i],1)
}
}
let ans = 0;
for (let [key,val] of mp) {
if (key % 2 == 0) {
if (mp.has(key+ 1) ) {
ans = max(ans,
(val+ mp.get(key+ 1)));
}
}
ans = Math.max(ans, val);
}
return n - ans;
}
let S = "24244" ;
let A=[];
for (let i = 0; i < S.length; i++) {
A.push(parseInt(S[i]));
}
let N = A.length;
console.log(minDeletion(A, N));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Share your thoughts in the comments
Please Login to comment...