Return the distinct visible curtains after placing all curtains on the wall
Given two arrays X[] and Y[] of length N(N >= 1), where X[] contains the height till which curtains are hung on the wall and Y[]
(1 <= Y[i] <= M) contains the integers denoting the color of curtains for all (1 <= i <= N). Then your task is to return the distinctly visible curtains after placing all curtains on the wall.
Note: It should be noted that one curtain having height H1 hides before the other curtain having height H2 if H1<H2. This point should be remembered while solving the problem.
Examples:
Input: N = 4, M = 3, X[] = { 5, 4, 3, 2 }, Y[] = { 1, 2, 3, 1 }
Output: 3
Explanation:
Graphical Explanation of input test case 1
There are three distinct colored curtains in Y[], which are 1, 2 and 3. Let us assign some colors to these curtains
Red = 1
Orange = 2
Green = 3
Then the process done as:
- X[1] = 5 and Y[1] = 1, So the Red color curtain is hanged till height 5 on the wall.
- X[2] = 4 and Y[2] = 2, So the Orange colored curtain is hanged till height 4.
- X[3] = 3 and Y[3] = 3, So the Green colored curtain is hanged till height 3.
- X[4] = 2 and Y[4] = 1, Again, another Red colored curtain is hanged till height 2.
Now there are three colored curtains are visible. Therefore, output is 3.
Input: N = 4, M = 5, X[] = { 5, 4, 6, 3 }, Y[] = { 1, 2, 4, 5 }
Output: 2
Explanation: It can be verified that only 2 distinct colored curtains will be visible.
Approach: Implement the idea below to solve the problem
The problem is observation based and can be solved by using those observations by code.
Steps were taken to solve the problem:
- Initialize an array F[] of size M+1.
- std::fill_n (F, M+1, 0)
- Create and initialize ans variable to zero.
- Create and initialize temp variable to X[N-1].
- Initialize F[ Y[N – 1] ] = 1.
- Run a loop for i = N-2 to i >= 0 and follow the below-mentioned steps under the scope of loop:
- If (X[i] > temp)
- temp = X[i]
- If( F[ Y[i] ] == 0 ), Then ans++ and F[ Y[i] ] = 1.
- Output the value of ans.
Code to implement the approach:
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find distinct curtains
void distinctCurtains(int X[], int Y[], int n, int m)
{
// Created a temprorary array
int f[m + 1];
// Filling the temprory array with
// zeros initially
std::fill_n(f, m + 1, 0);
// Initialized the ans variable with 1
int ans = 1;
// Initializing values
int temp = X[n - 1];
f[Y[n - 1]] = 1;
// Loop for traversing over X[]
for (int i = n - 2; i >= 0; i--) {
// Checking the conditions and
// incrementing ans variable
if (X[i] > temp) {
temp = X[i];
if (f[Y[i]] == 0) {
ans++;
f[Y[i]] = 1;
}
}
}
// Printing the output
cout << ans << endl;
}
// Drivers code
int main()
{
// Inputs
int n = 5, m = 5;
int X[n] = { 5, 4, 3, 2, 3 };
int Y[n] = { 1, 2, 3, 4, 5 };
// Function call
distinctCurtains(X, Y, n, m);
return 0;
}
Java
//Java code to implement the approach
class GFG
{
//Driver Function
public static void main(String[] args)
{
int N = 5, M = 5;
int X[] = {5, 4, 3, 2, 3};
int Y[] = {1, 2, 3, 4, 5};
//Function call
distinctCurtains(X, Y, N, M);
}
//Method to return distinct curtains
static void distinctCurtains(int[] X, int[] Y, int N, int M)
{
//Created a temproray array
int[] f = new int[M+1];
//filling the temprorary array with zeros initially
for(int i=0;i<f.length;i++)
f[i] = 0;
//Initialized the ans variable with 1
int ans = 1;
//Initialized values
int temp = X[N-1];
f[Y[N - 1]] = 1;
//Loop for traversing over X[]
for(int i = N-2;i>=0;i--)
{
//Checking the conditions and incrementing
//ans variable
if(X[i]>temp)
{
temp = X[i];
if(f[Y[i]]==0)
{
ans++;
f[Y[i]]=1;
}
}
}
//Printing output
System.out.println(ans);
}
}
Python3
# Function to find distinct curtains
def distinctCurtains(X, Y, n, m):
# Created a temprorary array
f = [0] * (m+1)
# Initialized the ans variable with 1
ans = 1
# Initializing values
temp = X[n-1]
f[Y[n-1]] = 1
# Loop for traversing over X[]
for i in range(n-2, -1, -1):
# Checking the conditions and
# incrementing ans variable
if X[i] > temp:
temp = X[i]
if f[Y[i]] == 0:
ans += 1
f[Y[i]] = 1
# Printing the output
print(ans)
# Drivers code
if __name__ == '__main__':
# Inputs
n, m = 5, 5
X = [5, 4, 3, 2, 3]
Y = [1, 2, 3, 4, 5]
# Function call
distinctCurtains(X, Y, n, m)
C#
//C# code to implement the approach
using System;
public class GFG{
//Driver Function
static public void Main (){
int N = 5, M = 5;
int[] X = {5, 4, 3, 2, 3};
int[] Y = {1, 2, 3, 4, 5};
//Function call
distinctCurtains(X, Y, N, M);
}
//Method to return distinct curtains
static void distinctCurtains(int[] X, int[] Y, int N, int M)
{
//Created a temproray array
int[] f = new int[M+1];
//filling the temprorary array with zeros initially
for(int i=0;i<f.Length;i++)
f[i] = 0;
//Initialized the ans variable with 1
int ans = 1;
//Initialized values
int temp = X[N-1];
f[Y[N - 1]] = 1;
//Loop for traversing over X[]
for(int i = N-2;i>=0;i--)
{
//Checking the conditions and incrementing
//ans variable
if(X[i]>temp)
{
temp = X[i];
if(f[Y[i]]==0)
{
ans++;
f[Y[i]]=1;
}
}
}
//Printing output
Console.WriteLine(ans);
}
}
Javascript
function distinctCurtains(X, Y, n, m)
{
// Created a temprorary array
let f = new Array(m + 1).fill(0);
// Initialized the ans variable with 1
let ans = 1;
// Initializing values
let temp = X[n - 1];
f[Y[n - 1]] = 1;
// Loop for traversing over X[]
for (let i = n - 2; i >= 0; i--) {
// Checking the conditions and incrementing ans variable
if (X[i] > temp) {
temp = X[i];
if (f[Y[i]] == 0) {
ans++;
f[Y[i]] = 1;
}
}
}
// Printing the output
console.log(ans);
}
// Drivers code
let n = 5, m = 5;
let X = [5, 4, 3, 2, 3];
let Y = [1, 2, 3, 4, 5];
// Function call
distinctCurtains(X, Y, n, m);
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
21 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...