?
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | |
---|---|---|---|---|---|---|---|---|---|
138966687 |
Practice: ScoobyDoobyDo |
1610B - 35 | Java 8 | Accepted | 327 ms | 0 KB | 2021-12-13 12:29:40 | 2021-12-13 12:29:40 |
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: In this approach, I thought we're doing the same thing? maybe I'm wrong. We're excluding the remove element within the loop only. What's the difference? public static boolean pal(int[] a, int remove, int i, int j) { while(i < j) { while(i < j && a[i] == remove) i++; while(j > i && a[j] == remove) j--; if(a[i] != a[j]) return false; i++; j--; } return true; } }
?
?
?
?