Maximum number by concatenating every element in a rotation of an array
Given an array of N elements. The task is to print the maximum number by concatenating every element in each rotation. In every rotation, the first element will take place of the last element in each rotation and vice versa.
Examples:
Input: a[]: {54, 546, 548, 60}
Output: 6054546548
1st Rotation: 5465486054
2nd Rotation: 5486054546
3rd Rotation: 6054546548
4th Rotation: 5454654860
Input: a[]: {1, 4, 18, 96}
Output: 961418
Approach: On observing carefully, it is found that the number which has the largest left-most digit in all elements will be the first element in the number. Since the concatenation has to be done in terms of rotation of arrays. Concatenate all the numbers from the largest left-most digit index to the end and then concatenate the elements from 0th index to the largest left-most digit index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printLargest( int a[], int n)
{
int max = -1;
int ind = -1;
for ( int i = 0; i < n; i++) {
int num = a[i];
while (num) {
int r = num % 10;
num = num / 10;
if (num == 0) {
if (max < r) {
max = r;
ind = i;
}
}
}
}
for ( int i = ind; i < n; i++)
cout << a[i];
for ( int i = 0; i < ind; i++)
cout << a[i];
}
int main()
{
int a[] = { 54, 546, 548, 60 };
int n = sizeof (a) / sizeof (a[0]);
printLargest(a, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GFG {
static void printLargest( int a[], int n)
{
int max = - 1 ;
int ind = - 1 ;
for ( int i = 0 ; i < n; i++) {
int num = a[i];
while (num > 0 ) {
int r = num % 10 ;
num = num / 10 ;
if (num == 0 ) {
if (max < r) {
max = r;
ind = i;
}
}
}
}
for ( int i = ind; i < n; i++)
System.out.print(a[i]);
for ( int i = 0 ; i < ind; i++)
System.out.print(a[i]);
}
public static void main(String args[])
{
int a[] = { 54 , 546 , 548 , 60 };
int n = a.length;
printLargest(a, n);
}
}
|
Python3
def printLargest(a, n):
max = - 1
ind = - 1
for i in range ( 0 , n):
num = a[i]
while (num):
r = num % 10 ;
num = num / 10 ;
if (num = = 0 ):
if ( max <r):
max = r
ind = i;
for i in range (ind, n):
print (a[i], end = ''),
for i in range ( 0 , ind) :
print (a[i], end = '')
if __name__ = = "__main__" :
a = [ 54 , 546 , 548 , 60 ]
n = len (a)
printLargest(a, n)
|
C#
using System;
class GFG {
static void printLargest( int [] a, int n)
{
int max = -1;
int ind = -1;
for ( int i = 0; i < n; i++) {
int num = a[i];
while (num > 0) {
int r = num % 10;
num = num / 10;
if (num == 0) {
if (max < r) {
max = r;
ind = i;
}
}
}
}
for ( int i = ind; i < n; i++)
Console.Write(a[i]);
for ( int i = 0; i < ind; i++)
Console.Write(a[i]);
}
public static void Main()
{
int [] a = { 54, 546, 548, 60 };
int n = 4;
printLargest(a, n);
}
}
|
PHP
<?php
function printLargest( $a , $n )
{
$max = -1;
$ind = -1;
for ( $i = 0 ; $i < $n ; $i ++)
{
$num = $a [ $i ];
while ( $num )
{
$r = $num % 10;
$num = (int) $num / 10;
if ( $num == 0)
{
if ( $max < $r )
{
$max = $r ;
$ind = $i ;
}
}
}
}
for ( $i = $ind ; $i < $n ; $i ++)
echo $a [ $i ];
for ( $i = 0; $i < $ind ; $i ++)
echo $a [ $i ];
}
$a = array (54, 546,
548, 60);
$n = sizeof( $a );
printLargest( $a , $n );
?>
|
Javascript
<script>
function printLargest(a, n)
{
let max = -1;
let ind = -1;
for (let i = 0; i < n; i++) {
let num = a[i];
while (num > 0) {
let r = num % 10;
num = Math.floor(num / 10);
if (num == 0) {
if (max < r) {
max = r;
ind = i;
}
}
}
}
for (let i = ind; i < n; i++)
document.write(a[i]);
for (let i = 0; i < ind; i++)
document.write(a[i]);
}
let a = [ 54, 546, 548, 60 ];
let n = a.length;
printLargest(a, n);
</script>
|
Time Complexity: O(n * log10(num)), where n is size of the array and num is the number of digits of maximum element of the array.
Auxiliary Space: O(1)
Last Updated :
28 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...