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

8804 - Two Sum #17957

Open
alexfung888 opened this issue Oct 25, 2022 · 0 comments
Open

8804 - Two Sum #17957

alexfung888 opened this issue Oct 25, 2022 · 0 comments
Labels
8804 answer Share answers/solutions to a question en in English

Comments

@alexfung888
Copy link

type Encode<X extends number, R extends unknown[] = []> = R['length'] extends X ? R : Code<X, [...R, unknown]>;

type Add<X extends number, Y> = Y extends any ? [...Encode<X>, ...Encode<Y>]['length'] : never;
// assumes X is a single number, Y can be unions like 3|4|5

type PickAdd<T extends number[]> = T extends [infer Head extends number, ...infer Tail extends number[]]
   ? Add<Head, Tail[number]> | PickAdd<Tail>
   : never;
// pick a table entry and ADD<> to the other entries of the array

type TwoSum<T extends number[], U extends number> = U extends PickAdd<T> ? true : false;
// convert result to required boolean
@alexfung888 alexfung888 added answer Share answers/solutions to a question en in English labels Oct 25, 2022
@github-actions github-actions bot added the 8804 label Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8804 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant