CodeVita Menu9>
- PrepInsta Home
- TCS CodeVita
- TCS CodeVita Previous Year Questions and Answers
- TCS CodeVita Syllabus
- TCS CodeVita Coding Questions
- TCS CodeVita Registration
- How To Prepare
- TCS CodeVita Exam Date
- Eligibility Criteria
- Interview Experiences
- Get Off-campus Hiring Updates
- Get Hiring Updates
- Contact us
- Prime Video
PREPINSTA PRIME
Minimize the sum problem (TCS Codevita)
Minimize the sum
Minimize the sum Problem is one of the question that was asked in previous year TCS Codevita competition. It is simple minimize sum problem where we have to find minimum sum by performing atmost k operations in an array. This is an article on Minimize the sum problem where we have to minimize the sum by performing at most k operations in an array.
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.
Java
Java
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int k = sc.nextInt ();
PriorityQueue < Integer > maxHeap =new PriorityQueue <> ((one, two)->two - one);
for (int i = 0; i < n; i++)
maxHeap.add (sc.nextInt ());
while (k > 0 && maxHeap.size () > 0)
{
int max = maxHeap.poll ();
maxHeap.add (max / 2);
k = k - 1;
}
int sum = 0;
while (maxHeap.size () > 0)
sum = sum + maxHeap.poll ();
System.out.println (sum);
}
}
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java
#include
long int i,j,k,a,b,sum=0,arr[10000000];
unsigned int arraymax();
int main()
{
printf(“enter the size of array and no of operations to be performed \n”);
scanf(“%ld””%ld”,&a,&b);
printf(“enter the array values\n”);
for(i=1;i<=a;i++)
{
scanf("%ld",&arr[i]);
}
for(j=1;j<=b;j++)
{
arraymax();
arr[1]=arr[1]/2;
}
for(i=1;i<=a;i++)
{
sum=sum+arr[i];
}
for(i=1;i<=a;i++)
{
printf("%ld\t",arr[i]);
}
printf("\n%ld",sum);
return 0;
}
unsigned int arraymax()
{
for (i=2;i<=a;i++)
{
if (arr[1] < arr[i])
{
x=arr[1];
arr[1] = arr[i];
arr[i]=x;
}
}
}
c++ solution for this problem
#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
priority_queuepq;
int n,k;
cin>>n>>k;
int arr[n];
int sum=0;
for(int i=0;i>arr[i];
pq.push(arr[i]);
}
while(k–)
{
int x=pq.top();
x=x/2;
pq.pop();
pq.push(x);
}
while(!pq.empty())
{
int y=pq.top();
sum+=y;
pq.pop();
}
cout<<sum<<endl;
}
return 0;
}