Reverse Gear | Code Vita 2015 | round 1

Reverse Gear

Problem Description:

        A futuristic company is building an autonomous car. The scientists at the company are training the car to perform Reverse parking. To park, the car needs to be able to move in backward as well as forward direction. The car is programmed to move backwards B meters and forwards again, say F meters, in a straight line. The car does this repeatedly until it is able to park or collides with other objects. The car covers 1 meter in T units of time. There is a wall after distance D from car's initial position in the backward direction.

The car is currently not without defects and hence often hits the wall. The scientists are devising a strategy to prevent this from happening. Your task is to help the scientists by providing them with exact information on amount of time available before the car hits the wall.
Input Format:

First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 4 values delimited by space
F B T D, where
  1. F denotes forward displacement in meters
  2. B denotes backward displacement in meters
  3. T denotes time taken to cover 1 meter
  4. D denotes distance from Car's starting position and the wall in backward direction

Output Format:

For each test case print time taken by the Car to hit the wall
Constraints:
First move will always be in backward direction
1 <= N <= 100
backward displacement > forward displacement i.e. (B > F)
forward displacement (F) > 0
backward displacement (B) > 0
time (T) > 0 
distance (D) > 0
All input values must be positive integers only

Sample Input and Output

SNo.InputOutput
1
2
6 9 3 18
3 7 5 20

162
220

Program:

#include <stdio.h>
int main() {
int f,b,d,t,sdist,sdisp,step,rd,td,tot,tt,test;
scanf("%d",&test);
while(test)
{
scanf("%d %d %d %d",&f,&b,&t,&d);
sdist=b+f;
sdisp=b-f;
step=(d-b)/sdisp;
if((d-b)%sdisp!=0)
step=step+1;
rd=d-(step*sdisp);
tot=(step*sdist)+rd;
tt=tot*t;
printf("%d\n",tt);
test--;
}
return 0;
}

Output:

2
6 9 3 18
3 7 5 20

162
220

You can also run it on an online IDE: https://ide.geeksforgeeks.org/S994F0QfMs
Your feedback and if you have any doubts it is welcomed! Do contact me or comment it below! Cheers!

Related Links: consecutive prime sum

Parallelograms | Code Vita 2018 | round 1


Parallelograms

Problem Description

A number (N) of lines (extending to infinity) in both directions are drawn on a plane. The lines are specified by the angle (positive or negative) made with the x axis (in degrees). It may be assumed that the different lines are not coincident (they do not overlap each other). The objective is to determine the number of parallelograms in the plane formed by these lines.
If the lines are given with an angle of 10, 70, 30 and 30, the figure looks like this
L1 is at 10 degrees to the x axis, L2 is at 70 degrees to the x axis, L3 and L4 are at 30 degrees to the x axis. It can be seen that there are no parallelograms formed by these lines

Constraints

N<=50
-89 <= angle for any line <=90

Input Format

The first line of the input consists of a single integer, N.
The next line consists of a series of comma separated integers (positive, zero or negative), each corresponding to a separate line, and giving the angle that it makes with the x axis (in degrees).

Output

The output is a single integer giving the number of parallelograms in the plane formed by the lines

Explanation

Example 1
Input
5
20,20,-20,-20,50
Output
1
Explanation
There are 5 lines (N=5), with angles at 20,20,-20,-20and 50 degrees with the x axis. The figure looks like this
There is one
Hence the output is 1.
Example 2
Input
6
20,20,-20,-20,50,50
Output
3
Explanation
There are 6 lines (N=6) with angles 20,20, -20,-20,50 and 50 degrees with the x axis.. The figure looks like this
There are three parallelograms formed by (L1, L2, L3, L4), (L1, L2, L5, L6) and (L3, L4, L5, L6). Hence the output is 3.

Program:

#include<stdio.h>
int pairfound(int angle[],int i,int n)
{
  int j;
  for(j=i+1;j<n;j++)
  {
    if(angle[i]==angle[j])
      return 1;
  }
  return 0;
}
int main()
{
  int n,i,count=0,pllgm,ang;
  scanf("%d",&n);
  int angle[n];
  for(i=0;i<n;i++)
  {
    scanf("%d,",&ang);
    if(ang>=-89&&ang<=90)
        angle[i]=ang;
  }
  for(i=0;i<n;i++)
  {
    if(pairfound(angle,i,n)==1)
    {
      count++;
    }
  }
  pllgm=(count-1)*count/2;
  printf("%d",pllgm);
  return 0;
}

Output

6
20,20,-20,-20,50,50
3
You can also try it in online IDE: https://ide.geeksforgeeks.org/vVEgz35Wso
Your comments and feedback are welcomed! You can contact me if you have any doubts! Cheers!
Related Links: Reverse Gear


Date Time | Code Vita 2018 | round 1


Date Time

Problem Description

Arun and his sister Usha are challenging each other with some mathematical puzzles. Usha, the cleverer one, has come up with the idea of givingArun 12 distinct digits from 0 to 9, and have him form the largest date time in 2018 with them. Arun is a little nervous, and asks you to help him with a computer program.
Usha will give Arun 12 distinct digits. He needs to create a date time combination in the year 2018: the date in the MM/DD form (all four digits must be present), and the time in the format HH:MM (all four digits must be present). The date may be from 01/01 to 12/31 and the time may be from 00:00 to 23:59 (in the 24 hour format). The digits provided may be used only once in the answer that Arun gives.
If more than one date time combination may be formed, Arun needs to give the latest valid date time possible in the year 2018.

Constraints

Single digits (any of 0-9)

Input Format

A line consisting of a sequence of 12 (not necessarily distinct) single digits (any of 0-9) separated by commas. The sequence will be non-decreasing.

Output

The maximum possible valid date time in the year 2018. The output must be in the format
MM/DD HH:MM
If no date time can be constructed, the output should be 0
 

Explanation

Example1 :
Input
0,0,1,2,2,2,3,5,9,9,9,9
Output
12/30 22:59
Explanation
The 12 digits to be used by Arun are given.
The maximum valid date time using only the digits given, and with each digit used at most once is
12/30 22:59
This is the output.
Example 2
Input
3,3,3,3,3,3,3,3,3,3,3,3
Output
0
Explanation
As no digit less than 3 is present in the input, a valid month cannot be formed. Hence no valid Date time can be formed with the input digits.

Program

#include<stdio.h>
int isLeapYear(int y)
{
    if(y % 4 == 0)
    {
        if(y % 100 == 0)
        {
            if(y % 400 == 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 1;
        }
    }
    
    return 0;
}

int is_valid_date_time(int date_time_arr[], int n)
{
    int month;
    if(n >= 2)
    {
        month = date_time_arr[0] * 10 + date_time_arr[1];
        if(!(month >= 1 && month <= 12))
        {
            return 0;
        }
    }
    
    if(n >= 4)
    {
        int days_in_each_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        days_in_each_month[1] += isLeapYear(2018);
        
        int day = date_time_arr[2] * 10 + date_time_arr[3];
        if(!(day >= 1 && day <= days_in_each_month[month - 1]))
        {
            return 0;
        }
    }
    
    if(n >= 6)
    {
        int hour = date_time_arr[4] * 10 + date_time_arr[5];
        if(!(hour >= 0 && hour <= 23))
        {
            return 0;
        }
    }
    
    if(n >= 8)
    {
        int min = date_time_arr[6] * 10 + date_time_arr[7];
        if(!(min >= 0 && min <= 59))
        {
            return 0;
        }
    }
    
    return 1;
}

int isFound = 0;
int max_time_in_2018[8] = {0};

int is_max(int inp_arr[])
{
    for(int i = 0; i < 8; i ++)
    {
        if(max_time_in_2018[i] == inp_arr[i])
        {
            continue;
        }
        else if(max_time_in_2018[i] >inp_arr[i])
        {
            return 0;
        }
        else 
        {
            return 1;
        }
    }
    
    return 0;
}

void time_computing(int inp_arr[], 
                    int date_time_arr[], int sel_cnt,
                    int used[])
{
    if(!is_valid_date_time(date_time_arr, sel_cnt)) 
    {
        return;
    }
    
    if(sel_cnt == 8)
    {
        if(is_valid_date_time(date_time_arr, sel_cnt))
        {
            isFound = 1;
            if(is_max(date_time_arr))
            {
                for(int i = 0; i < 8; i++)
                {
                    max_time_in_2018[i] = date_time_arr[i];
                }
            }
        }
        
        return;
    }
    
    // Select current item
    for(int i = 0; i< 12; i++)
    {
        if(used[i] == 1)
        {
            continue;
        }
        
        date_time_arr[sel_cnt] = inp_arr[i];
        used[i] = 1;
        time_computing(inp_arr,date_time_arr, sel_cnt + 1, used);
        used[i] = 0;
    }              
}

int main()
{
    int inp_arr[12], date_time_arr[8];
    scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
          &inp_arr[0],&inp_arr[1],&inp_arr[2],&inp_arr[3],
          &inp_arr[4],&inp_arr[5],&inp_arr[6],&inp_arr[7],
          &inp_arr[8],&inp_arr[9],&inp_arr[10],&inp_arr[11]);

    int used[12] = {0};
    
    time_computing(inp_arr, date_time_arr, 0, used);
    if(isFound == 1)
    {
        printf("%d%d/%d%d %d%d:%d%d",
         max_time_in_2018[0],max_time_in_2018[1],max_time_in_2018[2],max_time_in_2018[3],
         max_time_in_2018[4],max_time_in_2018[5],max_time_in_2018[6],max_time_in_2018[7]);
    }
    else
    {
        printf("0");
    }
    
    return 0;
}

Output

0,0,1,2,2,2,3,5,9,9,9,9

12/30 22:59

You can also run it on an online IDE: https://ide.geeksforgeeks.org/XZImlFQrQa

Your comments and feedback are welcomed!! If you have any doubts do contact me! Cheers!

Related Links: Parallelograms 2018

Super Ascii - Code Vita 2014 | round 2

Super Ascii


Problem Decription:

In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.

In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26.

Your task is to find out whether the given string is a super ascii string or not.

Input Format:


First line contains number of test cases T, followed by T lines, each containing a string "S".

Output Format:


For each test case print "Yes" if the String "S" is super ascii, else print "No"
Constraints:

1<=T<=100
1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').

Sample Input and Output

SNo.InputOutput
1
2
bba
scca

Yes
No



Program:

#include <stdio.h>
int main() {
    char s[30];
    int i,num[30]={0},isascii,n;
    scanf("%d",&n);
    while(n--)
    {
    scanf("%s",s);
    i=0;
    isascii=1;
    while(s[i]!='\0')
    {
        if((s[i]>='a')&&(s[i]<='z'))
        num[s[i]-'a']++;
        s[i]='\0';
        i++;
    }
    for(i=0;i<26;i++)
    {
        if((num[i]>0)&&(num[i]!=(i+1)))
        isascii=0;
        num[i]=0;
    }
    if(isascii)
    printf("yes\n");
    else
    printf("no");
    }
    return 0;
}

Output:

2
bba
scca

yes
no

You can also run it on the online IDE: https://ide.geeksforgeeks.org/q64aeIcLxM

Your feedback and comments are welcomed! If you have an doubt can contact me or comment below! Cheers!

Related Link: Date Time 2018

Codu And Sum Love 2018

Codu And Sum Love

Problem Description

```
Scanner sc = new Scanner(System.in);
long sum = 0;
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
final long x = sc.nextLong(); // read input
String str = Long.toString((long) Math.pow(1 << 1, x));
str = str.length() > 2 ? str.substring(str.length() - 2) : str;
sum += Integer.parseInt(str);
}
System.out.println(sum%100);
```
Given N number of x’'s, perform logic equivalent of the above Java code and print the output

Constraints

 1<=N<=10^7 0<=x<=10^18

Input Format

 First line contains an integer N
Second line will contain N numbers delimited by space

Output

 Number that is the output of the given code by taking inputs as specified above
 

Explanation

Example 1
 
Input
 4 8 6 7 4
 Output
 64
Example 2
Input
3
1 2 3
Output
14

Program

#include <stdio.h>
long power(long k)
{
    long i,j=1;
    for(i=1;i<=k;i++)
        j=j*2;
        return j;
}
long length(long k)
{
    long ct=0,n1;
    while(k)
    {
        ct++;
        k=k/10;
    }
    return ct;
}
long reduce(long p1,long r1)
{
    long ct=0,k,cnt,rev=0,a[1000],h=0,i,sum1=0;
    while(p1)
    {
      k=p1%10;
      a[h]=k;
      h++;
      p1=p1/10;
     }
      for(i=1;i>=0;i--)
      sum1=sum1*10+a[i];
    return sum1;
}
int main() {
 long x,n,sum=0,r[70],p,c,f=0,i;
 scanf("%ld",&n);
 for(i=0;i<n;i++)
 {
    scanf("%ld",&x);
    p=power(x);
    c=length(p);
    if(c>2)
    {
    r[f++]=reduce(p,c);
    }
    else
    r[f++]=p;
 }
 for(i=0;i<n;i++)
 
 sum=sum+r[i];
   printf("%ld",(sum%100));
 
 
 return 0;
}

Output:

4 8 6 7 4
64


You can also try running it on online IDE: https://ide.geeksforgeeks.org/G3gXokhTbW

Your doubts and feedback are welcomed! you can comment it below Cheers!

Related Link: Super Ascii

Bride Hunting -Code Vita 2018 | round 1 | TCS Code Vita Solution 2018 | Bride Hunting TCS Code Vita solution


Bride Hunting

Problem Description

Sam is an eligible bachelor. He decides to settle down in life and start a family. He goes bride hunting.
He wants to marry a girl who has at least one of the 8 qualities mentioned below:-
1) The girl should be rich.
2) The girl should be an Engineer/Doctor.
3) The girl should be beautiful.
4) The girl should be of height 5.3".
5) The girl should be working in an MNC.
6) The girl should be an extrovert.
7) The girl should not have spectacles.
8) The girl should be kind and honest.
He is in search of a bride who has some or all of the 8 qualities mentioned above. On bride hunting, he may find more than one contenders to be his wife.
In that case, he wants to choose a girl whose house is closest to his house. Find a bride for Sam who has maximum qualities. If in case, there are more than one contenders who are at equal distance from Sam’'s house; then
print "“Polygamy not allowed”".
In case there is no suitable girl who fits the criteria then print “"No suitable girl found"
Given a Matrix N*M, Sam's house is at (1, 1). It is denoted by 1. In the same matrix, the location of a marriageable Girl is also denoted by 1. Hence 1 at location (1, 1) should not be considered as the location of a marriageable Girl’s location.
The qualities of that girl, as per Sam’'s criteria, have to be decoded from the number of non-zero neighbors (max 8-way) she has. Similar to the condition above, 1 at location (1, 1) should not be considered as the quality of a Girl. See Example section to get a better understanding.
Find Sam, a suitable Bride and print the row and column of the bride, and find out the number of qualities that the Bride possesses.
NOTE: - Distance is calculated in number of hops in any direction i.e. (Left, Right, Up, Down and Diagonal)
Constraints
2 <= N,M <= 10^2
Input Format
First Line contains the row (N) and column (M) of the houses.
Next N lines contain the data about girls and their qualities.
Output
It will contain the row and column of the bride, and the number of qualities that Bride possess separated by a colon (i.e. :).
Explanation
Example 1
Input:
2 9
1 0 1 1 0 1 1 1 1
0 0 0 1 0 1 0 0 1
Output:
1:7:3
Explanation:
The girl and qualities are present at (1,3),(1,4),(1,6),(1,7),(1,8),(1,9),(2,4),(2,6),(2,9).
The girl present at (1,3) has 2 qualities (i.e. (1,4)and (2,4)).
The girl present at (1,4) has 2 qualities.
The Bride present at (1,6) has 2 qualities.
The Bride present at (1,7) has 3 qualities.
The Bride present at (1,8) has 3 qualities.
The Bride present at (1,9) has 2 qualities.
The Bride present at (2,4) has 2 qualities.
The Bride present at (2,6) has 2 qualities.
The Bride present at (2,9) has 2 qualities.
As we see, there are two contenders who have maximum qualities, one is at (1,7) and another at (1,8).
The girl who is closest to Sam's house is at (1,7). Hence, she is the bride.
Hence, the output will be 1:7:3.
Example 2
Input:
6 6
1 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 0 0 0 0
Output:
4:4:8
Explanation:
The bride and qualities are present at (3,3),(3,4),(3,5),(4,3),(4,4),(4,5),(5,3),(5,4),(5,5)
The Bride present at (3,3) has 3 qualities (i.e. (3,4),(4,3) and (4,4)).
The Bride present at (3,4) has 5 qualities.
The Bride present at (3,5) has 3 qualities.
The Bride present at (4,3) has 5 qualities.
The Bride present at (4,4) has 8 qualities.
The Bride present at (4,5) has 5 qualities.
The Bride present at (5,3) has 3 qualities.
The Bride present at (5,4) has 5 qualities.
The Bride present at (5,5) has 3 qualities.
As we see, the girl present in (4,4) has maximum number of Qualities. Hence, she is the bride.
Hence, the output will be 4:4:8.

Solution in C:
 

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

// Function to check if given indices are valid in the matrix
int isValid(int row, int col, int N, int M) {
return (row >= 0 && row < N && col >= 0 && col < M);
}

// Function to count the number of qualities a girl possesses
int countQualities(int matrix[][100], int row, int col, int N, int M) {
int count = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (isValid(i, j, N, M) && !(i == row && j == col) && matrix[i][j] == 1) {
count++;
}
}
}
return count;
}

// Function to find the suitable bride for Sam
void findBride(int matrix[][100], int N, int M) {
int minDistance = N + M; // Initialize to maximum possible distance
int maxQualities = 0;
int brideRow = -1, brideCol = -1;

// Loop through the matrix to find the bride with maximum qualities
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] == 1) {
int qualities = countQualities(matrix, i, j, N, M);
if (qualities > maxQualities) {
maxQualities = qualities;
brideRow = i;
brideCol = j;
} else if (qualities == maxQualities) {
// If multiple brides have the same number of qualities,
// find the one with minimum distance to Sam's house
int distance = abs(0 - i) + abs(0 - j);
if (distance < minDistance) {
minDistance = distance;
brideRow = i;
brideCol = j;
} else if (distance == minDistance) {
printf("Polygamy not allowed\n");
return;
}
}
}
}
}

// If no suitable bride found
if (brideRow == -1 || brideCol == -1) {
printf("No suitable girl found\n");
} else {
printf("%d:%d:%d\n", brideRow + 1, brideCol + 1, maxQualities);
}
}

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

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

findBride(matrix, N, M);

return 0;
}


Alternate Solution (Brute Force):

#include<stdio.h>

int main()

{

int n,m,i,g[50][50],j,p,q,max=0,cnt=0,k=1,c=0,u=1,x[30],y[30],t1,min=0, sc[50],e,f,ct=0,a[50],count=0,t2=0,t=0;

scanf("%d %d",&n,&m);

for(i=1;i<=n;i++)

{

for(j=1;j<=m;j++)

{

scanf("%d",&g[i][j]);

} }

g[1][1]=0;

for(i=1;i<=n;i++)

{

for(j=1;j<=m;j++)

{ cnt=0;

if(g[i][j]==1)

{

t++;

for(p=i-1;p<=i+1;p++)

{

for(q=j-1;q<=j+1;q++)

{

if(g[p][q]==1)

{ cnt++;

} } }

cnt=cnt-1;

a[k]=cnt;

k++;

} } }

for(k=1;k<=t;k++)

{ if(a[k]>max) max=a[k];

}

if(max==0)

{ printf("No suitable girl found"); goto x; }

for(k=1;k<=t;k++)

{ if(a[k]==max)

c++; }

for(k=1;k<=t;k++)

{ t2=0;

if(a[k]==max)

{ for(i=1;i<=n;i++)

{ for(j=1;j<=m;j++)

{ if(g[i][j]==1) t2++;

if(t2==k)

{ x[u]=i; y[u]=j; u++;

} } } } }

t1=u-1;

if(c==1)

printf("%d:%d:%d",x[1],y[1],max);

else

{ for(u=1;u<=t1;u++)

{ e=x[u]-1;

f=y[u]-1;

if(e>=f)

{ sc[u]=e;

}

else sc[u]=f;

}

min=sc[1];

for(u=1;u<=t1;u++)

{ if(sc[u]<min) min=sc[u];

}

for(u=1;u<=t1;u++)

{ if(sc[u]==min) count++;

}

if(count>1)

printf("Polygamy not allowed");

if(count==1)

{ for(u=1;u<=t1;u++)

{ if(sc[u]==min)

printf("%d:%d:%d",x[u],y[u],max);

} } }

x: return 0;

}






OUTPUT:
6 6
1 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 0 0 0 0

4:4:8

Hey guys you can run this program in the online ide in this link: 


Your feedback are welcomed and can comment or post your doubts in the comment Cheers!

Related Links: 

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