Counting Special Sub-Cubes


Problem Statement :


Given an n*n*n cube, let f(x,y,z) (where 1 <= x,y,z <= n) denote the value stored in cell (x,y,z).

A k*k*k sub-cube (where 1 <= k <= n) of an n*n*n cube is considered to be special if the maximum value stored in any cell in the sub-cube is equal to k.

For each k in the inclusive range [1,n], calculate the number of special sub-cubes. Then print each (count)k as a single line of space-separated integers (i.e., count1,count2,...,countn).

Input Format

The first line contains an integer, q, denoting the number of queries. The 2.q subsequent lines describe each query over two lines:

1.The first line contains an integer, n, denoting the side length of the initial cube.
2.The second line contains n^3 space-separated integers describing an array of n^3 integers in the form a0,a1,...,a(n^3-1). The integer in some cell (x,y,z) is calculated using the formula a[(x-1).n^2 + (y-1).n + z].

Constraints
1 <= q <= 5
1 <= n <= 50
1 <= f(x,y,z) <=n where 1 <= x,y,z <=n
Output Format

For each query, print n space-separated integers where the ith integer denotes the number of special sub-cubes for k=i.



Solution :



title-img


                            Solution in C :

In C++ :





#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>

using namespace std;
typedef long long ll;
const int MAXN = 52;

int Q;
int N;
int val[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];

int main()
{
    cin >> Q;
    for (int test = 0; test < Q; test++)
    {

    cin >> N;
    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXN; j++)
            for (int k = 0; k < MAXN; k++)
                val[i][j][k] = dp[i][j][k] = 0;

    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            for (int k = 0; k < N; k++)
            {
                cin >> val[i][j][k];
                dp[i][j][k] = val[i][j][k];
            }

    for (int i = 1; i <= N; i++)
    {
        int ans = 0;
        for (int j = 0; j <= N - i; j++)
            for (int k = 0; k <= N - i; k++)
                for (int l = 0; l <= N - i; l++)
                {
                    if (dp[j][k][l] == i) ans++;

                    for (int m = 0; m < 2; m++)
                        for (int n = 0; n < 2; n++)
                            for (int o = 0; o < 2; o++)
                                dp[j][k][l] = max (dp[j][k][l], dp[j+m][k+n][o+l]);
                
                }

        cout << ans;
        if (i < N)
            cout << " ";
    }
    cout << "\n";
    }
    return 0;
}








In Java :






import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author jin
 */
public class Solution {

    public static void main(String args[]) throws Exception {

        int q = input();
        for (int i = 0; i < q; i++) {
            int n = input();
            int ans[] = new int[n];

            int b = n * n * n, c = n * n;
            int arr[][][] = new int[50][50][50];
            int crr[][][] = new int[50][50][50];
            int ck[] = new int[b];
            input(ck, b);
            int count=0;
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    for (int l = 0; l < n; l++) {
                        arr[j][k][l] = ck[count++];
                //        System.out.print(arr[j][k][l]+" ");
                        if (arr[j][k][l] == 1) {
                            ans[0]++;
                        }

                    }
              //      System.out.println();
                }
            //    System.out.println("");

            }
            System.out.print(ans[0] + " ");
            for (int p = 1; p < n; p++) {

                for (int l = 0; l < n - p; l++) {
                    for (int j = 0; j < n; j++) {
                        for (int k = 0; k < n; k++) {
                            if (arr[j][k][l + 1] > arr[j][k][l]) {
                                arr[j][k][l] = arr[j][k][l + 1];
                            }

                        }

                    }
                }
                for (int l = 0; l < n - p; l++) {
                    for (int j = 0; j < n; j++) {
                        for (int k = 0; k < n-p; k++) {
                          //  crr[j][l][k] = arr[j][l][k];
                            if (arr[j][l + 1][k] > arr[j][l][k]) {
                                arr[j][l][k] = arr[j][l + 1][k];
                            }

                        }

                    }
                }
                for (int l = 0; l < n - p; l++) {
                    for (int j = 0; j < n-p; j++) {
                        for (int k = 0; k < n-p; k++) {
                            if (arr[l + 1][j][k] > arr[l][j][k]) {
                                arr[l][j][k] = arr[l + 1][j][k];
                            }

                        }

                    }
                }

                for (int l = 0; l < n - p; l++) {
                    for (int j = 0; j < n-p; j++) {
                        for (int k = 0; k < n-p; k++) {
                         //   System.out.println(l+" "+j+" "+k+"  "+(p+1));
                           if(arr[j][k][l]==p+1) 
                        ans[p]++;
                        }
                    }
                }

                System.out.print(ans[p] + " ");

            }
            System.out.println();
        }

    }

    static BufferedReader br = new BufferedReader(new InputStreamReader(
            System.in));
    private static String s[], w;

    public static void input(int a[], int p) throws IOException {
        s = br.readLine().split(" ");
        int i;
        for (i = 0; i < p; i++) {
            a[i] = Integer.parseInt(s[i]);
        }

    }

    public static void input(long a[], int p) throws IOException {
        s = br.readLine().split(" ");
        int i;
        for (i = 0; i < p; i++) {
            a[i] = Long.parseLong(s[i]);
        }
    }

    public static void input(double a[], int p) throws IOException {
        s = br.readLine().split(" ");
        int i;
        for (i = 0; i < p; i++) {
            a[i] = Double.parseDouble(s[i]);
        }
    }

    public static int input() throws IOException {
        int a;
        a = Integer.parseInt(br.readLine());
        return a;
    }

}








In C :





#include<stdio.h>


int cube(int x,int y,int z,int size,int count[],int table[50][50][50][50],int n,int arr[])
{
	if(table[x][y][z][size-1]==0)
	{
		if(size==1)
		   table[x][y][z][0] = arr[(x*n*n) + (y*n) +(z+1)];
		else
		{
			int max =  0;
			int a = cube(x,y,z,size-1,count,table,n,arr);
			if(a>max)
			max =a;
			int b = cube(x+1,y,z,size-1,count,table,n,arr);
			if(b>max)
			max  = b;
			int c = cube(x,y+1,z,size-1,count,table,n,arr);
			if(c>max)
			max =c;
			int d= cube(x+1,y+1,z,size-1,count,table,n,arr);
			if(d>max)
			max = d;
			int e= cube(x,y,z+1,size-1,count,table,n,arr);
			if(e>max)
			max = e;
			int f= cube(x+1,y,z+1,size-1,count,table,n,arr);
			if(f>max)
			max = f;
			int g= cube(x,y+1,z+1,size-1,count,table,n,arr);
			if(g>max)
			max = g;
			int h= cube(x+1,y+1,z+1,size-1,count,table,n,arr);
			if(h>max)
			max = h;
			table[x][y][z][size-1]=max;
		}
		if(table[x][y][z][size-1]==size)
		count[size-1]++;
	}
	return table[x][y][z][size-1];
}

int main()
{
  int query;
  scanf("%d",&query);
  while(query--)
  {
  	int n,i;
  scanf("%d",&n);
  int arr[(n*n*n)+1];
  for(i=1;i<=(n*n*n);i++)
  scanf("%d",&arr[i]);
  int table[50][50][50][50] = {0};
  int count[n];
  for(i=0;i<n;i++)
  count[i] =0;
  int p= cube(0,0,0,n,count,table,n,arr);
  for(i=0;i<n;i++)
  printf("%d ",count[i]);
  printf("\n");
  }
}








In Python3 :





def findMax(arr, s, indices, n):
    largest = 1
    for i in indices:
        cur = arr[s+i]
        if cur > largest:
            largest = cur
        elif cur == n:
            largest = n
            break
    return largest

def findSpecial(arr, k, n):
    temp = arr
    if k > 1:
        temp = []
        m = n - k + 2
        m2 = m*m
        indices = [0, 1, m, m+1, m2, m2+1, m2+m, m2+m+1]
        for i in range(m-1):
            start = m2*i
            for j in range(m-1):
                for z in range(m-1):
                    largest = findMax(arr, start, indices, n)
                    temp.append(largest)
                    start += 1
                start += 1
    return temp    
               
q = int(input())
for _ in range(q):
    n = int(input())
    arr = list(map(int, input().split()))
    for k in range(1, n+1):
        arr = findSpecial(arr, k, n)
        print(arr.count(k), end=' ')
    print('')
                        








View More Similar Problems

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →