CODING SOLUTION HELP
26.8K subscribers
28 photos
1 video
1.95K links
Download Telegram
Forwarded from BIG BILLION DAYS DEALS
#include <stdio.h>
int main() {

int number1, number2, sum;

printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

// calculating sum
sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);
return 0;
}

C language
Arrangement

Telegram - https://t.me/coding_solution_help
#include <stdio.h>

int main()
{
int blank_char, tab_char, new_line;
blank_char = 0;
tab_char = 0;
new_line = 0;
int c;
printf("Number of blanks, tabs, and newlines:\n");
printf("Input few words/tab/newlines\n");
for (; (c = getchar()) != EOF;)
{
if ( c == ' ' ){
++blank_char;
}
if ( c == '\t' ){
++tab_char;
}
if ( c == '\n' ){
++new_line;
}
}
printf("blank=%d,tab=%d,newline=%d\n",blank_char,tab_char,new_line);
}

C++
Find number of empty fields in a record Code
#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int count=0;
    int num=arr[0];
    for(int i=1;i<n;i++)
    {
       if(num!=arr[i])
            count++;
    }
    printf("%d",count);
}

C Language
TCS 1st Qsn

---------------------------------------------------------

N=int(input())
K=int(input())
price=list(map(int,input().split()))
vol=list(map(int,input().split()))
maxvol=0
volu=0
maxvol=max(vol)
for i in range(0,N):
    if (maxvol==vol[i] and price[i]<=K):
        K=K-price[i]
        volu=maxvol
for i in range(0,N):
    for j in range(i+1,N+1):
        if (price[i]<=K and price[i]==price[j]):
            if (vol[i]>vol[j]):
                volu=volu+vol[i]
                K=K-price[i]
            else:
                volu=volu+vol[j]
                K=K-price[j]
        elif (price[i]<=K and price[i]!=price[j]):
            K=K-price[i]
            -------

include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int count=0;
    int num=arr[0];
    for(int i=1;i<n;i++)
    {
       if(num!=arr[i])
            count++;
    }
    printf("%d",count);
}

Array Code in C language

https://t.me/coding_solution_help
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

struct Line {
int x1, y1, x2, y2;
};
int countCells(Line line, pair<int, int> star, bool split) {
if (line.x1 == line.x2) {
if (split) {
return min(abs(star.second - line.y1), abs(star.second - line.y2)) + 1;
}
else {
return abs(line.y1 - line.y2) + 1;
}
}
else {
if (split) {
return min(abs(star.first - line.x1), abs(star.first - line.x2)) + 1;
}
else {
return abs(line.x1 - line.x2) + 1;
}
}
}
bool intersects(Line a, Line b, pair<int, int>& intersection) {
if (a.x1 == a.x2 && b.y1 == b.y2) {
if (b.x1 <= a.x1 && a.x1 <= b.x2 && a.y1 <= b.y1 && b.y1 <= a.y2) {
intersection = {a.x1, b.y1};
return true;
}
}
if (a.y1 == a.y2 && b.x1 == b.x2) {
if (a.x1 <= b.x1 && b.x1 <= a.x2 && b.y1 <= a.y1 && a.y1 <= b.y2) {
intersection = {b.x1, a.y1};
return true;
}
}
return false;
}
int main() {
int N, K;
cin >> N;
vector<Line> lines(N);
for (int i = 0; i < N; ++i) {
cin >> lines[i].x1 >> lines[i].y1 >> lines[i].x2 >> lines[i].y2;
if (lines[i].x1 > lines[i].x2 || (lines[i].x1 == lines[i].x2 && lines[i].y1 > lines[i].y2)) {
swap(lines[i].x1, lines[i].x2);
swap(lines[i].y1, lines[i].y2);
}
}
cin >> K;
map<pair<int, int>, vector<Line>> stars;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
pair<int, int> intersection;
if (intersects(lines[i], lines[j], intersection)) {
stars[intersection].push_back(lines[i]);
stars[intersection].push_back(lines[j]);
}
}
}

int asiylam = 0;
for (auto& star : stars) {
if (star.second.size() / 2 == K) {
vector<int> intensities;
for (auto& line : star.second) {
intensities.push_back(countCells(line, star.first, true));
}
asiylam += *min_element(intensities.begin(), intensities.end());
}
}
cout << asiylam << endl;
return 0;
}

Magic Star Intensity Code
C++
TCS Exam

https://t.me/coding_solution_help
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

struct Line {
int x1, y1, x2, y2;
};
int countCells(Line line, pair<int, int> star, bool split) {
if (line.x1 == line.x2) {
if (split) {
return min(abs(star.second - line.y1), abs(star.second - line.y2)) + 1;
}
else {
return abs(line.y1 - line.y2) + 1;
}
}
else {
if (split) {
return min(abs(star.first - line.x1), abs(star.first - line.x2)) + 1;
}
else {
return abs(line.x1 - line.x2) + 1;
}
}
}
bool intersects(Line a, Line b, pair<int, int>& intersection) {
if (a.x1 == a.x2 && b.y1 == b.y2) {
if (b.x1 <= a.x1 && a.x1 <= b.x2 && a.y1 <= b.y1 && b.y1 <= a.y2) {
intersection = {a.x1, b.y1};
return true;
}
}
if (a.y1 == a.y2 && b.x1 == b.x2) {
if (a.x1 <= b.x1 && b.x1 <= a.x2 && b.y1 <= a.y1 && a.y1 <= b.y2) {
intersection = {b.x1, a.y1};
return true;
}
}
return false;
}
int main() {
int N, K;
cin >> N;
vector<Line> lines(N);
for (int i = 0; i < N; ++i) {
cin >> lines[i].x1 >> lines[i].y1 >> lines[i].x2 >> lines[i].y2;
if (lines[i].x1 > lines[i].x2 || (lines[i].x1 == lines[i].x2 && lines[i].y1 > lines[i].y2)) {
swap(lines[i].x1, lines[i].x2);
swap(lines[i].y1, lines[i].y2);
}
}
cin >> K;
map<pair<int, int>, vector<Line>> stars;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
pair<int, int> intersection;
if (intersects(lines[i], lines[j], intersection)) {
stars[intersection].push_back(lines[i]);
stars[intersection].push_back(lines[j]);
}
}
}

int asiylam = 0;
for (auto& star : stars) {
if (star.second.size() / 2 == K) {
vector<int> intensities;
for (auto& line : star.second) {
intensities.push_back(countCells(line, star.first, true));
}
asiylam += *min_element(intensities.begin(), intensities.end());
}
}
cout << asiylam << endl;
return 0;
}

Magic Star Intensity Code
C++
TCS Exam

https://t.me/coding_solution_help
#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
#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
#include <bits/stdc++.h>
using namespace std;

struct Point {
int x, y;
};

int squaredDistance(const Point& a, const Point& b) {
int dx = a.x - b.x;
int dy = a.y - b.y;
return dx * dx + dy * dy;
}

int main(){
ios::sync_with_stdio(false);
cin.tie(0);

int N;
cin >> N;
vector<vector<int>> earringEdges(N, vector<int>());

for(int i=0; i<N; i++){
int K;
cin >> K;
vector<Point> points(K);
for(int j=0; j<K; j++) cin >> points[j].x >> points[j].y;

vector<int> edges;
for(int j=0; j<K; j++){
int next = (j+1) % K;
edges.push_back(squaredDistance(points[j], points[next]));
}
earringEdges[i] = edges;
}

bool found = false;
int e1 = -1, e2 = -1;

for(int i=0; i<N && !found; i++){
for(int j=i+1; j<N && !found; j++){
if(earringEdges[i].size() != earringEdges[j].size()) continue;
int K = earringEdges[i].size();
for(int s=0; s<K && !found; s++){
bool match = true;
for(int k=0; k<K; k++){
if(earringEdges[i][k] != earringEdges[j][(k+s)%K]){
match = false;
break;
}
}
if(match){
e1 = i+1;
e2 = j+1;
found = true;
}
}
}
}

if(found){
cout << e1 << " " << e2;
} else {
cout << "No matching pair found.";
}

return 0;
}

C++
Find Pairs - Codevita
Telegram -
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int n;
cin >> n;
vector<int> ids(n), costs(n);

for (int i = 0; i < n; i++) cin >> ids[i];
for (int i = 0; i < n; i++) cin >> costs[i];

int budget;
cin >> budget;

int maxItems = 0, minCost = 0;

for (int i = 0; i < n; i++) {
int itemCost = costs[i];
int quantity = budget / itemCost;

if (quantity > 0) {
int currentItems = 0, currentCost = 0;

for (int j = 0; j < n; j++) {
if (i != j && ids[i] % ids[j] == 0) {
currentItems += quantity;
currentCost += costs[j] * quantity;
}
}

if (currentItems > maxItems || (currentItems == maxItems && currentCost > minCost)) {
maxItems = currentItems;
minCost = currentCost;
}
}
}

cout << maxItems << " " << minCost << endl;

return 0;
}


C++
Buzz Day Sale - Codevita
Telegram