Untitled

 avatar
unknown
java
10 months ago
4.3 kB
3
Indexable
import java.util.*;
import java.io.*;
class Pair{
    int x;
    int y;
    int z;
    
    Pair(int x, int y, int z){
        
        
        this.x = Math.min(x,y);
        this.x = Math.min(this.x,z);
        this.z = Math.max(x,y);
        this.z= Math.max(this.z,z);
        if(this.x != x && this.z != x) this.y = x;
        else if(this.x != y && this.z != y) this.y = y;
        else this.y = z;
    }
}
public class Main{
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br=new BufferedReader(new InputStreamReader(System.in));
        }
        String next(){
            while(st==null || !st.hasMoreTokens()){
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str="";
            try {
                str=br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    static class FastWriter {
		private final BufferedWriter bw;
		public FastWriter() {
			this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
		}
		public void print(Object object) throws IOException {
			bw.append("" + object);
		}
		public void println(Object object) throws IOException {
			print(object);
			bw.append("\n");
		}
		public void close() throws IOException {
			bw.close();
		}
	}
	
	
	
    public static void main(String[] args) {
        try {
            FastReader sc=new FastReader();
            FastWriter out = new FastWriter();
            
            //write code here
            int t = sc.nextInt();
            while(t-->0){
            	int n = sc.nextInt();
                int[] arr = new int[n];
                HashMap<String, Boolean> vis  = new HashMap<>();
                for(int i = 0; i<n;i++){
                    arr[i] = sc.nextInt();
                }
                List<Pair> pairs = new ArrayList<>();
                int count = 0;
                for(int j = 0; j< n;j++){
                    int k = -arr[j];
                    HashMap<Integer, Integer> map = new HashMap<>();
                    for(int i = 0;i<n;i++){
                        int res = k - arr[i];
                        // System.out.println(k+" "+arr[i]);
                        if(map.containsKey(arr[i]) && j != i && i != map.get(arr[i]) && j != map.get(arr[i])){
                            Pair newPair = new Pair(arr[j], arr[map.get(arr[i])],arr[i]); 
                            String str = newPair.x+" "+newPair.y+" "+newPair.z;
                            if(!vis.containsKey(str)){
                                vis.put(str,true);
                                pairs.add(newPair);
                                count++;
                            }
                        }
                        map.put(res,i);
                    }
                    System.out.println(map);
                }
                pairs.sort((p1, p2) -> {
                    if (p1.x != p2.x) {
                        return Integer.compare(p1.x, p2.x);
                    } 
                    else if(p1.y != p2.y) {
                        return Integer.compare(p1.y, p2.y);
                    }
                    else{
                        return Integer.compare(p1.z, p2.z);
                    }
                });
                System.out.println(count);
                for(Pair pair:pairs){
                    System.out.println(pair.x+" "+pair.y+" "+pair.z);
                    
                }
            }
            out.close();
        
        }
        catch (Exception e) {
            e.printStackTrace() ;
            return;
        }
    }
    
}
Editor is loading...
Leave a Comment