Repeatedly keep removing 2 smallest elements and add its sum back to the list, till it reduces to a single number? Need optimization for million+Given a list of numbers and a number k, return whether any two numbers from the list add up to k
If Manufacturer spice model and Datasheet give different values which should I use?
Motorized valve interfering with button?
Why can't I see bouncing of a switch on an oscilloscope?
Mathematical cryptic clues
How old can references or sources in a thesis be?
Is it possible to make sharp wind that can cut stuff from afar?
Today is the Center
Shell script not opening as desktop application
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
declaring a variable twice in IIFE
Is there any sparring that doesn't involve punches to the head?
The use of multiple foreign keys on same column in SQL Server
I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine
What is the offset in a seaplane's hull?
What do you call a Matrix-like slowdown and camera movement effect?
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
How to make payment on the internet without leaving a money trail?
What are these boxed doors outside store fronts in New York?
Infinite past with a beginning?
Awk syntax, strange variable?
Do Phineas and Ferb ever actually get busted in real time?
Can I interfere when another PC is about to be attacked?
XeLaTeX and pdfLaTeX ignore hyphenation
I probably found a bug with the sudo apt install function
Repeatedly keep removing 2 smallest elements and add its sum back to the list, till it reduces to a single number? Need optimization for million+
Given a list of numbers and a number k, return whether any two numbers from the list add up to k
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have a list of unsorted numbers. Each time, i need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list.
My logic was to use heap sort, remove 2 elems, insert elems sum back - repeat
This program of input of 10 elements has called heapify function 310 times.!!
Link: http://cpp.sh/8vkjd
How do i optimize this scenario?
- Use Binary Search Tree?
- Use Heap Sort with LinkedList?
- Is this possible with graphs?
- Since there are several insertions, I'm trying to only restrict to O(N) or O(log N) or O(n log N) insertion.
- can this be done with 2 lists?
- since the elements will be sorted after the 1st iteration, is there a good insertion algorithm that does faster insertion
#include <iostream>
#include <sstream>
#include <vector>
int count = 0; // A global variable to keep track of number of time heapify function is called
void heapify(std::vector<int> &arr, int n, int i)
count++;
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l<n && arr[l] >arr[largest])
largest = l;
if(r<n && arr[r] > arr[largest])
largest = r;
if(largest != i)
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
void heapSort(std::vector<int> &arr, int n)
for(int i=n/2 -1 ; i>=0 ; i--)
heapify(arr,n,i);
for(int i=n-1 ; i>=0 ; i--)
std::swap(arr[0],arr[i]);
heapify(arr,i,0);
void printArray(std::vector<int> &arr, int n)
for(int i=0 ; i<n ; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
int main()
std::vector<int> arr1,2,3,4,5,6,7,8,9,10;
int n = arr.size();
printArray(arr,n);
heapSort(arr,n);
int ans=0;
int tempEle=0;
while(arr.size()>1)
heapSort(arr,n);
tempEle = arr[0]+arr[1];
arr.erase(arr.begin(),arr.begin()+2);
ans+=tempEle;
arr.push_back(tempEle);
printArray(arr,arr.size());
ans+=arr[0];
std::cout << "Repeatedly adding all elements, 2 small elements at a time and reducing it to a single number : " << ans << std::endl;
std::cout << "Heapify was called " << count << " times" << std::endl;
return 0;
c++ algorithm sorting tree heap
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I have a list of unsorted numbers. Each time, i need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list.
My logic was to use heap sort, remove 2 elems, insert elems sum back - repeat
This program of input of 10 elements has called heapify function 310 times.!!
Link: http://cpp.sh/8vkjd
How do i optimize this scenario?
- Use Binary Search Tree?
- Use Heap Sort with LinkedList?
- Is this possible with graphs?
- Since there are several insertions, I'm trying to only restrict to O(N) or O(log N) or O(n log N) insertion.
- can this be done with 2 lists?
- since the elements will be sorted after the 1st iteration, is there a good insertion algorithm that does faster insertion
#include <iostream>
#include <sstream>
#include <vector>
int count = 0; // A global variable to keep track of number of time heapify function is called
void heapify(std::vector<int> &arr, int n, int i)
count++;
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l<n && arr[l] >arr[largest])
largest = l;
if(r<n && arr[r] > arr[largest])
largest = r;
if(largest != i)
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
void heapSort(std::vector<int> &arr, int n)
for(int i=n/2 -1 ; i>=0 ; i--)
heapify(arr,n,i);
for(int i=n-1 ; i>=0 ; i--)
std::swap(arr[0],arr[i]);
heapify(arr,i,0);
void printArray(std::vector<int> &arr, int n)
for(int i=0 ; i<n ; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
int main()
std::vector<int> arr1,2,3,4,5,6,7,8,9,10;
int n = arr.size();
printArray(arr,n);
heapSort(arr,n);
int ans=0;
int tempEle=0;
while(arr.size()>1)
heapSort(arr,n);
tempEle = arr[0]+arr[1];
arr.erase(arr.begin(),arr.begin()+2);
ans+=tempEle;
arr.push_back(tempEle);
printArray(arr,arr.size());
ans+=arr[0];
std::cout << "Repeatedly adding all elements, 2 small elements at a time and reducing it to a single number : " << ans << std::endl;
std::cout << "Heapify was called " << count << " times" << std::endl;
return 0;
c++ algorithm sorting tree heap
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I have a list of unsorted numbers. Each time, i need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list.
My logic was to use heap sort, remove 2 elems, insert elems sum back - repeat
This program of input of 10 elements has called heapify function 310 times.!!
Link: http://cpp.sh/8vkjd
How do i optimize this scenario?
- Use Binary Search Tree?
- Use Heap Sort with LinkedList?
- Is this possible with graphs?
- Since there are several insertions, I'm trying to only restrict to O(N) or O(log N) or O(n log N) insertion.
- can this be done with 2 lists?
- since the elements will be sorted after the 1st iteration, is there a good insertion algorithm that does faster insertion
#include <iostream>
#include <sstream>
#include <vector>
int count = 0; // A global variable to keep track of number of time heapify function is called
void heapify(std::vector<int> &arr, int n, int i)
count++;
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l<n && arr[l] >arr[largest])
largest = l;
if(r<n && arr[r] > arr[largest])
largest = r;
if(largest != i)
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
void heapSort(std::vector<int> &arr, int n)
for(int i=n/2 -1 ; i>=0 ; i--)
heapify(arr,n,i);
for(int i=n-1 ; i>=0 ; i--)
std::swap(arr[0],arr[i]);
heapify(arr,i,0);
void printArray(std::vector<int> &arr, int n)
for(int i=0 ; i<n ; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
int main()
std::vector<int> arr1,2,3,4,5,6,7,8,9,10;
int n = arr.size();
printArray(arr,n);
heapSort(arr,n);
int ans=0;
int tempEle=0;
while(arr.size()>1)
heapSort(arr,n);
tempEle = arr[0]+arr[1];
arr.erase(arr.begin(),arr.begin()+2);
ans+=tempEle;
arr.push_back(tempEle);
printArray(arr,arr.size());
ans+=arr[0];
std::cout << "Repeatedly adding all elements, 2 small elements at a time and reducing it to a single number : " << ans << std::endl;
std::cout << "Heapify was called " << count << " times" << std::endl;
return 0;
c++ algorithm sorting tree heap
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I have a list of unsorted numbers. Each time, i need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list.
My logic was to use heap sort, remove 2 elems, insert elems sum back - repeat
This program of input of 10 elements has called heapify function 310 times.!!
Link: http://cpp.sh/8vkjd
How do i optimize this scenario?
- Use Binary Search Tree?
- Use Heap Sort with LinkedList?
- Is this possible with graphs?
- Since there are several insertions, I'm trying to only restrict to O(N) or O(log N) or O(n log N) insertion.
- can this be done with 2 lists?
- since the elements will be sorted after the 1st iteration, is there a good insertion algorithm that does faster insertion
#include <iostream>
#include <sstream>
#include <vector>
int count = 0; // A global variable to keep track of number of time heapify function is called
void heapify(std::vector<int> &arr, int n, int i)
count++;
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if(l<n && arr[l] >arr[largest])
largest = l;
if(r<n && arr[r] > arr[largest])
largest = r;
if(largest != i)
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
void heapSort(std::vector<int> &arr, int n)
for(int i=n/2 -1 ; i>=0 ; i--)
heapify(arr,n,i);
for(int i=n-1 ; i>=0 ; i--)
std::swap(arr[0],arr[i]);
heapify(arr,i,0);
void printArray(std::vector<int> &arr, int n)
for(int i=0 ; i<n ; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
int main()
std::vector<int> arr1,2,3,4,5,6,7,8,9,10;
int n = arr.size();
printArray(arr,n);
heapSort(arr,n);
int ans=0;
int tempEle=0;
while(arr.size()>1)
heapSort(arr,n);
tempEle = arr[0]+arr[1];
arr.erase(arr.begin(),arr.begin()+2);
ans+=tempEle;
arr.push_back(tempEle);
printArray(arr,arr.size());
ans+=arr[0];
std::cout << "Repeatedly adding all elements, 2 small elements at a time and reducing it to a single number : " << ans << std::endl;
std::cout << "Heapify was called " << count << " times" << std::endl;
return 0;
c++ algorithm sorting tree heap
c++ algorithm sorting tree heap
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 mins ago
ManjunathManjunath
12
12
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Manjunath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Manjunath is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217034%2frepeatedly-keep-removing-2-smallest-elements-and-add-its-sum-back-to-the-list-t%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Manjunath is a new contributor. Be nice, and check out our Code of Conduct.
Manjunath is a new contributor. Be nice, and check out our Code of Conduct.
Manjunath is a new contributor. Be nice, and check out our Code of Conduct.
Manjunath is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217034%2frepeatedly-keep-removing-2-smallest-elements-and-add-its-sum-back-to-the-list-t%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown