April 2019

Don't Do Shit Unless

Solution

Implementation


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


int main() 
{
   
    int n,m;
    cin>>n>>m;
    int notes[n];
    long odd_m=-1,even_m=-1,sum=0;
    for(int i=0;i<n;i++)
        cin>>notes[i];
    // sort the array as we need minimum sum
    sort(notes,notes+n);
//find the sum of first m elements in the array.Find the largest odd and even elements in the first m elements int the //array
    
    for(int i=0;i<m;i++)
    {
        sum+=notes[i];
        if(notes[i]%2==1)
            odd_m = notes[i];
        else
            even_m = notes[i];
        
    }
    
    if(sum%2==0) // if sum is even, then we have already found the correct answer, print it and exit
    {
        cout<<sum<<endl;
        exit(0);
    }
    
    int odd_next=-1,even_next=-1; // find the smallest even and odd elements AFTER the first m elements in the array
    
    for(int i=m;i<n;i++)
    {
        if(notes[i]%2==1 && odd_next==-1)
            odd_next = notes[i];
        else if(notes[i]%2==0 && even_next==-1)
            even_next = notes[i];
        
    }
    long diff = 0; // mini difference of even element and odd element to be added to the sum to make it even
    
    if(odd_m!=-1 && even_next!=-1) 
        diff = even_next - odd_m;
    
    if(even_m!=-1 && odd_next!=-1)
    {
        if(diff!=0)
            diff = min(diff,odd_next-even_m);
        else
            diff = odd_next-even_m;
    }
    
    sum = sum+diff;
    
    if(sum%2==1)
        cout<<-1;
    else
        cout<<sum;
    
    
    
    
    return 0;
}


$Time$ $complexity$: $O(n)$ $Space$ $Complexity$ : $O(n)$