Skip to content
Related Articles

Related Articles

Maximum number by concatenating every element in a rotation of an array
  • Difficulty Level : Easy
  • Last Updated : 15 Oct, 2019

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 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
?>
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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :