Maximize height of tower using elements from one of the given Subarrays
Given an array arr[] of length N, two arrays start[] and end[] of size M each where {start[i], end[i]} denotes a subarray of arr[] and an integer base, the task is to determine the maximum height of a tower with given base that can be formed using elements from a single subarray bounded by any {start[i], end[i]} and satisfying the following conditions:
- All elements in each level should be equal.
- Number of elements in each level is the same as base.
- No two level can have the same element.
Example to understand the problem
Examples:
Input: N = 9, arr[] = {1, 5, 7, 3, 2, 4, 6, 9, 8}, M = 3, start[] = {0, 3, 5}, end[] = {8, 5, 6}, base = 1
Output: 9
Explanation:
First Subarray = {0, 8}. There are 9 different elements.
Each element can be kept at a different level.
So the height of the tower can be maximum 9 as base is 1.
Second Subarray = {3, 5}. There are 3 unique elements.
So it will have height 3 at maximum.
Third Subarray = {5, 6}. We can form a tower of max height 2.
The maximum height among all the sub-arrays = 9.
Input: N = 5, arr[] = {1, 3, 3, 7, 5}, M = 2, start[] = {0, 1}, end[] = {0, 3}, base = 2
Output: 1
Explanation:
First Subarray = {0, 0}. height = 0 for this sub-array,
because no tower of base 2 can be formed.
Second Sub-array = {1, 2}. Tower of height 1 can be formed by using
arr[1] and arr[2] at 1st level of tower.
The maximum height of the tower that can be obtained from all the above subarrays is 1.
Approach: To solve the problem follow the below idea:
Count the frequency of each distinct element for each sub-array using Hash-Map. Initialized current_height a variable to zero, Traverse on Hash-Map and if an element’s frequency is greater than or equal to base then increment the current_height. Print the maximum value of Height H among all the sub-arrays.
Follow the steps to solve the problem:
- Create a max_height variable and set it as max_height = 0. Then for each sub-array follow these sub-steps:
- Create a Hash-Map and a current_variable = 0.
- Count the frequency of each distinct element and store it in Hash-Map as pair of <element, frequency>.
- While traversing on Hash-Map, if the frequency of a pair in Hash-Map is greater than or equal to the base of Tower, then increment current_height, and if current_height is greater than max_height then update max_height as current_height.
- Print the value of the max_height variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Max_height( int n, int arr[], int m,
int start[], int end[], int base)
{
int max_height = 0;
for ( int i = 0; i < m; i++)
{
int current_height = 0;
int start_point = start[i];
int end_point = end[i];
unordered_map< int , int > map;
for ( int j = start_point; j <= end_point; j++)
{
map[arr[j]]++;
}
for ( auto it = map.begin(); it != map.end(); it++)
{
if (it->second >= base)
{
current_height++;
max_height = current_height > max_height
? current_height
: max_height;
}
}
}
return max_height;
}
int main()
{
int N = 9;
int arr[] = {1, 5, 7, 3, 2, 4, 6, 9, 8};
int M = 3;
int start[] = {0, 3, 5};
int end[] = {8, 5, 6};
int base = 1;
cout << (Max_height(N, arr, M, start, end, base));
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 9 ;
int arr[] = { 1 , 5 , 7 , 3 , 2 , 4 , 6 , 9 , 8 };
int M = 3 ;
int start[] = { 0 , 3 , 5 };
int end[] = { 8 , 5 , 6 };
int base = 1 ;
System.out.println(
Max_height(N, arr, M, start, end, base));
}
static int Max_height( int n, int [] arr, int m,
int [] start, int [] end, int base)
{
int max_height = 0 ;
for ( int i = 0 ; i < m; i++) {
int current_height = 0 ;
int start_point = start[i];
int end_point = end[i];
HashMap<Integer, Integer> map = new HashMap<>();
for ( int j = start_point; j <= end_point; j++) {
map.put(arr[j], map.get(arr[j]) == null
? 1
: map.get(arr[j]) + 1 );
}
for (Map.Entry<Integer, Integer> set :
map.entrySet()) {
if (set.getValue() >= base) {
current_height++;
max_height = current_height > max_height
? current_height
: max_height;
}
}
}
return max_height;
}
}
|
Python3
def Max_height(n, arr, m, start, end, base):
max_height = 0
for i in range (m):
current_height = 0
start_point = start[i]
end_point = end[i]
mp = {}
for j in range (start_point, end_point + 1 ):
if mp.get(arr[j]) = = None :
mp[arr[j]] = 1
else :
mp[arr[j]] + = 1
for first, second in mp.items():
if (second > = base):
current_height + = 1
max_height = current_height if (
current_height > max_height) else max_height
return max_height
if __name__ = = "__main__" :
N = 9
arr = [ 1 , 5 , 7 , 3 , 2 , 4 , 6 , 9 , 8 ]
M = 3
start = [ 0 , 3 , 5 ]
end = [ 8 , 5 , 6 ]
base = 1
print (Max_height(N, arr, M, start, end, base))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static public void Main()
{
int N = 9;
int [] arr = { 1, 5, 7, 3, 2, 4, 6, 9, 8 };
int M = 3;
int [] start = { 0, 3, 5 };
int [] end = { 8, 5, 6 };
int Base = 1;
Console.WriteLine(
Max_height(N, arr, M, start, end, Base));
}
static int Max_height( int n, int [] arr, int m,
int [] start, int [] end, int Base)
{
int max_height = 0;
for ( int i = 0; i < m; i++) {
int current_height = 0;
int start_point = start[i];
int end_point = end[i];
Dictionary< int , int > map
= new Dictionary< int , int >();
for ( int j = start_point; j <= end_point; j++) {
if (map.ContainsKey(arr[j])) {
map[arr[j]] += 1;
}
else {
map[arr[j]] = 1;
}
}
foreach (KeyValuePair< int , int > Set in map)
{
if (Set.Value >= Base) {
current_height++;
max_height = current_height > max_height
? current_height
: max_height;
}
}
}
return max_height;
}
}
|
Javascript
<script>
function Max_height( n, arr, m,
start, end, base)
{
let max_height = 0;
for (let i = 0; i < m; i++)
{
let current_height = 0;
let start_point = start[i];
let end_point = end[i];
let map = {};
for (let j = start_point; j <= end_point; j++)
{
if (map[arr[j]]==NaN || map[arr[j]]==undefined)map[arr[j]]=0;
map[arr[j]]++;
}
let count = 0;
for ( var c in map) {
count = count + 1;
}
for (let it = start_point; it <=count ; it++)
{
if (map[it] >= base)
{
current_height++;
max_height = current_height > max_height
? current_height
: max_height;
}
}
}
return max_height;
}
let N = 9;
let arr = [1, 5, 7, 3, 2, 4, 6, 9, 8];
let M = 3;
let start = [0, 3, 5];
let end = [8, 5, 6];
let base = 1;
console.log(Max_height(N, arr, M, start, end, base));
</script>
|
Time Complexity: O(M*N)
Auxiliary Space: O(N)
Last Updated :
24 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...