Code to find the sums of building heightsNot sure if I missed some obvious CPU brakesCode for sums in a...
Simulating rnorm() using runif()
Replacing Windows 7 security updates with anti-virus?
Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?
How many of these lines lie entirely in the interior of the original cube?
Running a subshell from the middle of the current command
Plot a function of two variables equal 0
How to deal with a cynical class?
Good allowance savings plan?
Life insurance that covers only simultaneous/dual deaths
Why do Australian milk farmers need to protest supermarkets' milk price?
What to do when during a meeting client's people start to (physically) fight with each other?
Why is "das Weib" grammatically neuter?
PTIJ: Coffee stains on Sefarim (holy books)
Could the Saturn V actually have launched astronauts around Venus?
What options are left, if Britain cannot decide?
Professor being mistaken for a grad student
Have researchers managed to "reverse time"? If so, what does that mean for physics?
If Invisibility ends because the original caster casts a non-concentration spell, does Invisibility also end on other targets of the original casting?
Why must traveling waves have the same amplitude to form a standing wave?
Instead of Universal Basic Income, why not Universal Basic NEEDS?
How to determine the cost of index creation?
Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?
Function to parse .NET composite string format
Can unconscious characters be unwilling?
Code to find the sums of building heights
Not sure if I missed some obvious CPU brakesCode for sums in a vectorMerging Digital RiverSolution to King Kohima problem in PythonFind All the Primes Between two NumbersBuilding abstract syntax tree for markargsTroublesome Frog (AIO Intermediate 2013) 3.xCalculating the number of zeroes at the end of a factorialAGGRCOW on SPOJ – Maximize the minimum distance between aggressive cowsFind the first palindrome larger than the given number
$begingroup$
Problem Statement
TLDR;
Heights of first two buildings and the total number of buildings are given. For any three consecutive buildings, the heights of the two smaller buildings sum up to the height of the largest building within that group of three. This property holds for all consecutive triples of buildings in the city.
Compute the sum of heights of all the buildings.
My Code:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long int t; // t = number of test cases
cin >> t;
long long int x, y, n; //x, y = Height of first & second building. n = number of buildings
long long int sum;
int arr[3];
long long int z;
for (long long int foo = 0; foo < t; foo++)
{
cin >> x >> y >> n;
sum = x + y;
for (long long int i = 2; i < n; i++)
{
z = abs(x - y);
sum += z;
x = y;
y = z;
}
cout << sum << endl;
}
}
Input 1: x, y, n ≤ 10
Input 2: 26 pts: x, y, n ≤ 100
Input 3: 13 pts: x, y, n ≤ 1,000,000,000
My code works for the first two inputs but goes on time limit exceeded on the third input.
c++ time-limit-exceeded c++14
New contributor
$endgroup$
add a comment |
$begingroup$
Problem Statement
TLDR;
Heights of first two buildings and the total number of buildings are given. For any three consecutive buildings, the heights of the two smaller buildings sum up to the height of the largest building within that group of three. This property holds for all consecutive triples of buildings in the city.
Compute the sum of heights of all the buildings.
My Code:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long int t; // t = number of test cases
cin >> t;
long long int x, y, n; //x, y = Height of first & second building. n = number of buildings
long long int sum;
int arr[3];
long long int z;
for (long long int foo = 0; foo < t; foo++)
{
cin >> x >> y >> n;
sum = x + y;
for (long long int i = 2; i < n; i++)
{
z = abs(x - y);
sum += z;
x = y;
y = z;
}
cout << sum << endl;
}
}
Input 1: x, y, n ≤ 10
Input 2: 26 pts: x, y, n ≤ 100
Input 3: 13 pts: x, y, n ≤ 1,000,000,000
My code works for the first two inputs but goes on time limit exceeded on the third input.
c++ time-limit-exceeded c++14
New contributor
$endgroup$
2
$begingroup$
It doesn't seem likeint arr[3]
is used for anything.
$endgroup$
– Juho
13 mins ago
$begingroup$
@Juho Yeah. But I don't think it hampers the speed much. I rewrote the code many a times so it must be from the last build.
$endgroup$
– harshit54
11 mins ago
$begingroup$
On what site is this challenge?
$endgroup$
– Mast
7 mins ago
1
$begingroup$
@Mast HackerEarth
$endgroup$
– harshit54
7 mins ago
add a comment |
$begingroup$
Problem Statement
TLDR;
Heights of first two buildings and the total number of buildings are given. For any three consecutive buildings, the heights of the two smaller buildings sum up to the height of the largest building within that group of three. This property holds for all consecutive triples of buildings in the city.
Compute the sum of heights of all the buildings.
My Code:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long int t; // t = number of test cases
cin >> t;
long long int x, y, n; //x, y = Height of first & second building. n = number of buildings
long long int sum;
int arr[3];
long long int z;
for (long long int foo = 0; foo < t; foo++)
{
cin >> x >> y >> n;
sum = x + y;
for (long long int i = 2; i < n; i++)
{
z = abs(x - y);
sum += z;
x = y;
y = z;
}
cout << sum << endl;
}
}
Input 1: x, y, n ≤ 10
Input 2: 26 pts: x, y, n ≤ 100
Input 3: 13 pts: x, y, n ≤ 1,000,000,000
My code works for the first two inputs but goes on time limit exceeded on the third input.
c++ time-limit-exceeded c++14
New contributor
$endgroup$
Problem Statement
TLDR;
Heights of first two buildings and the total number of buildings are given. For any three consecutive buildings, the heights of the two smaller buildings sum up to the height of the largest building within that group of three. This property holds for all consecutive triples of buildings in the city.
Compute the sum of heights of all the buildings.
My Code:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long int t; // t = number of test cases
cin >> t;
long long int x, y, n; //x, y = Height of first & second building. n = number of buildings
long long int sum;
int arr[3];
long long int z;
for (long long int foo = 0; foo < t; foo++)
{
cin >> x >> y >> n;
sum = x + y;
for (long long int i = 2; i < n; i++)
{
z = abs(x - y);
sum += z;
x = y;
y = z;
}
cout << sum << endl;
}
}
Input 1: x, y, n ≤ 10
Input 2: 26 pts: x, y, n ≤ 100
Input 3: 13 pts: x, y, n ≤ 1,000,000,000
My code works for the first two inputs but goes on time limit exceeded on the third input.
c++ time-limit-exceeded c++14
c++ time-limit-exceeded c++14
New contributor
New contributor
edited 14 mins ago
Toby Speight
26.2k742118
26.2k742118
New contributor
asked 19 mins ago
harshit54harshit54
1162
1162
New contributor
New contributor
2
$begingroup$
It doesn't seem likeint arr[3]
is used for anything.
$endgroup$
– Juho
13 mins ago
$begingroup$
@Juho Yeah. But I don't think it hampers the speed much. I rewrote the code many a times so it must be from the last build.
$endgroup$
– harshit54
11 mins ago
$begingroup$
On what site is this challenge?
$endgroup$
– Mast
7 mins ago
1
$begingroup$
@Mast HackerEarth
$endgroup$
– harshit54
7 mins ago
add a comment |
2
$begingroup$
It doesn't seem likeint arr[3]
is used for anything.
$endgroup$
– Juho
13 mins ago
$begingroup$
@Juho Yeah. But I don't think it hampers the speed much. I rewrote the code many a times so it must be from the last build.
$endgroup$
– harshit54
11 mins ago
$begingroup$
On what site is this challenge?
$endgroup$
– Mast
7 mins ago
1
$begingroup$
@Mast HackerEarth
$endgroup$
– harshit54
7 mins ago
2
2
$begingroup$
It doesn't seem like
int arr[3]
is used for anything.$endgroup$
– Juho
13 mins ago
$begingroup$
It doesn't seem like
int arr[3]
is used for anything.$endgroup$
– Juho
13 mins ago
$begingroup$
@Juho Yeah. But I don't think it hampers the speed much. I rewrote the code many a times so it must be from the last build.
$endgroup$
– harshit54
11 mins ago
$begingroup$
@Juho Yeah. But I don't think it hampers the speed much. I rewrote the code many a times so it must be from the last build.
$endgroup$
– harshit54
11 mins ago
$begingroup$
On what site is this challenge?
$endgroup$
– Mast
7 mins ago
$begingroup$
On what site is this challenge?
$endgroup$
– Mast
7 mins ago
1
1
$begingroup$
@Mast HackerEarth
$endgroup$
– harshit54
7 mins ago
$begingroup$
@Mast HackerEarth
$endgroup$
– harshit54
7 mins ago
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
});
}
});
harshit54 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%2f215443%2fcode-to-find-the-sums-of-building-heights%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
harshit54 is a new contributor. Be nice, and check out our Code of Conduct.
harshit54 is a new contributor. Be nice, and check out our Code of Conduct.
harshit54 is a new contributor. Be nice, and check out our Code of Conduct.
harshit54 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%2f215443%2fcode-to-find-the-sums-of-building-heights%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
2
$begingroup$
It doesn't seem like
int arr[3]
is used for anything.$endgroup$
– Juho
13 mins ago
$begingroup$
@Juho Yeah. But I don't think it hampers the speed much. I rewrote the code many a times so it must be from the last build.
$endgroup$
– harshit54
11 mins ago
$begingroup$
On what site is this challenge?
$endgroup$
– Mast
7 mins ago
1
$begingroup$
@Mast HackerEarth
$endgroup$
– harshit54
7 mins ago