You are on page 1of 15

ARRAYS

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Block swap algorithm

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Block swap algorithm for array rotation
Definition:
It is used to rotate an array by any number of positions with a Time Complexity of
O(N) and Space Complexity of O(1).
Algorithm : 
Initialize A = arr[0, 1, …, d-1] and B = arr[d, d+1, …, n-1]
1) Do following until size of A is equal to size of B
a) If A is shorter, divide B into Bl and Br such that Br is of same
length as A. Swap A and Br to change ABlBr into BrBlA. Now A
is at its final place, so recur on pieces of B.  
b) If A is longer, divide A into Al and Ar such that Al is of same
length as B Swap Al and B to change AlArB into BArAl. Now B
is at its final place, so recur on pieces of A. 
2) Finally when A and B are of equal size, block swap them.

© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Example:1
Recursive implementation:

        if(n - d == d){
            swap(arr, i, n - d + i, d);
            return;
import java.util.*;
 
        }           
class Main{         if(d < n - d){
        public static void leftRotate(int arr[], int d,  int n){             swap(arr, i, n - d + i, d);
        leftRotateRec(arr, 0, d, n);             leftRotateRec(arr, i, d, n - d);    
    }         }
 public static void leftRotateRec(int arr[], int i, int d, int         else {
n){ swap(arr, i, d, n - d);
               if(d == 0 || d == n)             leftRotateRec(arr, n - d + i, 2 * d - n, d);
            return;         }
}
               
 
         

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:1
Recursive implementation:

 public static void swap(int arr[], int fi,


                        int si, int d) public static void main (String[] args)
{ {
    int i, temp;     int arr[] = {1, 2, 3, 4, 5, 6, 7};
    for(i = 0; i < d; i++){     leftRotate(arr, 2, 7);
        temp = arr[fi + i];         for(int i = 0; i < size; i++)
        arr[fi + i] = arr[si + i];         System.out.print(arr[i] + " "); 
        arr[si + i] = temp; }
    }} }

     

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Iterative Implementation: 

static void leftRotate(int arr[], int d, int n) else


{     {
int i, j;     swap(arr, d-i, d, j);
if(d == 0 || d == n)     i -= j;
    return;     }
 if(d > n)  }
   d = d % n; swap(arr, d-i, d, i);
i = d; }
j = n - d;
while (i != j)
{
    if(i < j)     {
    swap(arr, d-i, d+j-i, i);
    j -= i;
    }

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Maximum Product Subarray

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Maximum Product Subarray

Definition:
Given an array that contains both positive and negative integers, find the
product of the maximum product subarray.

Examples:
Input: arr[] = {6, -3, -10, 0, 2}
Output: 180 // The subarray is {6, -3, -10}
 
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60 // The subarray is {60}
 
Input: arr[] = {-2, -40, 0, -2, -3}
Output: 80 // The subarray is {-2, -40}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Important Examples
{2,-5,0,-1,4,-3}

{-1,-2,0,4,2,-3}

{-1,5,0,3,-2}

{-1,0,-3}

{-3}
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Example:2
class Main{ {
        public static int findMaxProduct(int[] A){     int temp = max_ending;
 
                if (A.length == 0) {
   max_ending = Integer.max(A[i],
            return 0; Integer.max(A[i] * max_ending,
        }                                                  A[i] * min_ending)); 
 maximum and minimum product    min_ending = Integer.min(A[i],
Integer.min(A[i] * temp,
        
                                                A[i] * min_ending)); 
        int max_ending = A[0], min_ending = A[0];             max_so_far = Integer.max(max_so_far,
         max_ending);
        int max_so_far = A[0];          } 
                         return max_so_far;
    } 
        for (int i = 1; i < A.length; i++){     public static void main(String[] args)
    {
        int[] A = { -6, 4, -5, 8, -10, 0, 8 }; 
        System.out.print("The maximum product
of a subarray is " + findMaxProduct(A));
    }}
Output: The maximum productof a subarray is
1600

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Maximum sum of hour glass in matrix

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Maximum sum of hour glass in matrix

Definition: Output :
Given a 2D matrix, the task is that we Below is the hour glass with
find maximum sum of a hour glass. maximum sum:
Examples: 111
An hour glass is made of 7 cells 1
in following form. 111
ABC Input : 0 3 0 0 0
D 01000
EFG 11100
00244
Examples:  00024
Input : 1 1 1 0 0 Output : 11
01000 Below is the hour glass with
11100 maximum sum
00000 100
00000 4
024
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:1
for (int j = 0; j < C - 2; j++)
        { int sum = (mat[i][j] + mat[i][j + 1] +
                mat[i][j + 2]) + (mat[i + 1][j +
import java.io.*;  1]) +
class GFG {                         (mat[i + 2][j] + mat[i + 2][j +
static int R = 5; 1] +
                       mat[i + 2][j + 2]); 
static int C = 5; 
                max_sum = Math.max(max_sum, sum);
static int findMaxSum(int [][]mat)         }
{     }
    if (R < 3 || C < 3)     return max_sum;} 
        return -1;      static public void main (String[] args)
    {
        int max_sum = Integer.MIN_VALUE;         int [][]mat = {{1, 2, 3, 0, 0},
    for (int i = 0; i < R - 2; i++)                        {0, 0, 0, 0, 0},
    {                        {2, 1, 4, 0, 0},
for (int j = 0; j < C - 2; j++)                        {0, 0, 0, 0, 0},
                       {1, 1, 0, 1, 0}};
        {
        int res = findMaxSum(mat);
            int sum = (mat[i][j] + mat[i][j + 1] +         if (res == -1)
                       mat[i][j+2)+(mat[i+1][j+1])             System.out.println("Not possible");
+                       (mat[i + 2][j] + mat[i+2][j+ 1]         else
+ mat[i + 2][j + 2]);             System.out.println("Maximum sum of hour
glass = "
                                + res);
    } } 

Output: Maximum sum of hour glass = 13

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:2
import java.util.*; sum = (matrix[i][j] + matrix[i][j + 1] +
class hourglass matrix[i][j + 2]) + (matrix[i + 1][j + 1]) +
{ (matrix[i + 2][j] + matrix[i + 2][j + 1] +
matrix[i + 2][j + 2]);
public static void main(String[]args)
if(sum > max){max = sum;}}}
{
System.out.println("The maximum sum in the
Scanner scan = new Scanner(System.in);
hourglass is: "+max);
System.out.print("Enter the number of rows: ");
}
int rows = scan.nextInt();
}
System.out.print("Enter the number of columns:
");
Output:
int columns = scan.nextInt();
Enter the number of rows: 5
int[][]matrix = new int[rows][columns];
Enter the number of columns: 5
System.out.println("Enter the elements of the
Matrix: "); Enter the elements of the Matrix:
for(int i = 0; i < rows; i++) 12456
{for(int j = 0; j < columns; j++) 75236
{matrix[i][j]=scan.nextInt();}} 00000
int sum = 0,max = 0; 75 1 35
for(int i = 0; i < rows - 2; i++) 00000
{for(int j = 0; j < columns - 2; j++) The maximum sum in the hourglass is:27
{

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank You …

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.

You might also like