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:

Dining Table Seating Arrangement Problem | Code Vita 2020 | Code Vita season 9

 Problem Description:

In a Conference ,attendees are invited for a dinner after the conference.The Co-ordinator,Sagar arranged around round tables for dinner and want to have an impactful seating experience for the attendees.Before finalizing the seating arrangement,he wants to analyze all the possible arrangements.These are R round tables and N attendees.In case where N is an exact multiple of R,the number of attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding.

For example, R = 2 and N = 3

All possible seating arrangements are

(1,2) & (3)

(1,3) & (2)

(2,3) & (1)

Attendees are numbered from 1 to N.

Input Format:

The first line contains T denoting the number of test cases.

Each test case contains two space separated integers R and N, Where R denotes the number of round tables and N denotes the number of attendees.

Output Format:

Single Integer S denoting the number of possible unique arrangements.

Constraints:

0 <= R <= 10(Integer)

0 < N <= 20 (Integer)

Sample Input 1:

1

2 5

Sample Output 1:

10

Explanation:

R = 2, N = 5

(1,2,3) & (4,5)

(1,2,4) & (3,5)

(1,2,5) & (3,4)

(1,3,4) & (2,5)

(1,3,5) & (2,4)

(1,4,5) & (2,3)

(2,3,4) & (1,5)

(2,3,5) & (1,4)

(2,4,5) & (1,3)

(3,4,5) & (1,2)

Arrangements like

(1,2,3) & (4,5)

(2,1,3) & (4,5)

(2,3,1) & (4,5) etc.

But as it is a round table, all the above arrangements are same.

Solution in C:

#include <stdio.h>

// Function to calculate factorial
long long factorial(int n) {
long long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}

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

for (int i = 0; i < testcases; i++) {
int tables, people;
scanf("%d %d", &tables, &people);

if (tables >= people) {
printf("1\n");
} else {
int PA = people / tables;
int PB = PA + 1;
int TB = people % tables;
int TA = tables - TB;

// Using DP to store factorials pre-hand
long long fact[people + 2];
fact[0] = 1;
for (int j = 1; j <= people + 1; j++) {
fact[j] = j * fact[j - 1];
}

// Dividing people between tables
long long divide = fact[people] / (fact[PA] * fact[TB] * fact[TA] * fact[PB]);

long long result;
if (PB >= 4) {
result = divide * (factorial(PA - 1) / 2) * (factorial(TA) / 2) * (factorial(PB - 1) / 2) * (factorial(TB) / 2);
} else {
result = divide;
}

printf("%lld\n", result);
}
}

return 0;
}


You can also run it on an online IDE: 

https://ide.geeksforgeeks.org/online-c-compiler/c5c2e7bb-031b-4805-9d5d-022c5f61b61c

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

Related Links:

3 Palindromes | Code Vita 2020 Question and Solution | Code Vita 9

 Problem Description

In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes. Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring.

If there is no way to split the word into exactly three palindromic substrings, print “Impossible” (without quotes). Every character of the string needs to be consumed.

Cases not allowed –

After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed.

No character may be shared in forming 3 palindromes.

Constraints

1 <= the length of input sting <= 1000

Input

First line contains the input string consisting of characters between [a-z].

Output

Print 3 substrings one on each line.

Time Limit

1

Example 1

Input

nayannamantenet

Output

nayan

naman

tenet

Explanation

The original string can be split into 3 palindromes as mentioned in the output.

However, if the input was nayanamantenet, then the answer would be “Impossible”.


Example 2

Input

aaaaa

Output

a

a

aaa

Explanation

The other ways to split the given string into 3 palindromes are as follows –

[a, aaa, a], [aaa, a, a], [aa, aa, a], etc.

Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, a, aaa].


Example 3

Input

aaaabaaaa

Output

a

aaabaaa

a

Explanation

The other ways to split the given string into 3 palindromes are as follows –

[aaaa, b, aaaa], [aa, aabaa, aa], etc.

Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, aaabaaa, a].


Solution in  C:

#include <stdio.h>

#define mod 1000000007
#define MAX_LENGTH 1000

typedef long long int lld;

int isPalindrome(char str[], int start, int end) {
while (start < end) {
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Palindrome
}

int main() {
char s[MAX_LENGTH], s1[MAX_LENGTH], s2[MAX_LENGTH], s3[MAX_LENGTH];
scanf("%s", s);

int l = 0;
while (s[l] != '\0') {
l++;
}

for (int i = 1; i < l - 1; i++) {
for (int k = 0; k < i; k++) {
s1[k] = s[k];
}
s1[i] = '\0';

if (isPalindrome(s, 0, i - 1)) {
for (int j = 1; j < l - i; j++) {
for (int k = 0; k < j; k++) {
s2[k] = s[i + k];
}
s2[j] = '\0';

for (int k = 0; k < l - i - j; k++) {
s3[k] = s[i + j + k];
}
s3[l - i - j] = '\0';

if (isPalindrome(s2, 0, j - 1) && isPalindrome(s3, 0, l - i - j - 1)) {
printf("%s\n%s\n%s\n", s1, s2, s3);
return 0;
}
}
}
}

printf("Impossible\n");
return 0;
}



You can also run it on an online IDE: 

https://ide.geeksforgeeks.org/online-python3-compiler/294545fa-8a51-46da-ab50-8fb862187677

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

Related Links:



Television Set | Code Vita 2020 | Code Vita 9

 Television Set 

Problem Description

Dr. Vishnu is opening a new world class hospital in a small town designed to be the first preference of the patients in the city. Hospital has N rooms of two types – with TV and without TV, with daily rates of R1 and R2 respectively.

However, from his experience Dr. Vishnu knows that the number of patients is not constant throughout the year, instead it follows a pattern. The number of patients on any given day of the year is given by the following formula – (6-M)^2 + |D-15| , where M is the number of month (1 for jan, 2 for feb …12 for dec) and D is the date (1,2…31). All patients prefer without TV rooms as they are cheaper, but will opt for with TV rooms only if without TV rooms are not available. Hospital has a revenue target for the first year of operation. Given this target and the values of N, R1 and R2 you need to identify the number of TVs the hospital should buy so that it meets the revenue target. Assume the Hospital opens on 1st Jan and year is a non-leap year. Constraints Hospital opens on 1st Jan in an ordinary year 5 <= Number of rooms <= 100 500 <= Room Rates <= 5000 0 <= Target revenue < 90000000
Input Format First line provides an integer N that denotes the number of rooms in the hospital Second line provides two space-delimited integers that denote the rates of rooms with TV (R1) and without TV (R2) respectively Third line provides the revenue target
Output Minimum number of TVs the hospital needs to buy to meet its revenue target. If it cannot achieve its target, print the total number of rooms in the hospital. Test Case Example-1 : Input 20 1500 1000 7000000 Output 14 Explanation Using the formula, the number of patients on 1st Jan will be 39, on 2nd Jan will be 38 and so on. Considering there are only twenty rooms and rates of both type of rooms are 1500 and 1000 respectively, we will need 14 TV sets to get revenue of 7119500. With 13 TV sets Total revenue will be less than 7000000 Example-2 : Input 10 1000 1500 10000000 Output 10 Explanation

In the above example, the target will not be achieved, even by equipping all the rooms with TV. Hence, the answer is 10 i.e. total number of rooms in the hospital.

Solution in C:

#include <stdio.h>

#define mod 1000000007
#define MAX_LENGTH 1000

typedef long long int lld;

int isPalindrome(char str[], int start, int end) {
while (start < end) {
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Palindrome
}

int main() {
char s[MAX_LENGTH], s1[MAX_LENGTH], s2[MAX_LENGTH], s3[MAX_LENGTH];
scanf("%s", s);

int l = 0;
while (s[l] != '\0') {
l++;
}

for (int i = 1; i < l - 1; i++) {
for (int k = 0; k < i; k++) {
s1[k] = s[k];
}
s1[i] = '\0';

if (isPalindrome(s, 0, i - 1)) {
for (int j = 1; j < l - i; j++) {
for (int k = 0; k < j; k++) {
s2[k] = s[i + k];
}
s2[j] = '\0';

for (int k = 0; k < l - i - j; k++) {
s3[k] = s[i + j + k];
}
s3[l - i - j] = '\0';

if (isPalindrome(s2, 0, j - 1) && isPalindrome(s3, 0, l - i - j - 1)) {
printf("%s\n%s\n%s\n", s1, s2, s3);
return 0;
}
}
}
}

printf("Impossible\n");
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...