Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9896 - GetMiddleElement (Fast & Slow Pointers) #29942

Open
idebbarh opened this issue Sep 7, 2023 · 0 comments
Open

9896 - GetMiddleElement (Fast & Slow Pointers) #29942

idebbarh opened this issue Sep 7, 2023 · 0 comments
Labels
9896 answer Share answers/solutions to a question en in English

Comments

@idebbarh
Copy link

idebbarh commented Sep 7, 2023

type Shift<T> = T extends [any, ...infer Rest] ? Rest : never;

type GetMiddle<T extends unknown[], K extends unknown[]> = T extends [
  infer First,
  infer Second,
  ...any
]
  ? K["length"] extends 0
    ? [First, Second]
    : [First]
  : T;

type GetMiddleElement<
  T extends unknown[],
  Fast extends unknown[] = T,
  Slow extends unknown[] = T
> = Fast extends [any, any, ...infer Rest]
  ? GetMiddleElement<T, Rest, [] extends Rest ? Slow : Shift<Slow>>
  : GetMiddle<Slow, Fast>;
@idebbarh idebbarh added answer Share answers/solutions to a question en in English labels Sep 7, 2023
@github-actions github-actions bot added the 9896 label Sep 7, 2023
@idebbarh idebbarh changed the title 9896 - GetMiddleElement 9896 - GetMiddleElement (Fast & Slow Pointers) Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
9896 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant