-1

I am making a set of pairs of Max and Min elements of Every Subset in an Array.But its giving me these errors. And at last I need Size of set. (Edited with some suggestions)

In function 'int main()':

27:12: error: 'max_element' was not declared in this scope

27:12: note: suggested alternative: 'max_align_t'

28:12: error: 'min_element' was not declared in this scopeIn function 'int main()':

Code:

#include <iostream>
#include <set>
#include <vector>
#include <utility>


typedef std::pair<int,int> pairs;

using namespace std;

int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    int n, max, min;
    
    set<pairs> s;
    
    cin >> n;

    int a[n];
    for(int i=0;i<n;i++) {
        cin >> a[i];
    }
    for(int i=0;i<n;i++) {
        for(int j=i;j<n;j++) {
            vector<int> v;
            v.push_back(a[j]);
            if(v.size() > 1) {
                max = *max_element(v.begin(),v.end());
                min = *min_element(v.begin(),v.end());
                pairs p1 = make_pair(max, min);
                s.insert(p1);
                max = 0; 
                min = 0;
            }
            
        }
    }
    cout << s.size() << endl;
    
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Dhondi Pranav
  • 85
  • 1
  • 6
  • 2
    If you're learning C++, get rid of `bits/stdc++`, it's a crutch, and instead `#include` the appropriate header for each Standard Library tool you use. In this case, `#include `, among other things for `ios_base`. Check with a [good reference](https://en.cppreference.com/w/cpp/utility/pair) to be sure you're doing it right. – tadman Dec 03 '20 at 09:18
  • 1
    https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. – ForceBru Dec 03 '20 at 09:18
  • 3
    A using-directive needs to occur before you can use any of the names it brings in. The remaining errors are "cascade error" from that. – molbdnilo Dec 03 '20 at 09:18
  • 2
    `int a[n]` with non-`const` `n` is non-standard C++. Consider using `std::vector` and `push_back` entries as you need to. You're already doing this in other places. – tadman Dec 03 '20 at 09:19
  • 1
    On a side note, using plural names for singular things is going to cause you much confusion in the future. – molbdnilo Dec 03 '20 at 09:21
  • 1
    your wrote far too much code before caring about the errors. The first line `typedef pair pairs;` should have been enough to stop and see that it cannot work that way. Don't write a wall of code to get a wall of errors, write one line to get one error, fix it, then write the next line – 463035818_is_not_an_ai Dec 03 '20 at 09:22
  • 1
    Note: you are only considering subsets from `i` to `n`. If it is what you want, then your O(n^2) implementation is not efficient. A simple O(n) implementation is possible. – Damien Dec 03 '20 at 09:25
  • @Damien could u please share that piece of code please ??? – Dhondi Pranav Dec 03 '20 at 09:30
  • 1
    @DhondiPranav `max_element` is declared in the header file `` You really should be capable of looking that up yourself. Programming C++ is going to be very hard if you can't reference basic information on the standard library for yourself. Try this [site](https://en.cppreference.com/w/) – john Dec 03 '20 at 09:45
  • I will try to propose something. but please try to not modify your first post. The correct existing answer becomes invalid, which is not fair. – Damien Dec 03 '20 at 09:48
  • @Damien sorry about that. – Dhondi Pranav Dec 03 '20 at 10:01

2 Answers2

3
typedef pair<int,int> pairs;

should be

typedef std::pair<int,int> pairs;

(Or you could move using namespace std; so that it is before your typedef).

Plus typedefing a single pair as the plural pairs is a really really bad idea, that is going to confuse you and anyone else reading your code for the rest of this programs existence. If you want a typedef for a pair of ints, then call it that

typedef std::pair<int,int> pair_of_ints;
john
  • 85,011
  • 4
  • 57
  • 81
1

To make your last programme works, it was needed to move the declaration of std::vector<int> v;

Moreover, your code has a complexity O(n^3). In practice, it is possible to get a complexity O(n^2), by calculating iteratively the max and min values.

This code compares your code and the new one. The results are identical. However, I cannot be sure that your original code does what you intended to do.

#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>

typedef std::pair<int,int> pairs;

//using namespace std;

void print (const std::set<pairs> &s) {
    for (auto& p: s) {
        std::cout << "(" << p.first << ", " << p.second << ") ";
    }
    std::cout << "\n";
}

int count_pairs_op (const std::vector<int>& a) {
    int max, min;
    int n = a.size();
    std::set<pairs> s;

   for(int i = 0; i < n; i++) {
       std::vector<int> v;
       for(int j = i; j < n; j++) {
            v.push_back(a[j]);
            if(v.size() > 1) {
                max = *std::max_element(v.begin(), v.end());
                min = *std::min_element(v.begin(), v.end());
                pairs p1 = std::make_pair(max, min);
                s.insert(p1);
            }
        }
    }
    print (s);  
    return s.size();
}

int count_pairs_new (const std::vector<int>& a) {
    int max, min;
    int n = a.size();
    std::set<pairs> s;

   for(int i = 0; i < n; i++) {
       min = a[i];
       max = a[i];
       for(int j = i+1; j < n; j++) {
            max = std::max (max, a[j]);
            min = std::min (min, a[j]);
            pairs p1 = std::make_pair(max, min);
            s.insert(p1);
        }
    }
    print (s);  
    return s.size();
}

int main() {

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int n;
    std::cin >> n;

    std::vector<int> a(n);
    for(int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    std::cout << count_pairs_op(a) << std::endl;
    std::cout << count_pairs_new(a) << std::endl;
    
}

It appears that there was a mistake in the understanding of the problem. For each subarray, we have to consider the maximum and the second maximum. Moreover, we know that all elements are distinct.
As the size can be up to 10^5, we have to look for a complexity smaller than O(n^2).

In practice, each element can be the second element of two subarrays, if there exist a greater element before and after it. We just have to check it.

This can be perfomed by calculating, for each index i, the maximum value before and after it.

Total complexity: O(n)


#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>

int count_pairs_2nd_max (const std::vector<int>& a) {
    int n = a.size();
    int count = 0;
    std::vector<int> max_up(n), max_down(n);
    max_up[0] = -1;
    for (int i = 1; i < n; ++i) {
        max_up[i] = std::max(max_up[i-1], a[i-1]);
    }
    max_down[n-1] = -1;
    for (int i = n-2; i >= 0; --i) {
        max_down[i] = std::max(max_down[i+1], a[i+1]);
    }
    
    for(int i = 0; i < n; ++i) {
        if (max_up[i] > a[i]) count++;
        if (max_down[i] > a[i]) count++;
    }
    return count;
}

int main() {

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int n;
    std::cin >> n;

    std::vector<int> a(n);
    for(int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    std::cout << count_pairs_2nd_max(a) << std::endl;
    
}
Damien
  • 4,809
  • 4
  • 15
  • 20
  • Thanks for ur help, but i already moved the vector above 1 loop before and tried. But my code is partially correct. It only executes partial test cases and gives TLE for few. I should try other aproach. Man really this is difficult problem. Please solve it and explain me if u have free time SIR!! – Dhondi Pranav Dec 03 '20 at 12:24
  • 1
    What the code is supposed to do is not clear. Do you have a link with deeper explanations? An example of input/output where the code fails? Is it a problem of false output or of time limit? – Damien Dec 03 '20 at 12:29
  • https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/little-shino-and-pairs/description/ is the link of problem. And my code does has both false output and TLE – Dhondi Pranav Dec 03 '20 at 12:33
  • n = 50 arr = 40 12 16 5 28 8 36 37 21 39 41 20 19 1 29 24 6 22 25 33 26 48 13 11 14 27 38 32 2 49 44 42 30 18 4 7 47 15 35 43 17 45 50 9 23 10 31 3 46 34 the correct output is 92 and mine gives 2. This is one of the input where its fails – Dhondi Pranav Dec 03 '20 at 12:36
  • 1
    I read the link. It seems you had a misunderstanding of the problem. `b` is not the minimum of the subarray, but the second maximum. Moreover, modifying your code or mine could solve the correctness issue, not the efficiency issue. The new problem is rather far from the original one in your post. I guess it could be better to ask a new question, with a new description of the problem, and a code attempting to solve this new problem. – Damien Dec 03 '20 at 12:47
  • 1
    If I am not wrong, your code fails too with the simpler `1 2 3 4 5` test case.The correct answer is 4, not 10. In the test case that you just provided, I don't expect your code to provide an answer of 2. You could check with the code I put in my answer – Damien Dec 03 '20 at 12:50
  • yes instead of min = the smallest; i need to code it as it get second largest element of the present in current vector. The question is how????? – Dhondi Pranav Dec 03 '20 at 13:07
  • I solved the problem successfull.... thanks. Now just need to optimize it to pass my TLE testcases. – Dhondi Pranav Dec 03 '20 at 13:32
  • I just completed the answer with a O(n) solution. Please let me know if it is OK now. – Damien Dec 03 '20 at 13:43
  • Ok pls share it as ans. Guess u r using a stack? – Dhondi Pranav Dec 03 '20 at 13:53
  • I don't understand. The code is already at the *end* of the current answer. – Damien Dec 03 '20 at 13:54
  • ohhh sry my bad – Dhondi Pranav Dec 03 '20 at 14:09
  • Could u pls explain me your approach? – Dhondi Pranav Dec 03 '20 at 14:20
  • 1
    For example, `max_up[i]` is the largest element for all indexes lower than `i`. If this value is larger than `a[i]`, then there exists a pair `(a[j], a[i])`, such that `j < i` and `a[j] > a[i]`. Then, there exists at least a subarray, from `j` to `i`, such that `a[i]` is the 2nd max element. Moreover, there exists at most one such valid subarray, such that `a[i]` is the 2nd max element. Therefore, `count++;` – Damien Dec 03 '20 at 14:28