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 term 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 <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 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)
                {
                    r=max;
                    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 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)
                    {
                        r = max;
                        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 the last digit 
         while(num):
          
            r = num%10
            num = num/10
            if(num==0):
                # check for the largest left most digit 
                if(max<r):
                    r=max
                    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

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)
                {
                    $r = $max;
                    $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
?>

Output:

6054546548


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.