Showing posts with label TCS Code Vita questions and solutions. Show all posts
Showing posts with label TCS Code Vita questions and solutions. Show all posts

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 bags which differ in its quantity, price, brand, and type of rice in it.

Many variations of same products exist in a super market. Consider rice for example. We get it in varying quantities (q) and at different prices (p).

Thus rice bag is unique combination of {q,p}. Customers want to buy the rice bags of their own choices. Each bag has two attributes price and the quantity of rice. The customers have some conditions for buying the rice bags, they have a specific price and quantity that have to be compared with the rice bags before buying them. If the price of rice bag is less than or equal to the customer’s preference and if the quantity is more than given preference, then he/she will buy it. There is only one bag of each type and each customer can buy at most one bag.

Given n,m representing the number of customers and rice bags respectively, along with the variations of rice bags available in the market and the preferences of customers, find the maximum number of bags that can be sold by the end of the day.

Constraints

1 <= n, m <= 1000

1 <= a, b <= 10^5

1 <=p, q<= 10^5

Input

The first line contains two space separated integers n and m denoting the number of customers and number of rice bags respectively.

Next n lines consist of two space separated integers a and b denoting customer’s preferences viz. customer’s quantity and cost preferences, respectively.

Lastly, the next m lines consist of two space separated integers q and p denoting the bags quantity and cost, respectively.

Output

Print the maximum number of rice bags that can be sold.

Time Limit (secs)

1

Examples

Example 1

Input

5 4

35 2400

70 5500

87 6000

20 1700

12 500

50 2500

75 4875

100 7000

25 1250

Output

2

Explanation

Since price of bag should be less than or equal to customer’s preference and the quantity should be greater than the preferred quantity, customer 2 can buy bag 2 and customer 4 can buy bag 4.

So, in total, 2 bags can be sold to the customers.

Example 2

4 7

32 1500

58 5000

87 6200

45 3000

20 1200

60 4500

100 6000

80 5500

35 1400

40 2000

50 2800

Output

4

Explanation

Since price of bag should be less than or equal to customer’s preference and the quantity should be greater than the preferred quantity, customer 1 can buy bag 5, customer 2 can buy bag 2, customer 3 can buy bag 3 and customer 4 can buy 7th bag.

So, in total, 4 bags can be sold to the customers.

Solution in C:

#include <stdio.h>

// Function to find the maximum number of bags that can be sold
int maxBagsSold(int customers[][2], int bags[][2], int n, int m) {
int maxBags = 0;
for (int i = 0; i < n; i++) {
int customerQuantity = customers[i][0];
int customerPrice = customers[i][1];
for (int j = 0; j < m; j++) {
int bagQuantity = bags[j][0];
int bagPrice = bags[j][1];
if (bagPrice <= customerPrice && bagQuantity >= customerQuantity) {
maxBags++;
break;
}
}
}
return maxBags;
}

int main() {
int n, m;
scanf("%d %d", &n, &m);

int customers[n][2];
for (int i = 0; i < n; i++) {
scanf("%d %d", &customers[i][0], &customers[i][1]);
}

int bags[m][2];
for (int i = 0; i < m; i++) {
scanf("%d %d", &bags[i][0], &bags[i][1]);
}

int maxBags = maxBagsSold(customers, bags, n, m);
printf("%d\n", maxBags);

return 0;
}


You can also run it on an online IDE: 

https://ide.geeksforgeeks.org/online-c-compiler/fc646307-f362-4f36-b062-9b1ebe8c9bc3

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

Related Links:

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!!!

Zero Count | Code Vita 2023 | Zero Count Code Vita 2023 Solution | Code Vita 2023 season 11 solution

 Problem Description:

You are given a binary string B of length L which contains K ones and remaining zeros. You are required to place the K ones in the binary string in such a way that the longest consecutive zeros have the least length possible. Once such a binary string is constructed, you are required to print the length of the contiguous block of zeros, which has the largest length.

Constraints

0 <= K <= L

1 <= L <= 10^6

 Input

Single line consisting of two space separated integers denoting L and K.

Output

Print a single integer denoting the length of the longest consecutive zeros as per the problem. 

Time Limit (secs)

1

 Examples

 Example 1

Input

3 1

Output

1

Explanation

B is of length 3 and it has 1 one’s.

So the possible strings as per the problem are 010, 001, 100.

In the first case, the maximum length of consecutive zeros is 1 whereas in the other two cases it is 2. Hence the constructed binary string is 010 and the output is 1.

Example 2

Input

3 3

Output

0

Explanation

B is of length 3 and it has all three one’s. There is no block of zeros, hence the output is 0.


Solution in C:

#include <stdio.h>

// Function to count zeros based on values of L and K
int zerocount(int L, int K) {
// Base case: if K is 0, return L
if (K == 0) {
return L;
}
// Base case: if K is equal to L, return 0
if (K == L) {
return 0;
}
// Initialize max_zeros to 0
int max_zeros = 0;
// If K is greater than 0, set max_zeros to 1
if (K > 0) {
max_zeros = 1;
}
// Return the value of max_zeros
return max_zeros;
}

int main() {
// Declare variables L and K
int L, K;
// Input values for L and K
scanf("%d %d", &L, &K);
// Call the zerocount function and store the result in res
int res = zerocount(L, K);
// Print the result
printf("%d\n", res);

return 0;
}

You can also run it on an online IDE: 

https://ide.geeksforgeeks.org/online-c-compiler/17c95992-ebe1-4724-ae1f-ac10039b7fba

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

Related Links:

Minimize the sum problem | Code Vita 2020 | Code Vita 9


Problem Description:

Given an array of integers, perform atmost K operations so that the sum of elements of final array is minimum. An operation is defined as follows – Consider any 1 element from the array, arr[i]. Replace arr[i] by floor(arr[i]/2). Perform next operations on the updated array. The task is to minimize the sum after utmost K operations.

Constraints 1 <= N K <= 10^5.
Input First line contains two integers N and K representing size of array and maximum numbers of operations that can be performed on the array respectively. Second line contains N space separated integers denoting the elements of the array, arr.
Output

Print a single integer denoting the minimum sum of the final array. 

Input

4 3

20 7 5 4


Output

17


Explanation

Operation 1 -> Select 20. Replace it by 10. New array = [10, 7, 5, 4]

Operation 2 -> Select 10. Replace it by 5. New array = [5, 7, 5, 4].

Operation 3 -> Select 7. Replace it by 3. New array = [5,3,5,4].

Sum = 17.


Solution in C:

#include <stdio.h>

void insertionSort(int arr[], int n) {
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;

        /* Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position */
        while (j >= 0 && arr[j] < key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

int minimizeSum(int N, int K, int arr[]) {
    int i;
    // Sort the array in descending order
    insertionSort(arr, N);

    for (i = 0; i < K; i++) {
        // Perform the operation on the maximum element
        arr[0] = arr[0] / 2;
        // Re-sort the array
        insertionSort(arr, N);
    }

    int sum = 0;
    for (i = 0; i < N; i++) {
        sum += arr[i];
    }

    return sum;
}

int main() {
    int N, K;
    scanf("%d %d", &N, &K);

    int arr[N];
    for (int i = 0; i < N; i++) {
        scanf("%d", &arr[i]);
    }

    int result = minimizeSum(N, K, arr);
    printf("%d\n", result);

    return 0;
}


You can also run it on an online IDE: 


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

Related Links:

https://codepiggy.blogspot.com/2024/01/restaurants-code-vita-8-round-2.html


Restaurants - Code Vita 8 | Round 2

Restaurants

Problem Description:

Looking for a place to rent in New York, Codu wants to stay in a place close to as many restaurants as possible and walk as little as possible to reach them. As he is a vegetarian, and likes only some types of food, there are only a few restaurants that meet his criteria.

For convenience he models the roads as rectangular grid roads and plots the restaurant locations at the intersection of two roads there. To simplify further, he approximates the distance as the number of blocks between two places. Given the maximum distance he wants to walk, he wants to find the most optimal location(from which he can reach the maximum number of restaurants within that distance).

The grid starts at (0,0) at the southwest corner, and goes to (N,0) at the south east corner, and to (N,M) at the north east corner. If two locations the same (maximum)number of restaurants he can walk to, he would prefer to have the southern most location (lowest Y coordinate). If two locations lie on the same southernmost road and have the same (maximum) number of restaurants he can walk to, he would prefer the westernmost location (lowest X coordinate).

Constraints
1 < N,M,K <= 1000
House can’t be in the same block where there exists a restaurant
Input Format
First line contains two integers N, M the number of horizontal and vertical blocks(intervals between roads). The grid is given the coordinates starting with (0,0) on thelower left corner and (N,M) the upper right corner. The next line contains K, the number of restaurants. The next K pairs of integers give the coordinates of the K restaurants. The next line gives the distance he wants to walk (in blocks)
Output
Three integers, first two giving the coordinates of the optimal location and the next integer giving the number of restaurants reachable from that location.

Test Case

Example 1:

Input

4 4
5
1 1
1 2
3 3
4 4
2 4
4

Output

3 1 5

Solution:

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

struct Restaurant {
    int x, y;
};

int distance(int x1, int y1, int x2, int y2) {
    return abs(x1 - x2) + abs(y1 - y2);
}

int main() {
    int N, M, K;
    scanf("%d %d", &N, &M);
    
    scanf("%d", &K);
    struct Restaurant restaurants[K];
    for (int i = 0; i < K; i++) {
        scanf("%d %d", &restaurants[i].x, &restaurants[i].y);
    }
    
    int maxDistance;
    scanf("%d", &maxDistance);
    
    int maxRestaurants = 0;
    int optimalX, optimalY;

    for (int x = 0; x <= N; x++) {
        for (int y = 0; y <= M; y++) {
            int currentRestaurants = 0;
            
            for (int i = 0; i < K; i++) {
                if (distance(x, y, restaurants[i].x, restaurants[i].y) <= maxDistance) {
                    currentRestaurants++;
                }
            }

            if (currentRestaurants > maxRestaurants || 
                (currentRestaurants == maxRestaurants && (y < optimalY
|| (y == optimalY && x < optimalX)))) {
                maxRestaurants = currentRestaurants;
                optimalX = x;
                optimalY = y;
            }
        }
    }

    printf("%d %d %d\n", optimalX, optimalY, maxRestaurants);

    return 0;
}


You can also run it on an online IDE:  


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...