```class Solution
{
//Function to find minimum number of operations that are required
//to make the matrix beautiful.
static int findMinOperation(int matrix[][], int n)
{
int sumRow[] = new int[n];
int sumCol[] = new int[n];
Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);

//calculating sumRow[] and sumCol[] array.
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
sumRow[i] += matrix[i][j];
sumCol[j] += matrix[i][j];

}
}

//finding maximum sum value in either row or in column.
int maxSum = 0;
for (int i = 0; i < n; ++i)
{
maxSum = Math.max(maxSum, sumRow[i]);
maxSum = Math.max(maxSum, sumCol[i]);
}

int count = 0;
for (int i = 0, j = 0; i < n && j < n;)
{
//finding minimum increment required in either row or column.
int diff = Math.min(maxSum - sumRow[i], maxSum - sumCol[j]);

//adding difference in corresponding cell,
//sumRow[] and sumCol[] array.
matrix[i][j] += diff;
sumRow[i] += diff;
sumCol[j] += diff;

//updating the result.
count += diff;

//if ith row is satisfied, incrementing i for next iteration.
if (sumRow[i] == maxSum)
++i;

//if jth column is satisfied, incrementing j for next iteration.
if (sumCol[j] == maxSum)
++j;
}
//returning the result.
return count;
}
}```
```class Solution
{
//Function to modify the matrix such that if a matrix cell matrix[i][j]
//is 1 then all the cells in its ith row and jth column will become 1.
void booleanMatrix(int matrix[][])
{
int r = matrix.length;
int c = matrix[0].length;

//using two list to keep track of the rows and columns
//that needs to be updated with 1.
int row[] = new int[r];
int col[] = new int[c];

for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
//if we get 1 in matrix, we mark ith row and jth column as 1.
if(matrix[i][j] == 1){
row[i] = 1;
col[j] = 1;
}
}
}

for(int i =0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
//if ith row or jth column is marked as 1, then all elements
//of matrix in that row and column will be 1.
if(row[i] == 1 || col[j] == 1){
matrix[i][j] = 1;
}
}
}
}
}```
```class Solution
{
//Function to interchange the rows of a matrix.
static void interchangeRows(int matrix[][])
{
for(int i=0;i<matrix.length/2;i++){
for(int j=0;j<matrix[i].length;j++){
int temp=matrix[i][j];
matrix[i][j]=matrix[matrix.length-i-1][j];
matrix[matrix.length-i-1][j]=temp;
}
}
}
}```
```class Solution
{
//Function to reverse the columns of a matrix.
static void reverseCol(int matrix[][])
{
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length/2; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[i].length-j-1];
matrix[i][matrix[i].length-j-1] = temp;
}
}
}
}```
```class Solution
{
//Function to exchange first column of a matrix with its last column.
static void exchangeColumns(int matrix[][])
{
int temp = 0;
for (int i=0; i<matrix.length; i++)
{
temp = matrix[i][0];
matrix[i][0] = matrix[i][matrix[i].length-1];
matrix[i][matrix[i].length-1] = temp;
}
}
}```
```class Solution
{
//Function to get cofactor of matrix[p][q] in temp[][].
static void getCofactor(int matrix[][], int temp[][], int p, int q, int n)
{
int i = 0, j = 0;

for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
//copying only those elements into temporary matrix
//which are not in given row and column.
if(row != p && col != q)
{
temp[i][j++] = matrix[row][col];

//if row is filled, we increase row index and
//reset column index.
if(j == n - 1)
{
j = 0;
i++;
}
}
}
}
}

//Function for finding determinant of matrix.
static int determinantOfMatrix(int matrix[][], int n)
{
int D = 0;

//base case
if (n == 1)
return matrix[0][0];

//creating a list to store Cofactors.
int temp[][]  = new int[n][n];

int sign = 1;

//iterating for each element of first row.
for (int i = 0; i < n; i++)
{
//getting Cofactor of matrix[0][i].
getCofactor(matrix, temp, 0, i, n);
D += sign * matrix[0][i] * determinantOfMatrix(temp, n - 1);

//terms are to be added with alternate sign so changing the sign.
sign = -sign;
}
//returning the determinant.
return D;
}
}```
```class Solution
{
//Function to multiply two matrices.
static int[][] multiplyMatrix(int A[][], int B[][])
{
int n1 = a.length;
int m1 = a[0].length;
int n2 = b.length;
int m2 = b[0].length;

if(m1!=n2)
{
int arr0[][] = new int[1][1];
arr0[0][0] = -1;
return arr0;
}

int arr[][] = new int[n1][m2];

for(int i = 0 ; i<n1 ; i++)
for(int j = 0 ; j<m2 ; j++)
for(int q = 0; q<n2 ; q++)
arr[i][j]+= a[i][q]*b[q][j];

return arr;
}
}```
```class Solution
{
//Function to return sum of upper and lower triangles of a matrix.
static ArrayList<Integer> sumTriangles(int matrix[][], int n)
{
ArrayList<Integer> list=new ArrayList<>();
int sum1=0;
int sum2=0;
for(int g=0; g<matrix.length; g++){
for(int i=0, j=g; j<matrix.length; i++,j++){
sum1+=matrix[i][j];
}
}
list.add(sum1);
for(int g=0; g<matrix.length; g++){
for(int i=g,j=0; i<matrix.length; i++,j++){
sum2+=matrix[i][j];
}
}
list.add(sum2);
return list;
}
}```
```class Solution
{
//Function to add two matrices.
static int[][] sumMatrix(int A[][], int B[][])
{
int n = A.length, m = A[0].length;
int res[][] = new int[0][0];
//Check if two input matrix are of different dimensions
if(n != B.length || m != B[0].length)
return res;

res = new int[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
res[i][j] = A[i][j] + B[i][j];

return res;
}
}```
```class Solution
{
//Function to find median of the array elements.
public int median(int A[],int N)
{

Arrays.sort(A);
int k=N/2;
if(N%2!=0){
return A[k];
}
else{
return (A[k-1]+A[k])/2;
}
}
//Function to find median of the array elements.
public int mean(int A[],int N)
{
int sum=0;
for(int i=0;i<N;i++)
{
sum=sum+A[i];
}
return sum/N;
}

}```
```class Solution {
// Function to find element with more appearances between two elements in an
// array.
public int majorityWins(int arr[], int n, int x, int y)
{
int countX=0;
int countY=0;
for(int i=0;i<n;i++)
{
if(arr[i]==x)
countX++;

if(arr[i]==y)
countY++;
}

if(countX>countY)
return x;

else if(countX==countY && x<y)
return x;

else
return y;
}
}```
```class Solution{

// Function to find largest and second largest element in the array
public static ArrayList<Integer> largestAndSecondLargest(int sizeOfArray, int arr[])
{
//code here.
int largest=-1;
int sec_largest=-1;
for(int i=0;i<sizeOfArray;i++)
{
largest = Math.max(largest,arr[i]);
}
for(int i=0;i<sizeOfArray;i++)
{
if(arr[i]<largest)
{
sec_largest= Math.max(sec_largest,arr[i]);
}
}
ArrayList<Integer> al = new ArrayList<Integer>(2);
al.add(largest);
al.add(sec_largest);
return al;
}
}
}```
```class StrongestNeighbour{

// Function to find maximum for every adjacent pairs in the array.
static void maximumAdjacent(int sizeOfArray, int arr[])
{
int sum=0;
for(int i=0; i<sizeOfArray-1; i++)
{
sum = Math.max(arr[i], arr[i+1]);
System.out.print(sum+" ");

}
}
}```
```class Solution{
//Function to reverse any part of the array.
void reverse(ArrayList<Integer> arr, int n,int left, int right)
{
//reversing the sub-array from left index to right index.
while (left < right) {
//swapping values at index stored at left and right index.
int temp = arr.get(left);
arr.set(left, arr.get(right));
arr.set(right, temp);

//updating values of left and right index.
left+=1;
right-=1;
}
}

//Function to reverse every sub-array group of size k.
void reverseInGroups(ArrayList<Integer> arr, int n, int k) {
for (int i = 0; i < n; i += k) {

//If (ith index +k) is less than total number of elements it means
//k elements exist from current index so we reverse k elements
//starting from current index.
if(i+k < n){
//reverse function called to reverse any part of the array.
reverse(arr,n,i,i+k-1);
}
//Else k elements from current index doesn't exist.
//In that case we just reverse the remaining elements.
else{
//reverse function called to reverse any part of the array.
reverse(arr,n,i,n-1);
}

}
}
}```
```class Solution{

//Function to find minimum adjacent difference in a circular array.
// arr[]: input array
// n: size of array
public static int minAdjDiff(int arr[], int n) {

int min=Math.abs(arr[0]-arr[n-1]),diff;
for(int i=0;i<n-1;i++)
{
diff=Math.abs(arr[i]-arr[i+1]);
if(diff<min)
min=diff;
}
return min;

}
}```
```class Solution{

//Function to swap two elements in the array.
public static void swap(int arr[], int x, int y){
//Use of a temporary variable to swap the elements.
int tmp = arr[x];
arr[x] = arr[y];
arr[y] = tmp;
}
//Function to sort the array into a wave-like array.
public static void convertToWave(int arr[], int n){

//Iterating over the array.
for(int i=0;i<n-1;i=i+2){
//Swapping two neighbouring elements at a time.
swap(arr, i, i+1);
}
return;
}

}```
```class Solution{
//Function to count the frequency of all elements from 1 to N in the array.
public static void frequencyCount(int arr[], int N, int P)
{
//Decreasing all elements by 1 so that the elements
//become in range from 0 to n-1.
int maxi = Math.max(P,N);
int count[] = new int[maxi+1];
Arrays.fill(count, 0);
for(int i=0;i<N;i++){
count[arr[i]]++;
}

for(int i=0;i<N;i++){
arr[i] = count[i+1];
}
}
}```
```class Solution {
// Function to find equilibrium point in the array.
public static int equilibriumPoint(long a[], int n) {

//We store the sum of all array elements.
long sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];

//sum2 is used to store prefix sum.
long sum2 = 0;
int ans = -1;

for (int i = 0; i < n; i++) {

//Leaving out the value of current element from suffix sum.
sum = sum - a[i];

//Checking if suffix and prefix sums are same.
if (sum2 == sum) {
//returning the index or equilibrium point.
return (i + 1);
}

//Adding the value of current element to prefix sum.
sum2 = sum2 + a[i];
}
return -1;
}
}```
```class Solution{
//Function to find the leaders in the array.
static ArrayList<Integer> leaders(int arr[], int n){

int maxEle = arr[n-1];

ArrayList<Integer> res = new ArrayList<>();

//We start traversing the array from last element.
for(int i=n-1; i>=0; i--) {

//Comparing the current element with the maximum element stored.
//If current element is greater than max, we add the element.
if(arr[i] >= maxEle){
//Updating the maximum element.
maxEle = arr[i];
//Storing the current element in arraylist for leaders.
res.add(maxEle);
}
}

//Reversing the arraylist.
Collections.reverse(res);
//returning the arraylist.
return res;
}

}```
```class Solution
{
//Function that puts all non-positive (0 and negative) numbers on left
//side of arr[] and return count of such numbers.
static int segregate (int arr[], int size)
{
int j = 0, i;
for(i = 0; i < size; i++)
{
if (arr[i] <= 0)
{
int temp;
//changing the position of negative numbers and 0.
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//incrementing count of non-positive integers.
j++;
}
}
return j;
}

//Finding the smallest positive missing number in an array
//that contains only positive integers.
static int findMissingPositive(int arr[], int size)
{
int i;
//marking arr[i] as visited by making arr[arr[i] - 1] negative.
//note that 1 is subtracted because indexing starts from 0 and
//positive numbers start from 1.
for(i = 0; i < size; i++)
{
if(Math.abs(arr[i]) - 1 < size && arr[Math.abs(arr[i]) - 1] > 0)
arr[Math.abs(arr[i]) - 1] = -arr[Math.abs(arr[i]) - 1];
}

for(i = 0; i < size; i++)
{
if (arr[i] > 0)
{
//returning the first index where value is positive.
// 1 is added because indexing starts from 0.
return i+1;
}
}
return size+1;
}

//Function to find the smallest positive number missing from the array.
static int missingNumber(int arr[], int size)
{
//first separating positive and negative numbers.
int shift = segregate (arr, size);

int arr2[] = new int[size-shift];
int j=0;
//shifting the array to access only positive part.
for(int i=shift;i<(size);i++)
{
arr2[j] = arr[i];
j++;
}

//calling function to find result and returning it.
return findMissingPositive(arr2, j);
}

}```
```class Solution{

//Function to rearrange  the array elements alternately.
public static void rearrange(long arr[], int n)
{
//Initialising index of first minimum and first maximum element.
int max_idx = n - 1, min_idx = 0;

//Storing maximum element of array.
long max_elem = arr[n - 1] + 1;

for (int i = 0; i < n; i++) {

//At even index, we have to put maximum elements in decreasing order.
if (i % 2 == 0) {
arr[i] += (arr[max_idx] % max_elem) * max_elem;
//Updating maximum index.
max_idx--;
}

//At odd index, we have to put minimum elements in increasing order.
else {
arr[i] += (arr[min_idx] % max_elem) * max_elem;
//Updating minimum index.
min_idx++;
}
}

//Dividing array elements by maximum element to get the result.
for (int i = 0; i < n; i++)
arr[i] = arr[i] / max_elem;

}

}```
```class Solution
{
//Function to rearrange an array so that arr[i] becomes arr[arr[i]]
//with O(1) extra space.
static void arrange(long arr[], int n)
{
int i = 0;

//Increasing all values by (arr[arr[i]]%n)*n to store the new element.
for(i = 0; i < n; i++)
arr[(int)i]+=(arr[(int)arr[(int)i]]%n)*n;

//Since we had multiplied each element with n.
//We will divide by n too to get the new element at that
//position after rearranging.
for(i = 0; i < n; i++)
arr[(int)i] = arr[(int)i]/n;
}
}```
```class Solution{
// Function to find the maximum index difference.
static int maxIndexDiff(int arr[], int n) {
if(n==1){
return 0;
}
int RMax[] = new int[n];
int LMin[] = new int[n];

//Constructing LMin[] such that LMin[i] stores the minimum value
//from (arr[0], arr[1], ... arr[i]).
LMin[0] = arr[0];
for (int i = 1; i < n; ++i)
LMin[i] = Integer.min(arr[i], LMin[i - 1]);

//Constructing RMax[] such that RMax[j] stores the maximum value
//from (arr[j], arr[j+1], ..arr[n-1]).
RMax[n - 1] = arr[n - 1];
for (int j = n - 2; j >= 0; --j)
RMax[j] = Integer.max(arr[j], RMax[j + 1]);

int i = 0, j = 0, maxDiff = -1;
//Traversing both arrays from left to right to find optimum j-i.
//This process is similar to merge() of MergeSort.
while (j < n && i < n) {
if (LMin[i] <= RMax[j]) {
//Updating the maximum difference.
maxDiff = Integer.max(maxDiff, j - i);
j++;
} else
i++;
}
//returning the maximum difference.
return maxDiff;
}
}```
```class Interval {
int buy;
int sell;
}

class Solution{
//Function to find the days of buying and selling stock for max profit.
ArrayList<ArrayList<Integer> > stockBuySell(int A[], int n) {

ArrayList<ArrayList<Integer> > result = new ArrayList<ArrayList<Integer> >();
//Prices must be given for at least two days else return the empty result.
if(n==1){
return result;
}

//Creating solution vector.
ArrayList<Interval> sol = new ArrayList<Interval>();
int i=0, cnt=0;
//Traversing through given price array.
while (i < n-1) {
//Finding Local Minima. Note that the limit of loop is (n-2)
//as we are comparing present element to the next element.
while ((i < n-1) && (A[i+1] <= A[i])){
i++;
}
//If we reach the end, we break loop as no further
//solution is possible.
if (i == n-1){
break;
}
Interval e = new Interval();
//Storing the index of minima which gives the day of buying stock.
e.buy = i++;

//Finding Local Maxima. Note that the limit of loop is (n-1)
//as we are comparing present element to previous element.
while ((i < n) && (A[i] >= A[i-1]))
i++;

//Storing the index of maxima which gives the day of selling stock.
e.sell = i-1;
sol.add(e);
//Incrementing count of buy/sell pairs.
cnt++;
}
if(cnt==0){
return result;
} else {
//Storing the buy/sell pairs in a list.
for(int j=0; j<sol.size(); j++){
result.add(new ArrayList<Integer>());
result.get(j).add(0, sol.get(j).buy);
result.get(j).add(1, sol.get(j).sell);
}
}
//returning the result.
return result;
}

}
```
star

Tue Feb 08 2022 09:38:53 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/make-matrix-beautiful-1587115620/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #beautifulmatrix
star

Tue Feb 08 2022 09:35:30 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/boolean-matrix-problem-1587115620/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #booleanmatrix
star

Tue Feb 08 2022 09:32:41 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/reversing-the-rows-of-a-matrix-1587115621/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #interchangingrows
star

Tue Feb 08 2022 08:47:50 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/reversing-the-columns-of-a-matrix-1587115621/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #reversingcolumns
star

Tue Feb 08 2022 08:41:01 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/exchange-matrix-columns-1587115620/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #exchangecolumns
star

Tue Feb 08 2022 08:29:37 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/determinant-of-a-matrix-1587115620/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #determinant
star

Tue Feb 08 2022 08:21:15 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/multiply-the-matrices-1587115620/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #multiplication
star

Tue Feb 08 2022 08:14:36 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/sum-of-upper-and-lower-triangles-1587115621/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #practice #sum
star

Tue Feb 08 2022 08:09:29 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/adding-two-matrices3512/1/?track=DSASP-Matrix&batchId=190

#java #gfg #geeksforgeeks #2d #array #matrix #addition #practice
star

Tue Feb 08 2022 06:38:10 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/mean-and-median-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #mean #median
star

