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

C++Server Side ProgrammingProgramming

We are given a circular array of numbers. A circular array is the one in which the elements are arranged such that the first element is treated as just next to the last element. These are used to implement queues.

Each element has the same or different digit count. The goal is to create the highest number possible by concatenating the numbers, using rotation of elements if required. We will do this by finding the highest leftmost digit among all the leftmost digits of all elements. The number with the highest leftmost digit will take the first place.

  • If it is at first position, all numbers at indexes 1 to n-1 are placed as it is.

  • If it is somewhere in between, and it’s index is i, all at indexes i+1 to n-1 are appended first followed by those at indexes 0 to i-1.

  • If it is the last one, then all at indexes 0 to i-1 are appended after it.

Input

Arr[] = { 121, 43, 65, 32 }

Output

Highest number: 653212143

Explanation − Highest leftmost digit is 6. Place 65 at first position followed by 32, then 121,43. As Arr[] is a circular array.

Input

Arr[] = { 1101, 9, 321, 77 }

Output

Highest number: 9321771101

Explanation − Highest leftmost digit is 9321771101. Place 9 at first position followed by 321, then 77, 1101. As Arr[] is a circular array.

Approach used in the below program is as follows

  • Array arr[] stores the numbers.

  • Function Largets(int arr[],int n) takes array and its length n as input and prints the highest number that can be made..

  • Variable maxx is used to store the number with highest leftmost digit, initialized with 0.

  • Pos is used to store index of maxx.

  • Starting from i=0 to n find the leftmost digit of each arr[i] by dividing it by 10 until it becomes 0. Each time the remainder will represent the digit at units place.

  • If the current such digit is highest, when num==0, (means rem has leftmost digit), update maxx and pos.

  • Print the elements from index pos to end of array and then again from index 0 to pos-1.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void Largest(int arr[], int n){
   int maxx = 0;
   int pos = 0; //index of number with highest leftmost digit
   for (int i = 0; i < n; i++) {
      int num = arr[i];
      // check for the last digit
      while (num!=0) {
         int rem = num % 10;
         num = num / 10;
         if (num == 0) {
            if (maxx < rem) {
               maxx = rem;
               pos = i;
            }
         }
      }
   }
   // print the largest number
   cout<<"Largest number by concatenation: ";
   for (int i = pos; i < n; i++)
      cout << arr[i];
   for (int i = 0; i < pos; i++)
      cout << arr[i];
}
int main(){
   int Arr[] = { 12,34,56,98 };
   int size=4;
   Largest(Arr,size);
   return 0;
}

Output

Largest number by concatenation: 98123456
raja
Published on 28-Jul-2020 14:55:35
Advertisements