MaxElements of column - java

My question is how to find three largest elements in column of matrix. If there was about finding only largest element i would know, but here i have no idea. Here is my code for the largest element in column:
public static void Column(int m, int[][] arr)
{
for (int i = 0; i < m; i++) {
int max = arr[0][i];
for (int j = 1; j < arr[i].length; j++)
if (arr[j][i] > max)
max = arr[j][i];
System.out.println(max);
}
}

You need to reverse the i and j in the following pieces:
int max = arr[0][i];
if (arr[j][i] > max)
max = arr[j][i];
Also in the j loop the first value should be zero. if you wish to keep it at 1 then you should use <= as the conditional.

Related

I need to decrease first n elements of an array by 1

I would like to decrease the first n-elements in an array by 1. I tried this but its not working
int n = 3;
for(int i = 0 ; i <array.length;i++) {
while(i<=n){
array[i]-=1;
}
}
let's say n=3
int n =3;
for(i = n-1; i >= 0; i--)
{
array[i]-=1;
}
Alternatively as #locus2k points out we can also iterate through the array starting at the 0th index as so
int n = 3;
for(i = 0; i < n; i++)
{
array[i]-=1;
}

Index position of the smallest number in the array

I have a question about return the index position of the smallest number in the array. I picked out 3 answers from 5 answer were given that I provided below. I think one of them might possibly be correct. However, I quite don't understand the code very much, so I'm looking for an explanation how they work.
A
public int min(int[] a) {
int min = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] < a[min]) {
min = i;
}
}
return a[i];
}
B
public int min(int[] a) {
int min = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] < a[min]) {
min = i;
}
}
return i;
}
C
public int min(int[] a) {
int min = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] < a[min]) {
min = i;
}
}
return min;
}
It is pretty straight forward.. you are looking for the smallest index.
Imagine array a has 3 elements.
A
public int min(int[] a) {
int min = 0;
for (int i = 1; i < a.length, i++) {
if (a[i] < a[min]) {
min = i;
}
}
return a[i]; //returning value. Wrong!
//you are searching for the index, not value isnt' it?
}
B
public int min(int[] a) {
int min = 0;
for (int i = 1; i < a.length, i++) {
if (a[i] < a[min]) {
min = i;
}
}
return i; //returning i which only exist in for-loop scope. Wrong!
}
C
public int min(int[] a) { //assume a has 3 elements
int min = 0; //for storing smallest index
for (int i = 1; i < a.length, i++) { //loop index 1,2
if (a[i] < a[min]) { //if current array value < current smallest value
min = i; //store new found smallest index
}
}
return min; //returning min, the smallest index
}
If min is not updated through the looping, it means index 0 (default value of min) holds the smallest value. Option C will be the correct answer.
The first code will give an error (Index out of bound exception)because at the end of for loop your " i " will be equal to the length of array and you are returning a[i] so this solution is completely wrong. The second code will return the length of array so it is also wrong solution. The third code that you have entered will return the index of smallest value. So the solution is correct.

Find sum of every subarray in 2D array

I'm trying to find the sum of every subarray in a 2D array in Java and store them in a new array. This includes the sums of columns and rows. So far, all I can get down is code that can print out the sum of columns and rows.
I don't care about the complexity. In fact, I want to know the most brute force, obvious answer even if it is O(n^4). I am going to be comparing the subarray sums to a given value to see if that sum exists in the rectangular 2D array.
What I have:
public static void outputArray(int[][] array) {
for (int i = 0; i < array.length; i++){
int sum=0;
for (int j = 0; j < array[0].length; j++){
sum += array[i][j];
}
System.out.println("Print the sum of rows =" + sum);
}
int colSum=0;
for(int col=0;col<array[0].length;col++){
for(int row=0;row<array.length;row++){
colSum+=array[row][col];
}
System.out.println("Sum is "+colSum);
colSum = 0;
}
}
If u don't care about complexity && don't want to store the value then u can do it 0(n^6).
public static void outputArray(int[][] array) {
for(int i = 0; i < array.length; i++){
for(int j = 0; j< array[i].length; j++){
for(int k = i; k < array.length; k++){
for(int l = j; l < array[i].length; l++){
System.out.print("["+i+","+j+"]--->"+"["+k+","+l+"]");
int sum = 0;
for(int x = i; x <= k; x++){
for(int y = j; y<= l; y++){
sum+=array[x][y];
}
}
System.out.println("--->"+sum);
}
}
}
}
}
If u do some optimize like store prefix some then u can do it 0(n^4). With more optimization its possible to solve O(n^3).

