-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckStableOrNot.java
127 lines (119 loc) · 3.01 KB
/
CheckStableOrNot.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
The scenario is as below
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is stable if each of its digit occur the same number of times, i.e. the frequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,
583835, 1010 are examples of stable numbers.
Similarly. A number is unstable if the frequency of each digit in the number is NOT the same. For e g. 221, 4314, 101, 233, 58135, 101 are examples of unstable numbers..
The password can be found as below
password = Maximum of all stable numbers - Minimum of all Unstable numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4 and inputs, complete the function to find and return the password For Example
If input1 = 12, input2 = 1313, Input3= 122, input4= 678, and input5 898. = stable numbers are 12. 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all stable numbers - Minimum of
all Unstable numbers 1313-122=1191
*/
import java.util.*;
public class Main
{
public static int isStable(int n) {
int arr[] = {0,0,0,0,0,0,0,0,0,0};
int l=n;
while(n>0){
int digit = n% 10;
arr[digit]++;
n/=10;
}
ArrayList<Integer>res=new ArrayList<Integer>();
Boolean flag = true;
for(int i=0; i<arr.length; i++)
{
if(arr[i]!=0){
res.add(arr[i]);
}
}
for(int j=0; j<res.size()-1; j++)
{
if(res.get(j)!=res.get(j+1))
{
flag=false;
break;
}
}
if(flag){
return l;
}else{
return 0;
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
int e=sc.nextInt();
ArrayList<Integer> S = new ArrayList<Integer> ();
ArrayList<Integer> NS = new ArrayList<Integer> ();
int res1=isStable(a);
int res2=isStable(b);
int res3=isStable(c);
int res4=isStable(d);
int res5=isStable(e);
if(res1!=0)
{
S.add(a);
}
else
{
NS.add(a);
}
if(res2!=0)
{
S.add(b);
}
else
{
NS.add(b);
}
if(res3!=0)
{
S.add(c);
}
else
{
NS.add(c);
}
if(res4!=0)
{
S.add(d);
}
else
{
NS.add(d);
}
if(res5!=0)
{
S.add(e);
}
else
{
NS.add(e);
}
Collections.sort(S);
Collections.sort(NS);
int i=S.size();
int j=NS.size();
int out1=S.get(i-1);
int out2=NS.get(0);
System.out.println(out1-out2);
}
}
//OUTPUT:
12
1313
122
678
898
1191