- Related Questions & Answers
- Maximum possible XOR of every element in an array with another array in C++
- Divide every element of one array by other array elements in C++ Program
- Find maximum sum taking every Kth element in the array in C++
- Maximum Unique Element in every subarray of size K in c++
- Finding the rotation of an array in JavaScript
- Product of every K’th prime number in an array in C++
- Program to check whether every rotation of a number is prime or not in Python
- JavaScript: take every nth Element of Array and display a fixed number of values?
- Find maximum of minimum for every window size in a given array in C++
- Find if a number is divisible by every number in a list in C++
- Reduce an array to the sum of every nth element - JavaScript
- 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 and minimum of an array using minimum number of comparisons in C
- Maximum and Minimum element of a linked list which is divisible by a given number k 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