#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_LEN 10000
void sumAndJump(int arr[], int length);
bool Check_Edge(int value, int index, int length);
bool Check_Sum(int sum, int length);
bool Seen_Index (int array[], int legnth, int index);
void Zero_Array(int array[], int length);
int main() {
int len_array = 0;
// First enter the size of the array
// allocating the array - will be explained later in the course
int *arr
= malloc(sizeof(int) * len_array
); if (arr) {
// Enter the numbers that the array will hold
for (int start_index = 0; start_index < len_array; start_index++) {
scanf("%d", &(arr
[start_index
])); }
sumAndJump(arr, len_array);
}
// Freeing the array - will be explained later in the course
return 0;
}
void sumAndJump(int arr[], int length) {
// if (length > MAX_LEN) { printf("Error!\n"); return;}
int index = 0, k = 0, s = 0, jump_counter = 0, idx_array[length];
Zero_Array(idx_array, length);
// for (int i = 0; i < length; i++) {
// printf("%d ", idx_array[i]);
// }
while (arr[index] != 0 && length >= 1) {
if (arr[index] > 100 || !Check_Edge(arr[index], index, length)) {
}
k = arr[index];
for (int i = 0; i < k; i++)
{s += arr[index]; index++;}
if(s + 1 > length){
index = s; s = 0;
if(Seen_Index
(idx_array
, length
, index
)) {printf("-2\n"); return;} if (index != 0) { idx_array[index] = 1; }
jump_counter++;
} length
== 0 ? printf("Error!\n") : printf("%d\n", jump_counter
); }
bool Check_Edge(int value, int index, int length) {
if (length - index < value) {
return false;
}
return true;
}
bool Check_Sum(int sum, int length) {
if (sum + 1 > length) {
return false;
}
return true;
}
bool Seen_Index (int bin_array[], int length, int index) {
if(bin_array[index] == 1) {
return true;
}
return false;
}
void Zero_Array(int arr[], int length) {
for (int i = 0; i < length; i++) {
arr[i] = 0;
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0ZGJvb2wuaD4KCiNkZWZpbmUgTUFYX0xFTiAxMDAwMAoKdm9pZCBzdW1BbmRKdW1wKGludCBhcnJbXSwgaW50IGxlbmd0aCk7Cgpib29sIENoZWNrX0VkZ2U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ybjt9CiAgICBpbnQgaW5kZXggPSAwLCBrID0gMCwgcyA9IDAsIGp1bXBfY291bnRlciA9IDAsIGlkeF9hcnJheVtsZW5ndGhdOwogICAgWmVyb19BcnJheShpZHhfYXJyYXksIGxlbmd0aCk7CiAgICAvLyBmb3IgKGludCBpID0gMDsgaSA8IGxlbmd0aDsgaSsrKSB7CiAgICAvLyAgICAgcHJpbnRmKCIlZCAiLCBpZHhfYXJyYXlbaV0pOwogICAgLy8gfQogICAgd2hpbGUgKGFycltpbmRleF0gIT0gMCAmJiBsZW5ndGggPj0gMSkgewogICAgICAgIGlmIChhcnJbaW5kZXhdID4gMTAwIHx8ICFDaGVja19FZGdlKGFycltpbmRleF0sIGluZGV4LCBsZW5ndGgpKSB7CiAgICAgICAgICAgIHByaW50ZigiLTFcbiIpOyAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBrID0gYXJyW2luZGV4XTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGs7IGkrKykKICAgICAgICAgICAge3MgKz0gYXJyW2luZGV4XTsgICBpbmRleCsrO30KICAgICAgICBpZihzICsgMSA+IGxlbmd0aCl7CiAgICAgICAgICAgIHByaW50ZigiLTEhXG4iKTsgcmV0dXJuO30KICAgICAgICBpbmRleCA9IHM7ICAgcyA9IDA7CiAgICAgICAgaWYoU2Vlbl9JbmRleChpZHhfYXJyYXksIGxlbmd0aCwgaW5kZXgpKSB7cHJpbnRmKCItMlxuIik7IHJldHVybjt9CiAgICAgICAgaWYgKGluZGV4ICE9IDApIHsgaWR4X2FycmF5W2luZGV4XSA9IDE7IH0KICAgICAgICBqdW1wX2NvdW50ZXIrKzsKICAgIH0gICBsZW5ndGggPT0gMCA/IHByaW50ZigiRXJyb3IhXG4iKSA6IHByaW50ZigiJWRcbiIsIGp1bXBfY291bnRlcik7Cn0KCmJvb2wgQ2hlY2tfRWRnZShpbnQgdmFsdWUsIGludCBpbmRleCwgaW50IGxlbmd0aCkgewogICAgaWYgKGxlbmd0aCAtIGluZGV4IDwgdmFsdWUpIHsKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CiAgICByZXR1cm4gdHJ1ZTsKfQoKYm9vbCBDaGVja19TdW0oaW50IHN1bSwgaW50IGxlbmd0aCkgewogICAgaWYgKHN1bSArIDEgPiBsZW5ndGgpIHsKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CiAgICByZXR1cm4gdHJ1ZTsKfQoKYm9vbCBTZWVuX0luZGV4IChpbnQgYmluX2FycmF5W10sIGludCBsZW5ndGgsIGludCBpbmRleCkgewogICAgICAgIGlmKGJpbl9hcnJheVtpbmRleF0gPT0gMSkgewogICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICB9CiAgICByZXR1cm4gZmFsc2U7Cn0KCnZvaWQgWmVyb19BcnJheShpbnQgYXJyW10sIGludCBsZW5ndGgpIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbGVuZ3RoOyBpKyspIHsKICAgICAgICBhcnJbaV0gPSAwOwogICAgfQp9