๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.52K subscribers
5.56K photos
3 videos
95 files
9.7K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
def wolve(elements):
    n = len(elements)
    result = []
    level = 0
    idx = 0  
    while idx < n:
        nodes_in_level = 2 ** level
        if idx + nodes_in_level <= n:
            result.append(elements[idx + nodes_in_level - 1]) 
            idx += nodes_in_level
        else:
            break
        level += 1
    print(" ".join(map(str, result)))

Bnp โœ…
def first_meat_orders(num_of_orders, orders, size):
    result = []
   
    for i in range(0, num_of_orders-size+1):
        screen_orders = orders[i:i+size]
        meat_order_found = False
       
        for order in screen_orders:
            if order < 0:  # Checking for meat-based pizza order
                result.append(order)
                meat_order_found = True
                break

       
        if not meat_order_found:
            result.append(0)
        meat_order_found = False
   
    return result
DESERT QUEEN CODE
C++


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

int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

bool isValid(int x, int y, int n, const vector<vector<char>>& grid)
{
    return x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'M';
}

int fMW(int n, const vector<vector<char>>& grid, pair<int, int> start, pair<int, int> end)
{
    vector<vector<int>> EXAMASSISTANCE(n, vector<int>(n, INT_MAX));
    queue<pair<int, int>> q;

    q.push(start);
    EXAMASSISTANCE[start.first][start.second] = 0;

    while (!q.empty())
    {
        auto [x, y] = q.front();
        q.pop();

        for (int d = 0; d < 4; d++)
        {
            int nx = x + dir[d][0];
            int ny = y + dir[d][1];

            if (isValid(nx, ny, n, grid))
            {
                int cost = (grid[x][y] == 'T' && grid[nx][ny] == 'T') ? 0 : 1;
                if (EXAMASSISTANCE[x][y] + cost < EXAMASSISTANCE[nx][ny])
                {
                    EXAMASSISTANCE[nx][ny] = EXAMASSISTANCE[x][y] + cost;
                    q.push({nx, ny});
                }
            }
        }
    }
    return EXAMASSISTANCE[end.first][end.second];
}

int main()
{
    int n;
    cin >> n;
    vector<vector<char>> grid(n, vector<char>(n));
    pair<int, int> start, end;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> grid[i][j];
            if (grid[i][j] == 'S') start = {i, j};
            if (grid[i][j] == 'E') end = {i, j};
        }
    }

    int result = fMW(n, grid, start, end);
    cout << result << endl;

    return 0;
}


DESERT QUEEN
C++
โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
LOOP MASTER

JAVA


import java.util.*;

public class loopMaster {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        List<String> EXAMASSISTANCE = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            EXAMASSISTANCE.add(sc.nextLine().trim());
        }
        processCommands(EXAMASSISTANCE);
    }

    private static void processCommands(List<String> EXAMASSISTANCE) {
        Stack<Integer> loopCounts = new Stack<>();
        Stack<Integer> currIter = new Stack<>();
        StringBuilder output = new StringBuilder();
        int index = 0;

        while (index < EXAMASSISTANCE.size()) {
            String command = EXAMASSISTANCE.get(index);
            if (command.startsWith("for")) {
                int times = Integer.parseInt(command.split(" ")[1]);
                loopCounts.push(times);
                currIter.push(0);
            } else if (command.equals("do")) {
            } else if (command.equals("done")) {
                int current = currIter.pop() + 1;
                int max = loopCounts.pop();
                if (current < max) {
                    loopCounts.push(max);
                    currIter.push(current);
                    index = findLoopStart(EXAMASSISTANCE, index);
                    continue;
                }
            } else if (command.startsWith("break")) {
                int breakAt = Integer.parseInt(command.split(" ")[1]);
                if (currIter.peek() + 1 == breakAt) {
                    loopCounts.pop();
                    currIter.pop();
                    index = findLoopEnd(EXAMASSISTANCE, index);
                }
            } else if (command.startsWith("continue")) {
                int continueAt = Integer.parseInt(command.split(" ")[1]);
                if (currIter.peek() + 1 == continueAt) {
                    int max = loopCounts.peek();
                    int current = currIter.pop() + 1;
                    if (current < max) {
                        currIter.push(current);
                        index = findLoopStart(EXAMASSISTANCE, index);
                    }
                    continue;
                }
            } else if (command.startsWith("print")) {
                String message = command.substring(command.indexOf("\"") + 1, command.lastIndexOf("\""));
                output.append(message).append("\n");
            }
            index++;
        }
        System.out.print(output.toString());
    }

    private static int findLoopStart(List<String> EXAMASSISTANCE, int ci) {
        int nestedLoops = 0;
        for (int i = ci - 1; i >= 0; i--) {
            if (EXAMASSISTANCE.get(i).equals("done")) {
                nestedLoops++;
            } else if (EXAMASSISTANCE.get(i).equals("do")) {
                if (nestedLoops == 0) {
                    return i;
                }
                nestedLoops--;
            }
        }
        return 0;
    }

    private static int findLoopEnd(List<String> EXAMASSISTANCE, int ci) {
        int nestedLoops = 0;
        for (int i = ci + 1; i < EXAMASSISTANCE.size(); i++) {
            if (EXAMASSISTANCE.get(i).equals("do")) {
                nestedLoops++;
            } else if (EXAMASSISTANCE.get(i).equals("done")) {
                if (nestedLoops == 0) {
                    return i;
                }
                nestedLoops--;
            }
        }
        return EXAMASSISTANCE.size();
    }
}


LOOP MASTER CODE
JAVA
โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
TOGGLE CHALLENGE CODE
JAVA

import java.util.*;

public class Toggle{

    public static List<Integer> getPossibleDigits(Map<Integer, String> validDigits, String faultyDigit) {
        List<Integer> potentialDigits = new ArrayList<>();
       
        for (Map.Entry<Integer, String> entry : validDigits.entrySet()) {
            int mismatches = 0;
            String pattern = entry.getValue();
           
            for (int i = 0; i < faultyDigit.length(); i++) {
                if (faultyDigit.charAt(i) != pattern.charAt(i)) {
                    mismatches++;
                }
            }
           
            if (mismatches <= 1) {
                potentialDigits.add(entry.getKey());
            }
        }
       
        return potentialDigits;
    }

    public static void solve() {
        Scanner scanner = new Scanner(System.in);
       
        List<String> segmentDisplay = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            segmentDisplay.add(scanner.nextLine().strip());
        }
       
        List<String> EXAMASSISTANCE = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            EXAMASSISTANCE.add(scanner.nextLine().strip());
        }

        Map<Integer, String> digitPatterns = new HashMap<>();
       
        for (int digit = 0; digit < 10; digit++) {
            StringBuilder pattern = new StringBuilder();
            for (int row = 0; row < 3; row++) {
                pattern.append(segmentDisplay.get(row).substring(digit * 3, (digit + 1) * 3));
            }
            digitPatterns.put(digit, pattern.toString());
        }

        List<List<Integer>> possibleNumbers = new ArrayList<>();
       
        for (int i = 0; i < EXAMASSISTANCE.get(0).length() / 3; i++) {
            StringBuilder faultyDigit = new StringBuilder();
            for (int row = 0; row < 3; row++) {
                faultyDigit.append(EXAMASSISTANCE.get(row).substring(i * 3, (i + 1) * 3));
            }
           
            List<Integer> matchingDigits = getPossibleDigits(digitPatterns, faultyDigit.toString());
           
            if (matchingDigits.isEmpty()) {
                System.out.print("Invalid");
                return;
            }
            possibleNumbers.add(matchingDigits);
        }

        int totalSum = 0;
        for (List<Integer> combination : getCombinations(possibleNumbers)) {
            StringBuilder number = new StringBuilder();
            for (int digit : combination) {
                number.append(digit);
            }
            totalSum += Integer.parseInt(number.toString());
        }

        System.out.print(totalSum);
    }

    public static List<List<Integer>> getCombinations(List<List<Integer>> possibleNumbers) {
        List<List<Integer>> result = new ArrayList<>();
        getCombinationsHelper(possibleNumbers, 0, new ArrayList<>(), result);
        return result;
    }

    private static void getCombinationsHelper(List<List<Integer>> possibleNumbers, int index, List<Integer> currentCombination, List<List<Integer>> result) {
        if (index == possibleNumbers.size()) {
            result.add(new ArrayList<>(currentCombination));
            return;
        }

        for (int digit : possibleNumbers.get(index)) {
            currentCombination.add(digit);
            getCombinationsHelper(possibleNumbers, index + 1, currentCombination, result);
            currentCombination.remove(currentCombination.size() - 1);
        }
    }

    public static void main(String[] args) {
        solve();
    }
}


