@@ -7,9 +7,37 @@ Another example : if arr is[4, 5, 2, 3, 1, 0] then you can distribute the sandwi
77*/
88
99#include < iostream>
10- #include < string>
10+ #include < string>
11+ #include < vector>
1112using namespace std ;
1213
14+ void bubbleSort (vector <int >& list, vector <int >& index)
15+ {
16+ int temp, temp2;
17+ bool swap;
18+
19+ do
20+ {
21+ swap = false ;
22+
23+ for (int x = 1 ; x < list.size ()-1 ; x++)
24+ {
25+ // Will move the elements around in the new list
26+ // Also keeps track of the index in relation to the original array
27+ if (list[x] < list[x + 1 ])
28+ {
29+ temp = list[x];
30+ temp2 = index[x];
31+ index[x] = index[x + 1 ];
32+ index[x + 1 ] = temp2;
33+ list[x] = list[x + 1 ];
34+ list[x + 1 ] = temp;
35+ swap = true ;
36+ }
37+ }
38+ } while (swap);
39+ }
40+
1341int FoodDistribution (int arr[], int size)
1442{
1543 int total = arr[0 ]; // Number of Sandwiches
@@ -20,42 +48,52 @@ int FoodDistribution(int arr[], int size)
2048 // In some cases we might not need to do food distribution if the difference across is 0
2149 for (int x = 1 ; x < size-1 ; x++)
2250 {
23- difference += (arr[x] - arr[x + 1 ]);
51+ int temp = arr[x] - arr[x + 1 ];
52+ if (temp < 0 )
53+ {
54+ temp *= -1 ;
55+ }
56+ difference += temp;
2457 }
2558 if (difference == 0 )
2659 {
2760 return 0 ;
2861 }
2962
30- // Loop to distribute food to those that are hungry
31- // The distribution will be based by comparing the hunger levels adjacent to each other
32- // The goal is to get the hunger levels to be equal across all people if possible
63+ vector <int > newList, indexConect;
64+ newList.push_back (-1 ); // Garbage value just to keep the size parallel to original array
65+ indexConect.push_back (-1 );
66+
67+ for (int x = 1 ; x < size; x++)
68+ {
69+ newList.push_back (arr[x]);
70+ indexConect.push_back (x);
71+ }
72+
73+ bubbleSort (newList, indexConect);
74+
75+ // Loop to distribute the food
3376 do
3477 {
3578 give = false ;
36-
37- for (int x = 1 ; x < size - 1 ; x++)
79+ for (int x = 1 ; x < newList.size () - 1 && total > 0 ; x++)
3880 {
39- if (x == 1 )
40- {
41- if (arr[x] > arr[x + 1 ] && total > 0 )
42- {
43- give = true ;
44- arr[x]--;
45- cout << " index " << x << " now is " << arr[x] << endl;
46- total--;
47- }
48- }
49- else if (arr[x] > arr[x + 1 ] && arr[x] > arr[x - 1 ] && total > 0 )
81+ if (newList[x] > newList[x + 1 ])
5082 {
5183 give = true ;
52- arr[x]--;
53- cout << " index " << x << " now is " << arr[x] << endl;
84+ newList[x]--;
5485 total--;
86+ break ;
5587 }
5688 }
5789 } while (give);
5890
91+ // Loop to match the new list to the original order
92+ for (int x = 1 ; x < newList.size (); x++)
93+ {
94+ arr[x] = newList[indexConect[x]];
95+ }
96+
5997 // Checking for the difference after the distribution took place
6098 difference = 0 ;
6199 for (int x = 1 ; x < size - 1 ; x++)
@@ -78,28 +116,44 @@ int main()
78116 int C[] = { 5 , 2 , 2 , 2 , 2 , 2 };
79117 int D[] = { 5 , 2 , 3 , 4 , 5 };
80118 int E[] = { 3 , 2 , 1 , 0 , 4 , 1 , 0 };
119+ int F[] = { 4 , 5 , 4 , 5 , 2 , 3 , 1 , 2 };
120+ int G[] = { 7 , 5 , 4 , 3 , 4 , 5 , 2 , 3 , 1 , 4 , 5 };
81121 cout << FoodDistribution (A, sizeof (A)/sizeof (A[0 ])) << endl; // 0
82122 cout << FoodDistribution (B, sizeof (B) / sizeof (B[0 ])) << endl; // 2
83123 cout << FoodDistribution (C, sizeof (C) / sizeof (C[0 ])) << endl; // 0
84124 cout << FoodDistribution (D, sizeof (D) / sizeof (D[0 ])) << endl; // 1
85125 cout << FoodDistribution (E, sizeof (E) / sizeof (E[0 ])) << endl; // 4
86- return 0 ;
126+ cout << FoodDistribution (F, sizeof (F) / sizeof (F[0 ])) << endl; // 3
127+ cout << FoodDistribution (G, sizeof (G) / sizeof (G[0 ])) << endl; // 6
87128
88129 /*
89- 2 3 4 5 5
90- 2 2 4 5 4
91- 2 2 3 5 3
92- 2 2 3 4 2
93- 2 2 2 4 1
94- 2 2 2 3 0
95-
96-
97- 2 1 0 4 1 0 3
98- 2 1 0 3 1 0 2
99- 2 1 0 2 1 0 1
100- 2 1 0 1 1 0 0
130+ 5 4 5 2 3 1 2
131+
132+ 4 4 4 2 3 1 2
133+ 4 4 4 2 2 1 2
134+ 4 4 4 2 2 1 1
135+ 0 0 2 0 1 0
136+
137+
138+ 5 5 4 3 2 2 1 4
139+ 5 4 4 3 2 2 1 3
140+ 4 4 4 3 2 2 1 2
141+ 4 4 3 3 2 2 1 1
142+ 4 3 3 3 2 2 1 0
143+
144+ 4 3 3 2 3 1 2
145+ 1 0 1 1 2 1
146+
147+
101148
102- 1 1 1 0 1
149+ 5 4 3 4 5 2 3 1 4 5
150+
151+
103152
153+
104154 */
155+
156+
157+
158+ return 0 ;
105159}
0 commit comments