#Fails every case but sample

1 messages · Page 1 of 1 (latest)

fluid cradle
#
import java.util.*;
import java.io.*;
public class Triangle {
    static String name = "triangles";
    public static void main(String[] args) throws Exception{
        Scanner in = new Scanner(new FileReader(name+".in"));
        PrintWriter out = new PrintWriter(new FileWriter(name+".out"));
        ArrayList<Integer> x = new ArrayList<>();
        ArrayList<Integer> y = new ArrayList<>();
        long max = 0;
        int num = in.nextInt();
        for(int i = 0;i<num;i++) {
            x.add(in.nextInt());
            y.add(in.nextInt());
        }
        for(int i = 0;i<num;i++) {
            for(int j = 0;j<num;j++) {
                if(x.get(i)==x.get(j)) {
                    long l = Math.abs(y.get(i)-y.get(j));
                    
                    for(int k = 0;k<num;k++) {
                        
                        if(y.get(k)==y.get(i)||y.get(k)==y.get(j)) {
                            
                        long h = Math.abs(x.get(k)-x.get(i));
                        max = Math.max(max, l*h);
                        }
                    }
                }
                if(y.get(i)==y.get(j)) {
                    int l = Math.abs(x.get(i)-x.get(j));
                    for(int k = 0;k<num;k++) {
                        if(x.get(k)==x.get(i)||x.get(k)==x.get(j)) {
                        long h = Math.abs(y.get(k)-y.get(i));
                        max = Math.max(max, l*h);
                        }
                    }
                }
            }
            }
        out.println(max);
        out.close();
    }
}

This is my code. Complete Search for every possible triangle. Please let me know what I am missing. My thought process was -> Since the triangle has to be parallel to x and y -> right angle triangle.

high tigerBOT
# fluid cradle ```java import java.util.*; import java.io.*; public class Triangle { static...

Detected code, here are some useful tools:

Formatted code
import java.util. * ;
import java.io. * ;

public class Triangle {
  static String name = "triangles";
  public static void main(String[] args) throws Exception {
    Scanner in = new Scanner(new FileReader(name + ".in"));
    PrintWriter out = new PrintWriter(new FileWriter(name + ".out"));
    ArrayList<Integer> x = new ArrayList<>();
    ArrayList<Integer> y = new ArrayList<>();
    long max = 0;
    int num = in.nextInt();
    for (int i = 0; i < num; i++) {
      x.add(in.nextInt());
      y.add(in.nextInt());
    }
    for (int i = 0; i < num; i++) {
      for (int j = 0; j < num; j++) {
        if (x.get(i) == x.get(j)) {
          long l = Math.abs(y.get(i) - y.get(j));
          for (int k = 0; k < num; k++) {
            if (y.get(k) == y.get(i) || y.get(k) == y.get(j)) {
              long h = Math.abs(x.get(k) - x.get(i));
              max = Math.max(max, l * h);
            }
          }
        }
        if (y.get(i) == y.get(j)) {
          int l = Math.abs(x.get(i) - x.get(j));
          for (int k = 0; k < num; k++) {
            if (x.get(k) == x.get(i) || x.get(k) == x.get(j)) {
              long h = Math.abs(y.get(k) - y.get(i));
              max = Math.max(max, l * h);
            }
          }
        }
      }
    }
    out.println(max);
    out.close();
  }
}
#

<@&987246717831381062> please have a look, thanks.

high tigerBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

shrewd ravine
shrewd ravine
#

@fluid cradle need help

#

Runtime error or memory limit exceeded on sample input case -- details below
Your output file triangles.out:
[File missing!]

The correct answer:
2

#

i submitted a code and this error came on that website, what does it mean?

#

what to do when submitting a code there>

#

?

#

ohh got it lol

#

need to scan from a file and print on file lol

#

well got a solution in O(n^2)

#

HINT: just cal the max length it can achieve in both directions, the max of all nodes is the ans

#

for an vertex, u can find max length on both in O(n+n)