[text] fhcghkhfgsdh

Viewer

copydownloadembedprintName: fhcghkhfgsdh
  1. import java.util.concurrent.*;
  2. 2.  class Sum {
  3. 3.      int low, high;    
  4. 4.      int[] array;
  5. 5.      Sum(int[] arr, int lo, int hi) {
  6. 6.          array = arr;
  7. 7.          low   = lo;
  8. 8.          high  = hi;
  9. 9.      }
  10. 10.     protected Long process() {
  11. 11.         if(high - low <= 4) {
  12. 12.             long sum = 0;
  13. 13.             for(int i=low; i < high; ++i) 
  14. 14.                 sum += array[i];
  15. 15.             return sum;
  16. 16.          } else {
  17. 17.             int mid = low + (high - low) / 2;
  18. 18.             Sum left  = new Sum(array, low, mid);
  19. 19.             Sum right = new Sum(array, mid, high);
  20. 20.             left.fork();
  21. 21.             long rightAns = right.process();
  22. 22.             long leftAns  = left.join();
  23. 23.             return leftAns + rightAns;
  24. 24.          }
  25. 25.      }
  26. 26.      static long sumArray(int[] array) {
  27. 27.          return new ForkJoin().invoke(new Sum(array,0,array.length));
  28. 28.      }
  29. 29. }

Editor

You can edit this paste and save as new:


File Description
  • fhcghkhfgsdh
  • Paste Code
  • 23 Mar-2023
  • 966 Bytes
You can Share it: