Output Format
Print the largest (maximum) hourglass sum found in.
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
Explanation
contains the following hourglasses:
The hourglass with the maximum sum () is:
2 4 4
2
1 2 4
using System;
class Solution
{
static int R = 6;
static int C = 6;
// Returns maximum sum of
// hour glass in ar[][]
static int findMaxSum(int[,] arr)
{
if (R < 3 || C < 3)
return -1;
// Here loop runs (R-2)*(C-2)
// times considering different
// top left cells of hour glasses.
int max_sum = int.MinValue;
for (int i = 0; i < R - 2; i++)
{
for (int j = 0; j < C - 2; j++)
{
// Considering mat[i][j] as top
// left cell of hour glass.
int sum = (arr[i, j] + arr[i, j + 1] +
arr[i, j + 2]) + (arr[i + 1, j + 1]) +
(arr[i + 2, j] + arr[i + 2, j + 1] +
arr[i + 2, j + 2]);
// If previous sum is less then
// current sum then update
// new sum in max_sum
max_sum = Math.Max(max_sum, sum);
}
}
return max_sum;
}
// Driver code
static public void Main(String[] args)
{
int[,] mat = {{1, 1 ,1 ,0 ,0 ,0 },
{ 0, 1, 0 ,0, 0, 0 },
{ 1, 1, 1, 0, 0, 0},
{ 0, 0, 2 ,4, 4, 0},
{ 0, 0 ,0 ,2, 0, 0},
{ 0, 0 ,1, 2, 4, 0}};
int res = findMaxSum(mat);
if (res == -1)
Console.WriteLine("Not possible");
else
Console.WriteLine("Maximum sum of hour glass = "
+ res);
Console.ReadLine();
}
}
Rotate an array of integers in the left direction
using System;
class Solution
{
public static void Main()
{
int[] nums = { 1, 2, 3, 4, 5 };
bool flag = true;
Console.WriteLine("\nArray1: [{0}]", string.Join(", ", nums));
for (int s = 0; s < nums.Length - 1 && flag == true; s++)
{
flag = false;
var temp = nums[0];
for (var i = 0; i < nums.Length - 1; i++)
{
nums[i] = nums[i + 1];
flag = true;
}
nums[nums.Length - 1] = temp;
Console.WriteLine("\nAfter rotating array becomes: [{0}]", string.Join(", ", nums));
}
Console.ReadLine();
}
}
No comments:
Post a Comment