Diagonal Difference

  • + 4 comments

    I got your point, you are thinking in a very broad perspective applying it in a situation where memory is a constraint. Here it is not! Python takes 0.01 seconds to execute the above program unlike Java(0.1 - 0.29 seconds) that's why I stated it being very powerful. It is good to think in your terms where memory is getting wasted, but not here.

    To your point "The same exact solution is easily accomplished in Java, but it still relies on saving unneeded values to an array which, as has already been pointed out, is actually a less optimal solution because it needlessly stores n^2 - 2n integers in arrays." Even in the below solution you are saving every input into curInput n^2 times which contradicts your point.

    for(int j = 0; j < numInputs; j++){
       for(int k = 0; k < numInputs; k++){
          cin >> curInput;
          if(j == k){
            leftD += curInput;
          }
          if(j+k == (numInputs-1)){
            rightD += curInput;
          }
       }
     }
    

    Yours Java equivalent solution to Python looks so hard to read.

    Look at this Python solution:

    n = int(input().strip())
    dSumLeft = 0
    dSumRight = 0
    for i in range(n):
        matrixRow = input().split()
        dSumLeft += int(matrixRow[i])
        dSumRight += int(matrixRow[-(i + 1)])
    

    Simple and Powerful!

    We need to learn and understand the balance required between performance and memory usage.