TOGGLE CHALLENGE CODE IN
JAVA

โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
Plague 2050
PYTHON


from collections import deque

def infected_neighbors_count(grid, a, b):
    n = len(grid)
    count = 0
    directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for i in range(len(directions)):
        dx, dy = directions[i]
        nx, ny = a + dx, b + dy
        if 0 <= nx < n and 0 <= ny < n and grid[nx][ny] == 1:
            count += 1
    return count

def infection_process(grid):
    n = len(grid)
    new_grid = [[grid[i][j] for j in range(n)] for i in range(n)]
   
    for i in range(n):
        for j in range(n):
            neighbors = infected_neighbors_count(grid, i, j)
            if grid[i][j] == 0 and neighbors == 3:
                new_grid[i][j] = 1
            elif grid[i][j] == 1 and (neighbors < 2 or neighbors > 3):
                new_grid[i][j] = 0
               
    return new_grid

def find_path(size, initial_grid):
    grid = [[1 if cell == '1' else 0 for cell in row] for row in initial_grid]
    start_x, start_y, end_x, end_y = -1, -1, -1, -1
   
    def set_positions():
        nonlocal start_x, start_y, end_x, end_y
        for i in range(size):
            for j in range(size):
                if initial_grid[i][j] == 's':
                    start_x, start_y = i, j
                    grid[i][j] = 0
                if initial_grid[i][j] == 'd':
                    end_x, end_y = i, j
                    grid[i][j] = 0
   
    set_positions()
   
    queue = deque([(start_x, start_y, grid, 0)])
    seen_states = set()
   
    while queue:
        x, y, current_grid, days = queue.popleft()
       
        if (x, y) == (end_x, end_y):
            return days
       
        state = (x, y, tuple(map(tuple, current_grid)))
        if state in seen_states:
            continue
        seen_states.add(state)
       
        next_grid = infection_process(current_grid)
       
        moves = [(0, 0), (-1, 0), (1, 0), (0, -1), (0, 1)]
        for dx, dy in moves:
            nx, ny = x + dx, y + dy
            if 0 <= nx < size and 0 <= ny < size and next_grid[nx][ny] == 0:
                queue.append((nx, ny, next_grid, days + 1))
   
    return -1

if name == "main":
    n = int(input())
    pollution_map = [input().strip() for _ in range(n)]
    print(find_path(n, pollution_map) + 1)


PLAGUE  2050
Python

โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
MAXIMUM ROTATION
PYTHON


def rl(layer, pos, dir, ol):
    n = len(layer)
    rot = [None] * n

    if dir == "clockwise":
        for i in range(n):
            rot[(i + pos) % n] = layer[i]
    else:
        for i in range(n):
            rot[(i - pos) % n] = layer[i]

    for i in range(n):
        if ol:
            rot[i] = chr(((ord(rot[i]) - ord('A') - 1) % 26) + ord('A'))
        else:
            rot[i] = chr(((ord(rot[i]) - ord('A') + 1) % 26) + ord('A'))

    return rot


