C#
programming
algorithms
longest run
coding tutorial

Index Of Longest Run C

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The "Index Of Longest Run" problem is an intriguing challenge in the world of algorithms. It is often encountered when dealing with sequences and strings. The idea is to find the starting index of the longest contiguous subsequence (a "run") of identical elements within a string or array. Implementing a solution in C# offers both practice in understanding algorithms and an opportunity to review key programming concepts in the language.

Problem Explanation

The problem can be formally defined as follows:

  • Input: A string or an array of elements.
  • Output: The starting index of the longest run of identical consecutive elements.

For example, consider the string "aaabbcaaa". The longest run of identical elements is "aaa" starting at index 5.

Algorithm Implementation in C#

To solve this problem, you can iterate through the array or string and keep track of the current run of characters while maintaining the longest run encountered so far. Here is a step-by-step implementation:

Step-by-step Implementation

  1. Initialize Variables:
    • maxLength to store the length of the longest run found.
    • maxStartIndex to store the starting index of this run.
    • currentLength to store the length of the current ongoing run.
    • currentStartIndex to store the starting index of the current run.
  2. Iterate through the Sequence:
    • Compare each element with the previous one.
    • If they are the same, increase the currentLength .
    • If not, check if currentLength exceeds maxLength .
      • If it does, update maxLength and maxStartIndex .
    • Reset currentLength for the new run starting at the current element.
  3. Final Check:
    • After the loop, check once more in case the longest run ends at the last element.
  4. Return the Result:
    • Return maxStartIndex as the result.

Example Code

Here's what this implementation would look like in C#:

  • Edge Cases: Consider handling empty strings or arrays separately, as these require special attention to avoid index-out-of-range errors.
  • Complexity: This solution runs in O(n)O(n) time complexity, where nn is the length of the input, since it involves a single traversal of the string or array.
  • Space Complexity: The solution uses O(1)O(1) additional space as it primarily relies on fixed-size counters.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.