-
Notifications
You must be signed in to change notification settings - Fork 2
/
duplicate_number.c
67 lines (54 loc) · 1.38 KB
/
duplicate_number.c
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
62
63
64
65
66
/*
auther: Naman Tamrakar
date: 2023-07-18
level: medium
url: https://leetcode.com/problems/find-the-duplicate-number/
question: Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
You must solve the problem without modifying the array nums and uses only constant extra space.
*/
#include <stdio.h>
#include <stdlib.h>
int findDuplicate_c(int* nums, int n){
int k = 0;
while (1) {
int t = nums[k];
if (t < 0)
return k;
nums[k] = -t;
k = t;
}
return -1;
}
__attribute__((naked))
int findDuplicate(int* nums, int n){
__asm__(
// int k = 0;
"movl $0, %eax;"
"l1:"
// int t = nums[k];
"movl (%rdi,%rax,4), %ecx;"
// if (t < 0)
"cmpl $0, %ecx;"
"jl end;"
// nums[k] = -t;
"movl %ecx, %edx;"
"negl %ecx;"
"movl %ecx, (%rdi,%rax,4);"
// k = t;
"movl %edx, %eax;"
"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", findDuplicate(arr, n));
free(arr);
return 0;
}