[Testdome] Malware analysis

whitehousechef·2023년 12월 5일

but i didnt account when the given list could be short. If it is short, then it will result in index out of range error. So when we need to check these conditions in the first and last for loop.

For the first loop, we need to get list[i+4] index so we need to first check if i+4 does not exceed n

Same for the last loop, we need to get list[i-3] index so we check if i-3 does not go beyond 0 and has a minus value

that to java is

    public static int[] simulate(int[] entries) {
      
      int[] arr = Arrays.copyOf(entries, entries.length);
      
      for(int i = 0; i < 3; i++){
        if(i + 4 < entries.length && entries[i+4] >= entries[i]) {
           arr[i] = 0;
        }
      }
      
      for(int i = 3; i < entries.length - 4; i++) {
        if(entries[i-3] >= entries[i] || entries[i+4] >= entries[i]){
          arr[i] = 0;
        }
      }
      
      for(int i = entries.length-4; i < entries.length; i++) {
        if(i - 3 >= 0 && entries[i-3] >= entries[i]) {
          arr[i] = 0;
        }
      }

      return arr;
    }

0개의 댓글