fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. // An efficient method to calculate
  11. // stock span values implementing the
  12. // same idea without using stack
  13. static void calculateSpan(int A[],
  14. int n, int ans[])
  15. {
  16. // Span value of first element is always 1
  17. ans[0] = 1;
  18.  
  19. // Calculate span values for rest of the elements
  20. for (int i = 1; i < n; i++) {
  21. int counter = 1;
  22. printArray(A, i);
  23. while ((i - counter) >= 0 && A[i] > A[i - counter]) {
  24. counter += ans[i - counter];
  25. }
  26. ans[i] = counter;
  27. }
  28. }
  29.  
  30. // A utility function to print elements of array
  31. static void printArray(int arr[], int n)
  32. {
  33. for (int i = 0; i < n; i++)
  34. System.out.print(arr[i] + " ");
  35. System.out.println();
  36. }
  37.  
  38. // Driver code
  39. public static void main(String[] args)
  40. {
  41. int price[] = { 10, 4, 5, 90, 120, 80 };
  42. int n = price.length;
  43. int S[] = new int[n];
  44.  
  45. // Fill the span values in array S[]
  46. calculateSpan(price, n, S);
  47.  
  48. // print the calculated span values
  49. printArray(S, n);
  50. }
  51. }
Success #stdin #stdout 0.08s 33844KB
stdin
Standard input is empty
stdout
10 
10 4 
10 4 5 
10 4 5 90 
10 4 5 90 120 
1 1 2 4 5 1