You are given an array of integer elements. A pair of elements is said to be an inversion if and for all
Using this array , you create an undirected graph of vertices. For each inversion pair , there exists an edge between vertex and .
Find the maximum size of set consisting of vertices from graph such that there exists an edge between every pair of vertices present in .
Example:
Input: n=4, a[] = { 4, 3, 2, 1 }
Output: 4
Approach
Java
import java.util.Arrays;public class MaximumSet {public static void main(String[] args) {int n = 4;int[] a = { 4, 3, 2, 1 };int lisLength = LIS(a, n);System.out.println(lisLength);}static int findInd(int a[], int l, int r, int key) {while (r - l > 1) {int mid = l + (r - l) / 2;if (a[mid] >= key)r = mid;elsel = mid;}return r;}static int LIS(int a[], int size) {Arrays.sort(a);int[] arr = new int[size];int len;arr[0] = a[0];len = 1;for (int i = 1; i < size; i++) {if (a[i] < arr[0])arr[0] = a[i];else if (a[i] > arr[len - 1])arr[len++] = a[i];elsearr[findInd(arr, -1, len - 1, a[i])] = a[i];}return len;}}
No comments:
Post a Comment