Wednesday, June 29, 2022

Pair Em Up (Contest)

 Pair Em Up (Contest)

Problem Statement
Given an array of N elements where N is even. You have to pair up the elements into N/2 pairs such that each element is in exactly 1 pair. You need to find minimum possible X such that there exists a way to pair the N elements and for no pair sum of its elements is greater than X.

import java.io.*; // for handling input/output

import java.util.*; // contains Collections framework
class Main{
public static void main(String args[]){
int n;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int a[] = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);
        int maxi = -1;
        for(int i=0; i<n; i++) {
            int cur = a[i] + a[n-i-1];
            if(maxi<cur)
                maxi = cur;
        }
        System.out.println(maxi);
        }
}

No comments:

Post a Comment

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...