Question: Nvidia, Recent Online Assessment Question (6M Internship ; Pool Hiring) | Quarry Worker| Binary Sting regex | 2023
1
Entering edit mode

ADD COMMENTlink 15 months ago Delta 2.9k
1
Entering edit mode

// 1st Question Answer

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int lastStoneWeight(vector<int>& weights) {
    priority_queue<int> max_heap;

    for (int weight : weights) {
        max_heap.push(weight);
    }

    // Simulate stone smashing until there's only one stone left or all stones are destroyed
    while (max_heap.size() > 1) {
        int first = max_heap.top();
        max_heap.pop();
        int second = max_heap.top();
        max_heap.pop();

        // Calculate the result of smashing and push it back into the heap
        int smash_result = abs(first - second);
        if (smash_result > 0) {
            max_heap.push(smash_result);
        }
    }

    // Check if there's a stone left and return its weight, or return 0 if all stones shattered
    if (max_heap.empty()) {
        return 0;
    } else {
        return max_heap.top();
    }
}

int main() {
    vector<int> weights = {2,4,5};
    int result = lastStoneWeight(weights);
    cout << "The weight of the last stone: " << result << endl;
    return 0;
}
 

ADD COMMENTlink 15 months ago Nikhil Meshram • 10
0
Entering edit mode

// 2nd Question

#include <iostream>
#include <regex>
#include <string>

int main() {
    int n;
    std::cin >> n;
    std::cin.ignore();  // Consume the newline character after reading n

    std::regex pattern("^0*10*$");

    for (int i = 0; i < n; ++i) {
        std::string binary_str;
        std::getline(std::cin, binary_str);

        bool is_power_of_two = std::regex_match(binary_str, pattern);
        std::cout << (is_power_of_two ? "True" : "False") << std::endl;
    }

    return 0;
}
 

ADD COMMENTlink 15 months ago Nikhil Meshram • 10
0
Entering edit mode

#include <iostream>

#include <regex>

#include <string>

 

int main()

{

    int n;

    std::cin >> n;

    std::cin.ignore(); // Consume the newline character after reading n

 

    // Define the regex pattern for a binary number that is a power of two

    std::regex pattern("^0*10*$");

 

    for (int i = 0; i < n; ++i)

    {

        std::string binary_str;

        std::getline(std::cin, binary_str);

 

        // Use std::regex_match to check if the input matches the pattern

        bool is_power_of_two = std::regex_match(binary_str, pattern);

 

        std::cout << (is_power_of_two ? "True" : "False") << std::endl;

    }

 

    return 0;

}

 

ADD COMMENTlink 15 months ago SARTHAK NARANG • 40
0
Entering edit mode

#include <bits/stdc++.h>

using namespace std;

int weightOfLastStone(int arr[], int n)

{

    priority_queue<int> pq;

    for (int i = 0; i < n; i++)

        pq.push(arr[i]);

    int second;

    while (!pq.empty())

    {

        int first = pq.top();

        pq.pop();

        if (!pq.empty())

        {

            second = pq.top();

            pq.pop();

        }

        else

        {

            return first;

        }

        if (first != second)

            pq.push(first - second);

    }

    return 0;

}

int main()

{

    int n;

    cout << "Enter the number of stones: ";

    cin >> n;

    int stones[n];

    cout << "Enter the weights of the stones: ";

    for (int i = 0; i < n; i++)

    {

        cin >> stones[i];

    }

    int result = weightOfLastStone(stones, n);

    cout << "The weight of the last stone is: " << result << endl;

    return 0;

}

ADD COMMENTlink 15 months ago SARTHAK NARANG • 40

Login before adding your answer.

Similar Posts
Loading Similar Posts