Ganesh Ram Sundaram
BAN USER
Suppose L1,L2,...Ln are length of the sticks with L1 <= L2 <= L3 <= ... <=Ln.
No matter how you form the final stick, each stick is used twice except for the outer ones, which are used only once. Hence, any combination which puts the largest two sticks at the left & right ends will be an optimal one.
Minimal cost = 2*[ L1 + L2 + ... + L(n-2)] + L(n-1) + Ln
An arrangement having this cost: Ln,L1,L2,L3,...L(n-1)
static int getMaxIndex(int[] arr) {
int left=0,right = arr.length-1;
//arr[left] >= anything to left of it, arr[right] >= anything to right of it
while(left<right-1) {
int mid = (left+right)/2;
if (arr[mid]<arr[mid+1]) left=mid+1; else right=mid;
}
if(arr[left]>arr[right]) return left; else return right;
}
I'm making use of the fact that two digits can be stored in a single character using 10*d1 + d2.
static void relocate(char[] arr) {
int n = arr.length/2;
char firstDigit = arr[1];
//Replacing each digit char in 2nd half with INTEGER that combines two
//consecutive digits as 10*(digit at pos-2) + digit
//For example, if input is a0b1c2d3e4f5,
// char[11]=45,char[9]=23,char[7]=1 (integer equivalent)
for(int i=2*n-1;i>n;i-=2) {
arr[i] = (char)((arr[2*i-2*n+1]-'0') + 10*(arr[2*i-2*n-1]-'0'));
}
for(int i=1;i<n;i++) {//Move all letters to correct pos
arr[i]=arr[2*i];
}
//From integers constructed above, extract the digits
for(int i=2*n-1;i>n;i-=2) {
arr[i-1] = (char)(arr[i]/10 + '0');
arr[i] = (char)(arr[i]%10 + '0');
}
if(n%2 == 1) arr[n] = firstDigit;
}
call the method as attrib(lol); //lol = list of lists
void attrib(list<list<string> > &lol, list<string> &outlst = *(new list<string>)) {
int n = outlst.size();
if (n == lol.size()) {//outlst has attribute from last list
for(list<string>::iterator itr=outlst.begin();itr!=outlst.end();itr++) {
cout<<(*itr)<<'\t';
}
cout<<endl;
return;
}
list<list<string> >::iterator lolItr = lol.begin();
if (n) advance(lolItr,n);
list<string> currlst = *lolItr;
//Iterate over elements of n-th list.
for(list<string>::iterator itr=currlst.begin();itr!=currlst.end();itr++) {
outlst.push_back(*itr);
attrib(lol,outlst);
outlst.pop_back();
}
}
Repeated subtraction is not an O(n) operation, depending on your implementation.
- Ganesh Ram Sundaram March 07, 2014Nice solution but think the operations in the first loop are reversed. I mean, they should be
arr[i] = productSoFar;
productSoFar *= arr[i];
Thanks for all your comments! The logic I used is exactly as pointed out by aka.
In the first loop, I add (size * "OLD arr[arr[i]]") to arr[i] so that, when doing integer division by size, I get old arr[arr[i]], when doing %size, I get old arr[i]. However I add arr[arr[i]]%size to get old arr[arr[i]], in case it was already modified in the loop. The second loop simply replaces each arr[i] with old arr[arr[i]], as mentioned above.
void relocate(int *arr,int size) {
for(int i=0;i<size;i++) {
arr[i] += (arr[arr[i]]%size)*size;
}
for(int i=0;i<size;i++) {
arr[i] /= size;
}
}
class node{
public:
int key;
node* left;
node* right;
node(int key) {
this->key = key;
left = right = 0;
}
};
node *degenerate(node *root) {
if (!root) return 0;
node *newroot = 0;
for(newroot = root;newroot->left;newroot = newroot->left);
convertToList(root);
return newroot;
}
void convertToList(node *p) {
node *leftHighest = 0;
if (p->left) {
for(leftHighest = p->left;leftHighest->right;leftHighest = leftHighest->right);
convertToList(p->left);
leftHighest->right = p;
leftHighest->left = 0;
}
node *rightLowest = 0;
if (p->right) {
for(rightLowest = p->right;rightLowest->left;rightLowest = rightLowest->left);
convertToList(p->right);
p->right = rightLowest;
p->left = 0;
}
}
Replisafergusona, Consultant at Myntra
I am Lisa from Chicago,I am working as a Show host in the New World. I also work Performs ...
RepProcess Technicians are responsible for monitoring and improving manufacturing and engineering processes. They are employed by a wide variety of ...
Repchristinetcollazoc, Software Analyst
I am a pediatric nurse.I administer directly procedures and medicines to children according to prescribed I also continually assess ...
Repjoyceeallard, Associate at Adap.tv
Hi, i am working as a training manager as a business professional who assesses the growth and development needs of ...
Repalicesreedg, Accountant at ADP
Hi my name is Alice and i am working in an IT company. I am working here from last 5 ...
Repharrylseay, Aghori Mahakal Tantrik at ASAPInfosystemsPvtLtd
I am Harry and I live in the USA but basically I am from Bangor. I live my life very ...
Repjesselblagg9, Cloud Support Associate at 247quickbookshelp
Hello, I am Jesse and I live in Springfield, USA. I am working in a company as an engineering technician ...
Reppaulinedsisk, Testing / Quality Assurance at Cloudmere, Inc.
I want to become a successful professional in a highly competitive technological world where performance is rewarded with exciting new ...
Repgradyjvierra, Animator at Alliance Global Servies
Je suis Grady de Phoenix USA. Je travaille en tant que technicien de laboratoire clinique dans la société Samuels Men ...
Repileenjconnelly, Cloud Support Associate at Absolute Softech Ltd
I have extensive experience in providing breaking news and giving precise details to the viewers. Interested in leading/working with ...
Repsusanaminick, Research Scientist at CapitalIQ
I am Susan from Dallas, I am working as a Property manager in Kohl's Food Stores company. I love ...
Repbrandysolsen, Area Sales Manager at AMD
I am a Social butterflies who enjoy travel.Have fun doing this job and project an atmosphere that reflects Amazon ...
Repmarisamsan7, Cloud Support Associate at ADP
Hi, I am Photoengraver from an CO,USA. I am a girl with a strong desire to travel the world ...
Repdorarwoods, Kuwait airways reservations at Aspire Systems
I am an Application engineer in the network chef company. In my free time, I enjoy reading programming and technology ...
Repshinchunigo, Benefits clerk at Good Times
Votaw, Top Duties and Qualifications Benefits clerk . It is responsible for performing administrative tasks to support daily business operations. My ...
Repeffiefranke, Mail clerk at Envirotecture Design
Hello, I am Effie Mail clerk . I sort mail by department and category, utilizing sorting machines and similar administrative technology ...
Repmanueladturner, Associate at Accenture
I am a content writer with years of successful track record of writing concise and fact-filled content on different topics ...
Repmadan@kukooo.in, Analyst at Absolute Softech Ltd
I am a content writer with years of successful track record of writing concise and fact-filled content on different topics ...
RepMaryLopal, Applications Developer at Coupondesh
I am a 41 year old dermatologist in Saginaw USA. I do like to face the challenges . I'm active ...
RepGenesisCruz, Integration Software Engineer at NetApp
I am a highly professional and experienced board director with many years of experience leading non-profit as well as for-profit ...
Repmonicaralvarado94, Korean Air Change Flight at Athena Health
I am fond of reading stories then reading articles which are all about a story of a beautiful nature and ...
RepDonnaArvin, Analyst at Apple
Hii am from the United States. I work as a computer control programmer at Dynatronics Accessories. I am a social ...
RepArnoldTate, Title searcher at Keeney's
I am a Title searcher with 8+ years of experience . In real estate business and law, a title search or ...
I apologise for the delay in replying. The method convertToList(node *p) is basically a slight variant of the recursive inorder algorithm.
- Ganesh Ram Sundaram June 26, 2014This converts a binary tree with root p into a linked list where nodes are ordered in inorder fashion. (Hence the first node will be the extreme left of the tree).
The leftHighest node is basically the largest node of p->left subtree if it were a BST (the rightmost node of p->left).
Once I convert subtree p->left to linkedlist (by recursion), I add p to the end of the list (using 'right' pointer).
Next, I convert p->right subtree to linkedlist (by recursion) and attach it to the end of the above tree. The first node of the former will be the lowest node of p->right (rightLowest) if it were a BST, which is the leftmost node of p->right.