Solve according to C language | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Solve according to C language

You are given a sorted (either in the increasing or in the decreasing order) sequence of numbers, ending with a -1. You can assume that are at least two numbers before the ending -1.  Let us call the sequence x0 x1 ... xn -1. You have to output the number of distinct elements the sorted sequence. Kindly do not use arrays in the code. 

6th Sep 2018, 1:36 PM
Vaibhav Jo-C
Vaibhav Jo-C - avatar
2 Answers
+ 1
Given the input is sorted, when the current number is equal to the previous number it is not distinct so shouldn't be counted.
6th Sep 2018, 2:47 PM
John Wells
John Wells - avatar
0
#include<stdio.h> int main() { int prev,curr,diff=0; /* at least two numbers are present */ scanf("%d %d",&prev,&curr); printf("%d", curr-prev); do { prev = curr; // shift current to previous scanf("%d", &curr); // read next : the new current if (curr == -1) { // we are done printf("\n"); // print newline and break break; } diff = curr - prev; // compute new sum of pairs printf(" %d", diff); // print sum with a space } while(1); return 0; }
6th Sep 2018, 2:48 PM
Vaibhav Jo-C
Vaibhav Jo-C - avatar