📌  相关文章
📜  更改为形成Recaman序列的最小数组元素

📅  最后修改于: 2021-04-22 03:18:41             🧑  作者: Mango

给定N个元素的数组arr [] 。任务是找到数组中要更改的最小元素数,以使该数组包含前N个Recaman序列项。请注意,Recaman术语可以以任何顺序出现在数组中。
Recaman序列的前几个术语是:

例子:

方法:

  • 在集合中插入前N个Recaman的序列项。
  • 从左到右遍历数组,并检查集合中是否存在数组元素。
  • 如果当前元素存在于集合中,则将其从集合中删除。
  • 所需的最小更改是最终精简集的大小。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
int recamanGenerator(int arr[], int n)
{
    // First term of the sequence is always 0
    arr[0] = 0;
  
    // Fill remaining terms using recursive
    // formula
    for (int i = 1; i <= n; i++) {
        int temp = arr[i - 1] - i;
        int j;
  
        for (j = 0; j < i; j++) {
  
            // If arr[i-1] - i is negative or
            // already exists
            if ((arr[j] == temp) || temp < 0) {
                temp = arr[i - 1] + i;
                break;
            }
        }
  
        arr[i] = temp;
    }
}
  
// Function that returns minimum changes required
int recamanArray(int arr[], int n)
{
  
    // Set to store first n Recaman numbers
    unordered_set s;
  
    // Generate and store
    // first n Recaman numbers
    int recaman[n];
    recamanGenerator(recaman, n);
  
    // Insert first n Recaman numbers to set
    for (int i = 0; i < n; i++)
        s.insert(recaman[i]);
  
    for (int i = 0; i < n; i++) {
  
        // If current element of the array
        // is present in the set
        auto it = s.find(arr[i]);
        if (it != s.end())
            s.erase(it);
    }
  
    // Return the remaining number of
    // elements in the set
    return s.size();
}
  
// Driver code
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
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
static int recamanGenerator(int arr[], int n)
{
    // First term of the sequence is always 0
    arr[0] = 0;
  
    // Fill remaining terms using recursive
    // formula
    for (int i = 1; i <= n; i++) 
    {
        int temp = arr[i - 1] - i;
        int j;
  
        for (j = 0; j < i; j++)
        {
  
            // If arr[i-1] - i is negative or
            // already exists
            if ((arr[j] == temp) || temp < 0) 
            {
                temp = arr[i - 1] + i;
                break;
            }
        }
  
        arr[i] = temp;
    }
    return 0;
}
  
// Function that returns minimum changes required
static int recamanArray(int arr[], int n)
{
      
  
    // Set to store first n Recaman numbers
    Set s=new HashSet();
  
    // Generate and store
    // first n Recaman numbers
    int recaman[]=new int[n+1];
    recamanGenerator(recaman, n);
  
    // Insert first n Recaman numbers to set
    for (int i = 0; i < n; i++)
        s.add(recaman[i]);
  
    for (int i = 0; i < n; i++) 
    {
        // If current element of the array
        // is present in the set
        if (s.contains(arr[i]))
            s.remove(arr[i]);
    }
  
    // Return the remaining number of
    // elements in the set
    return s.size();
}
  
// Driver code
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));
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
def recamanGenerator(arr, n):
      
    # First term of the sequence
    # is always 0
    arr[0] = 0
  
    # Fill remaining terms using 
    # recursive formula
    for i in range(1, n):
        temp = arr[i - 1] - i
        j = 0
  
        for j in range(i):
  
            # If arr[i-1] - i is negative or
            # already exists
            if ((arr[j] == temp) or temp < 0):
                temp = arr[i - 1] + i
                break
  
        arr[i] = temp
  
# Function that returns minimum 
# changes required
def recamanArray(arr, n):
  
    # Set to store first n Recaman numbers
    s = dict()
  
    # Generate and store
    # first n Recaman numbers
    recaman = [0 for i in range(n)]
    recamanGenerator(recaman, n)
  
    # Insert first n Recaman numbers to set
    for i in range(n):
        s[recaman[i]] = s.get(recaman[i], 0) + 1
  
    for i in range(n):
  
        # If current element of the array
        # is present in the set
        if arr[i] in s.keys():
            del s[arr[i]]
  
    # Return the remaining number of
    # elements in the set
    return len(s)
  
# Driver code
arr = [7, 11, 20, 4, 2, 1, 8, 6 ]
n = len(arr)
  
print(recamanArray(arr, n))
  
# This code is contributed
# by mohit kumar


C#
// C# implementation of the approach 
using System;
using System.Collections.Generic; 
  
class GFG 
{ 
  
static int recamanGenerator(int []arr, int n) 
{ 
    // First term of the sequence is always 0 
    arr[0] = 0; 
  
    // Fill remaining terms using recursive 
    // formula 
    for (int i = 1; i <= n; i++) 
    { 
        int temp = arr[i - 1] - i; 
        int j; 
  
        for (j = 0; j < i; j++) 
        { 
  
            // If arr[i-1] - i is negative or 
            // already exists 
            if ((arr[j] == temp) || temp < 0) 
            { 
                temp = arr[i - 1] + i; 
                break; 
            } 
        } 
  
        arr[i] = temp; 
    } 
    return 0; 
} 
  
// Function that returns minimum changes required 
static int recamanArray(int []arr, int n) 
{ 
      
    // Set to store first n Recaman numbers 
    HashSet s=new HashSet(); 
  
    // Generate and store 
    // first n Recaman numbers 
    int[] recaman=new int[n+1]; 
    recamanGenerator(recaman, n); 
  
    // Insert first n Recaman numbers to set 
    for (int i = 0; i < n; i++) 
        s.Add(recaman[i]); 
  
    for (int i = 0; i < n; i++) 
    { 
        // If current element of the array 
        // is present in the set 
        if (s.Contains(arr[i])) 
            s.Remove(arr[i]); 
    } 
  
    // Return the remaining number of 
    // elements in the set 
    return s.Count; 
} 
  
// Driver code 
static void Main() 
{ 
  
    int []arr = { 7, 11, 20, 4, 2, 1, 8, 6 }; 
    int n = arr.Length; 
  
    Console.Write( recamanArray(arr, n)); 
} 
} 
  
// This code is contributed by mits


输出:
3