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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[] arr = {1,2,2,3};
  13. int target = 2;
  14.  
  15. System.out.println(countMajoritySubarrays(arr,target));
  16. }
  17.  
  18.  
  19. static int countMajoritySubarrays(int[] nums, int target) {
  20. int n = nums.length;
  21.  
  22. int max_subarray = 0;
  23. int length = 0;
  24. for(int i=0;i<n;i++){
  25. int c = 0;
  26. for(int j = i;j<n;j++){
  27. if(nums[j] == target){
  28. c++;
  29. }
  30. length = j - i + 1;
  31. if(c > length / 2){
  32. max_subarray++;
  33. }
  34. }
  35. }
  36.  
  37. return max_subarray;
  38. }
  39. }
Success #stdin #stdout 0.08s 52528KB
stdin
Standard input is empty
stdout
5