Polygon | Polygon TCS Code vita question | Code Vita 2023 | Polygon code Vita 2023 Solution | Code Vita 2023 season 11 solution

Problem Description:

You are given N number of coordinates and you have to create a polygon from these points such that they will make a polygon with maximum area.

Note: coordinates provided in the input may or may not be in sequential form.

Constraints

1 <= N <= 10

Input:

First line contains an integer N which depicts number of co-ordinates

Next N lines consist of two space separated integer depicting coordinates of in form of xy

Output:

Print the maximum possible area possible by creating a polygon by joining the coordinates. 

If the area is in decimal form, print the absolute value as output.

Time Limit (secs):

1

Examples:

Input:

4

0  0

2  0

0  2

2  2

Output:

4

Explanation:

As we can imagine that these points will make a square shape and the maximum possible area made by the polygon will be 4.

Solution in C :

#include <stdio.h>
#include <stdlib.h>

// Structure to represent a point
struct Point {
int x, y;
};

// Function to compare two points based on x-coordinate (and y-coordinate in case of a tie)
int compare(const void *vp1, const void *vp2) {
struct Point *p1 = (struct Point *)vp1;
struct Point *p2 = (struct Point *)vp2;

// If x-coordinates are different, return the difference
if (p1->x != p2->x)
return (p1->x - p2->x);
// If x-coordinates are the same, return the difference in y-coordinates
return (p1->y - p2->y);
}

// Function to compute the cross product of vectors (p1-p0) and (p2-p0)
int crossProduct(struct Point p0, struct Point p1, struct Point p2) {
int x1 = p1.x - p0.x;
int y1 = p1.y - p0.y;
int x2 = p2.x - p0.x;
int y2 = p2.y - p0.y;
return x1 * y2 - x2 * y1;
}

// Function to find the convex hull using Graham's Scan algorithm
int convexHull(struct Point points[], int n, struct Point *convexHullPoints) {
// If number of points is less than or equal to 3, return all points as the convex hull
if (n <= 3) {
for (int i = 0; i < n; i++)
convexHullPoints[i] = points[i];
return n;
}

// Sort points based on x-coordinate (and y-coordinate in case of a tie)
qsort(points, n, sizeof(struct Point), compare);

// Initialize upper hull and lower hull lists
struct Point upperHull[n], lowerHull[n];
int upperHullSize = 0, lowerHullSize = 0;

// Compute upper hull
for (int i = 0; i < n; i++) {
while (upperHullSize >= 2 && crossProduct(upperHull[upperHullSize - 2], upperHull[upperHullSize - 1], points[i]) <= 0)
upperHullSize--;
upperHull[upperHullSize++] = points[i];
}

// Compute lower hull
for (int i = n - 1; i >= 0; i--) {
while (lowerHullSize >= 2 && crossProduct(lowerHull[lowerHullSize - 2], lowerHull[lowerHullSize - 1], points[i]) <= 0)
lowerHullSize--;
lowerHull[lowerHullSize++] = points[i];
}

// Remove last points from upper hull and lower hull as they will be repeated
upperHullSize--;
lowerHullSize--;

// Merge upper hull and lower hull to get the convex hull
for (int i = 0; i < upperHullSize; i++)
convexHullPoints[i] = upperHull[i];
for (int i = 0; i < lowerHullSize; i++)
convexHullPoints[i + upperHullSize] = lowerHull[i];

// Return the size of convex hull
return upperHullSize + lowerHullSize;
}

// Function to calculate the area of a polygon
long calculateArea(struct Point polygon[], int n) {
// If number of points is less than 3, it's not a valid polygon, return 0
if (n < 3)
return 0;

long area = 0;

// Calculate the area using shoelace formula
for (int i = 0; i < n; i++) {
int x1 = polygon[i].x;
int y1 = polygon[i].y;
int x2 = polygon[(i + 1) % n].x;
int y2 = polygon[(i + 1) % n].y;
area += (x1 * (long)y2 - x2 * (long)y1);
}

// Return the absolute value of area divided by 2
return labs(area) / 2;
}

int main() {
int n;
scanf("%d", &n); // Input the number of points

// Array to store input points
struct Point points[n];

// Input the points
for (int i = 0; i < n; i++)
scanf("%d %d", &points[i].x, &points[i].y);

// Array to store convex hull points
struct Point convexHullPoints[n];

// Compute the convex hull
int convexHullSize = convexHull(points, n, convexHullPoints);

// Calculate the area of convex hull
long maxArea = calculateArea(convexHullPoints, convexHullSize);

// Output the maximum area
printf("%ld\n", maxArea);

return 0;
}


You can also run it on an online IDE:

https://ide.geeksforgeeks.org/online-c-compiler/0ba96fba-85cd-423f-894b-16e875822380

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Seating Arrangement | Sit Together | Code Vita 2023 | Seating Arrangement Code Vita 2023 Solution | Code Vita 2023 season 11 solution

 Problem Description

You are a caretaker of a waiting room and you have to take care of empty seats such that all the people should sit together. Imagine the seats are in a straight line like in a movie theatre. People are seated on random seats initially. Your task is to make them sit together so that minimum number of people change their position. Also, they can be made to sit together in many ways. Find the number of ways you can make them sit together by requiring only minimal people movement.

“E” depicts an empty seat and “O” depicts an occupied seat. Input will be given in the form of a string.

Example: OEOEO

As we can see, only seat number 1, 3, 5 are occupied and 2 and 4 are empty.

Case 1: If we move 5th person to 2nd position, they can all be together with only one person moving his/her place.

Case 2: If we movement 1st person to 4th position, they can all be together with only one person moving his/her place.

They can all be together with only one movement and this can be done in 2 ways. Print the minimum number of movements required and the number of ways this minimum movement can help achieve the objective.

Note: If they are already sitting together, Print “00” as output.

Constraints

0

Input

First line contains an integer N which depicts the number of seats

Second line contains N characters each of which are either “O” or “E”. “O” denotes an occupied seat and “E” denotes an empty seat.

Output

Print minimum number of movements required and the number of ways in which all people can be made to sit together without exceeding minimum number of movements by space

Time Limit (secs)

1

Examples

Input

5

OEOEO

Output

1 2

Explanation:

Given data of 5 seats in the queue,

Seat number 2 and 4 are unoccupied and all the other seats are occupied.

We can make them sit together by moving only one person near to the other. It can be done in 2 ways:

OOOEE (Moving 4 person to 2 position)

EE000 (Moving 1 person to 4 position)

Solution in C:

#include <stdio.h>
#include <string.h>

int* minMovesTogether(char seats[]);

int main() {
int N;
scanf("%d", &N); // Input number of seats
char seats[N+1];
scanf("%s", seats); // Input seat arrangement

int *result = minMovesTogether(seats);
printf("%d %d\n", result[0], result[1]);

return 0;
}

int* minMovesTogether(char seats[]) {
int occupied = 0;
int length = strlen(seats);
for (int i = 0; i < length; i++) {
if (seats[i] == 'O') {
occupied++;
}
}

if (occupied == 0 || occupied == length) {
static int zeros[2]; // Static array to hold return values
zeros[0] = 0;
zeros[1] = 0;
return zeros;
}

int emptyCounts[length];
memset(emptyCounts, 0, sizeof(emptyCounts));
int currentAvailable = 0;
int index = 0;
for (int i = 0; i < length; i++) {
if (seats[i] == 'E') {
currentAvailable++;
} else {
if (currentAvailable > 0) {
emptyCounts[index++] = currentAvailable;
}
currentAvailable = 0;
}
}

int minEmptySeats = __INT_MAX__;
for (int i = 0; i < index; i++) {
if (emptyCounts[i] < minEmptySeats) {
minEmptySeats = emptyCounts[i];
}
}

int minMoves = minEmptySeats - 1;
int ways = 0;
for (int i = 0; i < index; i++) {
if (emptyCounts[i] == minEmptySeats) {
ways++;
}
}

static int results[2]; // Static array to hold return values
results[0] = minMoves * occupied + 1;
results[1] = ways;
return results;
}

You can also run it on an online IDE: 

https://ide.geeksforgeeks.org/online-c-compiler/04270dde-f3c6-4a80-bbb8-69314322c030

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...