Maximum number by concatenating every element in a rotation of an array - GeeksforGeeks

28 Feb, 2022
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: 5454654860Input: 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++
Java
Python3
C#
PHP
Javascript
// 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 numbervoid 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 Codeint main(){    int a[] = { 54, 546, 548, 60 };    int n = sizeof(a) / sizeof(a[0]);    printLargest(a, n);    return 0;}
// Java program to print the// Maximum number by concatenating// every element in rotation of arrayimport 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);    }}
# Python program to print the# Maximum number by concatenating# every element in rotation of array  # Function to print the largest numberdef 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 Codeif __name__ == "__main__":    a = [54, 546, 548, 60]    n = len(a)    printLargest(a, n)  # This code is contributed by Shivi_Aggarwal
// C# program to print the// Maximum number by concatenating// every element in rotation of arrayusing 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 program to print// the Maximum number by// concatenating every// element in rotation of array // Function to print// the largest numberfunction 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?>
<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
 
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)
jit_t
Nishant Tanwar
Shivi_Aggarwal
mohit kumar 29
AyushKumar34
Akanksha_Rai
chinmoy1997pal
samim2000
arorakashish0911
Arrays
large-numbers
Greedy
Arrays
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
Program for First Fit algorithm in Memory Management
Optimal Page Replacement Algorithm
Program for Best Fit algorithm in Memory Management
Bin Packing Problem (Minimize number of used Bins)
Program for Worst Fit algorithm in Memory Management
Max Flow Problem Introduction
Optimal File Merge Patterns
Huffman Decoding
Maximum items that can be bought from the cost Array based on given conditions