Given a number **N**, the task is to create an array **arr[]** of size **N**, where the value of the element at every index **i** is filled according to the following rules:

*arr[i] = ((i – 1) – k)*, where k is the index of arr[i – 1] that has appeared second most recently. This rule is applied when**arr[i – 1]**is present more than once in the array*arr[i] = 0*. This rule is applied when**arr[i – 1]**is present only once or when**i = 1**.

**Examples:**

Input:N = 8

Output:0 0 1 0 2 0 2 2

Explanation:

For i = 0: There is no element in the array arr[]. So, 0 is placed in the first index. arr[] = {0}.

For i = 1: There is only one element in the array arr[]. The occurrence of arr[i – 1] (= 0) is only one. Therefore, the array is filled according to the rule 2. arr = {0, 0}.

For i = 2: There are two elements in the array. The second most occurrence of arr[i – 1] = arr[0] = 0. So, arr[2] = 1. arr[] = {0, 0, 1}.

For i = 3: There is no second occurrence of arr[i – 1] = 1. Therefore, arr[3] = 0. arr[] = {0, 0, 1, 0}

For i = 4: The second recent occurrence of arr[i – 1] = 0 is 1. Therefore, (i – 1 – k) = (4 – 1 – 1) = 2. arr[] = {0, 0, 1, 0, 2}.

For i = 5: There is only one occurrence of arr[i – 1] = 2. Therefore, arr[5] = 0. arr[] = {0, 0, 1, 0, 2, 0}.

For i = 6: The second recent occurrence of arr[i – 1] = 0 is 3. Therefore, (i – 1 – k) = (6 – 1 – 3) = 2. arr[] = {0, 0, 1, 0, 2, 0, 2}.

For i = 7: The second recent occurrence of arr[i – 1] = 2 is 4. Therefore, (i – 1 – k) = (7 – 1 – 4) = 2. arr[] = {0, 0, 1, 0, 2, 0, 2, 2}

Input:N = 5

Output:0 0 1 0 2

**Approach:** The idea is to first create an array filled with zeroes of size N. Then for every iteration, we search if the element has occurred in the near past. If yes, then we follow **rule 1**. Else, **rule 2** is followed to fill the array.

Below is the implementation of the above approach:

## C++

`// C++ implementation to generate ` `// an array of size N by following ` `// the given rules ` `#include <bits/stdc++.h> ` `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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

[0, 0, 1, 0, 2]

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the element in the matrix generated by given rules
- Find the sum of elements of the Matrix generated by the given rules
- Generate an array of size K which satisfies the given conditions
- Generate an array of given size with equal count and sum of odd and even numbers
- Reverse the substrings of the given String according to the given Array of indices
- Count of groups having largest size while grouping according to sum of its digits
- Find the size of largest group where groups are according to the xor of digits
- Structure Sorting (By Multiple Rules) in C++
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Generate an array using given conditions from a given array
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Merge an array of size n into another array of size m+n
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Change the given string according to the given conditions
- Reorder an array according to given indexes
- Sort an array according to absolute difference with given value
- Check loop in array according to given constraints
- Split the array into equal sum parts according to given conditions
- Sort an array according to absolute difference with given value using Functors
- Minimize the sum of the array according the given condition

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.