- Related Questions & Answers
- Maximum possible XOR of every element in an array with another array in C++
- Maximum Unique Element in every subarray of size K in c++
- Product of every K’th prime number in an array in C++
- How to find the maximum element of an Array using STL in C++?
- Program to find the minimum (or maximum) element of an array in C++
- Maximum rational number (or fraction) from an array in C++
- C# program to find maximum and minimum element in an array
- Maximum product subset of an array in C++
- Find last element after deleting every second element in array of n integers in C++
- Maximum element in a sorted and rotated array in C++
- Find closest value for every element in array in C++
- XOR of Sum of every possible pair of an array in C++
- C++ Program to Find Maximum Element in an Array using Binary Search
- Maximum average sum partition of an array in C++
- Maximum element in a very large array using pthreads in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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

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.

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

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.

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.

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

Largest number by concatenation: 98123456

Advertisements