📌  相关文章
📜  根据给定规则生成大小为N的数组

📅  最后修改于: 2021-04-27 21:17:00             🧑  作者: Mango

给定数字N ,任务是创建一个大小为N的数组arr [] ,其中根据以下规则填充每个索引i处的元素值:

  1. arr [i] =((i – 1)– k) ,其中k是最近出现的第二个arr [i – 1]的索引。当数组中存在多次arr [i – 1]时,将应用此规则
  2. arr [i] = 0 。当arr [i – 1]仅出现一次或当i = 1时,将应用此规则。

例子:

方法:想法是首先创建一个数组,该数组填充大小为N的零。然后对于每次迭代,我们搜索该元素是否在不久的将来发生。如果是,则我们遵循规则1 。否则,遵循规则2来填充数组。
下面是上述方法的实现:

C++
// C++ implementation to generate
// an array of size N by following
// the given rules
#include 
using namespace std;
 
// Function to search the most recent
// location of element N
// If not present in the array
// it will return -1
int search(int a[], int k, int x)
{
    int j;
     
    for ( j = k - 1; j > -1 ; j--)
    {
        if(a[j] == x)
            return j ;
    }
                 
        return -1 ;
}
 
// Function to generate an array
// of size N by following the given rules
void genArray(int arr[], int N)
{
 
    // Loop to fill the array
    // as per the given rules
    for(int i = 0; i < N - 1; i++)
    {
 
        // Check for the occurrence
        // of arr[i - 1]
        if(search(arr, i, arr[i]) == -1)
                arr[i + 1] = 0 ;
 
        else
            arr[i + 1] = (i-search(arr, i, arr[i])) ;
    }
}
 
// Driver code
int main()
{
    int N = 5 ;
    int size = N + 1 ;
    int a[] = {0, 0, 0, 0, 0};
    genArray(a, N) ;
     
    for (int i = 0; i < N ; i ++)
        cout << a[i] << " " ;
        return 0;
}
 
 
// This code is contributed by shivanisinghss2110


Java
// Java implementation to generate
// an array of size N by following
// the given rules
class GFG
{
 
static int a[];
 
// Function to search the most recent
// location of element N
// If not present in the array
// it will return -1
static int search(int a[],int k, int x)
{
    int j;
     
    for ( j = k - 1; j > -1 ; j--)
    {
            if(a[j] == x)
                return j ;
        }
                 
        return -1 ;
}
 
// Function to generate an array
// of size N by following the given rules
static void genArray(int []arr, int N)
{
 
    // Loop to fill the array
    // as per the given rules
    for(int i = 0; i < N - 1; i++)
    {
 
        // Check for the occurrence
        // of arr[i - 1]
        if(search(arr, i, arr[i]) == -1)
                arr[i + 1] = 0 ;
 
        else
            arr[i + 1] = (i-search(arr, i, arr[i])) ;
    }
}
 
// Driver code
public static void main (String[] args)
{
    int N = 5 ;
    int size = N + 1 ;
    int a[] = new int [N];
    genArray(a, N) ;
     
    for (int i = 0; i < N ; i ++)
        System.out.print(a[i]+" " );
 
}
}
 
// This code is contributed by Yash_R


Python3
# Python implementation to generate
# an array of size N by following
# the given rules
 
 
# Function to search the most recent
# location of element N
# If not present in the array
# it will return -1
def search(a, k, x):
        for j in range(k-1, -1, -1) :
            if(a[j]== x):
                return j
                 
        return -1
 
# Function to generate an array
# of size N by following the given rules
def genArray(arr, N):
 
    # Loop to fill the array
    # as per the given rules
    for i in range(0, N-1, 1):
 
        # Check for the occurrence
        # of arr[i - 1]
        if(search(arr, i, arr[i])==-1):
                arr[i + 1]= 0
 
        else:
            arr[i + 1]=(i-search(arr, i, arr[i]))
             
# Driver code     
if __name__ == "__main__":
    N = 5
    size = N + 1
    a =[0]*N
    genArray(a, N)
     
    print(a)


C#
// C# implementation to generate
// an array of size N by following
// the given rules
 
using System;
 
public class GFG
{
 
static int []a;
 
// Function to search the most recent
// location of element N
// If not present in the array
// it will return -1
static int search(int []a,int k, int x)
{
    int j;
     
    for ( j = k - 1; j > -1 ; j--)
    {
            if(a[j] == x)
                return j ;
        }
                 
        return -1 ;
}
 
// Function to generate an array
// of size N by following the given rules
static void genArray(int []arr, int N)
{
 
    // Loop to fill the array
    // as per the given rules
    for(int i = 0; i < N - 1; i++)
    {
 
        // Check for the occurrence
        // of arr[i - 1]
        if(search(arr, i, arr[i]) == -1)
                arr[i + 1] = 0 ;
 
        else
            arr[i + 1] = (i-search(arr, i, arr[i])) ;
    }
}
 
// Driver code
public static void Main (string[] args)
{
    int N = 5 ;
    int size = N + 1 ;
    int []a = new int [N];
    genArray(a, N) ;
     
    for (int i = 0; i < N ; i ++)
        Console.Write(a[i]+" " );
 
}
}
// This code is contributed by AnkitRai01


Javascript


输出:
[0, 0, 1, 0, 2]