Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
IPL Auctions are back. There are n players who will go under hammer in the sequence. There prices are given in a form an array in the order. A team wants to buy as much as player they can, but they have a condition that, they can only buy the current player if and only if the price of their last buyed player is less than the price of current player. Team can buy any player as their first player.

You need to find out the maximum player the team can buy following the above condition.

Input Format

First line of input contains an integer N, denoting the number of players in the auction.

Second line of input contains N space seperated integers denoting prices of players.

Constraints

1 ≤ N ≤ 10^5

0 ≤ A[i] ≤ 10^6

Output Format

The largest number of players they can buy

Sample Input :

6
5 8 3 7 9 1
Sample Output :
3

What I have tried:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n ; 
    cin>>n;
    int a[n];
    for(int i = 0;i<n;i++){
        cin>>a[i];
    }
    int mxcount = 0;
    for(int i = 0;i<n;i++){
        int  k = i;
         int count = 1;
        for(int  j = i;j<n;j++){
            
            if(a[j] > a[k]){
                count++;
                k++;
            }
        }
        mxcount = max(mxcount,count);
    }
    cout<<mxcount;
    return 0;
}
Posted
Updated 30-Jan-22 23:44pm
v4
Comments
Patrice T 31-Jan-22 3:47am    
What is the problem ?
Exceeded time limit or wrong answers.
Member 15518268 31-Jan-22 4:17am    
this approach is giving wrong answer
Patrice T 31-Jan-22 4:52am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You have a bug and a severe problem bug in your code. First you must allocate the array for the player dynamicly

C++
int *salaries = new int[n];
delete salaries;
More important is that you need to sort your array for smallest salaries before calculation. Read some sorting tutorial and implements it. When you sort you will change the order so it isnt clear which player are hired. So I would use an struct.
C++
struct Player {
int index;
int salary;
}
Tip: use speaking names for all coding elements to improve readability and write some test.
 
Share this answer
 
Quote:
What approach should I use in this question , the approach I used below is not getting accepted

Your code is simply wrong.
I fear you have to rethink your algorithm.
Try this inputs:
9
1 2 9 3 4 8 5 6 7
output should be 7

Try this inputs:
9
1 2 3 8 9 4 5 6 7
output should be 7


Try this inputs:
9
1 2 3 4 9 8 5 6 7
output should be 7

You may have to skip an arbitrary number of values in order to get desired result.

Understanding what is wrong in your code is the first step to correction.
 
Share this answer
 
Comments
CPallini 31-Jan-22 5:48am    
5.
Patrice T 31-Jan-22 5:49am    
Thank you.

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