Minimum array elements to be changed to make Recaman’s sequence
Given an array arr[] of N elements. The task is to find the minimum number of elements to be changed in the array such that the array contains first N Recaman’s Sequence terms. Note that Recaman terms may be present in any order in the array.
First few terms of Recaman’s Sequence are:
0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8, …..
Examples:
Input: arr[] = {44, 0, 2, 3, 9}
Output: 2
N = 5 and first 5 Recaman Numbers are 0, 1, 3, 6 and 2
44 and 9 must be replaced with 6 and 1
Hence 2 changes are required.
Input: arr[] = {0, 33, 3, 1}
Output: 1
Approach:
- Insert first N Recaman’s Sequence terms in a set.
- Traverse the array from left to right and check if array element is present in the set.
- If current element is present in the set that remove it from the set.
- Minimum changes required is the size of the final reduced set.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int recamanGenerator( int arr[], int n)
{
arr[0] = 0;
for ( int i = 1; i <= n; i++) {
int temp = arr[i - 1] - i;
int j;
for (j = 0; j < i; j++) {
if ((arr[j] == temp) || temp < 0) {
temp = arr[i - 1] + i;
break ;
}
}
arr[i] = temp;
}
}
int recamanArray( int arr[], int n)
{
unordered_set< int > s;
int recaman[n];
recamanGenerator(recaman, n);
for ( int i = 0; i < n; i++)
s.insert(recaman[i]);
for ( int i = 0; i < n; i++) {
auto it = s.find(arr[i]);
if (it != s.end())
s.erase(it);
}
return s.size();
}
int main()
{
int arr[] = { 7, 11, 20, 4, 2, 1, 8, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << recamanArray(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int recamanGenerator( int arr[], int n)
{
arr[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++)
{
int temp = arr[i - 1 ] - i;
int j;
for (j = 0 ; j < i; j++)
{
if ((arr[j] == temp) || temp < 0 )
{
temp = arr[i - 1 ] + i;
break ;
}
}
arr[i] = temp;
}
return 0 ;
}
static int recamanArray( int arr[], int n)
{
Set<Integer> s= new HashSet<Integer>();
int recaman[]= new int [n+ 1 ];
recamanGenerator(recaman, n);
for ( int i = 0 ; i < n; i++)
s.add(recaman[i]);
for ( int i = 0 ; i < n; i++)
{
if (s.contains(arr[i]))
s.remove(arr[i]);
}
return s.size();
}
public static void main(String args[])
{
int arr[] = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 };
int n = arr.length;
System.out.print( recamanArray(arr, n));
}
}
|
Python3
def recamanGenerator(arr, n):
arr[ 0 ] = 0
for i in range ( 1 , n):
temp = arr[i - 1 ] - i
j = 0
for j in range (i):
if ((arr[j] = = temp) or temp < 0 ):
temp = arr[i - 1 ] + i
break
arr[i] = temp
def recamanArray(arr, n):
s = dict ()
recaman = [ 0 for i in range (n)]
recamanGenerator(recaman, n)
for i in range (n):
s[recaman[i]] = s.get(recaman[i], 0 ) + 1
for i in range (n):
if arr[i] in s.keys():
del s[arr[i]]
return len (s)
arr = [ 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 ]
n = len (arr)
print (recamanArray(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int recamanGenerator( int []arr, int n)
{
arr[0] = 0;
for ( int i = 1; i <= n; i++)
{
int temp = arr[i - 1] - i;
int j;
for (j = 0; j < i; j++)
{
if ((arr[j] == temp) || temp < 0)
{
temp = arr[i - 1] + i;
break ;
}
}
arr[i] = temp;
}
return 0;
}
static int recamanArray( int []arr, int n)
{
HashSet< int > s= new HashSet< int >();
int [] recaman= new int [n+1];
recamanGenerator(recaman, n);
for ( int i = 0; i < n; i++)
s.Add(recaman[i]);
for ( int i = 0; i < n; i++)
{
if (s.Contains(arr[i]))
s.Remove(arr[i]);
}
return s.Count;
}
static void Main()
{
int []arr = { 7, 11, 20, 4, 2, 1, 8, 6 };
int n = arr.Length;
Console.Write( recamanArray(arr, n));
}
}
|
Javascript
<script>
function recamanGenerator(arr, n)
{
arr[0] = 0;
for ( var i = 1; i <= n; i++) {
var temp = arr[i - 1] - i;
var j;
for (j = 0; j < i; j++) {
if ((arr[j] == temp) || temp < 0) {
temp = arr[i - 1] + i;
break ;
}
}
arr[i] = temp;
}
}
function recamanArray(arr, n)
{
var s = [];
var recaman = Array(n).fill(0);
recamanGenerator(recaman, n);
for ( var i = 0; i < n; i++)
s.push(recaman[i]);
s.sort((a,b)=> b-a)
for ( var i = 0; i < n; i++) {
if (s.includes(arr[i]))
{
s.splice(s.indexOf(arr[i]), 1);
}
}
return s.length;
}
var arr = [7, 11, 20, 4, 2, 1, 8, 6 ];
var n = arr.length;
document.write( recamanArray(arr, n));
</script>
|
Time Complexity: O(n*n)
Auxiliary Space: O(n), The space complexity of the recamanGenerator function is O(n) because it creates an array of size n to store the Recaman sequence. The space complexity of the recamanArray function is O(n) because it creates an unordered set of size n to store the first n Recaman numbers. Hence, the overall space complexity of the function is O(n).
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...