Showing posts with label code vita. Show all posts
Showing posts with label code vita. Show all posts

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


Minimum Gift - CodeVita 9 | Zonal Round

 Minimum Gift

A Company has decided to give some gifts to all of its employees. For that, company has given some rank to each employee. Based on that rank, company has made certain rules to distribute the gifts.

The rules for distributing the gifts are:

Each employee must receive at least one gift.

Employees having higher ranking get a greater number of gifts than their neighbours.

What is the minimum number of gifts required by company?

Constraints

1 < T < 10

1 < N < 100000

1 < Rank < 10^9

Input

First line contains integer T, denoting the number of test cases.

For each test case:

First line contains integer N, denoting the number of employees.

Second line contains N space separated integers, denoting the rank of each employee.

Output

For each test case print the number of minimum gifts required on new line.

Example 1

Input

2

5

1 2 1 5 2

2

1 2

Output

7

3

Explanation

For testcase 1, adhering to rules mentioned above,

Employee # 1 whose rank is 1 gets one gift

Employee # 2 whose rank is 2 gets two gifts

Employee # 3 whose rank is 1 gets one gift

Employee # 4 whose rank is 5 gets two gifts

Employee # 5 whose rank is 2 gets one gift

Therefore, total gifts required is 1 + 2 + 1 + 2 + 1 = 7

Similarly, for testcase 2, adhering to rules mentioned above,

Employee # 1 whose rank is 1 gets one gift

Employee # 2 whose rank is 2 gets two gifts

Therefore, total gifts required is 1 + 2 = 3

Program:

#include<stdio.h>

long int arr[100010];

long int brr[100010];

int main()

{

  int test_case;

  scanf("%d",&test_case);

  for(int i = 1; i <= test_case; i++)

  {

    int n;

    long int gift = 0, temp = 0;

    scanf("%d",&n);

    for(int i = 0; i < n; i++)

    {

        scanf("%ld",&arr[i]);

    }

    brr[0] = 1;

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

    {

      if(arr[i] > arr[i-1])

      {

        brr[i] = brr[i-1] + 1;

      }

      else

      {

        brr[i] = 1;

      }

    }

    gift = brr[n-1];

    for(int i = n-2; i >= 0; i--)

    {

      if(arr[i] > arr[i+1])

      {

        temp = brr[i+1] + 1;

      }

      else

        temp = 1;

      gift = gift + ((temp > brr[i]) ? temp : brr[i]);

      brr[i] = temp;

    }

    printf("%ld\n",gift);

  }

  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:




Constellation - CodeVita 9 | Zonal Round

Constellation 

 Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in shape of vowels { A, E, I, O, U } . A collection of * in the shape of the vowels is a star. A star is contained in a 3x3 block. Stars cannot be overlapping. The dot(.) character denotes empty space. 

 Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them. 

 Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples section below. 

 Constraints 

 3 <= N <= 10^5 

 Input

 Input consists of single integer N denoting number of columns. 

 Output 

 Output contains vowels (stars) in order of their occurrence within the given galaxy. Galaxy itself is represented by # character. 

Example 1:

Input:

18

* . * # * * * # * * * # * * * . * .

* . * # * . * # . * . # * * * * * *

* * * # * * * # * * * # * * * * . *

Output: 

U#O#I#EA

Example 2:

Input:

12

* . * # . * * * # . * .

* . * # . . * . # * * *

* * * # . * * * # * . *
Output:

U#I#A

Program:

#include <stdio.h>
int main()
{
  int n,x1,y1;
  scanf("%d",&n);
  char x[3][n];
  for(int i=0;i<3;i++)
{
    for(int j=0;j<n;j++)
{
      scanf("%s",&x[i][j]);
    }
  }
  for(int i=0;i<n;i++)
  {
    if(x[0][i]=='#' && x[1][i]=='#' && x[2][i]=='#')
{
      printf("#");
    }
else if(x[0][i]=='.' && x[1][i]=='.' && x[2][i]=='.')
{}
else
{
      char a,b,c,a1,b1,c1,a2,b2,c2;
      x1 = i;
      a = x[0][x1];
      b = x[0][x1+1];
      c = x[0][x1+2];
      a1 = x[1][x1];
      b1 = x[1][x1+1];
      c1 = x[1][x1+2];
      a2 = x[2][x1];
      b2 = x[2][x1+1];
      c2 = x[2][x1+2];
      if(a=='.' && b=='*' && c=='.' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='.' && c2=='*')
 
      printf("A");
        i = i + 2;
      }
      if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
 
        printf("E");
        i = i + 2;
      }
      if(a=='*' && b=='*' && c=='*' && a1=='.' && b1=='*' && c1=='.' && a2=='*' && b2=='*' && c2=='*')
 
        printf("I");
        i = i + 2;
      }
      if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
 
        printf("O");
        i = i + 2;
      }
      if(a=='*' && b=='.' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
 
          printf("U");
        i = i + 2;
      }
    }
  }

}

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:

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

Minimum Product Array



         Minimum Product Array
TCS codevita 2016 round 1:         The task is to find the minimum sum of Products of two arrays of the same size, given that k modifications are allowed on the first array. In each modification, one array element of the first array can either be increased or decreased by 2.Note- the product sum is Summation (A[i]*B[i]) for all i from 1 to n where n is the size of both arrays.
Input Format:          First line of the input contains n and k delimited by white space Second line contains the Array A (modifiable array) with its values delimited by spaces Third line contains the Array B (non-modifiable array) with its values delimited by spaces.
Output Format:
Output the minimum sum of products of the two arrays.
Constraints:
1 ≤ N ≤ 10^50 ≤ |A[i]|, |B[i]| ≤ 10^50 ≤ K ≤ 10^9
Sample       Input                   Output
1.                3 5                            -31
                   1 2 -3
                  -2 3 -5
2.               5 3                              25
                  2 3 4 5 4
                  3 4 2 3 2
Explanation for sample 1:
  Here total numbers are 3 and total modifications allowed are 5. So we modified A[2], which is -3 and increased it by 10 (as 5 modifications are allowed). Now final sum will be (1 * -2) + (2 * 3) + (7 * -5) -2 + 6 - 35 -31
-31 is our final answer.
Explanation for sample 2:
  Here total numbers are 5 and total modifications allowed are 3. So we modified A[1], which is 3 and decreased it by 6 (as 3 modifications are allowed). Now final sum will be (2 * 3) + (-3 * 4) + (4 * 2) + (5 * 3) + (4 * 2) 6 - 12 + 8 + 15 + 8 25
25 is our final answer. 
PROGRAM:
#include<stdio.h>
int main()
{
 int m[10],i,min,max,k,n,p=1,sum=0,u[10],mins;
 scanf("%d",&n);
 scanf("%d",&k);
 for(i=0;i<n;i++)
 scanf("%d",&u[i]);
 for(i=0;i<n;i++)
 scanf("%d",&m[i]);
 min=m[0];
 max=m[0];
 for(i=0;i<n;i++)
 {
  if(m[i]<min)
  min=m[i];
  if(m[i]>max)
  max=m[i];
 }
 if (min<0&&max<0)
 mins=min<max?min:max;
 else if(min>0&&max>0)
 mins=max>min?max:min;
 else
 {
 if((min-max)<-(2*min))
  mins=min;
  else 
  mins=max;
 }
 for(i=0;i<n;i++)
 {
  if(mins==m[i]&&mins>0)
   {
u[i]=u[i]-(2*k);
   goto x;
   }
   if(mins==m[i]&&mins<0)
    { 
    u[i]=u[i]+(2*k);
    goto x;
    }
  }
x:for(i=0;i<n;i++)
 {
 p=u[i]*m[i];
 sum=sum+p;
 }
 printf("%d",sum);
 return 0;
 
}


OUTPUT:
5 3                              
2 3 4 5 4
3 4 2 3 2

25
You can directly run it on a IDE: https://ide.geeksforgeeks.org/lP9ISaZ9yx
You can comment your feedback and doubts if any cheers!
Related links: Zombie World

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