|
| 1 | +import java.util.*; |
| 2 | +public class matrices_2DArrays { |
| 3 | + |
| 4 | + |
| 5 | + // public static void getLargeSmall(int arr[][]){ |
| 6 | + // int largest = Integer.MIN_VALUE; |
| 7 | + // int smallest = Integer.MAX_VALUE; |
| 8 | + // for(int i = 0 ; i < arr.length ; i++){ |
| 9 | + // for(int j = 0; j < arr[0].length;j++){ |
| 10 | + // largest = Math.max(largest,arr[i][j]); |
| 11 | + // smallest = Math.min(arr[i][j],smallest); |
| 12 | + // } |
| 13 | + // } |
| 14 | + // System.out.println("largest number = " + largest); |
| 15 | + // System.out.println("smallest number = " + smallest); |
| 16 | + // for(int i = 0 ; i < arr.length ; i++){ |
| 17 | + // for(int j = 0; j < arr[0].length;j++){ |
| 18 | + // if(largest == arr[i][j]){ |
| 19 | + // System.out.println(" largest At cell ("+ i + "," + j + ")" ); |
| 20 | + // } |
| 21 | + // if(smallest == arr[i][j]){ |
| 22 | + // System.out.println(" smallest At cell ("+ i + "," + j + ")" ); |
| 23 | + // } |
| 24 | + // } |
| 25 | + // } |
| 26 | + // } |
| 27 | + |
| 28 | + |
| 29 | + // public static boolean search(int arr[][] , int key) { |
| 30 | + // for(int i = 0 ; i < arr.length ; i++){ |
| 31 | + // for(int j = 0; j < arr[0].length;j++){ |
| 32 | + // if(arr[i][j] == key){ |
| 33 | + // System.out.println("Found at cell (" + i +"," + j + ")"); |
| 34 | + // return true; |
| 35 | + // } |
| 36 | + // } |
| 37 | + // } |
| 38 | + // System.out.println("Not found"); |
| 39 | + // return false; |
| 40 | + // } |
| 41 | + |
| 42 | + |
| 43 | + // public static void spiralMatrix(int arr[][]){ |
| 44 | + // int startRow = 0; |
| 45 | + // int startColumn = 0; |
| 46 | + // int endRow = arr.length-1; |
| 47 | + // int endColumn = arr.length-1; |
| 48 | + |
| 49 | + // while( startRow <= endRow && startColumn <= endColumn ){ |
| 50 | + // // top |
| 51 | + // for(int j = startColumn ; j <= endColumn ; j++){ |
| 52 | + // System.out.print(arr[startRow][j] + " " ); |
| 53 | + // } |
| 54 | + // // right |
| 55 | + // for (int i = startRow + 1; i <= endRow; i++) { |
| 56 | + // System.out.print(arr[i][endColumn] + " " ); |
| 57 | + // } |
| 58 | + // // bottom / down |
| 59 | + // for(int j = endColumn - 1 ; j >= startColumn ; j-- ){ |
| 60 | + // if(startRow == endRow){ |
| 61 | + // break; |
| 62 | + // } |
| 63 | + // System.out.print(arr[endRow][j] + " " ); |
| 64 | + // } |
| 65 | + // // left |
| 66 | + // for (int i = endRow -1; i >= startRow + 1; i--) { |
| 67 | + // if(startColumn == endColumn){ |
| 68 | + // break; |
| 69 | + // } |
| 70 | + // System.out.print(arr[i][startColumn] + " " ); |
| 71 | + // } |
| 72 | + // startRow++ ; startColumn++ ; endRow-- ; endColumn --; |
| 73 | + // } |
| 74 | + // } |
| 75 | + |
| 76 | + |
| 77 | + // public static int diagonalsum(int arr[][]){ |
| 78 | + // int sum = 0; |
| 79 | + |
| 80 | + // // // O n^2 time complexity |
| 81 | + // // for(int i = 0; i < arr.length ; i++ ){ |
| 82 | + // // for(int j = 0 ; j < arr[0].length ; j++){ |
| 83 | + // // if( i == j ){ |
| 84 | + // // sum += arr[i][j]; |
| 85 | + // // } |
| 86 | + // // if(i != j){ |
| 87 | + // // if(i+j == arr.length-1){ |
| 88 | + // // sum += arr[i][j]; |
| 89 | + // // } |
| 90 | + // // } |
| 91 | + // // } |
| 92 | + // // } |
| 93 | + |
| 94 | + |
| 95 | + // // // O n time complexity |
| 96 | + // for(int i = 0 ; i<arr.length ; i++ ){ |
| 97 | + // // // primary diagonal |
| 98 | + // sum += arr[i][i]; |
| 99 | + // if(i != arr.length-1-i){ |
| 100 | + // // // Seconary diagonal |
| 101 | + // sum += arr[i][arr.length-1-i]; |
| 102 | + // } |
| 103 | + // } |
| 104 | + |
| 105 | + // return sum; |
| 106 | + // } |
| 107 | + |
| 108 | + |
| 109 | + // public static boolean stairCaseSearch(int matrix[][] ,int key) { |
| 110 | + // int row = 0; int col = matrix[0].length-1; |
| 111 | + // while(row <= matrix.length && col >= 0){ |
| 112 | + // if(matrix[row][col] == key){ |
| 113 | + // System.out.println("found at index ( " + row + " , " + col + " )"); |
| 114 | + // return true; |
| 115 | + // } |
| 116 | + // else if(key > matrix[row][col]){ |
| 117 | + // row++; |
| 118 | + // } |
| 119 | + // else{ |
| 120 | + // col--; |
| 121 | + // } |
| 122 | + // } |
| 123 | + // System.out.println("index doesn't exist"); |
| 124 | + // return false; |
| 125 | + // } |
| 126 | + |
| 127 | + // public static boolean stairCaseSearch2(int arr[][] , int key) { |
| 128 | + // int row = arr.length-1 , col = 0; |
| 129 | + // while(row >= 0 && col <= arr[0].length){ |
| 130 | + // if(arr[row][col] == key){ |
| 131 | + // System.out.println("Found at Index (" + row + "," + col + ")"); |
| 132 | + // return true; |
| 133 | + // } |
| 134 | + // else if(arr[row][col] < key){ |
| 135 | + // col++; |
| 136 | + // } |
| 137 | + // else{ |
| 138 | + // row--; |
| 139 | + // } |
| 140 | + // } |
| 141 | + // System.out.println("Index does not Exist"); |
| 142 | + // return false; |
| 143 | + // } |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +// public static void sumofR2(int arr [][] ) { |
| 148 | +// int sum = 0; |
| 149 | +// for(int i = 0 ; i< arr.length ; i++){ |
| 150 | +// for(int j = 0 ; j < arr[0].length ; j++){ |
| 151 | +// if( i == 1 ){ |
| 152 | +// sum += arr[i][j]; |
| 153 | +// } |
| 154 | +// } |
| 155 | +// } |
| 156 | +// System.out.println("Sum = "+ sum); |
| 157 | +// } |
| 158 | + |
| 159 | +// public static void sumofR2_2(int arr[][]) { |
| 160 | +// int sum = 0; |
| 161 | +// for(int i = 0 ; i < arr.length ; i++){ |
| 162 | +// sum += arr[1][i]; |
| 163 | +// } |
| 164 | +// System.out.println("Sum = " + sum); |
| 165 | +// } |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +public static void print(int arr[][]) { |
| 170 | + for (int i = 0; i < arr.length; i++) { |
| 171 | + for (int j = 0; j < arr[0].length; j++) { |
| 172 | + System.out.print(arr[i][j] + " "); |
| 173 | + } |
| 174 | + System.out.println(); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +public static void main(String[] args) { |
| 180 | + Scanner sc = new Scanner(System.in); |
| 181 | + // int matrix[][] = new int [3][3]; |
| 182 | + // int n = matrix.length , m = matrix[0].length ; |
| 183 | + |
| 184 | + // System.out.println("Enter the Elements"); |
| 185 | + // for(int i = 0 ; i < n ; i++){ |
| 186 | + // for(int j = 0; j < m ;j++){ |
| 187 | + // matrix[i][j] = sc.nextInt(); |
| 188 | + // } |
| 189 | + // } |
| 190 | + // for(int i = 0 ; i < n ; i++){ |
| 191 | + // for(int j = 0; j < m ;j++){ |
| 192 | + // System.out.print(matrix[i][j]+" "); |
| 193 | + // } |
| 194 | + // System.out.println(); |
| 195 | + // } |
| 196 | + |
| 197 | + |
| 198 | + // search(matrix , 5); |
| 199 | + // getLargeSmall(matrix); |
| 200 | + |
| 201 | + |
| 202 | + // int matrix[][] = {{1,2,3,4}, |
| 203 | + // {5,6,7,8}, |
| 204 | + // {9,10,11,12}, |
| 205 | + // {13,14,15,16},}; |
| 206 | + // spiralMatrix(matrix); |
| 207 | + // System.out.println("Diagonal sum is " + diagonalsum(matrix)); |
| 208 | + |
| 209 | + |
| 210 | + // int arr[][] = {{ 10 , 20 , 30 , 40 }, |
| 211 | + // { 15 , 25 , 35 , 45 }, |
| 212 | + // { 27 , 29 , 37 , 48 }, |
| 213 | + // { 32 , 33 , 39 , 50 }}; |
| 214 | + // int key = 33; |
| 215 | + // stairCaseSearch(arr,key); |
| 216 | + // key = 30; |
| 217 | + // stairCaseSearch2(arr,key); |
| 218 | + |
| 219 | + |
| 220 | +// // Print the number of 7’s that are in the 2d array. |
| 221 | + // int arr[][] = {{4,7,8}, |
| 222 | +// {8,8,7}}; |
| 223 | +// int total = 0; |
| 224 | +// for (int i = 0; i < arr.length; i++) { |
| 225 | +// for(int j = 0 ; j < arr[0].length ; j++){ |
| 226 | +// if(arr[i][j] == 7){ |
| 227 | +// total++; |
| 228 | +// } |
| 229 | +// } |
| 230 | +// } |
| 231 | +// System.out.println("Total Number of 7's in 2d array = " + total ); |
| 232 | + |
| 233 | + |
| 234 | +// // // Print out the sum of the numbers in the second row of the “nums” array. |
| 235 | +// int[][] nums = {{1,4,9}, |
| 236 | +// {11,4,3}, |
| 237 | +// {2,2,3}}; |
| 238 | +// // Method - 1; --> O(n^2) |
| 239 | +// sumofR2(nums); |
| 240 | +// // Method - 2; --> O(n) |
| 241 | +// sumofR2_2(nums); |
| 242 | + |
| 243 | + |
| 244 | +// // // Write a program to Find Transpose of a Matrix , for (2*3)Matrix |
| 245 | +int matrix[][] = {{11,12,13}, |
| 246 | + {21,22,23}}; |
| 247 | + |
| 248 | + int transpose[][] = new int[matrix[0].length][matrix.length]; |
| 249 | + for (int i = 0; i < matrix.length; i++) { |
| 250 | + for (int j = 0; j< matrix[0].length; j++) { |
| 251 | + transpose[j][i] = matrix[i][j]; |
| 252 | + } |
| 253 | + } |
| 254 | + print(transpose); |
| 255 | + } |
| 256 | + |
| 257 | +} |
0 commit comments