Related Articles

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

• Difficulty Level : Easy
• Last Updated : 21 May, 2021

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++

 `// C++ program to print the``// Maximum number by concatenating``// every element in rotation of array``#include ``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``

## 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

 ``

## Javascript

 ``
Output:
`6054546548`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up