forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedRank.java
More file actions
47 lines (34 loc) · 801 Bytes
/
SortedRank.java
File metadata and controls
47 lines (34 loc) · 801 Bytes
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
package Math;
/**
* Author - archit.s
* Date - 30/09/18
* Time - 1:00 AM
*/
public class SortedRank {
int fact(int A){
if(A <= 1){
return A;
}
return A*fact(A-1)%1000003;
}
int smallerRight(String A, int low, int high){
char c = A.charAt(low);
int count = 0;
for(int i=low+1;i<=high;i++){
if(c > A.charAt(i)){
count++;
}
}
return count;
}
public int findRank(String A) {
int rank = 1;
int countRight = 0;
for(int i=0;i<A.length();i++){
countRight = smallerRight(A, i, A.length()-1);
rank += (countRight*fact(A.length()-i-1));
rank = rank % 1000003;
}
return rank;
}
}