22public int calculateBeautyValues (int [] arr , int [][] pairs )
33{
44 //Base Check
5- if (arr .length == null )
5+ if (arr .length == 0 )
66 {
77 return 0 ;
88 }
@@ -13,28 +13,44 @@ public int calculateBeautyValues(int[] arr, int[][] pairs)
1313 //Compuate
1414 /** find the used indices */
1515 Set <Integer > set = new HashSet <>();
16+ List <Integer > bList = new ArrayList <>();
1617 for (int [] pair : pairs )
1718 {
19+ //update set
1820 set .add (pair [0 ]);
1921 set .add (pair [1 ]);
20- }
22+
23+ //update bList
24+ if (pair [0 ] != pair [1 ])
25+ {
26+ bList .add (arr [pair [0 ]]);
27+ bList .add (arr [pair [1 ]]);
28+ }
29+ else
30+ {
31+ bList .add (arr [pair [0 ]]);
32+ }
33+
34+ }//
2135
22- /** compute the freqeuncey for each element at the used index */
23- Map <Integer , Integer > freqMap = new HashMap <>();
36+ /** get all the unused indicies */
2437 Set <Integer > unused = new HashSet <>();
2538 for (int i = 0 ; i < arr .length ; i ++)
2639 {
27- if (set .contains (i ))
28- {
29- freqMap .put (arr [i ], freqMap .getOrDefault (arr [0 ], 0 ) + 1 );
30- }
31- else
40+ if (!set .contains (i ))
3241 {
3342 unused .add (i );
3443 }
3544
3645 }// for
3746
47+ /** compute the freqeuncey for each element at the used index */
48+ Map <Integer , Integer > freqMap = new HashMap <>();
49+ for (int num : bList )
50+ {
51+ freqMap .put (num , freqMap .getOrDefault (num , 0 ) + 1 );
52+ }
53+
3854 /** count the total number of elements that are greater than elements at the unused indicies */
3955 int count = 0 ;
4056 for (int key : freqMap .keySet ())
@@ -43,12 +59,12 @@ public int calculateBeautyValues(int[] arr, int[][] pairs)
4359 int localCount = 0 ;
4460 for (int index : unused )
4561 {
46- if (key < arr [i ])
62+ if (key < arr [index ])
4763 {
4864 localCount += freqMap .get (key );
4965 }
5066
51- }
67+ }// for
5268
5369 //update count
5470 count += localCount ;
0 commit comments