Pattern(24 question- 22 mcq ,2 coding quesitons)

1st question

WhatsApp Image 2024-08-13 at 22.04.41_bddfbafb.jpg

WhatsApp Image 2024-08-13 at 22.04.41_5995b4e0.jpg

WhatsApp Image 2024-08-13 at 22.04.42_83f07022.jpg

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

void solve(int cs) {
    vector<int> low = {0, 2}; // Lower bounds
    vector<int> high = {2, 4}; // Upper bounds
    vector<int> arr = {1, 2, 2, 3, 4}; // Input array
    int q = low.size(); // Number of queries
    vector<int> ans(q); // Vector to store results

    // Ensure the array is sorted
    sort(arr.begin(), arr.end());

    for (int i = 0; i < q; i++) {
        int l_i = lower_bound(arr.begin(), arr.end(), low[i]) - arr.begin(); // First index >= low[i]
        int l_j = upper_bound(arr.begin(), arr.end(), high[i]) - arr.begin(); // First index > high[i]
        
        // Calculate count of elements in range [low[i], high[i]]
        ans[i] = l_j - l_i;
    }

    // Output the results
    for (const auto& count : ans) {
        cout << count << " ";
    }
}

int main() {
    int cs = 1; // Example case number
    solve(cs); // Call the solve function
    return 0;
}

2nd question

WhatsApp Image 2024-08-13 at 22.04.42_2f67e529.jpg

WhatsApp Image 2024-08-13 at 22.04.42_7fc9b3f4.jpg

WhatsApp Image 2024-08-13 at 22.04.42_48bfd41f.jpg

WhatsApp Image 2024-08-13 at 22.04.43_1b8f219d.jpg

WhatsApp Image 2024-08-13 at 22.04.43_1e1ea994.jpg

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

// Function to check if a cell is within maze bounds and is not an obstacle
bool isValid(int x, int y, vector<vector<int>>& maze) {
    int n = maze.size();
    int m = maze[0].size();
    return (x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0);
}

// Function to find the minimum number of moves
int getMinimumMoves(vector<vector<int>>& maze, int k) {
    int n = maze.size();
    int m = maze[0].size();
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    queue<pair<int, int>> q;
    q.push({0, 0});
    visited[0][0] = true;
    int moves = 0;

    // Directions for all possible jumps
    vector<pair<int, int>> directions = {
        {1, 0}, {0, 1}, {-1, 0}, {0, -1} // down, right, up, left
    };

    // Perform BFS
    while (!q.empty()) {
        int size = q.size();
        for (int i = 0; i < size; ++i) {
            auto [x, y] = q.front();
            q.pop();

            // If we've reached the destination
            if (x == n - 1 && y == m - 1) {
                return moves;
            }

            // Explore all possible jumps
            for (auto [dx, dy] : directions) {
                for (int jump = 1; jump <= k; ++jump) {
                    int newX = x + dx * jump;
                    int newY = y + dy * jump;

                    // Check if within bounds and not visited
                    if (isValid(newX, newY, maze) && !visited[newX][newY]) {
                        // Ensure all cells between the current and target cell are free
                        bool pathClear = true;
                        for (int step = 1; step < jump; ++step) {
                            int checkX = x + dx * step;
                            int checkY = y + dy * step;
                            if (!isValid(checkX, checkY, maze)) {
                                pathClear = false;
                                break;
                            }
                        }
                        if (pathClear) {
                            q.push({newX, newY});
                            visited[newX][newY] = true;
                        }
                    }
                }
            }
        }
        moves++;
    }

    // If the destination is unreachable, return -1
    return -1;
}

int main() {
    int n, m;
    cout << "Enter the number of rows (n) and columns (m): ";
    cin >> n >> m;

    vector<vector<int>> maze(n, vector<int>(m));
    cout << "Enter the maze (0 for empty, 1 for obstacle):" << endl;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> maze[i][j];
        }
    }

    int k;
    cout << "Enter the jump parameter (k): ";
    cin >> k;

    int result = getMinimumMoves(maze, k);
    cout << "Minimum moves: " << result << endl;

    return 0;
}

Another slot

1st question

WhatsApp Image 2024-08-13 at 22.14.29_199ab988.jpg

WhatsApp Image 2024-08-13 at 22.14.50_d8dfa199.jpg

def count_level_up_players(n, k, scores):
    # Sort the scores in descending order to determine ranks
    sorted_scores = sorted(scores, reverse=True)
    
    # Initialize the rank and the counter for level-up players
    rank = 1
    level_up_count = 0
    
    for i in range(n):
        # Skip players with a score of 0
        if sorted_scores[i] == 0:
            continue
        
        # If the current player has a score that places them at a rank lower than k, stop counting
        if rank > k:
            break
        
        # Count this player as able to level up
        level_up_count += 1
        
        # If the next player's score is different, update the rank
        if i < n - 1 and sorted_scores[i] != sorted_scores[i + 1]:
            rank = i + 2
    
    return level_up_count

# Example usage:
n = 4
k = 3
scores = [100, 50, 50, 25]
print(count_level_up_players(n, k, scores))  # Output: 3

2nd question

WhatsApp Image 2024-08-13 at 22.14.50_d8dfa199.jpg

WhatsApp Image 2024-08-13 at 22.39.07_6c377466.jpg

WhatsApp Image 2024-08-13 at 22.39.07_1e6468d1.jpg

const int mod = 1e9+7;
int countDistinctColorings(vector<string> domino) {
    string a = domino[0], b = domino[1];
    int res = 1, cur  = -1;
    int n = a.size();
    for(int i=0; i<n; i++)
    {
        if(a[i] == b[i])
        {
            if(cur == -1)
            {
                res *= 3;
                res %= mod;
            }
            else if(cur == 0)
            {
                res *= 2;
                res %= mod;
            }
            cur = 0;
        }
        else
        {
            if(cur == -1)
            {
                res *= 6;
                res %= mod;
            }
            else if(cur == 0)
            {
                res *= 2;
                res %= mod;
            }
            else
            {
                res *= 3;
                res %= mod;
            }
            cur = 1;
            i++;
        }
    }
    return res;
}