No result illustrationNo result illustration

DevLoom

search

Lets Connect

chevron right
Menu
Home

Home

Community

Community

Blog Posts

Blog Posts

Programming

Programming

Tech Topics

Tech Topics

Explore

Explore

Find Jobs

Find Jobs

Tags

Tags

DevLoomPerfect Place for devs
    profile

    SHAIK JUBER AHMED

    upvoteupvote

    1

    downvote

    0

    star

    Accenture removing Duplicate Marks of First and Second Highest

    clock icon

    Asked 11 months ago

    message

    0Comments

    eye

    2 Views

    Let's start with a dry run of the logic:

    1. First, we sort the marks in descending order. This puts the highest marks at the beginning.
    2. We identify the highest mark (first element after sorting) and the second highest mark (first different mark we encounter).
    3. We count how many students have the highest mark and how many have the second highest mark.
    4. For the highest mark, we keep one student and remove the rest. Same for the second highest mark.
    5. The total number of removed students is our answer.
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int helper(vector<int> &marks) {
        // If there's only one student, no duplicates to remove
        if (marks.size() < 2) {
            return 0;
        }
        
        // Sort marks in descending order to easily find highest and second highest
        sort(marks.rbegin(), marks.rend());
        
        // The highest mark is now the first element
        int highest_mark = marks[0];
        int second_highest = -1;
        
        // Find the second highest mark
        for (int mark : marks) {
            if (mark < highest_mark) {
                second_highest = mark;
                break;
            }
        }
        
        // If all marks are the same, no need to remove anyone
        if (second_highest == -1) return 0;
        
        // Count students with highest and second highest marks
        int count_highest = 0;
        int count_second_highest = 0;
        
        for (int mark : marks) {
            if (mark == highest_mark) {
                count_highest++;
            } else if (mark == second_highest) {
                count_second_highest++;
            }
        }
        
        // Calculate students to remove:
        // For highest mark: remove all but one
        // For second highest: remove all but one (if more than one exists)
        int students_to_remove = 0;
        
        if (count_highest > 1) {
            students_to_remove += count_highest - 1;
        }
        if (count_second_highest > 1) {
            students_to_remove += count_second_highest - 1;
        }
        
        return students_to_remove;
    }
    
    int main() {
        vector<int> marks;
        int mark;
        
        // Read marks until a newline is encountered
        while (cin >> mark) {
            marks.push_back(mark);
            if (cin.get() == '\n')
                break;
        }
        
        // Print the number of students to remove
        cout << helper(marks) << endl;
        return 0;
    }
    Accenture
    DSA
    Contest
    medium

    Share

    Write your comment here !

    0 Responses