Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
Last Updated :
14 Mar, 2023
Given a sequence of integers of even length ‘n’, the task is to find the minimum number of operations required to convert the sequence to follow the rule a[i]=a[i+2] where ‘i’ is the index.
The operation here is to replace any element of the sequence with any element.
Examples:
Input : n=4 ; Array : 3, 1, 3, 2
Output : 1
If we change the last element to '1' then,
the sequence will become 3, 1, 3, 1 (satisfying the condition)
So, only 1 replacement is required.
Input : n=6 ; Array : 105 119 105 119 105 119
Output : 0
As the sequence is already in the required state.
So, no replacement of elements is required.
Approach: As we see that the indices 0, 2, …, n-2 are connected independently and 1, 3, 5, …, n are connected independently and must have the same value. So,
- We have to find the most occurring number in both the sequences (even and odd) by storing the numbers and their frequency in a map.
- Then every other number in that sequence will have to be replaced with the most occurring number in the same sequence.
- Finally, the count of replacements from the previous step will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minReplace( int a[], int n)
{
int i;
map< int , int > te;
map< int , int > to;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
te[a[i]]++;
else
to[a[i]]++;
}
int me = -1;
int mo = -1;
int ce = -1;
int co = -1;
for ( auto it : te)
{
if (it.second > ce)
{
ce = it.second;
me = it.first;
}
}
for ( auto it : to)
{
if (it.second > co)
{
co = it.second;
mo = it.first;
}
}
int res = 0;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (a[i] != me) res++;
}
else
{
if (a[i] != mo) res++;
}
}
return res;
}
int main()
{
int n = 4;
int a[] = {3, 1, 3, 2};
cout << minReplace(a, n) << endl;
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
public static int minReplace( int a[], int n)
{
int i;
HashMap<Integer, Integer> te = new HashMap<>();
HashMap<Integer, Integer> to = new HashMap<>();
for (i = 0 ; i < n; i++) {
if (i % 2 == 0 ) {
if (te.containsKey(a[i]))
te.put(a[i], te.get(a[i]) + 1 );
else
te.put(a[i], 1 );
}
else {
if (to.containsKey(a[i]))
to.put(a[i], to.get(a[i]) + 1 );
else
to.put(a[i], 1 );
}
}
int me = - 1 ;
int mo = - 1 ;
int ce = - 1 ;
int co = - 1 ;
for (Integer It : te.keySet()) {
if (te.get(It) > ce) {
ce = te.get(It);
me = It;
}
}
for (Integer It : to.keySet()) {
if (to.get(It) > co) {
co = to.get(It);
mo = It;
}
}
int res = 0 ;
for (i = 0 ; i < n; i++) {
if (i % 2 == 0 ) {
if (a[i] != me)
res++;
}
else {
if (a[i] != mo)
res++;
}
}
return res;
}
public static void main(String[] args)
{
int n;
n = 4 ;
int a[] = { 3 , 1 , 3 , 2 };
System.out.println(minReplace(a, n));
}
}
|
Python3
def minReplace(a: list , n) - > int :
te = dict ()
to = dict ()
for i in range (n):
if i % 2 = = 0 :
if a[i] not in te:
te[a[i]] = 1
else :
te[a[i]] + = 1
else :
if a[i] not in to:
to[a[i]] = 1
else :
to[a[i]] + = 1
me = - 1
mo = - 1
ce = - 1
co = - 1
for it in te:
if te[it] > ce:
ce = te[it]
me = it
for it in to:
if to[it] > co:
co = to[it]
mo = it
res = 0
for i in range (n):
if i % 2 = = 0 :
if a[i] ! = me:
res + = 1
else :
if a[i] ! = mo:
res + = 1
return res
if __name__ = = "__main__" :
n = 4
a = [ 3 , 1 , 3 , 2 ]
print (minReplace(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int minReplace( int []a, int n)
{
int i;
Dictionary< int ,
int > te = new Dictionary< int ,
int >();
Dictionary< int ,
int > to = new Dictionary< int ,
int >();
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (te.ContainsKey(a[i]))
te[a[i]] = te[a[i]] + 1;
else
te.Add(a[i], 1);
}
else
{
if (to.ContainsKey(a[i]))
to[a[i]] = te[a[i]] + 1;
else
to.Add(a[i], 1);
}
}
int me = -1;
int mo = -1;
int ce = -1;
int co = -1;
foreach ( int It in te.Keys)
{
if (te[It] > ce)
{
ce = te[It];
me = It;
}
}
foreach ( int It in to.Keys)
{
if (to[It] > co)
{
co = to[It];
mo = It;
}
}
int res = 0;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (a[i] != me)
res++;
}
else
{
if (a[i] != mo)
res++;
}
}
return res;
}
public static void Main(String[] args)
{
int n;
n = 4;
int []a = { 3, 1, 3, 2 };
Console.WriteLine(minReplace(a, n));
}
}
|
Javascript
<script>
function minReplace(a, n)
{
var i;
var te = new Map();
var to = new Map();
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
if (te.has(a[i]))
te.set(a[i], te.get(a[i])+1)
else
te.set(a[i],1)
else
if (to.has(a[i]))
to.set(a[i], to.get(a[i])+1)
else
to.set(a[i],1)
}
var me = -1;
var mo = -1;
var ce = -1;
var co = -1;
te.forEach((value, key) => {
if (value > ce)
{
ce = value;
me = key;
}
});
to.forEach((value, key) => {
if (value > co)
{
co = value;
mo = key;
}
});
var res = 0;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (a[i] != me)
res++;
}
else
{
if (a[i] != mo)
res++;
}
}
return res;
}
var n = 4;
var a = [3, 1, 3, 2];
document.write( minReplace(a, n) );
</script>
|
Time Complexity: O(N Log(N)).
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...