Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
Find Sequences of any range

Given an array of numbers, print the each and every range available. For example Array : 9, 3, 5, 7, 4, 8, 1 Output: 1, 3-5, 7-9 Note: Please execute this problem without using an additional array.

How do i proceed? *

#include<stdio.h>
int main()
{
int a[]={9,8,8,7,6,5,14};
int n= sizeof(a) / sizeof(a[0]);
int i,j;
int temp;
for(i=0;i<n;i++)
         {
               for(j=i+1;j<n;j++)
               {
                     if(a[i]>a[j])
                     {
                           temp=a[i];
                           a[i]=a[j];
                           a[j]=temp;
                     }
               }
        }
}

* 1st i will sort in ascending order, i don't know what to do next? P.S : I am coding this in C.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The next step is to identify sequences. Try the following loop (not fully debugged):

first= next= a[0];
for (i=1; i<n; i++) {
    if (a[i] > next+1) {
        if (next>first)
             printf("%d-%d,", first, next);
        else printf("%d,", first);
        first= next= a[i];
    }
    else next++;
}
over 4 years ago · Santiago Trujillo Denunciar

0

I wrote a simple, readable function for you, take a look:

void printRange(int sortedArray[], int len) {
    int i, current, next, printStart, printEnd, startIndex = 0;
    bool print = false;

    for (i = 0; i < len; i++) {
        printStart = sortedArray[startIndex];
        printEnd = sortedArray[i];

        current = sortedArray[i];
        if(i < len -1) {
             next = sortedArray[i + 1];
        } else
              next = current;

        if (next - current != 1) {
            startIndex = i + 1;
            print = true;
        }

        if (print) {
            if (printStart - printEnd == 0) {
                printf("%d,", printStart);
            } else {
                printf("%d-%d,", printStart, printEnd);
            }
            print = false;
        }
    }
}

Run live.

Note, for good understanding variable current is declared whereas current and printEnd is same. You may replace current by printEnd.

over 4 years ago · Santiago Trujillo Denunciar

0

If you may to change the original array that is if you may to sort it then the program can look like

#include <stdlib.h>
#include <stdio.h>

int cmp( const void *lhs, const void *rhs )
{
    int a = *( const int * )lhs;
    int b = *( const int * )rhs;

    return ( b < a ) - ( a < b );
}

int main()
{
    int a[] = { 9, 8, 8, 7, 6, 5, 14 };
    const size_t N = sizeof( a ) / sizeof( *a );

    qsort( a, N, sizeof( int ), cmp );
/*    
    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
*/    
    int *p = a;
    int *start = a, *end = a;
    do
    {
        if ( ++p == a + N || *p  != *end + 1 )
        {
            printf( "{ %d", *start );
            start == end ? printf( " }\n" ) : printf( ", %d }\n", *end );
            start = end = p;
        }
        else
        {
            end = p;
        }
    } while ( p != a + N );            
}    

The program output is

{ 5, 8 }
{ 8, 9 }
{ 14 }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda