my code is running well during execution…but i when i go to submit its show segmentation faultGiven integer n decide if it is possible to represent it as a sum of two squares of integersReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsc++ - hackerrank project euler #1 terminated due to timeoutWhen I tried to run the code I was getting the 2nd output as garbage value. Can anyone tell fault in that codeInversion counting giving trouble (implementation of modified merge sort)Maximum sub array modulo using Kadane algorithmc++ Variable Sized ArraysHow to debug my program that runs properly but returns segmentation fault in an ide?Reduce subsequence problem complexity from exponential to polynomial?I am having problem with this question my output is not correct
Is floating in space similar to falling under gravity?
Employer demanding to see degree after poor code review
Rename photos to match video titles
Ticket sales for Queen at the Live Aid
Is it ok to put a subplot to a story that is never meant to contribute to the development of the main plot?
Were pens caps holes designed to prevent death by suffocation if swallowed?
Windows 10 Programs start without visual Interface
What do different value notes on the same line mean?
Dictionary size reduces upon increasing one element
How can I get exact maximal value of this expression?
Is healing by fire possible?
Is there a general effective method to solve Smullyan style Knights and Knaves problems? Is the truth table method the most appropriate one?
Why do airplanes use an axial flow jet engine instead of a more compact centrifugal jet engine?
How bitcoin nodes update UTXO set when their latests blocks are replaced?
What is the largest (size) solid object ever dropped from an airplane to impact the ground in freefall?
Are there situations when self-assignment is useful?
Where did Wilson state that the US would have to force access to markets with violence?
How did early x86 BIOS programmers manage to program full blown TUIs given very few bytes of ROM/EPROM?
When did God say "let all the angels of God worship him" as stated in Hebrews 1:6?
Seed ship, unsexed person, cover has golden person attached to ship by umbilical cord
How to capture more stars?
Mother abusing my finances
Why doesn't the Earth's acceleration towards the Moon accumulate to push the Earth off its orbit?
Is there a down side to setting the sampling time of a SAR ADC as long as possible?
my code is running well during execution…but i when i go to submit its show segmentation fault
Given integer n decide if it is possible to represent it as a sum of two squares of integersReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsc++ - hackerrank project euler #1 terminated due to timeoutWhen I tried to run the code I was getting the 2nd output as garbage value. Can anyone tell fault in that codeInversion counting giving trouble (implementation of modified merge sort)Maximum sub array modulo using Kadane algorithmc++ Variable Sized ArraysHow to debug my program that runs properly but returns segmentation fault in an ide?Reduce subsequence problem complexity from exponential to polynomial?I am having problem with this question my output is not correct
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
i have done this in c++
#include <iostream>
using namespace std;
int main()
int a,n,arr[50],j=1,element;
int sum,total[50],i;
cin>>n;
a=n;
while(n!=0)
cin>>element;
sum=0;
total[j]=0;
for(i=1;i<=element-1;i++)
cin>>arr[i];
sum=sum+arr[i];
total[j]=(element*(element+1)/2) -sum;
j++;
n--;
for(i=1;i<=a;i++)
cout<<total[i]<<endl;
return 0;
Output:
4
9
c++
add a comment |
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
i have done this in c++
#include <iostream>
using namespace std;
int main()
int a,n,arr[50],j=1,element;
int sum,total[50],i;
cin>>n;
a=n;
while(n!=0)
cin>>element;
sum=0;
total[j]=0;
for(i=1;i<=element-1;i++)
cin>>arr[i];
sum=sum+arr[i];
total[j]=(element*(element+1)/2) -sum;
j++;
n--;
for(i=1;i<=a;i++)
cout<<total[i]<<endl;
return 0;
Output:
4
9
c++
1
Look at the constraints, and look at the sizes of the arrays you declared, and I think you'll see the problem.
– john
Mar 24 at 7:24
Nice way to solve the problem BTW, although you don't need thetotal
array. Just output each result as you get it, instead of putting them all in an array and outputting them at the end.
– john
Mar 24 at 7:27
1
Ifn
is 50 or more,total[j]
will perform access beyond the array limit... Indexing in C starts from 0.
– Alex Lop.
Mar 24 at 7:28
Why have you included "python" tag ??
– Vishal Srivastav
Mar 24 at 7:33
1
@VishalSrivastav I've deleted python and C
– john
Mar 24 at 7:34
add a comment |
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
i have done this in c++
#include <iostream>
using namespace std;
int main()
int a,n,arr[50],j=1,element;
int sum,total[50],i;
cin>>n;
a=n;
while(n!=0)
cin>>element;
sum=0;
total[j]=0;
for(i=1;i<=element-1;i++)
cin>>arr[i];
sum=sum+arr[i];
total[j]=(element*(element+1)/2) -sum;
j++;
n--;
for(i=1;i<=a;i++)
cout<<total[i]<<endl;
return 0;
Output:
4
9
c++
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
i have done this in c++
#include <iostream>
using namespace std;
int main()
int a,n,arr[50],j=1,element;
int sum,total[50],i;
cin>>n;
a=n;
while(n!=0)
cin>>element;
sum=0;
total[j]=0;
for(i=1;i<=element-1;i++)
cin>>arr[i];
sum=sum+arr[i];
total[j]=(element*(element+1)/2) -sum;
j++;
n--;
for(i=1;i<=a;i++)
cout<<total[i]<<endl;
return 0;
Output:
4
9
c++
c++
edited Mar 24 at 8:05
Omnifarious
41.3k11101164
41.3k11101164
asked Mar 24 at 7:21
user11249680user11249680
4
4
1
Look at the constraints, and look at the sizes of the arrays you declared, and I think you'll see the problem.
– john
Mar 24 at 7:24
Nice way to solve the problem BTW, although you don't need thetotal
array. Just output each result as you get it, instead of putting them all in an array and outputting them at the end.
– john
Mar 24 at 7:27
1
Ifn
is 50 or more,total[j]
will perform access beyond the array limit... Indexing in C starts from 0.
– Alex Lop.
Mar 24 at 7:28
Why have you included "python" tag ??
– Vishal Srivastav
Mar 24 at 7:33
1
@VishalSrivastav I've deleted python and C
– john
Mar 24 at 7:34
add a comment |
1
Look at the constraints, and look at the sizes of the arrays you declared, and I think you'll see the problem.
– john
Mar 24 at 7:24
Nice way to solve the problem BTW, although you don't need thetotal
array. Just output each result as you get it, instead of putting them all in an array and outputting them at the end.
– john
Mar 24 at 7:27
1
Ifn
is 50 or more,total[j]
will perform access beyond the array limit... Indexing in C starts from 0.
– Alex Lop.
Mar 24 at 7:28
Why have you included "python" tag ??
– Vishal Srivastav
Mar 24 at 7:33
1
@VishalSrivastav I've deleted python and C
– john
Mar 24 at 7:34
1
1
Look at the constraints, and look at the sizes of the arrays you declared, and I think you'll see the problem.
– john
Mar 24 at 7:24
Look at the constraints, and look at the sizes of the arrays you declared, and I think you'll see the problem.
– john
Mar 24 at 7:24
Nice way to solve the problem BTW, although you don't need the
total
array. Just output each result as you get it, instead of putting them all in an array and outputting them at the end.– john
Mar 24 at 7:27
Nice way to solve the problem BTW, although you don't need the
total
array. Just output each result as you get it, instead of putting them all in an array and outputting them at the end.– john
Mar 24 at 7:27
1
1
If
n
is 50 or more, total[j]
will perform access beyond the array limit... Indexing in C starts from 0.– Alex Lop.
Mar 24 at 7:28
If
n
is 50 or more, total[j]
will perform access beyond the array limit... Indexing in C starts from 0.– Alex Lop.
Mar 24 at 7:28
Why have you included "python" tag ??
– Vishal Srivastav
Mar 24 at 7:33
Why have you included "python" tag ??
– Vishal Srivastav
Mar 24 at 7:33
1
1
@VishalSrivastav I've deleted python and C
– john
Mar 24 at 7:34
@VishalSrivastav I've deleted python and C
– john
Mar 24 at 7:34
add a comment |
3 Answers
3
active
oldest
votes
Try this as a test case:
2
55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
55
33 54 1 55 23 51 35 5 49 26 43 42 41 32 7 14 9 50 34 29 37 25 52 24 8 53 15 45 44 19 39 47 17 48 38 21 36 31 3 18 46 20 6 27 12 11 16 10 2 13 28 4 30 22
your code should then crash under your control, and then you can find out why.
Think hard about the constraints you were given for various values and how your code operates at the edges of those constraints.
Also, there is a way to solve this problem that doesn't require you to use any arrays at all. It's the fastest way to solve the problem.
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
add a comment |
Because you have declared the array of size 50 blocks.
But , according to given constraints : 1 ≤ N ≤ 107 , 1 ≤ C[i] ≤ 107
if you take value of element more than 50 , it will give segmentation fault because the maximum size of array is 50.
So , increase the size of arrays to arr[107]; and total[107];
And then submit the solution , it would work then.
1 ≤ T ≤ 200
sototal
needs to betotal[201]
given the code as is (or it could just be eliminated)
– john
Mar 24 at 7:31
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
add a comment |
One of the best solution for finding Missing element is using XOR operator :
Question: You are given a list of n-1 integers , And these integers are in the range of 1
to n. There are no duplicates in the list . One of the integer is missing in the list.
Now, we need to find that missing number.
Solution :
Method 1 : By finding sum of first “n” natural numbers.
Step 1 : First, find the sum of all numbers from 1 to n ( means find the sum of first “n” natural numbers)
Step 2 : Subtract each element (all elements) of the given list from the sum. And we’ll get the missing number.
Note : There might be an integer overflow while adding large numbers in method 1 solution.
Method 2: Using XOR operator :
Remember these properties : n ^ n = 0 AND n ^ 0 = n
Step 1: We’ll take the xor of all numbers from 1 to n (Means, we’ll take xor of first “n” natural numbers).
Step 2: We’ll take the xor of all elements of the given array (list).
Step 3: xor of Step 1 and Step 2 will give the required missing number.
Example : Given list arr = [4,2,1,6,8,5,3,9] n=9 (given)
Step1 : Step1_result = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 = 1
Step2 : Step2_result = 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 = 6
Step3 : Final_Result = Step1_result ^ Step2_result = 1 ^ 6 = 7
But, How Final_Result calculated the missing number ?
Final_Result= ( 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
So , here ,
Final_Result = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
= 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ^ 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9
= ( 1 ^ 1 ) ^ ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ ( 4 ^ 4 ) ^ ( 5 ^ 5 ) ^ ( 6 ^ 6 ) ^ (7) ^ ( 8 ^ 8 ) ^ ( 9 ^ 9 )
= 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 7 ^ 0 ^ 0 ( property applied )
= 0 ^ 7 ( because we know 0 ^ 0 = 0 )
= 000 ^ 111
= 111
= 7 ( Required Result )
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
add a comment |
Your Answer
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: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);
);
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%2fstackoverflow.com%2fquestions%2f55321564%2fmy-code-is-running-well-during-execution-but-i-when-i-go-to-submit-its-show-se%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this as a test case:
2
55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
55
33 54 1 55 23 51 35 5 49 26 43 42 41 32 7 14 9 50 34 29 37 25 52 24 8 53 15 45 44 19 39 47 17 48 38 21 36 31 3 18 46 20 6 27 12 11 16 10 2 13 28 4 30 22
your code should then crash under your control, and then you can find out why.
Think hard about the constraints you were given for various values and how your code operates at the edges of those constraints.
Also, there is a way to solve this problem that doesn't require you to use any arrays at all. It's the fastest way to solve the problem.
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
add a comment |
Try this as a test case:
2
55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
55
33 54 1 55 23 51 35 5 49 26 43 42 41 32 7 14 9 50 34 29 37 25 52 24 8 53 15 45 44 19 39 47 17 48 38 21 36 31 3 18 46 20 6 27 12 11 16 10 2 13 28 4 30 22
your code should then crash under your control, and then you can find out why.
Think hard about the constraints you were given for various values and how your code operates at the edges of those constraints.
Also, there is a way to solve this problem that doesn't require you to use any arrays at all. It's the fastest way to solve the problem.
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
add a comment |
Try this as a test case:
2
55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
55
33 54 1 55 23 51 35 5 49 26 43 42 41 32 7 14 9 50 34 29 37 25 52 24 8 53 15 45 44 19 39 47 17 48 38 21 36 31 3 18 46 20 6 27 12 11 16 10 2 13 28 4 30 22
your code should then crash under your control, and then you can find out why.
Think hard about the constraints you were given for various values and how your code operates at the edges of those constraints.
Also, there is a way to solve this problem that doesn't require you to use any arrays at all. It's the fastest way to solve the problem.
Try this as a test case:
2
55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
55
33 54 1 55 23 51 35 5 49 26 43 42 41 32 7 14 9 50 34 29 37 25 52 24 8 53 15 45 44 19 39 47 17 48 38 21 36 31 3 18 46 20 6 27 12 11 16 10 2 13 28 4 30 22
your code should then crash under your control, and then you can find out why.
Think hard about the constraints you were given for various values and how your code operates at the edges of those constraints.
Also, there is a way to solve this problem that doesn't require you to use any arrays at all. It's the fastest way to solve the problem.
answered Mar 24 at 7:55
OmnifariousOmnifarious
41.3k11101164
41.3k11101164
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
add a comment |
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
what is the other way..i want to know.....
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
and i have run by code its working fine....but while submitting they showed and error... Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:38
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680 - You actually have the bones of the correct answer in your answer. You're doing a lot more work than you need to.
– Omnifarious
Mar 24 at 14:23
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
@user11249680- godbolt.org/z/02hS7p
– Omnifarious
Mar 24 at 14:55
add a comment |
Because you have declared the array of size 50 blocks.
But , according to given constraints : 1 ≤ N ≤ 107 , 1 ≤ C[i] ≤ 107
if you take value of element more than 50 , it will give segmentation fault because the maximum size of array is 50.
So , increase the size of arrays to arr[107]; and total[107];
And then submit the solution , it would work then.
1 ≤ T ≤ 200
sototal
needs to betotal[201]
given the code as is (or it could just be eliminated)
– john
Mar 24 at 7:31
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
add a comment |
Because you have declared the array of size 50 blocks.
But , according to given constraints : 1 ≤ N ≤ 107 , 1 ≤ C[i] ≤ 107
if you take value of element more than 50 , it will give segmentation fault because the maximum size of array is 50.
So , increase the size of arrays to arr[107]; and total[107];
And then submit the solution , it would work then.
1 ≤ T ≤ 200
sototal
needs to betotal[201]
given the code as is (or it could just be eliminated)
– john
Mar 24 at 7:31
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
add a comment |
Because you have declared the array of size 50 blocks.
But , according to given constraints : 1 ≤ N ≤ 107 , 1 ≤ C[i] ≤ 107
if you take value of element more than 50 , it will give segmentation fault because the maximum size of array is 50.
So , increase the size of arrays to arr[107]; and total[107];
And then submit the solution , it would work then.
Because you have declared the array of size 50 blocks.
But , according to given constraints : 1 ≤ N ≤ 107 , 1 ≤ C[i] ≤ 107
if you take value of element more than 50 , it will give segmentation fault because the maximum size of array is 50.
So , increase the size of arrays to arr[107]; and total[107];
And then submit the solution , it would work then.
answered Mar 24 at 7:30
Vishal SrivastavVishal Srivastav
12118
12118
1 ≤ T ≤ 200
sototal
needs to betotal[201]
given the code as is (or it could just be eliminated)
– john
Mar 24 at 7:31
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
add a comment |
1 ≤ T ≤ 200
sototal
needs to betotal[201]
given the code as is (or it could just be eliminated)
– john
Mar 24 at 7:31
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
1 ≤ T ≤ 200
so total
needs to be total[201]
given the code as is (or it could just be eliminated)– john
Mar 24 at 7:31
1 ≤ T ≤ 200
so total
needs to be total[201]
given the code as is (or it could just be eliminated)– john
Mar 24 at 7:31
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
i have change peremiter from 50 to 500...thanks to u..i forget to look at constraints now my code is working fine..one problem is their ..while submitting its showing me Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: 5 1 2 3 5 Its Correct output is: 4 And Your Code's output is: 501
– user11249680
Mar 24 at 9:41
add a comment |
One of the best solution for finding Missing element is using XOR operator :
Question: You are given a list of n-1 integers , And these integers are in the range of 1
to n. There are no duplicates in the list . One of the integer is missing in the list.
Now, we need to find that missing number.
Solution :
Method 1 : By finding sum of first “n” natural numbers.
Step 1 : First, find the sum of all numbers from 1 to n ( means find the sum of first “n” natural numbers)
Step 2 : Subtract each element (all elements) of the given list from the sum. And we’ll get the missing number.
Note : There might be an integer overflow while adding large numbers in method 1 solution.
Method 2: Using XOR operator :
Remember these properties : n ^ n = 0 AND n ^ 0 = n
Step 1: We’ll take the xor of all numbers from 1 to n (Means, we’ll take xor of first “n” natural numbers).
Step 2: We’ll take the xor of all elements of the given array (list).
Step 3: xor of Step 1 and Step 2 will give the required missing number.
Example : Given list arr = [4,2,1,6,8,5,3,9] n=9 (given)
Step1 : Step1_result = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 = 1
Step2 : Step2_result = 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 = 6
Step3 : Final_Result = Step1_result ^ Step2_result = 1 ^ 6 = 7
But, How Final_Result calculated the missing number ?
Final_Result= ( 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
So , here ,
Final_Result = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
= 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ^ 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9
= ( 1 ^ 1 ) ^ ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ ( 4 ^ 4 ) ^ ( 5 ^ 5 ) ^ ( 6 ^ 6 ) ^ (7) ^ ( 8 ^ 8 ) ^ ( 9 ^ 9 )
= 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 7 ^ 0 ^ 0 ( property applied )
= 0 ^ 7 ( because we know 0 ^ 0 = 0 )
= 000 ^ 111
= 111
= 7 ( Required Result )
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
add a comment |
One of the best solution for finding Missing element is using XOR operator :
Question: You are given a list of n-1 integers , And these integers are in the range of 1
to n. There are no duplicates in the list . One of the integer is missing in the list.
Now, we need to find that missing number.
Solution :
Method 1 : By finding sum of first “n” natural numbers.
Step 1 : First, find the sum of all numbers from 1 to n ( means find the sum of first “n” natural numbers)
Step 2 : Subtract each element (all elements) of the given list from the sum. And we’ll get the missing number.
Note : There might be an integer overflow while adding large numbers in method 1 solution.
Method 2: Using XOR operator :
Remember these properties : n ^ n = 0 AND n ^ 0 = n
Step 1: We’ll take the xor of all numbers from 1 to n (Means, we’ll take xor of first “n” natural numbers).
Step 2: We’ll take the xor of all elements of the given array (list).
Step 3: xor of Step 1 and Step 2 will give the required missing number.
Example : Given list arr = [4,2,1,6,8,5,3,9] n=9 (given)
Step1 : Step1_result = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 = 1
Step2 : Step2_result = 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 = 6
Step3 : Final_Result = Step1_result ^ Step2_result = 1 ^ 6 = 7
But, How Final_Result calculated the missing number ?
Final_Result= ( 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
So , here ,
Final_Result = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
= 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ^ 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9
= ( 1 ^ 1 ) ^ ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ ( 4 ^ 4 ) ^ ( 5 ^ 5 ) ^ ( 6 ^ 6 ) ^ (7) ^ ( 8 ^ 8 ) ^ ( 9 ^ 9 )
= 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 7 ^ 0 ^ 0 ( property applied )
= 0 ^ 7 ( because we know 0 ^ 0 = 0 )
= 000 ^ 111
= 111
= 7 ( Required Result )
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
add a comment |
One of the best solution for finding Missing element is using XOR operator :
Question: You are given a list of n-1 integers , And these integers are in the range of 1
to n. There are no duplicates in the list . One of the integer is missing in the list.
Now, we need to find that missing number.
Solution :
Method 1 : By finding sum of first “n” natural numbers.
Step 1 : First, find the sum of all numbers from 1 to n ( means find the sum of first “n” natural numbers)
Step 2 : Subtract each element (all elements) of the given list from the sum. And we’ll get the missing number.
Note : There might be an integer overflow while adding large numbers in method 1 solution.
Method 2: Using XOR operator :
Remember these properties : n ^ n = 0 AND n ^ 0 = n
Step 1: We’ll take the xor of all numbers from 1 to n (Means, we’ll take xor of first “n” natural numbers).
Step 2: We’ll take the xor of all elements of the given array (list).
Step 3: xor of Step 1 and Step 2 will give the required missing number.
Example : Given list arr = [4,2,1,6,8,5,3,9] n=9 (given)
Step1 : Step1_result = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 = 1
Step2 : Step2_result = 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 = 6
Step3 : Final_Result = Step1_result ^ Step2_result = 1 ^ 6 = 7
But, How Final_Result calculated the missing number ?
Final_Result= ( 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
So , here ,
Final_Result = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
= 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ^ 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9
= ( 1 ^ 1 ) ^ ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ ( 4 ^ 4 ) ^ ( 5 ^ 5 ) ^ ( 6 ^ 6 ) ^ (7) ^ ( 8 ^ 8 ) ^ ( 9 ^ 9 )
= 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 7 ^ 0 ^ 0 ( property applied )
= 0 ^ 7 ( because we know 0 ^ 0 = 0 )
= 000 ^ 111
= 111
= 7 ( Required Result )
One of the best solution for finding Missing element is using XOR operator :
Question: You are given a list of n-1 integers , And these integers are in the range of 1
to n. There are no duplicates in the list . One of the integer is missing in the list.
Now, we need to find that missing number.
Solution :
Method 1 : By finding sum of first “n” natural numbers.
Step 1 : First, find the sum of all numbers from 1 to n ( means find the sum of first “n” natural numbers)
Step 2 : Subtract each element (all elements) of the given list from the sum. And we’ll get the missing number.
Note : There might be an integer overflow while adding large numbers in method 1 solution.
Method 2: Using XOR operator :
Remember these properties : n ^ n = 0 AND n ^ 0 = n
Step 1: We’ll take the xor of all numbers from 1 to n (Means, we’ll take xor of first “n” natural numbers).
Step 2: We’ll take the xor of all elements of the given array (list).
Step 3: xor of Step 1 and Step 2 will give the required missing number.
Example : Given list arr = [4,2,1,6,8,5,3,9] n=9 (given)
Step1 : Step1_result = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 = 1
Step2 : Step2_result = 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 = 6
Step3 : Final_Result = Step1_result ^ Step2_result = 1 ^ 6 = 7
But, How Final_Result calculated the missing number ?
Final_Result= ( 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
So , here ,
Final_Result = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ) ^ ( 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9 )
= 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9 ^ 4 ^ 2 ^ 1 ^ 6 ^ 8 ^ 5 ^ 3 ^ 9
= ( 1 ^ 1 ) ^ ( 2 ^ 2 ) ^ ( 3 ^ 3 ) ^ ( 4 ^ 4 ) ^ ( 5 ^ 5 ) ^ ( 6 ^ 6 ) ^ (7) ^ ( 8 ^ 8 ) ^ ( 9 ^ 9 )
= 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 7 ^ 0 ^ 0 ( property applied )
= 0 ^ 7 ( because we know 0 ^ 0 = 0 )
= 000 ^ 111
= 111
= 7 ( Required Result )
answered Mar 24 at 7:55
Vishal SrivastavVishal Srivastav
12118
12118
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
add a comment |
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
This doesn't answer the original poster's question at all. And additionally, just gives the original poster an alternate answer to a problem. This is clearly a problem from some coding problem website. Giving them the answer is the opposite of helpful. The point of those websites is so you can figure out the answer yourself. Lastly, your answer is less efficient than it could be for either method you chose.
– Omnifarious
Mar 24 at 7:59
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
Though the xor method is clever. I'll give you that. Though, given that an alternate set of operations for binary numbers that allows all algebraic operations to work is to use xor for add, the two methods are basically equivalent formulations of the same solution, just using different numeric fields.
– Omnifarious
Mar 24 at 8:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f55321564%2fmy-code-is-running-well-during-execution-but-i-when-i-go-to-submit-its-show-se%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
1
Look at the constraints, and look at the sizes of the arrays you declared, and I think you'll see the problem.
– john
Mar 24 at 7:24
Nice way to solve the problem BTW, although you don't need the
total
array. Just output each result as you get it, instead of putting them all in an array and outputting them at the end.– john
Mar 24 at 7:27
1
If
n
is 50 or more,total[j]
will perform access beyond the array limit... Indexing in C starts from 0.– Alex Lop.
Mar 24 at 7:28
Why have you included "python" tag ??
– Vishal Srivastav
Mar 24 at 7:33
1
@VishalSrivastav I've deleted python and C
– john
Mar 24 at 7:34