To find the maximum integer value in a matrix I try to code some of that:
/*
* #param ints
* #return the max value in the array of chars
*/
public static int maxMatrix(int [][] ints) {
int max = ints[0][0];
for(int i = 0; i < ints.length; i++) {
for(int j = 0; j < ints.length){
max = inst[i][j];
}
}
return max;
}
My questions are:
Why am I assigning the variable to the next one in the array?
What are the conditions and why?
max = inst[i][j];
Should be max = Math.max(max, ints[i][j]);
and...
for(int j = 0; j < ints.length){
should be for(int j = 0; j < ints[i].length; j++){
So...
public static int maxMatrix(int [][] ints) {
int max = ints[0][0];
for(int i = 0; i < ints.length; i++) {
for(int j = 0; j < ints[i].length; j++){
max = Math.max(max, ints[i][j]);
}
}
return max;
}
You need to do Math.max. Otherwise you're just assigning the variable to the next in the array
Math.max(max, ints[i][j]) is equivalent to:
if (max > ints[i][j] {
return ints[i][j]; // or inline in your loop: max = ints[i][j];
} else {
return max; // or inline in your loop: max = max; which is a not needed
}
I suggest this
for (int[] a : ints) {
for (int e : a) {
if (e > max) {
max = e;
}
}
}
You need an if statement inside of your loop for that to work. Also you need to check the length of the second part of the array.
public static int maxMatrix(int [][] ints) {
int max = ints[0][0];
for(int i = 0; i < ints.length; i++) {
for(int j = 0; j < ints[].length){
if(inst[i][j] > max){
max = inst[i][j];
}
}
}
return max;
}
Related
I am trying to create a program, which counts the minimum of each dimension in a two dimensional array. So for ex. if i had an array:
int[][] test = {{1,2,3},{2,3,4},{4,5,6}}
the program would display: [1,2,4] - the minimum of each dimension.
For that I've created a method called minimum, which looks like this
static int[] minimum(int[][] arr) {
int[] result = new int [arr.length];
for (int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {
int min = arr[i][0];
if(arr[i][j] < min) {
min = arr [i][j];
result [i] = min;
} else{
}
}
}
return result;
}
But when i call out this method in my main, with a sample array
public static void main(String[] args) {
int[][] arr = {{1,2,3,},{3,4,5},{6,6,6}};
System.out.println(Arrays.toString(minimum(arr)));
}
The program displays [0,0,0,]. Do You have any clue where is the problem and how to fix it?
The problem is that if the first element in the array is min, it never gets recorded to the result array. Try:
static int[] minimum(int[][] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i][0];
for (int j = 1; j < arr[i].length; j++) {
if (arr[i][j] < result[i]) {
result[i] = arr[i][j];
}
}
}
return result;
}
Note that there needs to be at least one element per row in the input matrix for the above function; add a conditional or use Integer.MIN_VALUE to handle empty rows if you wish.
This should work. You reset the min to the first element every time. So you are basically comparing if there is any value smaller than the first one.
static int[] minimum(int[][] arr){
int[] result = new int [arr.length];
for (int i = 0; i < arr.length; i++){
result[i] = Integer.MAX_VALUE;
for(int j = 0; j < arr[i].length; j++){
if(arr[i][j] < result[i]) {
result [i] = arr[i][j];
}
}
}
return result;
}
I created a function to multiply a matrix by itself which gets 2 parameters, one is the matrix, the other one is an int n. The problem is that I cant figure out where should I use the n in my code so that it multiplies the matrix by itself an n number of times (in other words matrix^n). At current stage it only does matrix^2;
public static int[][] lungimeDrumuri(int[][] array, int n) {
int[][] newArray = new int[array.length][array.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int sum = 0;
for (int x = 0; x < array.length; x++) {
sum += array[i][x] * array[x][j];
}
newArray[i][j] = sum;
}
}
return newArray;
}
Add a third for loop that goes from 1 < k < n . You will need to remain array untouched in order to maintain the values of the initial matrix, will also need a matrix newArray to keep the values of the previous multiplication and a temporary matrix tmp that just hold values during the multiplication itself and then is copied to newArray.
Take a look in the sample below.
FULL CODE
public static int[][] lungimeDrumuri(int[][] array, int n) {
int[][] newArray = new int[array.length][array.length];
// Just holds values during multiplication between two matrices
int[][] tmp = new int[array.length][array.length];
// Initialize newArray to be equal to array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
newArray[i][j] = array[i][j];
}
}
// Outer loop that multiplies as many times as you want
for (int k = 1; k < n; k++) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int sum = 0;
for (int x = 0; x < array.length; x++) {
sum += newArray[i][x] * array[x][j]; // Use newArray here
}
tmp[i][j] = sum;
}
}
// Copy the result from multiplication to newArray and restart tmp
System.arraycopy(tmp, 0, newArray, 0, tmp.length);
tmp = new int[array.length][array.length];
}
return newArray;
}
Hope it helped!
You can create two methods for clarity: the first to multiply a square matrix, and the second to call the first n number of times.
public static int[][] lungimeDrumuri(int[][] array, int n) {
int[][] newArray = array;
for (int i = 0; i < n; i++) {
newArray = squareMatrixMultiplication(newArray);
}
return newArray;
}
public static int[][] squareMatrixMultiplication(int[][] array) {
int[][] newArray = new int[array.length][array.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
for (int x = 0; x < array.length; x++) {
newArray[i][j] += array[i][x] * array[x][j];
}
}
}
return newArray;
}
Initialize newArray to be equal to array, then
add a loop around the matrix multiplication and use newArray in your nested loops: multiply newArray by array.
public static int[][] lungimeDrumuri(int[][] array, int n) {
int[][] newArray = new int[array.length][array.length];
// Add loops to initialize newArray to array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
newArray[i][j] = array[i][j];
}
}
for (int j = 0; j < n; j++) { // Add this loop
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int sum = 0;
for (int x = 0; x < array.length; x++) {
sum += newArray[i][x] * array[x][j]; // Use newArray here
}
newArray[i][j] = sum;
}
}
} // and this
return newArray;
}
public class MyClass {
public static void main(String args[]) {
int array[][] = new int[2][2];
array[0][0] = 1;
array[0][1] = 2;
array[1][0] = 3;
array[1][1] = 4;
int newArray[][] = new int[2][2];
//initialize array with these elements
newArray[0][0] = 1;
newArray[0][1] = 0;
newArray[1][0] = 0;
newArray[1][1] = 1;
int n = 5;
for (int i = 0; i < n; i++) {
newArray = lungimeDrumuri(array, newArray, i);
}
}
public static int[][] lungimeDrumuri(int[][] array, int newArray[][], int n) {
int newArray1[][] = new int[array.length][array.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
int sum = 0;
for (int x = 0; x < array.length; x++) {
sum += array[i][x] * newArray[x][j];
}
newArray1[i][j] = sum;
}
}
return newArray1;
}
}
Hope this one will help you.
Recently, I took Linkedin placement test in which there was a question in which output for 4 test cases were wrong for me. I could not figure out what was my mistake becasue inputs/outputs were hidden.
Anyways here was the question:
Find the maximum element from an array where product of any other two elements would be equal to that number and return that number .If no, such element is there then return -1.
Here was my solution:
static int maxElement(int[] arr) {
Arrays.sort(arr);
int max = arr[arr.length-1];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
return -1;
}
I guess you need to find the possible maximum number in the array and the product of two elements in the array.
If I assume this, your code fails for this test case:
int[] arr = {2,4,5,3,7,6}; , where the answer should be 6
Check this below code it will work for above test-case.
Just add one more reverse for loop to check the possible value and product.
static int maxElement(int[] arr) {
Arrays.sort(arr);
for (int k = arr.length-1; k >= 0; k--) {
int max = arr[k];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
}
return -1;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] * a[j]) > maxSum) {
if(list.contains(a[i] * a[j]))
maxSum = a[i] * a[j];
}
}
}
if (maxSum != 0)
return maxSum;
return -1;
I was trying to write a method that finds the largest number in a column. However, it seems that I am having problem to find a way to return the highest number in a column rather than taking into consideration all numbers combined in the array. I would really appreciate any comments or feedback!
This is my code:
public static void max1(int[][] score) {
for (int i = 0; i < score.length; i++) {
int max = score[0][0];
for (int j = 0; j < score[i].length; j++)
if (score[i][j] > max)
max = score[i][j];
System.out.println(max + " ");
}
}
You are trying to find the max in row not in column. Do some changes in your code
public static void max1(int[][] score) {
for (int i = 0; i < score[0].length; i++) { // i should be your column
int max = score[0][i];// assign 1st value of the column as max
for (int j = 0; j < score.length; j++){ // j is your row
if (score[j][i] > max){ // check the column elements instead of row elements
max = score[j][i];// get the column values
}
}
System.out.println(max + " ");
}
}
Do int max = score[0][i]; instead of int max = score[0][0]; Because if you always intialize max with score[0][0] and this int is bigger then the biggest value in a column, you will get a wrong result.
And do:
if (score[innerloop][outerloop] > max)
max = score[innerloop][outerloop];
instead of:
if (score[outerloop][innerloop] > max)
max = score[outerloop][innerloop];
That was logical failure because the first index is the column and the secound is the row.
score[0][0] = 1;
score[0][1] = 2;
score[0][2] = 3;
score[1][0] = 4;
score[1][1] = 5;
score[1][2] = 6;
score[2][0] = 7;
score[2][1] = 8;
score[2][2] = 9;
The Array now look like this matrix:
1 2 3
4 5 6
7 8 9
You want compare for example 1,4,7 so you habe to compare score[0][0], score[0][1], score[0][2]. So the secound index must be the counter of the inner for-loop.
You need to specify the index of the column you want to find the max in and loop on rows:
public static int maxCol(int [][] score, int colIndex) {
int max = score[0][colIndex];
for(int i = 0 ; i < score.length ; ++i) {
max = Math.max(max, score[i][colIndex]);
}
return max;
}
public static void max1(int[][] score) {
for (int i = 0; i < score.length; i++) {
int max = -1;
for (int j = 0; j < score[i].length; j++)
if (score[i][j] > max)
max = score[i][j];
System.out.println("max: '" + max + "'");
}
}
And perhaps you switched your columns and rows, means you have to switch the code too, happened already a lot to me. Try to check if i is your rows and j is your columns.
The following code prints the Maximum Number in each column.
public static void max1(int[][] score) {
for (int i = 0; i < score.length; i++) {
int max = Integer.MIN_VALUE; // set max to minimum value before starting loop
for (int j = 0; j < score[i].length; j++)
{
if (score[i][j] > max)
max = score[i][j];
}
System.out.println(max + " ");
}
}
First you should change the int max = score[0][0]; into as int max = Integer.MIN_VALUE; as it might produce not valid result.
And the second is that you need to swap the index while compare
if (score[j][i] > max) {
max = score[j][i];
}
static int max1(int[][] score) {
int max = score[0][0]; // set initial value to the first index in array
for (int i = 0; i < score.length; i++) { // cycle through row
for (int j = 0; j < score[i].length; j++) { // cycle through colmn
if (score[i][j] > max) { // if the index value is greater than largest number
max = score[i][j]; // make it the new index value
}
}
} return max;
}
Write a function int bentley(int a) that return the index, j, from a pair of indices (i,j) such that a[i]+a[i+1]++a[j-1] is maximum over all such pairs (i,j).
^^This is the question i have to answer. Im assuming "a[i]+a[i+1]++a[j-1]" means from a[i ] to a[j-1]. So far this is what i have:
public static int bentley(int[] a) {
int max = 0;
int oldsum = 0;
int sum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
sum = oldsum + a[j];
if (sum > oldsum) {
max = j;
oldsum = sum;
}
else {
sum = oldsum;
}
}
}
return max;
}
It doesnt work, i dont know why or how to fix it.