Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. // The Original Problem Description
  2. // # Write a function, `nearest_larger(arr, i)` which takes an array and an
  3. // # index. The function should return another index, `j`: this should
  4. // # satisfy:
  5. // #
  6. // # (a) `arr[i] < arr[j]`, AND
  7. // # (b) there is no `j2` closer to `i` than `j` where `arr[i] < arr[j2]`.
  8. // #
  9. // # In case of ties (see example below), choose the earliest (left-most)
  10. // # of the two indices. If no number in `arr` is larger than `arr[i]`,
  11. // # return `nil`.
  12. // #
  13. // # Difficulty: 2/5
  14. //
  15. // def nearest_larger(arr, idx)
  16. // diff = 1
  17. // while true
  18. // left = idx - diff
  19. // right = idx + diff
  20.  
  21. // if (left >= 0) && (arr[left] > arr[idx])
  22. // return left
  23. // elsif (right < arr.length) && (arr[right] > arr[idx])
  24. // return right
  25. // elsif (left < 0) && (right >= arr.length)
  26. // return nil
  27. // end
  28.  
  29. // diff += 1
  30. // end
  31. // end
  32.  
  33. // What I have found is that
  34. // when a number which is greater than the number at the given index is found,
  35. // the function immediately returns it without considering numbers left in the array
  36. // though there might be numbers which are less than the number found earlier and
  37. // greater than the number at the given value
  38. // For example
  39. // nearest_larger([2,4,3,8], 0) should return 2 because the nearest larger number of
  40. // 2 at the index of 0 is 3 at the index of 2. However the solution function returns 1 where
  41. // 4 is at because the function returns when it finds a number
  42. // which is greater than the number at the given index for the first time
  43. // So I have tried to fix the issue below.
  44. // I made it to keep iterating through the whole array even after a number which is greater than
  45. // the number at the given index is found.
  46.  
  47. #include <iostream>
  48. #include <vector>
  49. using namespace std;
  50. int nearest_larger(vector<int> &array, int index) {
  51. int result_index = 0;
  52. int temp_index = 0;
  53. bool found_one = false;
  54. for (int i=0; i<array.size(); i++) {
  55. if (i != index) {
  56. if (array.at(index) < array.at(i)) {
  57. temp_index = i;
  58. if (!found_one) {
  59. // if a number which is greater than the number at the given index
  60. // for the first time
  61. result_index = temp_index;
  62. found_one = true;
  63. } else {
  64. // if at least a number which is greater than the number at the given index
  65. // is already found, then need to compare it with upcoming numbers
  66. // to determine the number at result_index is really a nearest larger number
  67. if (array.at(temp_index) < array.at(result_index)) {
  68. result_index = temp_index;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return result_index;
  75. }
  76.  
  77. int main () {
  78. vector<int> array = {2,4,3,8};
  79. cout << nearest_larger(array, 0) << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement