1
+ // inputArr[
2
+ // 2, 3, 4, 4, 5, 9, 7,
3
+ // 8, 6, 10, 4, 5, 10, 10,
4
+ // 8, 4, 6, 4, 10, 1
5
+ // ]
6
+
7
+ // inputArr[
8
+ // 8, 34, 40, 2, 2, 22, 32, 22, 3, 32, 7, 31,
9
+ // 16, 29, 22, 46, 45, 10, 45, 46, 45, 23, 16, 4,
10
+ // 45, 12, 5, 39, 45, 4, 47, 31, 1, 7, 35, 12,
11
+ // 27, 8, 46, 47, 50, 27, 14, 26, 11, 20, 45, 15,
12
+ // 38, 24, 10, 13, 6, 6, 9, 17, 13, 28, 43, 41,
13
+ // 33, 46, 17, 21, 25, 4, 9, 3, 32, 33, 4, 50,
14
+ // 24, 30, 37, 27, 34, 13, 15, 9, 37, 26, 38, 16,
15
+ // 19, 47, 3, 43, 22, 13, 28, 17, 23, 35, 44, 17,
16
+ // 32
17
+ // ]
18
+
19
+ // 5, 4, 2, 4, 1, 2 = 4, 5
20
+ // 3, 7, 5, 6, 2 = 6, 7
21
+ // 4, 2, 5, 1, 6 = 5, 6
22
+
23
+
24
+
25
+ /*
26
+ * Complete the 'minimalHeaviestSetA' function below.
27
+ *
28
+ * The function is expected to return an INTEGER_ARRAY.
29
+ * The function accepts INTEGER_ARRAY arr as parameter.
30
+ */
31
+
32
+ function minimalHeaviestSetA ( arr ) {
33
+ // Write your code here
34
+ // 6, 9, 5, 10
35
+ // 7, 3, 8
36
+ // 6, 11
37
+ // 7
38
+ // const pairSumMap = new Map()
39
+ console . log ( 'inputArr' , arr )
40
+ function getArrSumExpectValues ( completeArr , expectIndexArr ) {
41
+ let bSum = 0 ;
42
+ for ( let i = 0 ; i < completeArr . length ; i ++ ) {
43
+ if ( ! expectIndexArr . includes ( i ) ) {
44
+ bSum += completeArr [ i ]
45
+ }
46
+ }
47
+ return bSum
48
+ }
49
+
50
+ // let resultPairSet = new Set()
51
+ let resultPairArr = [ ]
52
+ let maxPairSum = 0
53
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
54
+ for ( let j = i + 1 ; j < arr . length ; j ++ ) {
55
+ let left = arr [ i ]
56
+ let right = arr [ j ]
57
+ const currentPairSum = left + right
58
+ const expectValuesSum = getArrSumExpectValues ( arr , [ i , j ] ) ;
59
+ console . log ( `${ left } , ${ right } ` , currentPairSum , expectValuesSum )
60
+ if ( currentPairSum > expectValuesSum && currentPairSum > maxPairSum ) {
61
+ [ left , right ] = ( left > right ) ? [ right , left ] : [ left , right ] ;
62
+ // resultPairSet.add(`${left}, ${right}`)
63
+ // resultPairArr.push(left, right)
64
+ console . log ( left , right )
65
+ resultPairArr [ 0 ] = left
66
+ resultPairArr [ 1 ] = right
67
+ maxPairSum = currentPairSum
68
+ }
69
+ }
70
+ }
71
+ // let resultPairArr = Array.from(resultPairSet)
72
+ // resultPairArr = resultPairArr.flat(2)
73
+ // let finalResultArr = []
74
+ // resultPairArr.map((pair) => {
75
+ // let singlePairArr = pair.split(',')
76
+ // finalResultArr.push(parseInt(singlePairArr[0]), parseInt(singlePairArr[1]))
77
+ // // console.log(singlePairArr[0])
78
+ // // console.log(singlePairArr[1])
79
+ // })
80
+ // console.log(finalResultArr)
81
+ return resultPairArr
82
+ }
83
+
84
+ function minimalHeaviestSet ( A ) {
85
+ // Sort the array in descending order to consider the largest numbers first
86
+ A . sort ( ( a , b ) => b - a ) ;
87
+
88
+ let totalSum = A . reduce ( ( sum , num ) => sum + num , 0 ) ;
89
+ let subsetSum = 0 ;
90
+ let subset = [ ] ;
91
+
92
+ // Loop through the sorted array and pick elements for the heaviest set
93
+ for ( let i = 0 ; i < A . length ; i ++ ) {
94
+ subsetSum += A [ i ] ;
95
+ subset . push ( A [ i ] ) ;
96
+ if ( subsetSum > totalSum - subsetSum ) {
97
+ break ;
98
+ }
99
+ }
100
+
101
+ return subset ;
102
+ }
103
+
104
+ console . log ( minimalHeaviestSet ( [ 8 , 34 , 40 , 2 , 2 , 22 , 32 , 22 , 3 , 32 , 7 , 31 , ] ) ) ; // Output: [5, 4]
105
+ // console.log(minimalHeaviestSet([10, 20, 30, 40, 50])); // Output: [50, 40]
106
+
107
+
108
+ // console.log(minimalHeaviestSetA([2, 3, 4, 4, 5, 9, 7, 8, 6, 10, 4, 5, 10, 10, 8, 4, 6, 4, 10, 1]))
109
+ // console.log(minimalHeaviestSetA([1, 2, 3, 4, 5]))
110
+
111
+
112
+ // ===========================
113
+
114
+ // |||||******|**|****|******|*|*||*|******|*||**|***|***|**||*|**|***|*|*|**||***|******|*|||*****||||
115
+ // *|*|*| = 1,6
116
+ // *|*| = 1,3
117
+ // |**|*|* = 1,1 OR 5,6
118
+
119
+
120
+
121
+ /*
122
+ * Complete the 'numberOfItems' function below.
123
+ *
124
+ * The function is expected to return an INTEGER_ARRAY.
125
+ * The function accepts following parameters:
126
+ * 1. STRING s
127
+ * 2. INTEGER_ARRAY startIndices
128
+ * 3. INTEGER_ARRAY endIndices
129
+ */
130
+
131
+ function numberOfItems ( s , startIndices , endIndices ) {
132
+ console . log ( s , startIndices , endIndices )
133
+ let substringArrSet = new Set ( )
134
+ for ( let i = 0 ; i < startIndices . length ; i ++ ) {
135
+ let leftIndex = startIndices [ i ] - 1 ; // as generally comes 1
136
+ let rightIndex = endIndices [ i ] ;
137
+ // console.log('indexes values', leftIndex, rightIndex)
138
+ // console.log('s length', s.length)
139
+ substringArrSet . add ( s . slice ( leftIndex , rightIndex ) )
140
+ }
141
+ let resultArr = [ ]
142
+ for ( const substring of substringArrSet ) {
143
+ let compartment , totalCompartment = 0 ;
144
+ // console.log('substring',substring)
145
+ for ( const char of substring ) {
146
+ if ( substring [ 0 ] === '|' ) { //if a 0 index pipe so already open
147
+ compartment = 'open'
148
+ }
149
+ if ( char === '|' ) {
150
+ compartment = ( compartment === 'open' ) ? 'close' : 'open' ;
151
+ }
152
+ if ( compartment === 'close' ) {
153
+ totalCompartment ++
154
+ compartment = 'open'
155
+ }
156
+ }
157
+ resultArr . push ( totalCompartment )
158
+ }
159
+ return resultArr
160
+ }
161
+
162
+
163
+ // ============== Subset
164
+
165
+ // 2
166
+ // 3
167
+ // 2 3
168
+ // 4
169
+ // 2 4
170
+ // 3 4
171
+ // 2 3 4
172
+
173
+ // ----------
174
+
175
+ // 2 3 4 5
176
+
177
+ // 2
178
+ // 3
179
+ // 2 3
180
+ // 2 4
181
+ // 2 5
182
+ // 3 4
183
+ // 3 5
184
+ // 4
185
+ // 4 5
186
+ // 5
187
+ // 2 3 4
188
+ // 3 4 5
189
+ // 2 3 4 5
190
+ console . log ( '...........................' )
191
+
192
+ function staircase ( n ) {
193
+ for ( let i = 0 ; i < n ; i ++ ) {
194
+ let str = ''
195
+ for ( let j = 0 ; j < ( n - i ) - 1 ; j ++ ) {
196
+ str = `${ str } `
197
+ }
198
+ for ( let k = 0 ; k <= i ; k ++ ) {
199
+ str = `${ str } #`
200
+ }
201
+ console . log ( str )
202
+ }
203
+ return true
204
+ }
205
+
206
+ console . log ( staircase ( 5 ) )
0 commit comments