Generate Lexicographically Largest Array through Even Swaps
Last Updated :
16 Jan, 2024
Given an array a[] and you need to generate an array b[]. You are allowed to apply only one type of operation on the array a[], any number of times. In one operation you can swap ai with ai+1 only if ai+ai+1 is even. Array b[] thus generated by applying the given operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Examples:
Input: N = 3, a[] = {1, 3, 5}
Output: 5, 3, 1
Explanation: [1, 3, 5], [1, 5, 3], [3, 1, 5], [3, 5, 1], [5, 1, 3] and [5, 3, 1] are all possible values of array b while the last one is lexicographically largest.
Input: N = 4, a[] = {1, 3, 4, 2}
Output: b[] = {3, 1, 4, 2}
Explanation: [1, 3, 4, 2], [1, 3, 2, 4], [3, 1, 2, 4] and [3, 1, 4, 2] are all possible values of b among which the last one is lexicographically largest one.
Approach: To solve the problem follow the below observations:
Observations:
- Basically separating segments on the basis of same parity and as in same parity segments we can do any number of swaps so sorting it in decreasing order will give us the lexicographically largest array.
- Also we know that the summation of two even numbers i.e. even +even=even and odd+even=odd so this will give us two separate segments and odd+ odd =even so these numbers will be in same segments. that’s why we are separating the segments on the basis of same parity.
Step-by-step approach:
- Initialize an index variable i to 0.
- Iterate through the array while i is less than n.
- Find the largest segment with the same parity by checking if the current number and the next number have the same parity.
- Sort the subarray in descending order using the sort function.
- Update the index i to the next segment.
- Return the modified array, which is now lexicographically largest.
Below is the implementation of the above approach:
C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector< int > lexicographicallyLargest(vector< int >& a, int n)
{
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n && a[j] % 2 == a[j - 1] % 2) {
j++;
}
sort(a.begin() + i, a.begin() + j, greater< int >());
i = j;
}
return a;
}
int main()
{
int n = 3;
vector< int > a = { 1, 3, 5 };
vector< int > result = lexicographicallyLargest(a, n);
cout << "Lexicographically Largest Array: ";
for ( int i = 0; i < n; i++) {
cout << result[i] << " ";
}
cout << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
public class LexicographicallyLargest {
static ArrayList<Integer>
lexicographicallyLargest(ArrayList<Integer> a, int n)
{
int i = 0 ;
while (i < n) {
int j = i + 1 ;
while (j < n
&& a.get(j) % 2 == a.get(j - 1 ) % 2 ) {
j++;
}
Collections.sort(a.subList(i, j),
Collections.reverseOrder());
i = j;
}
return a;
}
public static void main(String[] args)
{
int n = 3 ;
ArrayList<Integer> a = new ArrayList<>();
a.add( 1 );
a.add( 3 );
a.add( 5 );
ArrayList<Integer> result
= lexicographicallyLargest(a, n);
System.out.print(
"Lexicographically Largest Array: " );
for ( int i = 0 ; i < n; i++) {
System.out.print(result.get(i) + " " );
}
System.out.println();
}
}
|
Python3
def lexicographically_largest(a, n):
i = 0
while i < n:
j = i + 1
while j < n and a[j] % 2 = = a[j - 1 ] % 2 :
j + = 1
a[i:j] = sorted (a[i:j], reverse = True )
i = j
return a
if __name__ = = "__main__" :
n = 3
a = [ 1 , 3 , 5 ]
result = lexicographically_largest(a, n)
print ( "Lexicographically Largest Array:" , end = " " )
for i in range (n):
print (result[i], end = " " )
print ()
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static List< int > LexicographicallyLargest(List< int > a, int n)
{
int i = 0;
while (i < n)
{
int j = i + 1;
while (j < n && a[j] % 2 == a[j - 1] % 2)
{
j++;
}
a.Sort(i, j - i, Comparer< int >.Create((x, y) => y.CompareTo(x)));
i = j;
}
return a;
}
static void Main()
{
int n = 3;
List< int > a = new List< int > { 1, 3, 5 };
List< int > result = LexicographicallyLargest(a, n);
Console.Write( "Lexicographically Largest Array: " );
for ( int i = 0; i < n; i++)
{
Console.Write(result[i] + " " );
}
Console.WriteLine();
Console.ReadLine();
}
}
|
Javascript
function lexicographicallyLargest(a, n) {
let i = 0;
while (i < n) {
let j = i + 1;
while (j < n && a[j] % 2 === a[j - 1] % 2) {
j++;
}
a = a.slice(0, i).concat(a.slice(i, j).sort((x, y) => y - x), a.slice(j));
i = j;
}
return a;
}
function main() {
const n = 3;
const a = [1, 3, 5];
const result = lexicographicallyLargest(a, n);
console.log( "Lexicographically Largest Array: " + result.join( ' ' ));
}
main();
|
Output
Lexicographically Largest Array: 5 3 1
Time complexity: O(NlogN) logN for sorting the array (subsegments)
Auxiliary Space : O(1) as we are using no space .
Share your thoughts in the comments
Please Login to comment...