IBM Coding Exam Answers
2.45K subscribers
13 photos
70 links
Download Telegram
#include<bits/stdc++.h>
using namespace std;

int main()
{
int x, y, x1, y1, x2, y2;
cin >> x >> y >> x1 >> y1 >> x2 >> y2;

x = abs(x);
y = abs(y);
x1 = abs(x1);
y1 = abs(y1);
x2 = abs(x2);
y2 = abs(y2);

int r1 = sqrt(((x1 - x) * (x1 - x)) + ((y1 - y) * (y1 - y)));
int r2 = sqrt(((x2 - x) * (x2 - x)) + ((y2 - y) * (y2 - y)));

int a1 = M_PI * r1 * r1;
int a2 = M_PI * r2 * r2;

if (a2 > a1)
{
int e = a2 - a1;
int f = sqrt(e);
int g = f * f;
int h = g - e;
int i = h * 20;
cout << "Krishna " << i;
}
else if (a1 > a2)
{
int p = a1 - a2;
int q = p * 20;
cout << "Shiva " << q;
}
else if (a1 == a2)
{
cout << "-1";
}

return 0;
}

Fencing Problem

Pls share

https://t.me/TCS_Codevita_Exam_Help
#include<bits/stdc++.h>
using namespace std;

void mark(int xa, int ya, int ha, vector<vector<int>> &matrix, vector<vector<int>> &visited) {
int rows = matrix.size(), cols = matrix[0].size();
visited[xa][ya] = 1;
int delta[5] = {-1, 0, +1, 0, -1};
for(int i = 0; i < 4; i++) {
int ax = xa + delta[i];
int ay = ya + delta[i+1];
if(min(ax, ay) >= 0 && ax < rows && ay < cols && matrix[ax][ay] <= ha && !visited[ax][ay]) {
mark(ax, ay, ha, matrix, visited);
}
}
}

void go2(int x, int y, int cell, vector<vector<int>> &matrix, vector<vector<int>> &visited) {
int rows = matrix.size(), cols = matrix[0].size();
int delta[5] = {-1, 0, +1, 0, -1};
visited[x][y] = 0;
for(int i = 0; i < 4; i++) {
int nx = x + delta[i];
int ny = y + delta[i+1];
if(min(nx, ny) >= 0 && nx < rows && ny < cols && matrix[nx][ny] == cell && visited[nx][ny]) {
go2(nx, ny, cell, matrix, visited);
}
}
}

int main() {
int rows, cols;
cin >> rows >> cols;
vector<vector<int>> matrix(rows, vector<int>(cols)), visited(rows, vector<int>(cols, 0));
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
cin >> matrix[i][j];

int x, y, h;
cin >> x >> y >> h;

if(matrix[x][y] == 0) mark(x, y, h, matrix, visited);

int count = 0;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(visited[i][j]) {
count += matrix[i][j] != 0;
go2(i, j, matrix[i][j], matrix, visited);
}
}
}

if (count == 0) cout << "NONE";
else cout << count;

return 0;
}

BlackBeard Code

Pls share

https://t.me/TCS_Codevita_Exam_Help
Fencing Code
C++
TCS Codevita

https://t.me/TCS_Codevita_Exam_Help

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

int main()
{
    int x, y, x1, y1, x2, y2;
    cin >> x >> y >> x1 >> y1 >> x2 >> y2;
   
    x = abs(x);
    y = abs(y);
    x1 = abs(x1);
    y1 = abs(y1);
    x2 = abs(x2);
    y2 = abs(y2);
   
    int r1 = sqrt(((x1 - x) * (x1 - x)) + ((y1 - y) * (y1 - y)));
    int r2 = sqrt(((x2 - x) * (x2 - x)) + ((y2 - y) * (y2 - y)));
   
    int a1 = M_PI * r1 * r1;
    int a2 = M_PI * r2 * r2;
   
    if (a2 > a1)
    {
        int e = a2 - a1;
        int f = sqrt(e);
        int g = f * f;
        int h = g - e;
        int i = h * 20;
        cout << "Krishna " << i;
    }
    else if (a1 > a2)
    {
        int p = a1 - a2;
        int q = p * 20;
        cout << "Shiva " << q;
    }
    else if (a1 == a2)
    {
        cout << "-1";
    }

    return 0;
}

Fencing Code
C++
TCS Codevita

https://t.me/TCS_Codevita_Exam_Help
#include<bits/stdc++.h>
using namespace std;

int main()
{
int x, y, x1, y1, x2, y2;
cin >> x >> y >> x1 >> y1 >> x2 >> y2;

x = abs(x);
y = abs(y);
x1 = abs(x1);
y1 = abs(y1);
x2 = abs(x2);
y2 = abs(y2);

int r1 = sqrt(((x1 - x) * (x1 - x)) + ((y1 - y) * (y1 - y)));
int r2 = sqrt(((x2 - x) * (x2 - x)) + ((y2 - y) * (y2 - y)));

int a1 = M_PI * r1 * r1;
int a2 = M_PI * r2 * r2;

if (a2 > a1)
{
int e = a2 - a1;
int f = sqrt(e);
int g = f * f;
int h = g - e;
int i = h * 20;
cout << "Krishna " << i;
}
else if (a1 > a2)
{
int p = a1 - a2;
int q = p * 20;
cout << "Shiva " << q;
}
else if (a1 == a2)
{
cout << "-1";
}

return 0;
}

Fencing Problem

Pls share

https://t.me/TCS_Codevita_Exam_Help
#include <stdio.h>

void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n, int arr2[]) {
int i,j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(&arr[j], &arr[j+1]);
swap(&arr2[j], &arr2[j+1]);
}
}
}
}

int main() {
int n,i,j;
printf("Enter size of arrays: ");
scanf("%d", &n);
int a1[n], a2[n];
printf("\nEnter first array: ");
for ( i = 0; i < n; i++) {
scanf("%d", &a1[i]);
}
printf("\nEnter second array: ");
for ( i = 0; i < n; i++) {
scanf("%d", &a2[i]);
}
bubbleSort(a1, n, a2);
for ( i = 0; i < n; i++) {
printf("%d ", a2[i]);
}

return 0;
}


Bubble Sort C++ Working Code
TCS Codevita

Pls share

https://t.me/TCS_Codevita_Exam_Help
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
typedef struct {
int x, y;
} Point;
typedef struct {
char name[20];
Point location;
} Passenger;
typedef struct {
char number[20];
Point location;
} Vehicle;
int manhattanDistance(Point p1, Point p2) {
return abs(p1.x - p2.x) + abs(p1.y - p2.y);
}
Vehicle* findNearestVehicle(Passenger passenger, Vehicle* vehicles, int m, int* assigned) {
int i;
int minDistance = INT_MAX;
Vehicle* nearestVehicle = NULL;
for (i = 0; i < m; i++) {
if (!assigned[i]) {
int distance = manhattanDistance(passenger.location, vehicles[i].location);
if (distance < minDistance || (distance == minDistance && strcmp(vehicles[i].number, nearestVehicle->number) < 0)) {
minDistance = distance;
nearestVehicle = &vehicles[i];
}
}
}
assigned[nearestVehicle - vehicles] = 1;
return nearestVehicle;
}
int main() {
int n, m,i;
scanf("%d %d", &n, &m);
Passenger passengers[n];
for ( i = 0; i < n; i++) {
scanf("%s %d %d", passengers[i].name, &passengers[i].location.x, &passengers[i].location.y);
}
Vehicle vehicles[m];
for ( i = 0; i < m; i++) {
scanf("%s %d %d", vehicles[i].number, &vehicles[i].location.x, &vehicles[i].location.y);
}
int assigned[m];
memset(assigned, 0, sizeof(assigned));
int totalDistance = 0;
for ( i = 0; i < n; i++) {
Vehicle* assignedVehicle = findNearestVehicle(passengers[i], vehicles, m, assigned);
totalDistance += manhattanDistance(passengers[i].location, assignedVehicle->location);
}
printf("%d\n", totalDistance);
return 0;
}

Solo Rider
C++ Working Code
TCS Codevita

Pls share

https://t.me/TCS_Codevita_Exam_Help
Solo Rider


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
typedef struct {
int x, y;
} Point;
typedef struct {
char name[20];
Point location;
} Passenger;
typedef struct {
char number[20];
Point location;
} Vehicle;
int manhattanDistance(Point p1, Point p2) {
return abs(p1.x - p2.x) + abs(p1.y - p2.y);
}
Vehicle* findNearestVehicle(Passenger passenger, Vehicle* vehicles, int m, int* assigned) {
int i;
int minDistance = INT_MAX;
Vehicle* nearestVehicle = NULL;
for (i = 0; i < m; i++) {
if (!assigned[i]) {
int distance = manhattanDistance(passenger.location, vehicles[i].location);
if (distance < minDistance || (distance == minDistance && strcmp(vehicles[i].number, nearestVehicle->number) < 0)) {
minDistance = distance;
nearestVehicle = &vehicles[i];
}
}
}
assigned[nearestVehicle - vehicles] = 1;
return nearestVehicle;
}
int main() {
int n, m,i;
scanf("%d %d", &n, &m);
Passenger passengers[n];
for ( i = 0; i < n; i++) {
scanf("%s %d %d", passengers[i].name, &passengers[i].location.x, &passengers[i].location.y);
}
Vehicle vehicles[m];
for ( i = 0; i < m; i++) {
scanf("%s %d %d", vehicles[i].number, &vehicles[i].location.x, &vehicles[i].location.y);
}
int assigned[m];
memset(assigned, 0, sizeof(assigned));
int totalDistance = 0;
for ( i = 0; i < n; i++) {
Vehicle* assignedVehicle = findNearestVehicle(passengers[i], vehicles, m, assigned);
totalDistance += manhattanDistance(passengers[i].location, assignedVehicle->location);
}
printf("%d\n", totalDistance);
return 0;
}
Solo rider


Only public Test Cases Passed

I'm Trying to Modify So for now you can try this

I will update new soon

Keep sharing
https://t.me/TCS_Codevita_Exam_Help
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

map<pair<char, string>, int> expenses;
map<pair<char, char>, int> loans;
map<char, int> balances;

void processTransaction(string transaction) {
    stringstream ss(transaction);
    string type;
    ss >> type;

    if (type == "L") {
        char borrower, lender;
        int amount;
        ss >> borrower >> lender >> type >> amount;
        loans[{borrower, lender}] += amount;
    } else {
        char paidBy;
        int amount;
        char temp;
        ss >> paidBy >> temp >> amount;

        balances[paidBy] -= amount;
        int sharedAmong = 0;
        while (ss >> temp) {
            char person;
            ss >> person;
            balances[person] += amount / 3;
            sharedAmong++;
        }
        balances[paidBy] += amount - (amount / 3 * sharedAmong);
    }
}

void calculateAndProcessInterest(int currentDay) {
    for (auto &loan : loans) {
        char borrower = loan.first.first;
        char lender = loan.first.second;
        int amount = loan.second;

        int weeks = (currentDay - 1) / 7;
        double interest = amount * pow(1.01, weeks) - amount;
        expenses[{lender, "I" + borrower}] += static_cast<int>(round(interest));
    }
}

void reconcileBalances() {
    for (const auto &expense : expenses) {
        char borrower = expense.first.second[1];
        char lender = expense.first.first;
        int amount = expense.second;

        if (amount <= loans[{borrower, lender}]) {
            loans[{borrower, lender}] -= amount;
        } else {
            amount -= loans[{borrower, lender}];
            loans[{borrower, lender}] = 0;

            balances[borrower] += amount;
            balances[lender] -= amount;
        }
    }

    vector<pair<char, int>> creditors, debtors;

    for (const auto &balance : balances) {
        if (balance.second > 0) {
            creditors.push_back({balance.first, balance.second});
        } else if (balance.second < 0) {
            debtors.push_back({balance.first, -balance.second});
        }
    }

    sort(creditors.begin(), creditors.end());
    sort(debtors.begin(), debtors.end());

    auto creditor = creditors.begin();
    auto debtor = debtors.begin();

    while (creditor != creditors.end() && debtor != debtors.end()) {
        int settleAmount = min(creditor->second, debtor->second);
        cout << debtor->first << "/" << creditor->first << "/" << settleAmount << endl;
        creditor->second -= settleAmount;
        debtor->second -= settleAmount;
        if (creditor->second == 0) ++creditor;
        if (debtor->second == 0) ++debtor;
    }

    if (creditor == creditors.end() && debtor == debtors.end()) {
        cout << "NO DUES" << endl;
    }
}

int main() {
    int N;
    cin >> N;
    cin.ignore();

    for (int i = 0; i < N; ++i) {
        string transaction;
        getline(cin, transaction);
        processTransaction(transaction);
        calculateAndProcessInterest(i + 1);
    }

    reconcileBalances();

    return 0;
}