通过将数组旋转中的每个元素串联在一起而获得的最大数

给定N个元素的数组。任务是通过连接每个旋转中的每个元素来打印最大数量。在每次旋转中,第一个元素将取代最后一个元素,反之亦然。 例如:

null

输入: a[]:{546548,60} 输出: 6054546548 第1轮:5465486054 第二轮:5486054546 第三轮:60545446548 第四轮:54654860 输入: a[]:{1,4,18,96} 输出: 961418

方法: 仔细观察发现,在所有元素中最左边数字最大的数字将是数字中的第一个元素。因为串联必须根据数组的旋转来完成。将从最左边最大的数字索引到末尾的所有数字连接起来,然后将0中的元素连接起来 th 索引到最左边的最大数字索引。 以下是上述方法的实施情况:

C++

// C++ program to print the
// Maximum number by concatenating
// every element in rotation of array
#include <bits/stdc++.h>
using namespace std;
// Function to print the largest number
void printLargest( int a[], int n)
{
// store the index of largest
// left most digit of elements
int max = -1;
int ind = -1;
// Iterate for all numbers
for ( int i = 0; i < n; i++) {
int num = a[i];
// check for the last digit
while (num) {
int r = num % 10;
num = num / 10;
if (num == 0) {
// check for the largest left most digit
if (max < r) {
max = r;
ind = i;
}
}
}
}
// print the largest number
// print the rotation of array
for ( int i = ind; i < n; i++)
cout << a[i];
// print the rotation of array
for ( int i = 0; i < ind; i++)
cout << a[i];
}
// Driver Code
int main()
{
int a[] = { 54, 546, 548, 60 };
int n = sizeof (a) / sizeof (a[0]);
printLargest(a, n);
return 0;
}


JAVA

// Java program to print the
// Maximum number by concatenating
// every element in rotation of array
import java.util.*;
import java.lang.*;
public class GFG {
// Function to print the largest number
static void printLargest( int a[], int n)
{
// store the index of largest
// left most digit of elements
int max = - 1 ;
int ind = - 1 ;
// Iterate for all numbers
for ( int i = 0 ; i < n; i++) {
int num = a[i];
// check for the last digit
while (num > 0 ) {
int r = num % 10 ;
num = num / 10 ;
if (num == 0 ) {
// check for the largest left most digit
if (max < r) {
max = r;
ind = i;
}
}
}
}
// print the largest number
// print the rotation of array
for ( int i = ind; i < n; i++)
System.out.print(a[i]);
// print the rotation of array
for ( int i = 0 ; i < ind; i++)
System.out.print(a[i]);
}
// Driver Code
public static void main(String args[])
{
int a[] = { 54 , 546 , 548 , 60 };
int n = a.length;
printLargest(a, n);
}
}


Python3

# Python program to print the
# Maximum number by concatenating
# every element in rotation of array
# Function to print the largest number
def printLargest(a, n):
# store the index of largest
# left most digit of elements
max = - 1
ind = - 1
# Iterate for all numbers
for i in range ( 0 , n):
num = a[i]
# check for the last digit
while (num):
r = num % 10 ;
num = num / 10 ;
if (num = = 0 ):
# check for the largest left most digit
if ( max <r):
max = r
ind = i;
# print the largest number
# print the rotation of array
for i in range (ind, n):
print (a[i], end = ''),
# print the rotation of array
for i in range ( 0 , ind) :
print (a[i], end = '')
# Driver Code
if __name__ = = "__main__" :
a = [ 54 , 546 , 548 , 60 ]
n = len (a)
printLargest(a, n)
# This code is contributed by Shivi_Aggarwal


C#

// C# program to print the
// Maximum number by concatenating
// every element in rotation of array
using System;
class GFG {
// Function to print the largest number
static void printLargest( int [] a, int n)
{
// store the index of largest
// left most digit of elements
int max = -1;
int ind = -1;
// Iterate for all numbers
for ( int i = 0; i < n; i++) {
int num = a[i];
// check for the last digit
while (num > 0) {
int r = num % 10;
num = num / 10;
if (num == 0) {
// check for the largest left most digit
if (max < r) {
max = r;
ind = i;
}
}
}
}
// print the largest number
// print the rotation of array
for ( int i = ind; i < n; i++)
Console.Write(a[i]);
// print the rotation of array
for ( int i = 0; i < ind; i++)
Console.Write(a[i]);
}
// Driver Code
public static void Main()
{
int [] a = { 54, 546, 548, 60 };
int n = 4;
printLargest(a, n);
}
}
// This code is contributed by mohit kumar 29


PHP

<?php
// PHP program to print
// the Maximum number by
// concatenating every
// element in rotation of array
// Function to print
// the largest number
function printLargest( $a , $n )
{
// store the index of largest
// left most digit of elements
$max = -1;
$ind = -1;
// Iterate for
// all numbers
for ( $i = 0 ; $i < $n ; $i ++)
{
$num = $a [ $i ];
// check for the
// the last digit
while ( $num )
{
$r = $num % 10;
$num = (int) $num / 10;
if ( $num == 0)
{
// check for the largest
// left most digit
if ( $max < $r )
{
$max = $r ;
$ind = $i ;
}
}
}
}
// print the largest number
// print the
// rotation of array
for ( $i = $ind ; $i < $n ; $i ++)
echo $a [ $i ];
// print the
// rotation of array
for ( $i = 0; $i < $ind ; $i ++)
echo $a [ $i ];
}
// Driver Code
$a = array (54, 546,
548, 60);
$n = sizeof( $a );
printLargest( $a , $n );
// This code is contributed by m_kit
?>


Javascript

<script>
// Javascript program to print the
// Maximum number by concatenating
// every element in rotation of array
// Function to print the largest number
function printLargest(a, n)
{
// store the index of largest
// left most digit of elements
let max = -1;
let ind = -1;
// Iterate for all numbers
for (let i = 0; i < n; i++) {
let num = a[i];
// check for the last digit
while (num > 0) {
let r = num % 10;
num = Math.floor(num / 10);
if (num == 0) {
// check for the largest
// left most digit
if (max < r) {
max = r;
ind = i;
}
}
}
}
// print the largest number
// print the rotation of array
for (let i = ind; i < n; i++)
document.write(a[i]);
// print the rotation of array
for (let i = 0; i < ind; i++)
document.write(a[i]);
}
// driver code
let a = [ 54, 546, 548, 60 ];
let n = a.length;
printLargest(a, n);
</script>


输出:

6054546548

时间复杂性: O(n*log) 10 (num)),其中n是数组的大小,num是数组最大元素的位数。

辅助空间: O(1)

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享