-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumberFormedFromAnArray.c
More file actions
75 lines (72 loc) · 2.13 KB
/
LargestNumberFormedFromAnArray.c
File metadata and controls
75 lines (72 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// https://practice.geeksforgeeks.org/problems/largest-number-formed-from-an-array/0
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,k,t,n,*a,max,temp,kk,jj;
char *s;
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
a=malloc(sizeof(int)*n);
s=malloc(sizeof(char)*n);
for(j=0;j<n;j++){
scanf("%d",a+j);
}
max=0;
for(j=0;j<n-1;j++){
for(k=j+1;k<n;k++){
if(a[k]>=100){
jj=a[j]*1000;
jj=jj+a[k];
if(a[j]>=100){
kk=a[k]*1000;
kk=kk+a[j];
}else if(a[j]<100 && a[j]>9){
kk=a[k]*100;
kk=kk+a[j];
}else if(a[j]<10){
kk=a[k]*10;
kk=kk+a[j];
}
}
if(a[k]<100 && a[k]>9){
jj=a[j]*100;
jj=jj+a[k];
if(a[j]>=100){
kk=a[k]*1000;
kk=kk+a[j];
}else if(a[j]<100 && a[j]>9){
kk=a[k]*100;
kk=kk+a[j];
}else if(a[j]<10){
kk=a[k]*10;
kk=kk+a[j];
}
}
if(a[k]<9){
jj=a[j]*10;
jj=jj+a[k];
if(a[j]>=100){
kk=a[k]*1000;
kk=kk+a[j];
}else if(a[j]<100 && a[j]>9){
kk=a[k]*100;
kk=kk+a[j];
}else if(a[j]<10){
kk=a[k]*10;
kk=kk+a[j];
}
}
if(kk>jj){
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for(j=0;j<n;j++){
printf("%d",a[j]);
}
printf("\n");
}
}