A tough TCS Codevita 2020?

This is My code what is wrong with this code ???
****** Minimum Sum *****

#include
#include<bits/stdc++.h>

using namespace std;

int minSum(vector& a, int k) {
priority_queue<int,vector> pq(a.begin(),a.end());
while(k–)
{
int p = pq.top();
pq.pop();
pq.push(p/2);
}

    int sum = 0;
    while(!pq.empty())
    {
        sum += pq.top();
        pq.pop();
    }
    return sum;
}

int main()
{
int n, k,arr;
cin>>n>>k;
vector v1;
for (int i=0;i<n;i++)
{
cin>>arr;
v1.push_back(arr);
}
int result=minSum(v1, k);
cout<<result;
return 0;
}

while(k--) not while(k–)

(double hyphen not single)

Its K-- code not paste correctly

1 Like

Is getting Presentation error less superior than AC? as I have solved 2 problems both with presentation error!

1 Like

presentation error means Its AC , dont worry, its given in their site somewhere, i came across once. It is considered as AC.

2 Likes

You have to maintain a priority queue for accessing the largest element in O(1) time. So initially, we have to enter all the integers in the priority queue. Now, we can minimize the sum to the least only by performing the operation (floor(x/2)) on the largest interger. So in each opeation, we first store the largest integer in a variable and pop the top interger. Then divide the variable by 2 and push the updated value back in the priority queue. Perform this operation K times. At last, pop the elements and get their minimized sum. (Overall complexity : O(nlogn)

And one more important thing…the sum should be stored in a long long int type variable otherwise it gives WA.

1 Like

No…they both are the same… nothing superior or inferior.

2 Likes

not necessarily … they can call u for interview even if u solve 1 question … depends on submission stats overall

OK , jesa theek lage :slight_smile:

In my set there was a problem “Count Pairs”, Has any one done that?
Rest were Minimize Sum(done), Constellations(done), Hedger was 01 knapsack problem on submitting it showed memory limit exceeded or TLE.

Mine as well.

I think this solution will also got accepted for minimize the sum

code

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main()
{
fastio;
ll n,s=0,i,k;
cin>>n>>k;
ll a[n];
for(i=0;i<n;i++)
cin>>a[i];

sort(a,a+n,greater<int>());
 i=0;
while(k--)
{
    if(a[i]>a[i+1])
    a[i]=a[i]/2;
    else
    {
        i++;
        a[i]=a[i]/2;
    }
}
for(i=0;i<n;i++)
s+=a[i];
cout<<s<<endl;

}

Do any of you know how many question sto solve for qualifying for second round

Do you have correct solution for the problem Hedger then it would be very kind of you if you share it.

Guys wanting questions can visit here

1 Like

As i said Hedger showed MLE when using efficient approach probably because range of W was in 10^6 if you would like to know then go through problem after reading 01 knapsack.

Can you share your MLE code? What I thought was the problem was variation of unbounded knapsack where the catch is we can take fixed K amount of any item and not infinite number of any item.Correct me if I am wrong.

Yes, we can take any stock depending on the profit it generates for <=K times such that our total cost doesn’t exceeds total amount we have that is W.I am not sure of credibility as i just passed sample cases but it may help you.
Take the code for reference:

def knapSack(W, wt, val, n):
K = [[0 for x in range(W + 1)] for x in range(n + 1)]

# Build table K[][] in bottom up manner
for i in range(n + 1):
    for w in range(W + 1):
        if i == 0 or w == 0:
            K[i][w] = 0
        elif wt[i - 1] <= w:
            K[i][w] = max(val[i - 1]
                          + K[i - 1][w - wt[i - 1]], K[i - 1][w])
        else:
            K[i][w] = K[i - 1][w]

return K[n][W]

tCases = int(input())
for _ in range(tCases):
n1, k1, W1 = map(int, input().split())
wt1 = list(map(int, input().split()))
profit = list(map(int, input().split()))
newWt = []
newVal = []
val1 = []
for i in range(n1):
tempVal = wt1[i]*profit[i]/100
val1.append(tempVal) #Profit list is made
for i in range(n1):
for j in range(k1):
newWt.append(wt1[i])
newVal.append(val1[i]) #append each value weight and profit K times
n1 *= k1
print(round(knapSack(W1, newWt, newVal, n1)))

1 Like
  1. code kart
  2. signal connection
  3. best sequence
  4. lift
  5. engagement ring
  6. hedger
    solved None…

can anyone please make me understand faulty keyboard question ?
I am confused with the given test cases .
| denotes cursor position-
my thought : T= empower , S=ew then , for me minimum operations will be : e|w (p+m)-> empow|(m)->empowe|(p+b)-> empower| ,
so total operations will be 5 , am i doing anything wrong ?