The chocolate rooms

Your task is to take some chocolates from N different rooms. Each room contains P[i] number of chocolates of the same or different brands. You are given an integer K.

Write a program to determine whether you can visit each room and find K different brands of chocolates.

Example:

Input:  n=3, k=2, arr=[1,2,1], s={{"KITKAT"},{"FIVESTAR","KITKAT"},{"KITKAT"}}
Output: Yes

Approach

Java

import java.util.HashSet;

public class ChocolateRooms {
    public static void main(String[] args) {

        int n = 3, k = 2;

        int[] arr = { 121 };

        String[][] s = { { "KITKAT" }, { "FIVESTAR""KITKAT" },
 { "KITKAT" } };

        System.out.println(chocolateRooms(arr, s, n, k));

    }

    static String chocolateRooms(int[] arrString[][] s,
 int nint k)

    {
        HashSet<Stringst = new HashSet<String>();
        for (int i = 0; i < n; i++) {
            int p = arr[i];

            for (int j = 0; j < p; j++) {

                st.add(s[i][j]);
            }
        }
        if (st.size() >= k)
            return "Yes";
        else
            return "No";
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

string chocolateRooms(vector<int&arr
vector<vector<string>> &s,
                      int nint k)

{
    set<stringst;
    for (int i = 0i < ni++)
    {
        int p = arr[i];

        for (int j = 0j < pj++)
        {

            st.insert(s[i][j]);
        }
    }
    if (st.size() >= k)
        return "Yes";
    else
        return "No";
}
int main()
{
    int n = 3k = 2;

    vector<intarr = {121};

    vector<vector<string>> s = {{"KITKAT"},
                                {"FIVESTAR""KITKAT"},
                                {"KITKAT"}};

    cout << chocolateRooms(arrsnk<< "\n";

    return 0;
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


No comments:

Post a Comment