You are viewing a single comment's thread. Return to all comments →
using two pointers, instead of iterating over all the elements to calculate the sum: O(n), you can improve that to O(n/2)
half the number of steps to complete the opertaion:
C# code: public static void Optimal(List bill, int k, int b) { var leftPointer = 0; var rightPointer = bill.Count - 1; var totalBill = 0;
while (leftPointer <= rightPointer) { if(leftPointer == rightPointer) totalBill = totalBill + leftPointer; else totalBill = totalBill + bill[leftPointer] + bill[rightPointer]; leftPointer++; rightPointer--; } totalBill = (totalBill - bill[k]) / 2; if (totalBill == b) { Console.WriteLine("Bon Appetit"); } Console.WriteLine(b - totalBill);
}
Seems like cookies are disabled on this browser, please enable them to open this website
Bill Division
You are viewing a single comment's thread. Return to all comments →
using two pointers, instead of iterating over all the elements to calculate the sum: O(n), you can improve that to O(n/2)
half the number of steps to complete the opertaion:
C# code: public static void Optimal(List bill, int k, int b) { var leftPointer = 0; var rightPointer = bill.Count - 1; var totalBill = 0;
}