Help me in solving THREEAPFREE problem

My issue

Why wouldn’t my logic work?

My code

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            HashSet<Integer> hs = new HashSet<>();
            int arr[] = new int[n];
            int flag=0;
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
                for(int j=0;j<i;j++){
                    int a=arr[i]-arr[j];
                    if(hs.contains(a)){
                        flag=1;
                        break;
                    }else{
                        hs.add(a);
                    }

                }
            }
            if(flag==1){
                System.out.println("NO");
            }else{
                System.out.println("YES");
            }
        }
    }
}

Problem Link: AP Free Sequences Practice Coding Problem - CodeChef

Check this out @ashishprakashp
My solution

# cook your dish here
for i in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    a=True
    for i in range(n):
        for j in range(i+1,n):
            for k in range(j+1,n):
                if 2*l[j]==l[i]+l[k]:
                    a=False
    
    if a!=True:
        print("NO")
    else:
        print("YES")