Printing a K-ary tree stored in level-order in an array I am given an array that represents a k-ary tree (I am also given the branching factor "k") that is stored in level order. My task was to print the tree in level order. 

For example a trinary tree stored as {0,1,2,3,null,null,null,7,8,9,10,11,12} (It doesn't have to be full or complete) would print out as:

0
1 2 3
null null null 7 8 9 10 11 12


My solution currently works fine but it is very messy and not as elegant as I would prefer. The runtime complexity is also very bad (I don't know if Math.pow is O(1) or O(n) but if it's O(1), my code is still O(n) which is not ideal. If Math.pow is O(n) then my code is O(n2) which is garbage.

Is there another approach I am not seeing or can my existing code be optimized at all?

public static void main(String[] args) {
  int k = 3;
  Integer[] arrInt = {0,1,2,3,null,null,null,7,8,9,10,11,12};

  String ans = arrInt[0] + "\n";
  int currLvl = 1;
  int prevLvl = 0;
  //Print all values
  for(int i = 1; i &lt; arrInt.length-1; ++i){
    //Add a line if we reach a new level
    if(i == (Math.pow(k, currLvl)+Math.pow(k, prevLvl))){
      currLvl++;
      prevLvl++;
      ans+= "\n";  
    }
    ans+= " " + arrInt[i];
  }
  //EDGE CASE TO HANDLE LAST ELEMENT
  ans+= " " + arrInt[arrInt.length-1];
  System.out.println(ans);
}
Your biggest performance problem is the repeated string concatenation using the + operator.  Since Java strings are immutable, every += operation requires allocating a new string and copying the previous contents.  Use a StringBuilder instead.

Math.pow() should be avoided, since it performs floating-point arithmetic, and this task should be accomplished using just integer arithmetic.  Why not keep track of the desired width of the current row, and use an inner loop to print each row?

public static void main(String[] args) {
    int k = 3;
    Integer[] arrInt = {0,1,2,3,null,null,null,7,8,9,10,11,12};

    StringBuilder ans = new StringBuilder();
    for (int i = 0, width = 1; i &lt; arrInt.length; i += width, width *= k) {
        for (int j = i, end = Math.min(i + width, arrInt.length); j &lt; end; ++j) {
            ans.append((j &gt; i) ? " " : (j &gt; 0) ? "\n" : "")
               .append(arrInt[j]);
        }
    }
    System.out.println(ans);
}