Cart - TCS Codevita
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
Toggle Challenge - TCS Codevita
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
Office Rostering - TCS Codevita
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
Count Press - TCS Codevita
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
14 segments - TCS Codevita
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
Shannon Circuit
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
Dessert Queen - Codevita
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
Equal puzzle
Telegram - t.me/codingsolution_IT
Telegram - t.me/codingsolution_IT
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
vector<vector<int>> z(x, vector<int>(y));
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
cin >> z[i][j];
}
}
int w;
cin >> w;
map<int, vector<pair<int, int>>> a;
set<int> b;
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
int c = z[i][j];
a[c].emplace_back(i, j);
b.insert(c);
}
}
set<int> d;
for (int i = 0; i < x; ++i) {
vector<int> e;
for (int j = 0; j < y; ++j) {
if (z[i][j] == w) {
e.push_back(j);
}
}
if (!e.empty()) {
int f = *max_element(e.begin(), e.end());
for (int j = f + 1; j < y; ++j) {
int g = z[i][j];
if (g != w) {
d.insert(g);
}
}
}
}
set<int> h = b;
d.erase(w);
int i = 0;
for (auto j = d.begin(); j != d.end(); ++j) {
if (h.find(*j) != h.end()) {
h.erase(*j);
i++;
}
}
auto k = [&](const set<int>& l) -> set<int> {
set<int> m;
for (auto& n : l) {
for (auto& [o, p] : a[n]) {
if (o == x - 1) {
m.insert(n);
break;
}
}
}
queue<int> q;
for (auto& r : m) {
q.push(r);
}
while (!q.empty()) {
int s = q.front();
q.pop();
for (auto& t : l) {
if (m.find(t) != m.end()) continue;
bool u = false;
for (auto& [v, w] : a[t]) {
if (v + 1 < x) {
int x = z[v + 1][w];
if (m.find(x) != m.end()) {
u = true;
break;
}
}
}
if (u) {
m.insert(t);
q.push(t);
}
}
}
return m;
};
while (true) {
set<int> y = k(h);
set<int> z;
for (auto& aa : h) {
if (y.find(aa) == y.end()) {
z.insert(aa);
}
}
if (z.empty()) break;
for (auto& bb : z) {
h.erase(bb);
i++;
}
}
cout << i;
return 0;
}
Block Extraction - Codevita
Telegram - t.me/codingsolution_IT
using namespace std;
int main() {
int x, y;
cin >> x >> y;
vector<vector<int>> z(x, vector<int>(y));
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
cin >> z[i][j];
}
}
int w;
cin >> w;
map<int, vector<pair<int, int>>> a;
set<int> b;
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
int c = z[i][j];
a[c].emplace_back(i, j);
b.insert(c);
}
}
set<int> d;
for (int i = 0; i < x; ++i) {
vector<int> e;
for (int j = 0; j < y; ++j) {
if (z[i][j] == w) {
e.push_back(j);
}
}
if (!e.empty()) {
int f = *max_element(e.begin(), e.end());
for (int j = f + 1; j < y; ++j) {
int g = z[i][j];
if (g != w) {
d.insert(g);
}
}
}
}
set<int> h = b;
d.erase(w);
int i = 0;
for (auto j = d.begin(); j != d.end(); ++j) {
if (h.find(*j) != h.end()) {
h.erase(*j);
i++;
}
}
auto k = [&](const set<int>& l) -> set<int> {
set<int> m;
for (auto& n : l) {
for (auto& [o, p] : a[n]) {
if (o == x - 1) {
m.insert(n);
break;
}
}
}
queue<int> q;
for (auto& r : m) {
q.push(r);
}
while (!q.empty()) {
int s = q.front();
q.pop();
for (auto& t : l) {
if (m.find(t) != m.end()) continue;
bool u = false;
for (auto& [v, w] : a[t]) {
if (v + 1 < x) {
int x = z[v + 1][w];
if (m.find(x) != m.end()) {
u = true;
break;
}
}
}
if (u) {
m.insert(t);
q.push(t);
}
}
}
return m;
};
while (true) {
set<int> y = k(h);
set<int> z;
for (auto& aa : h) {
if (y.find(aa) == y.end()) {
z.insert(aa);
}
}
if (z.empty()) break;
for (auto& bb : z) {
h.erase(bb);
i++;
}
}
cout << i;
return 0;
}
Block Extraction - Codevita
Telegram - t.me/codingsolution_IT
import java.util.*;
public class LoopMaster {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfCommands = Integer.parseInt(scanner.nextLine());
List<String> commands = new ArrayList<>();
for (int i = 0; i < numberOfCommands; i++) {
commands.add(scanner.nextLine().trim());
}
processCommands(commands);
}
private static void processCommands(List<String> commands) {
Stack<Integer> loopIterations = new Stack<>();
Stack<Integer> currentIterations = new Stack<>();
StringBuilder output = new StringBuilder();
int commandIndex = 0;
while (commandIndex < commands.size()) {
String command = commands.get(commandIndex);
if (command.startsWith("for")) {
int times = Integer.parseInt(command.split(" ")[1]);
loopIterations.push(times);
currentIterations.push(0);
} else if (command.equals("do")) {
// No operation for "do"
} else if (command.equals("done")) {
int current = currentIterations.pop() + 1;
int maxIterations = loopIterations.pop();
if (current < maxIterations) {
loopIterations.push(maxIterations);
currentIterations.push(current);
commandIndex = findLoopStart(commands, commandIndex);
continue;
}
} else if (command.startsWith("break")) {
int breakCondition = Integer.parseInt(command.split(" ")[1]);
if (currentIterations.peek() + 1 == breakCondition) {
loopIterations.pop();
currentIterations.pop();
commandIndex = findLoopEnd(commands, commandIndex);
}
} else if (command.startsWith("continue")) {
int continueCondition = Integer.parseInt(command.split(" ")[1]);
if (currentIterations.peek() + 1 == continueCondition) {
int maxIterations = loopIterations.peek();
int current = currentIterations.pop() + 1;
if (current < maxIterations) {
currentIterations.push(current);
commandIndex = findLoopStart(commands, commandIndex);
}
continue;
}
} else if (command.startsWith("print")) {
String message = command.substring(command.indexOf("\"") + 1, command.lastIndexOf("\""));
output.append(message).append("\n");
}
commandIndex++;
}
System.out.print(output.toString());
}
private static int findLoopStart(List<String> commands, int currentIndex) {
int nestedLoops = 0;
for (int i = currentIndex - 1; i >= 0; i--) {
if (commands.get(i).equals("done")) {
nestedLoops++;
} else if (commands.get(i).equals("do")) {
if (nestedLoops == 0) {
return i;
}
nestedLoops--;
}
}
return 0;
}
private static int findLoopEnd(List<String> commands, int currentIndex) {
int nestedLoops = 0;
for (int i = currentIndex + 1; i < commands.size(); i++) {
if (commands.get(i).equals("do")) {
nestedLoops++;
} else if (commands.get(i).equals("done")) {
if (nestedLoops == 0) {
return i;
}
nestedLoops--;
}
}
return commands.size();
}
}
Java
Loop Master - Codevita
Telegram - t.me/codingsolution_IT
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)
Python
Plague 2050 - Codevita
Telegram - t.me/codingsolution_IT
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="")
Python
Maximum Rotation - Codevita
Telegram - t.me/codingsolution_IT
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;
polygon using the Shoelace Theorem
int calculateArea(const vector<pair<int, int>>& polygon) {
int n = polygon.size();
int area = 0;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
area += polygon[i].first * polygon[j].second;
area -= polygon[j].first * polygon[i].second;
}
return abs(area) / 2;
}
are connected
bool areConnected(pair<int, int> a, pair<int, int> b, pair<int, int>& p1, pair<int, int>& p2) {
return (p1 == a && p2 == b) || (p1 == b && p2 == a);
}
int main() {
int N;
cin >> N;
vector<pair<int, int>> coordinates;
vector<vector<pair<int, int>>> segments(N);
set<pair<int, int>> points;
store them as pairs
for (int i = 0; i < N; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
coordinates.push_back({x1, y1});
coordinates.push_back({x2, y2});
points.insert({x1, y1});
points.insert({x2, y2});
}
vector<pair<int, int>> polygon;
int maxArea = 0;
to form polygons
for (auto p1 = points.begin(); p1 != points.end(); ++p1) {
for (auto p2 = next(p1); p2 != points.end(); ++p2) {
polygon.push_back(*p1);
polygon.push_back(*p2);
maxArea = max(maxArea, calculateArea(polygon));
}
}
cout << maxArea << endl;
return 0;
}
C++
Maximum Area - Codevita
Telegram - t.me/codingsolution_IT