-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmissing_numbers.c
More file actions
62 lines (51 loc) · 1.26 KB
/
missing_numbers.c
File metadata and controls
62 lines (51 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
auther: Naman Tamrakar
date: 2023-07-19
level: easy
url: https://leetcode.com/problems/missing-number/
question: Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
*/
#include <stdio.h>
#include <stdlib.h>
int missingNumber_c(int *arr, int n) {
int s = n * (n + 1) / 2;
int i = 0;
while (i < n) {
s -= arr[i];
i++;
}
return s;
}
__attribute__((naked))
int missingNumber(int* arr, int n){
__asm__(
// int s = n * (n + 1) / 2;
"movq %rsi, %rax;"
"incq %rax;" // n+1
"imul %rsi, %rax;" // n * (n+1)
"shr $1, %rax;" // divide by by right shift
// int i = 0;
"movl $0, %ecx;"
"l1:;"
// while (i < n) {
"cmpl %esi, %ecx;"
"je end;"
// s -= arr[i];
"subq (%rdi,%rcx,4), %rax;"
// i++;
"incl %ecx;"
"jmp l1;"
"end:;"
"ret;"
);
}
int main() {
int n;
scanf("%d", &n);
int *arr = malloc(sizeof(int) * n);
for (int i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("%d", missingNumber(arr, n));
free(arr);
return 0;
}