?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
138966477 |
Practice: ScoobyDoobyDo |
1610B - 35 | Java 8 | Wrong answer on test 2 | 326 ms | 0 KB | 2021-12-13 12:26:33 | 2021-12-13 12:26:33 |
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Practice { static Scanner scn = new Scanner(System.in); static StringBuilder sb = new StringBuilder(); public static void main(String[] HastaLaVistaLa) { int t = scn.nextInt(); // int t = 1; for(int tests = 0; tests < t; tests++) solve(); System.out.println(sb); } // main function is written here public static void solve() { int n = scn.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = scn.nextInt(); int i = 0, j = n - 1; while(i <= j && a[i] == a[j]) { i++; j--; // trimming all a(i) == a(n - 1 - i) from both sides of course } if(j <= i || pal(a, a[i], i, j) || pal(a, a[j], i, j)) { // if either is true, then YES otherwise NO sb.append("YES"); }else { sb.append("NO"); } sb.append("\n"); } // This function checks if the array is palindrome after removing the remove element i.e. a[i] or a[j] (for which a[i] != a[j]) // Approach: I decided to make a new list of integer a[i, j] excluding remove element, then checked if it's palindrome or not public static boolean pal(int[] a, int remove, int i, int j) { List<Integer> l = new ArrayList<>(); for(int k = i; k <= j; k++) if(a[k] != remove) l.add(a[k]); int left = 0, right = l.size() - 1; while(left < right){ if(l.get(left) != l.get(right)) return false; // found a(i) != a(n - 1 - i) left++; right--; } return true; } }
?
?
?
?