
Sponsored
Sponsored
The idea behind this approach is to use recursion to flatten the array up to the specified depth n. If the current depth is less than n, we continue flattening; otherwise, we do not flatten further. We leverage recursive calls to process each element and manage the depth level.
Time Complexity: O(m) where m is the total number of elements in the array considering all levels.
Space Complexity: O(m) due to recursive stack space and storage of flattened elements.
using System.Collections.Generic;
class FlattenArray {
public static List<int> Flatten(List<object> arr, int maxDepth) {
List<int> result = new List<int>();
FlattenHelper(arr, maxDepth, 0, result);
return result;
}
private static void FlattenHelper(List<object> arr, int maxDepth, int currentDepth, List<int> result) {
foreach (var element in arr) {
if (element is List<object> sublist) {
if (currentDepth < maxDepth)
FlattenHelper(sublist, maxDepth, currentDepth + 1, result);
else
result.AddRange((List<int>)sublist);
} else {
result.Add((int)element);
}
}
}
static void Main() {
var arr = new List<object> { 1, 2, new List<object> { 3, 4 }, 5 };
var flattened = Flatten(arr, 1);
Console.WriteLine(string.Join(", ", flattened));
}
}This C# solution involves a recursive method FlattenHelper, which processes a nested List structure. It checks objects for List types and regulates depth accordingly. Upon encountering integers, it accumulates them into result, signalling depth compliance.
This technique leverages a stack data structure to simulate recursion iteratively. By using an explicit stack, we can iteratively flatten the array, taking control of the depth level without using the actual recursive calls within the stack frames.
Time Complexity: O(m), iterating through each array element.
Space Complexity: O(m) due to storage in stack elements.
1#include <stdio.h>
2#include <stdlib.h>
3#include <assert.h>
4
5struct Element {
6 int* data;
7 int size;
8 int index;
9 int depth;
10};
11
12void push(struct Element* stack, int* top, int* data, int size, int depth) {
13 stack[(*top)++] = (struct Element){ data, size, 0, depth};
14}
15
16int* flattenArray(int* arr, int arrSize, int n, int* returnSize) {
17 int* result = (int*)malloc(arrSize * sizeof(int));
18 int resultIndex = 0;
19 struct Element stack[1000];
20 int top = 0;
21 push(stack, &top, arr, arrSize, 0);
22
23 while (top > 0) {
24 struct Element* current = &stack[top-1];
25
26 if (current->index >= current->size) {
27 top--;
28 continue;
29 }
30 int value = current->data[current->index++];
31
32 // Check if the current value is a marker for a nested array
33 if (value == -1) { // Assume this is to mock subarray occurrence
34 int subArray[] = {3, 4}; // Mock subarray
35 if (current->depth < n) {
36 push(stack, &top, subArray, 2, current->depth + 1);
37 } else {
38 result[resultIndex++] = value; // Keep the entire subArray as is
39 }
40 continue;
41 }
42 result[resultIndex++] = value;
43 }
44
45 *returnSize = resultIndex;
46 return result;
47}
48
49int main() {
50 int arr[] = {1, -1, 2, 3}; // Mock
51 int returnSize;
52 int* flattened = flattenArray(arr, 4, 1, &returnSize);
53 for (int i = 0; i < returnSize; i++) {
54 printf("%d ", flattened[i]);
55 }
56 free(flattened);
57 return 0;
58}This iterative C solution uses a stack to manage array processing, avoiding direct recursion. This mock setup involves using markers and demonstrates how such a stack could work for real subarrays. It pushes all values and sub-arrays into a custom stack, managing the depth manually.