How to sum a row in a matrix

Write the method:
public int sumRow(int[][] matrix, int row)
that sums row row in the 2D array called matrix.
Given:
public void run()
{
System.out.println(sumRow(new int[][]{{70,93,68,78,83},{68,89,91,93,72},{98,68,69,79,88}}, 2));
System.out.println(sumRow(new int[][]{{1,1,1}, {2,2,2}, {3,3,3}}, 0));
System.out.println(sumRow(new int[][]{{2,4,6,8,10}, {1,2,3,4,5}, {10,20,30,40,50}}, 2));
}
So far I have:
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix.length; j++)
{
sum = sum + matrix[j][i];
}
}
return sum;
}
The outputs I get are 714, 18, and 78 when they should be 402, 3, and 150. What am I doing wrong?
You're currently trying to sum all of the elements in the 2D array when you were asked to sum a specific row within the 2D array. In this case, you only need one for loop to traverse a single row like you would traverse a single array. The loop would start at the first element, matrix[row][0] and run until the last element, matrix[row][matrix[row].length - 1] since matrix[row].length is the number of columns/elements in that specific row of the matrix. Therefore, matrix[row].length - 1 would be the index of the last element in matrix[row]. Here's what it should look like,
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix[row].length; i++)
{
sum += matrix[row][i];
}
return sum;
}
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
int colSize = matrix[row].length;
for(int j = 0; j < colSize; j++){
sum += matrix[row][j];
}
return sum;
}
HINT
Length of row:
int row = matrix.length;
Length of column :
int col = matrix[0].length;
With Java Streams can be done very elegantly:
public static int sumRow2(int[][] matrix, int row) {
return Arrays.stream(matrix[row]).sum();
}
In the internal loop, modify the condition to:
for(int j = 0; j < matrix[i].length; j++)
and then switch i and j in the sum
there is a problem with your second for loop's condition , matrix.length is the length of first dimension , for the second dimension it looks like matrix[i].length
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[i].length; j++){
sum = sum + matrix[i][j];
}
}
i prefer to use sum+=matrix[i][j] instead of sum = sum + matrix[i][j]
calculating for one row :
for(int j = 0; j < matrix[row].length; j++){
sum = sum + matrix[row][j];
}
just note that row's range is from 0 to matrix.length-1
You need to reference the second dimension of the array for j.
//ex: first dimension is matrix.length
//second dimension is matrix[any index in the first dimension].length
//and this cycle would continue with more and more [num] on the end
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix**[0]**.length; j++)
{
sum = sum + matrix[j][i];
}
}
return sum;
}

Code is printing out largest subscript of the array when it should be printing maximum and minimum

I'm trying to print the largest and smallest values on an array, as well as their respective subscripts. I'm running into a problem where my code, though printing the highest and lowest values of the array, is returning the largest subscript (regardless of value) for both maximum and minimum arrays. The problem lies somewhere in the for loops, below.
int max = arrayOfNumbers[0];
int min = arrayOfNumbers[0];
int indexMax = 0;
int indexMin = 0;
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (max < arrayOfNumbers[i])
max = arrayOfNumbers[i];
indexMax = i;
}
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (min > arrayOfNumbers[i])
min = arrayOfNumbers[i];
indexMin = i;
This should work:
int max = arrayOfNumbers[0];
int min = arrayOfNumbers[0];
int indexMax = 0;
int indexMin = 0;
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (max < arrayOfNumbers[i]) {
max = arrayOfNumbers[i];
indexMax = i;
}
}
for (int i = 0; i < arrayOfNumbers.length; i++) {
if (min > arrayOfNumbers[i]) {
min = arrayOfNumbers[i];
indexMin = i;
}
}
You forgot to put both lines after the ifs into braces. Each time the code inside the loops executed, the current index had been written into both indexMax and indexMin, and that's why after the loops finished, they contained the "largest subscript".

Categories

Resources