Although Java does not have native tuple support, the good news is that you do not need to use tuples to implement this algorithm, except for the return value. Two ordinary variables int r and q will be executed.
// (q,r)=(0,0) int q = 0, r = 0; // (q,r)=divide(โx/2โ, y) q = (x/2) / y; r = (x/2) % y;
Returning is a bit complicated because you need to return two values. An idiomatic way to do this in Java is to define a class:
class QandR { private final int q; private final int r; public QandR(int q, int r) { this.q = q; this.r = r; } }
Now you can return new QandR(q, r) from your method.
source share