Deutsch   English   Français   Italiano  
<vrfuqs$2608a$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: DFS <nospam@dfs.com>
Newsgroups: comp.lang.c
Subject: Re: Suggested method for returning a string from a C program?
Date: Wed, 19 Mar 2025 22:34:04 -0400
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <vrfuqs$2608a$2@dont-email.me>
References: <vrd77d$3nvtf$2@dont-email.me>
 <slrnvtkrqc.ii1.ike@faeroes.freeshell.org>
 <875xk5v1td.fsf@nosuchdomain.example.com> <vrfadv$1jska$2@dont-email.me>
 <87o6xwu1la.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 20 Mar 2025 03:34:07 +0100 (CET)
Injection-Info: dont-email.me; posting-host="6ed1d979db33810c8b2880032b171297";
	logging-data="2294026"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+LqYGo4HEWi6xIYUS1ACzB"
User-Agent: Betterbird (Windows)
Cancel-Lock: sha1:I/wQQOQfvOF5q0rBOm9js8ppxSE=
In-Reply-To: <87o6xwu1la.fsf@nosuchdomain.example.com>
Content-Language: en-US
Bytes: 2865

On 3/19/2025 5:56 PM, Keith Thompson wrote:
> DFS <nospam@dfs.com> writes:
>> On 3/19/2025 4:53 AM, Keith Thompson wrote:

> 
> I used a different approach.  I'll encode the description of the
> solution using rot13.
> 
> The program is given an integer n and a list of n-1 integers, not
> necessarily ordered.
> 
> The task is to determine which number is missing from the list.
> 
> Gurer'f n jryy xabja sbezhyn sbe gur fhz bs nyy a ahzoref sebz bar
> gb a.  Pbzchgr gur rkcrpgrq fhz, gura fhogenpg gur fhz bs gur ahzoref
> tvira ba gur frpbaq vachg yvar.  Gur qvssrerapr vf gur zvffvat ahzore.



That's dead simple!  It works because the input numbers start with 1.

I'll give it a quick try.

edit:  my attempt passed 9 of 14 tests, but fails on large N because 
it's not calculating sum(1..N) correctly. Line 10.  See anything?


//identify the missing number in a set of otherwise consecutive integers
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
	int i = 0, N = 0, temp = 0;		
	int64_t totN = 0, totInputs = 0;
	scanf("%d", &N);                 //number of elements
	for (i = 0; i < N-1; i++) {      //list of elements
		scanf("%d", &temp);      //to temp var
		totInputs += temp;       //running total
	}
	totN = (N * (N + 1)) / 2;        //sum of numbers 1 to N
	printf("N          %lld\n",N);
	printf("tot N      %lld\n",totN);
	printf("tot inputs %lld\n",totInputs);
	printf("%d\n", totN - totInputs);  //solution
	return 0;
}	


sample output
N          50000
tot N      -897458648  FAIL  (should be 1250025000)
tot inputs 1250017374  GOOD
-2147476022            FAIL  (should be 7626)