В массиве счётчиков каждому возможному значению соответствует индекс. По этому индексу хранится число появлений значения во входных данных.
Предыдущая глава — 9б. Массивы — примеры и пояснения
Массив счётчиков
Пример вопроса
Дан массив чисел от 0 до 100. Напишите метод, который возвращает массив счётчиков: по индексу i должно стоять число появлений i.
Решение:
public static int[] CountOccurrences(int[] arr) {
int[] counts = new int[101];
foreach (int x in arr) {
if (x >= 0 && x <= 100) counts[x]++;
}
return counts;
}
Массив логических флагов
В массиве bool[] индекс соответствует значению. Флаг true может означать, что значение уже встретилось, а false — что его пока не было.
Пример вопроса
Дан массив чисел от 0 до 100. Напишите метод, который проверяет, встретилось ли число n хотя бы один раз.
Решение:
public static bool[] BuildFlags(int[] arr) {
bool[] flags = new bool[101];
foreach (int x in arr) {
if (x >= 0 && x <= 100) flags[x] = true;
}
return flags;
}
public static bool Exists(int n, bool[] flags) {
return (n >= 0 && n < flags.Length) && flags[n];
}
Важно. Если значения могут занимать весь диапазон int, массив прямой адресации потребует слишком много памяти. В таких случаях используют, например, Dictionary<int, int> или HashSet<int>.
Пример для большого диапазона значений (май 2025)
Для запуска могут понадобиться дополнительные настройки компиляции.
using System;
using System.Collections;
using System.Collections.Generic;
public class Program
{
private const int FLAG_VALUE = int.MinValue; //using Queue<int?> nullable takes 2X space
/// <summary>
/// working on queue from Collections generic (didn't check perf differences on Queue from Mivney)
/// validates all whole numbers beween min, and max are in the queue.
/// need to test again - it might be possible to revert to bool[] and still get it going
/// under the 2.1 billion limit.
/// </summary>
/// <param name="q"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static bool CheckValidQ(Queue<int> q)
{
int max = int.MinValue;
int min = int.MaxValue;
int current = 0;
int length = 0;
// Find min, max, and count by looping through queue once
// Add flag marker to detect when we've completed the loop
q.Enqueue(FLAG_VALUE);
do
{
current = q.Dequeue();
if (current != FLAG_VALUE)
{
//Console.WriteLine(current);
if (current > max)
max = current;
if (current < min)
min = current;
length++;
q.Enqueue(current); // Put it back at the end
}
} while (current != FLAG_VALUE); // Stop when we hit the flag
// Check if range matches length (consecutive sequence)
if (max - min + 1 != length)
return false;
// Create BitArray to track which values we've seen
long range = (long)max - (long)min + 1;
if (range > int.MaxValue)
throw new ArgumentException("Range too large for BitArray");
var bits = new BitArray((int)range);
// Loop through queue again to mark bits (flag is still at front)
//int check = q.Dequeue(); // Remove the flag marker
q.Enqueue(FLAG_VALUE); // Add it back at the end
do
{
current = q.Dequeue();
if (current != FLAG_VALUE)
{
bits.Set(current - min, true);
q.Enqueue(current); // Put it back at the end
}
} while (current != FLAG_VALUE);
// Check if all positions are set to true
for (int i = 0; i < 100; i++)
//Console.WriteLine(bits.Get(i));
if (!bits.Get(i))
return false;
return true;
}
/// <summary>
/// Creates a test queue at the desired size, with sequential whole numbers.
/// starting from int.MinValue + 5
/// </summary>
public static Queue<int> CreateReasonableTestQueue(int millions = 100)
{
long count = millions * 1_000_000L;
const int startValue = int.MinValue + 5;
Console.WriteLine($"Creating test queue with {count:N0} elements ({millions} million)...");
var queue = new Queue<int>();
for (long i = 0; i < count; i++)
{
queue.Enqueue(startValue + (int)i);
if (i % 10_000_000 == 0 && i > 0) // Progress bar
Console.WriteLine($"Added {i:N0} elements...");
}
Console.WriteLine($"Test queue created! Size: {queue.Count:N0}");
return queue;
}
public static void Main()
{
int millionElements = 2000; // above int.MaxValue, it's impossible to configure the bits array.
try
{
Console.WriteLine("=== Queue Validation Test ===");
var startTime = DateTime.Now;
Queue<int> testQueue;
testQueue = CreateReasonableTestQueue(millionElements);
var creationTime = DateTime.Now;
Console.WriteLine($"Queue creation took: {(creationTime - startTime).TotalSeconds:F2} seconds");
Console.WriteLine("Starting validation...");
bool isValid = CheckValidQ(testQueue);
var endTime = DateTime.Now;
Console.WriteLine($"Validation result: {isValid}");
Console.WriteLine($"Validation took: {(endTime - creationTime).TotalSeconds:F2} seconds");
Console.WriteLine($"Total time: {(endTime - startTime).TotalSeconds:F2} seconds");
Console.WriteLine($"Final queue size: {testQueue.Count:N0}");
}
catch (OutOfMemoryException)
{
Console.WriteLine("ERROR: Out of memory! Try a smaller test size.");
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: {ex.Message}");
}
}
}
Массив префиксных сумм
По индексу i хранится сумма элементов исходного массива от 0 до i включительно. После построения такого массива сумму на отрезке от l до r можно найти за O(1).
Пример вопроса
Для массива [1, 3, 5, 2, 4] постройте массив префиксных сумм и найдите сумму элементов от индекса 1 до индекса 3 включительно.
Решение:
int[] BuildPrefixSums(int[] arr) {
int n = arr.Length;
int[] prefix = new int[n];
prefix[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}
return prefix;
}
int RangeSum(int l, int r, int[] prefix) {
return (l == 0) ? prefix[r] : prefix[r] - prefix[l - 1];
}
// Использование:
int[] arr = { 1, 3, 5, 2, 4 };
int[] prefix = BuildPrefixSums(arr); // {1, 4, 9, 11, 15}
int sum_1_3 = RangeSum(1, 3, prefix); // 11 - 1 = 10
Пример вопроса
Дан массив чисел от 0 до 100. Напишите метод, который возвращает массив счётчиков: по индексу i должно стоять число появлений i.
Решение:
public static int[] CountOccurrences(int[] arr) {
int[] counts = new int[101];
foreach (int x in arr) {
if (x >= 0 && x <= 100) counts[x]++;
}
return counts;
}
Массив логических флагов
В массиве bool[] каждому значению соответствует индекс. Флаг true показывает, что значение присутствует, а false — что оно отсутствует.
Пример вопроса
Дан массив чисел от 0 до 100. Напишите метод, который проверяет, встретилось ли число n хотя бы один раз.
Решение:
public static bool[] BuildFlags(int[] arr) {
bool[] flags = new bool[101];
foreach (int x in arr) {
if (x >= 0 && x <= 100) flags[x] = true;
}
return flags;
}
public static bool Exists(int n, bool[] flags) {
return (n >= 0 && n < flags.Length) && flags[n];
}
Важно. Если значения могут занимать весь диапазон int, массив прямой адресации потребует слишком много памяти. В таких случаях используют, например,
Dictionary<int, int>илиHashSet<int>.
Ссылки
Переходим к разделу 9a - версия без анимации.
Переходим к разделу 9c - Массивы счетчиков и аккумуляторов.
Практика 9.3. Экзаменационные задачи на массивы и классы
Практика
Переходим к практическому упражнению 9.1 - одномерный массив.
Переходим к практическому упражнению 9.2 - “Массивы” - задачи на CodeWars.