'Simplest way to check if a number is in the same row as another in a numpad [closed]
Hard to explain easily in the title. I have two numbers and need to find it they are both in the same row in a number pad:
7 8 9
4 5 6
1 2 3
So a row would be 7, 8, 9 or 1, 2, 3 etc. If I had the numbers 1 and 5 that would be negative since they are not both in the same row, while if I had the numbers 7 and 9 that would be positive since they would be in the same row. I have a feeling there is some kind of simple maths that I'm missing that would get me the answer.
Also in the same way I would need to find out if two numbers are in the same column in a separate query. A column being of course 1, 4, 7 or 3, 6, 9.
The only solution I found myself for these was to make arrays of all the rows and columns, like [4, 5, 6] etc and then check if the two numbers are in the same array but it seems a bit long winded.
Is my solution already the simplest/shortest one or is there a better one?
EDIT: Edited to make it clearer that I want the simplest and not necessarily the best. Clearly closed in error for lack of clarity? since there has already been an answer which was exactly what I was looking for.
Solution 1:[1]
Yes, there is a quick mathematical method.
Two integers are in the same column if num1 % 3 == num2 % 3
Two integers are in the same row if (num1 - 1) / 3 == (num2 - 1) / 3
The important thing for the row-check is that they are integers since integer division will drop the remainder.
Solution 2:[2]
By the way, "best" is a very ambiguous term. It could mean most efficient, best looking, or perhaps even the "coolest" way to do said task. I am unsure as to what you are looking for. But, in my opinion, the coolest way to do this, and the best way for future cases, is to be able to find the row and column of an element and then compare these values with the other element you are looking for; in your case a number like 4 and 7.
This gives you both answers at the same time. In the example below, we use a generic 4x5 matrix to show how this solution applies to your specific use case and is future-proof.
import java.util.*;
public class abc {
public static void main(String[] args) {
int nums[][] = {{12, 20, 30, 40},
{15, 25, 35, 45},
{24, 29, 39, 51},
{35, 30, 39, 50},
{50, 60, 75, 72}};
int rows = 5;
int search_element = 39;
int ans[] = Saddleback(nums, rows - 1, 0, search_element);
System.out.println("Position of "+search_element+" in the matrix is ("+ans[0] + "," + ans[1]+")");
}
/**
* @param nums, the matrix.
* @param row the current row.
* @param col the current column.
* @param search_element the element that we want to search for.
* @return value: If found the index(row and column) of the element.
* else return (-1 -1).
*/
private static int[] Saddleback(int nums[][], int row, int col, int search_element) {
//numsay to store the row and column of the searched element
int element_pos[] = {-1, -1};
if (row < 0 || col >= nums[row].length) {
return element_pos;
}
if (nums[row][col] == search_element) {
element_pos[0] = row;
element_pos[1] = col;
return element_pos;
}
//move up if the current element is greater than the given element
else if (nums[row][col] > search_element) {
return Saddleback(nums, row - 1, col, search_element);
}
//otherwise move right
return Saddleback(nums, row, col + 1, search_element);
}
/**
* Main method
*
* @param args Command line arguments
*/
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jacob K |
| Solution 2 | Kirby |
