fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #define MAX_LEN 10000
  6.  
  7. void sumAndJump(int arr[], int length);
  8.  
  9. bool Check_Edge(int value, int index, int length);
  10.  
  11. bool Check_Sum(int sum, int length);
  12.  
  13. bool Seen_Index (int array[], int legnth, int index);
  14.  
  15. void Zero_Array(int array[], int length);
  16.  
  17. int main() {
  18. int len_array = 0;
  19. // First enter the size of the array
  20. scanf("%d", &len_array);
  21. // allocating the array - will be explained later in the course
  22. int *arr = malloc(sizeof(int) * len_array);
  23. if (arr) {
  24. // Enter the numbers that the array will hold
  25. for (int start_index = 0; start_index < len_array; start_index++) {
  26. scanf("%d", &(arr[start_index]));
  27. }
  28. sumAndJump(arr, len_array);
  29. }
  30. // Freeing the array - will be explained later in the course
  31. free(arr);
  32. return 0;
  33. }
  34.  
  35. void sumAndJump(int arr[], int length) {
  36. // if (length > MAX_LEN) { printf("Error!\n"); return;}
  37. int index = 0, k = 0, s = 0, jump_counter = 0, idx_array[length];
  38. Zero_Array(idx_array, length);
  39. // for (int i = 0; i < length; i++) {
  40. // printf("%d ", idx_array[i]);
  41. // }
  42. while (arr[index] != 0 && length >= 1) {
  43. if (arr[index] > 100 || !Check_Edge(arr[index], index, length)) {
  44. printf("-1\n"); return;
  45. }
  46. k = arr[index];
  47. for (int i = 0; i < k; i++)
  48. {s += arr[index]; index++;}
  49. if(s + 1 > length){
  50. printf("-1!\n"); return;}
  51. index = s; s = 0;
  52. if(Seen_Index(idx_array, length, index)) {printf("-2\n"); return;}
  53. if (index != 0) { idx_array[index] = 1; }
  54. jump_counter++;
  55. } length == 0 ? printf("Error!\n") : printf("%d\n", jump_counter);
  56. }
  57.  
  58. bool Check_Edge(int value, int index, int length) {
  59. if (length - index < value) {
  60. return false;
  61. }
  62. return true;
  63. }
  64.  
  65. bool Check_Sum(int sum, int length) {
  66. if (sum + 1 > length) {
  67. return false;
  68. }
  69. return true;
  70. }
  71.  
  72. bool Seen_Index (int bin_array[], int length, int index) {
  73. if(bin_array[index] == 1) {
  74. return true;
  75. }
  76. return false;
  77. }
  78.  
  79. void Zero_Array(int arr[], int length) {
  80. for (int i = 0; i < length; i++) {
  81. arr[i] = 0;
  82. }
  83. }
Success #stdin #stdout 0.01s 5276KB
stdin
2 2 2 0 1 4 3
stdout
-1!