Tue Feb 08 2022 06:33:11 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/who-has-the-majority/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #majority
star

Tue Feb 08 2022 06:26:48 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/max-and-second-max/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #max #secondmax
star

Tue Feb 08 2022 06:22:48 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/strongest-neighbour/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #strongestneighbour
star

Tue Feb 08 2022 06:17:15 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #arrays #practice #reversearray
star

Tue Feb 08 2022 06:10:45 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/minimum-absloute-difference-between-adjacent-elements-in-a-circular-array-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #circulararray #adjacentdifference #minimumadjacentdifference
star

Tue Feb 08 2022 05:46:24 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/wave-array-1587115621/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #wavearray
star

Tue Feb 08 2022 05:43:55 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/frequency-of-array-elements-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #frequencycount #frequencies #limitedrange
star

Tue Feb 08 2022 05:40:29 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/equilibrium-point-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #equilibriumpoint
star

Tue Feb 08 2022 05:35:58 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #leaders
star

Tue Feb 08 2022 05:32:27 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/smallest-positive-missing-number-1587115621/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #smallestpositive #missingnumber
star

Tue Feb 08 2022 05:22:26 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/-rearrange-array-alternately-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #rearrange #alternately
star

Tue Feb 08 2022 05:08:33 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/rearrange-an-array-with-o1-extra-space3142/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #rearrange
star

Tue Feb 08 2022 05:04:45 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/maximum-index-1587115620/1/?track=DSASP-Arrays&batchId=190

#java #gfg #geeksforgeeks #arrays #practice #maximumindex #maximumdifference
star

Tue Feb 08 2022 04:43:41 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/stock-buy-and-sell-1587115621/1/?track=DSASP-Arrays&batchId=190#

#java #gfg #geeksforgeeks #arrays #stock #practice #buysell #stockbuysell