1497A - Meximization - CodeForces Solution


brute force data structures greedy sortings *800

Please click on ads to support us..

Python Code:

numTests = int(input())

for i in range(numTests):
    numNumber = int(input())
    seq = list(map(int, input().split()))
    seq.sort()
    lastNum = -1
    seqMex = []
    rest = []
    for j in range(len(seq)):
        actual = seq[j]
                if(lastNum != actual):
            seqMex.append(actual)
                else:
            rest.append(actual)
        lastNum = actual
        for k in range(len(seqMex)):
                if(k > 0):
            print(" ", end="")
        print(seqMex[k], end="")
    for l in range(len(rest)):
        print(" ", end="")
        print(rest[l], end="")
    print("")
	  			   	 	 	      		 	 	    	

C++ Code:

#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <algorithm>


int main(){
    int t;
    cin >> t;
    while(t--){
        int n,a;
        cin >> n;
        set<int> s;
        vector<int> v;
        for(int i = 0; i < n; i++){
            cin >> a;
            if(s.find(a) == s.end()) s.insert(a);
            else v.push_back(a);
        }
        for(auto it:s){
            cout << it << " ";
        }
        sort(v.begin(),v.end());
        for(auto it: v){
            cout << it << " ";
        }
        cout << endl;
    }
}


Comments

Submit
0 Comments
More Questions

1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections
1598C - Delete Two Elements
1400C - Binary String Reconstruction
1734D - Slime Escape
1499A - Domino on Windowsill
991A - If at first you don't succeed
1196C - Robot Breakout
373A - Collecting Beats is Fun
965A - Paper Airplanes
863E - Turn Off The TV
630E - A rectangle
1104A - Splitting into digits