Advertisement
Guest User

Untitled

a guest
May 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. Note: Write a solution with O(n) complexity, since this is what you would be asked to do during a real interview.
  2.  
  3. Given an array a composed of distinct elements, find the next larger element for each element of the array, i.e. the first element to the right that is greater than this element, in the order in which they appear in the array, and return the results as a new array of the same length. If an element does not have a larger element to its right, put -1 in the appropriate cell of the result array.
  4.  
  5. Example
  6.  
  7. For a = [6, 7, 3, 8], the output should be
  8. nextLarger(a) = [7, 8, 8, -1].
  9.  
  10. In this array, the next larger element for 6 is 7, for 7 is 8, for 3 is 8 (7 is not a valid option since elements from a can only be compared to elements to their right), and for 8 there is no such element, so we put -1 in the last cell.
  11.  
  12. Input/Output
  13.  
  14. [execution time limit] 0.5 seconds (cpp)
  15.  
  16. [input] array.integer a
  17.  
  18. An array composed of distinct elements.
  19.  
  20. Guaranteed constraints:
  21. 1 ≤ a.length ≤ 104,
  22. 0 ≤ a[i] ≤ 105.
  23.  
  24. [output] array.integer
  25.  
  26. The result array, in which the ith cell corresponds to the first element in a to the right of a[i] that is larger than a[i], or -1 if there is no such element.
  27.  
  28. [C++] Syntax Tips
  29.  
  30. // Prints help message to the console
  31. // Returns a string
  32. std::string helloWorld(std::string name) {
  33. std::cout << "This prints to the console when you Run Tests" << std::endl;
  34. return "Hello, " + name;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement