Click here to Skip to main content
15,920,109 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Micro purchased an array A having N integer values. After playing it for a while, he got bored of it and decided to update value of its element. In one second he can increase value of each array element by 1. He wants each array element's value to become greater than or equal to K. Please help Micro to find out the minimum amount of time it will take, for him to do so.

Input:
First line consists of a single integer, T, denoting the number of test cases.
First line of each test case consists of two space separated integers denoting N and K.
Second line of each test case consists of N space separated integers denoting the array A.

Output:
For each test case, print the minimum time in which all array elements will become greater than or equal to K. Print a new line after each test case.

What I have tried:

C++
#include <iostream>

using namespace std;

int add(int a[], int n, int k)
{    
    int count=0;
    int p;
    for(int i=0; i<n; i++)
        {
            a[i]=a[i]+1;
            count++;
        }
      p=min(a, n);
    if(p>=k)
    {
        return count;
    }
    else
    {
        add(a, n, k);
    }
    
}
int min(int a[], int n)
{
    int m=1000;
    for(int i=0;i<n; i++)
    {
        if(a[i]<m)
        {
        m=a[i];
        }
    }
    return m;
}
int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
    
        int ans;
        cin>>n;
        cin>>k;
        int a[n];
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
        }
    ans= add(a, n, k);
    cout<<ans<<"\n";
}
return 0;
}
Posted
Updated 17-Aug-21 20:34pm
v2
Comments
KarstenK 17-Jun-18 14:11pm    
add some user output and debugging messages. Than use the debugger.

The only problem I can spot - and you can too by reading the compiler errors - is the order of the functions...
add calls min, but at that point min not defined yet...
The immediate solution is to move min up, before add...
For long term read this: Functions - C++ Tutorials[^]
 
Share this answer
 
Comments
Member 13875519 17-Jun-18 5:53am    
Thank you @Kornfeld, now my code is compiled successfully but there is a problem with my logic, can you help me with this?
Kornfeld Eliyahu Peter 17-Jun-18 5:58am    
No. As far as I can see (took your code into GNU's C++ compiler) the code does not compile...
However, to help with logical issued you have to give us more clues: What is the input? What is the expected output? What is the real output? Have you used your debugger?
Member 13875519 17-Jun-18 6:09am    
Sample input     Sample expected output              Real Output
2                 3                                  3
3 4               0                                  3
1 2 5
3 2
2 5 5
Kornfeld Eliyahu Peter 17-Jun-18 6:51am    
Unclear... You have 3 expected input values (t, n and k), but in the table above I can't see it...
Member 13875519 17-Jun-18 7:00am    
read the question posted above ...you will understand how the inputs are arranged
How do you expect us to "tell you the problem"? We have no idea what data you gave it, what results you got, or what values you expected.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the function, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
You've had some advice on how to be more specific in your questions so I won't spend any time there. What I'd like to check is the logic of your solution. Micro has an array and he wants to increase the value of each element until that element is >= k and it will take one second for each increment. So the answer to amount of time for each element is k - a[i] seconds. You don't have to actually increase the element and track a count. You just have to return how much time it will take.
Perhaps I'm misreading the question. It says that in 1 second he can increase the value of each array element. It doesn't say that he must increase all elements by an equal amount.
The next thing I wonder about is why the function add is calling the function add recursively? If p < k it doesn't need to start over again with a, n, and k, it just needs to continue counting the seconds until it gets to the end of the array and return the total seconds.
int sec = 0;
for (int i=0; i<n; i++)
{
    if (a[i] < k) {
        sec += k - a[i];
    }
}
return sec;
sec is the number of seconds it will take to make sure each element is at least = k.

Let's say I'm wrong about my interpretation and you are supposed to increment each value an equal amount. In that case it'll be the time it takes to increment the smallest element to equal K times the number of elements in the array. Your function 'min' will fail for an array of which all values are > 1000. I can't remember what constants are available at this low level -- is MAXINT available? To make it absolutely 100% bullet proof you need to start with a number that it the maximum amount the storage will allow. The rest of the logic there seems fine. The calculation would be
k - min(a, n) * n;
That would be how many seconds it would take to increase all elements such that the smallest one is at least == k.

I'm open to comments of course. Is the logic here sound?

Mike

[update] thought of a flaw in my logic. If any elements are > k then the 'sec' value would be reduced because k - a[i] would result in a negative value. Added 'if' check.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900