summaryrefslogtreecommitdiff
path: root/graphs/piscine/hill_array/hill_array.c
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/piscine/hill_array/hill_array.c
add: graphs et rushs
Diffstat (limited to 'graphs/piscine/hill_array/hill_array.c')
-rw-r--r--graphs/piscine/hill_array/hill_array.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/graphs/piscine/hill_array/hill_array.c b/graphs/piscine/hill_array/hill_array.c
new file mode 100644
index 0000000..14d3a85
--- /dev/null
+++ b/graphs/piscine/hill_array/hill_array.c
@@ -0,0 +1,43 @@
+#include "hill_array.h"
+
+int top_of_the_hill(int tab[], size_t len)
+{
+ if (len == 0)
+ {
+ return -1;
+ }
+
+ int top = 0;
+ size_t i = 0;
+
+ while (i + 1 < len && tab[i] <= tab[i + 1])
+ {
+ if (tab[i] < 0 || tab[i + 1] < 0)
+ {
+ return -1;
+ }
+ if (tab[i] != tab[i + 1])
+ {
+ top = i + 1;
+ }
+ i++;
+ }
+
+ while (i + 1 < len && tab[i] >= tab[i + 1])
+ {
+ if (tab[i] < 0 || tab[i + 1] < 0)
+ {
+ return -1;
+ }
+ i++;
+ }
+
+ if (i + 1 == len)
+ {
+ return top;
+ }
+ else
+ {
+ return -1;
+ }
+}