def aq(pl, row, col, size):
    layers = []
    for layer in range(size // 2):
        cl = []
        for j in range(col + layer, col + size - layer):
            cl.append(pl[row + layer][j])
        for i in range(row + layer + 1, row + size - layer - 1):
            cl.append(pl[i][col + size - layer - 1])
        for j in range(col + size - layer - 1, col + layer - 1, -1):
            cl.append(pl[row + size - layer - 1][j])
        for i in range(row + size - layer - 2, row + layer, -1):
            cl.append(pl[i][col + layer])
        layers.append(cl)

    for lidx, layer in enumerate(layers):
        ol = (lidx + 1) % 2 == 1
        dir = "counterclockwise" if ol else "clockwise"
        pos = lidx + 1
        rotated_layer = rl(layer, pos, dir, ol)

        idx = 0
        for j in range(col + lidx, col + size - lidx):
            pl[row + lidx][j] = rotated_layer[idx]
            idx += 1
        for i in range(row + lidx + 1, row + size - lidx - 1):
            pl[i][col + size - lidx - 1] = rotated_layer[idx]
            idx += 1
        for j in range(col + size - lidx - 1, col + lidx - 1, -1):
            pl[row + size - lidx - 1][j] = rotated_layer[idx]
            idx += 1
        for i in range(row + size - lidx - 2, row + lidx, -1):
            pl[i][col + lidx] = rotated_layer[idx]
            idx += 1

def MAX_ROTATION(n, pl, queries):
    for row, col, size in queries:
        aq(pl, row, col, size)
    result = ''.join(''.join(row) for row in pl)
    return result


n = int(input())
pl = [list(input().strip().split()) for _ in range(n)]
q = int(input().strip())
queries = [tuple(map(int, input().strip().split())) for _ in range(q)]

result = MAX_ROTATION(n, pl, queries)
print(result, end="")

MAXIMUM ROTATION
PYTHON

โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
OFFICE ROSTERING
C++


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

int main()
{
    int n, m, k, d = 1, EXAMASSISTANCE = 0;
    cin >> n >> m;
    vector<set<int>> f(n);
    for (int i = 0, u, v; i < m; ++i)
    {
        cin >> u >> v;
        f[u].insert(v);
        f[v].insert(u);
    }

    cin >> k;
    vector<bool> w(n, true);
    EXAMASSISTANCE = n;
    while (EXAMASSISTANCE < k)
    {
        vector<bool> nw(n, false);
        for (int i = 0; i < n; ++i)
        {
            int cnt = 0;
            for (int fr : f[i]) cnt += w[fr];
            if (w[i] && cnt == 3) nw[i] = true;
            else if (!w[i] && cnt < 3) nw[i] = true;
        }

        w = nw;
        EXAMASSISTANCE += count(w.begin(), w.end(), true);
        ++d;
    }
    cout << d;
    return 0;
}


OFFICE ROSTERING
C++
โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
Equation Puzzle Code
C++
TCS CodeVita Season 12 Round 1 Zone 2


int main()
{
    string e1, e2;
    int r;
    cin >> e1 >> e2 >> r;
    int a1, b1, c1, d1, n1, a2, b2, c2, d2, n2;
    sscanf(e1.c_str(), "%dx+%dy+%dz+%dw<=%d", &a1, &b1, &c1, &d1, &n1);
    sscanf(e2.c_str(), "%dx+%dy+%dz+%dw<=%d", &a2, &b2, &c2, &d2, &n2);

    vector<vector<int>> combinations;
    for (int x = 0; x <= r; x++)
    {
        for (int y = 0; y + x <= r; y++)
        {
            for (int z = 0; z + x + y <= r; z++)
            {
                int w = r - x - y - z;
                combinations.push_back({x, y, z, w});
            }
        }
    }

    set<int> results;
    for (auto &vars1 : combinations)
    {
        int v1 = a1 * vars1[0] + b1 * vars1[1] + c1 * vars1[2] + d1 * vars1[3];
        if (v1 > n1) continue;

        for (auto &vars2 : combinations)
        {
            int v2 = a2 * vars2[0] + b2 * vars2[1] + c2 * vars2[2] + d2 * vars2[3];
            if (v2 > n2) continue;
            if (v1 == v2) results.insert(v1);
        }
    }

    cout << results.size() << endl;
    return 0;
}



Equation Puzzle Code
C++

โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ โ˜‘๏ธ
def rotate_layer(layer, positions, direction, odd_layer):
    n = len(layer)
    rotated = [None] * n

    if direction == "clockwise":
        for i in range(n):
            rotated[(i + positions) % n] = layer[i]
    else:  # counterclockwise
        for i in range(n):
            rotated[(i - positions) % n] = layer[i]

    for i in range(n):
        if odd_layer:
            rotated[i] = chr(((ord(rotated[i]) - ord('A') - 1) % 26) + ord('A'))
        else:
            rotated[i] = chr(((ord(rotated[i]) - ord('A') + 1) % 26) + ord('A'))

    return rotated


def apply_query(matrix, row, col, size):
    layers = []
    for layer in range(size // 2):
        current_layer = []
        for j in range(col + layer, col + size - layer):
            current_layer.append(matrix[row + layer][j])
        for i in range(row + layer + 1, row + size - layer - 1):
            current_layer.append(matrix[i][col + size - layer - 1])
        for j in range(col + size - layer - 1, col + layer - 1, -1):
            current_layer.append(matrix[row + size - layer - 1][j])
        for i in range(row + size - layer - 2, row + layer, -1):
            current_layer.append(matrix[i][col + layer])

        layers.append(current_layer)

    for layer_idx, layer in enumerate(layers):
        odd_layer = (layer_idx + 1) % 2 == 1
        direction = "counterclockwise" if odd_layer else "clockwise"
        positions = layer_idx + 1
        rotated_layer = rotate_layer(layer, positions, direction, odd_layer)

        idx = 0
        for j in range(col + layer_idx, col + size - layer_idx):
            matrix[row + layer_idx][j] = rotated_layer[idx]
            idx += 1
        for i in range(row + layer_idx + 1, row + size - layer_idx - 1):
            matrix[i][col + size - layer_idx - 1] = rotated_layer[idx]
            idx += 1
        for j in range(col + size - layer_idx - 1, col + layer_idx - 1, -1):
            matrix[row + size - layer_idx - 1][j] = rotated_layer[idx]
            idx += 1
        for i in range(row + size - layer_idx - 2, row + layer_idx, -1):
            matrix[i][col + layer_idx] = rotated_layer[idx]
            idx += 1


def process_matrix(n, matrix, queries):
    for row, col, size in queries:
        apply_query(matrix, row, col, size)
    result = ''.join(''.join(row) for row in matrix)
    return result


n = int(input())
matrix = [list(input().strip().split()) for _ in range(n)]
q = int(input().strip())
queries = [tuple(map(int, input().strip().split())) for _ in range(q)]

result = process_matrix(n, matrix, queries)
print(result, end="")

Matrix Rotation
๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
Photo
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int M;
    cin >> M;
    vector<vector<int>> matrix(M, vector<int>(M));
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < M; ++j) {
            cin >> matrix[i][j];
        }
    }
    for (int i = 0; i < M; ++i) {
        if (i % 2 == 0) {
            sort(matrix[i].begin(), matrix[i].end());
        } else {
            sort(matrix[i].begin(), matrix[i].end(), greater<int>());
        }
    }

    for (int j = 0; j < M; ++j) {
        vector<int> column(M);
        for (int i = 0; i < M; ++i) {
            column[i] = matrix[i][j];
        }

        if (j % 2 == 0) {
            sort(column.begin(), column.end());
        } else {
            sort(column.begin(), column.end(), greater<int>());
        }

        for (int i = 0; i < M; ++i) {
            matrix[i][j] = column[i];
        }
    }
    for (int i = M - 1; i >= 0; --i) {
        for (int j = 0; j < M; ++j) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

BNP โœ