Program to find number of elements between two elements a and b (where a and b both are inclusive)

Issue

Given an unsorted array of size n, write a program to find number of elements between two user-defined elements a and b (where a and b both are inclusive) of a user-defined array.
Input : arr = [1, 2, 2, 7, 5, 4]
a=2 b=5
Output : 4
(The numbers are: 2, 2, 5, 4).
If a=6 b=15, then output will be 3 (The numbers are: 6, 7, 15)

I tried the following code, but for arr = [1, 3, 3, 9, 10, 4] and for a=9 & b=12, it is showing the output:- 2. But the output should be 3. I can’t figure out the proper logic to solve the question.

#include <stdio.h>

int main()
{
    int n,i,a,b,c=0,d=2;

    printf("Enter size of array: ");
    scanf("%d",&n);
    
    printf("Enter elements of array: ");

    int arr[n];
    
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
 
    printf("\nEnter lower limit element & upper limit element respectively: ");
    scanf("%d %d",&a,&b);

    for(i=0;i<n;i++){ 
        if(arr[i]==a || arr[i]==b){
            c++;
            d=0;
        }
        
        if(arr[i]>a && arr[i]<b){
            c++;
        }
    }

    printf("Number of elements in between two elements (Both Inclusive) = %d",c+d);

    return 0;
}

Please suggest the logic. I will be very thankful for that.

Solution

This statement in the last for loop

d=0;

produces a logical error.

One of approaches is to declare two variables instead of the one variable d.

For example

int lower_limit = 0, upper_limit = 0;

And then in the for loop to write

for ( i = 0; i < n; i++ ){ 
    if ( arr[i] >= a && arr[i] <= b ){
        c++;
        lower_limit |= a == arr[i];
        upper_limit != b == arr[i];  
    }
}

c += !lower_limit + !upper_limit;

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    int arr1[] = { 1, 2, 2, 7, 5, 4 };
    size_t n = sizeof( arr1 ) / sizeof( *arr1 );
    
    int a = 2, b = 5;
    
    size_t count = 0;
    int lower_limit = 0, upper_limit = 0;
    
    for ( size_t i = 0; i < n; i++ )
    {
        if ( a <= arr1[i] && arr1[i] <= b )
        {
            ++count;
            lower_limit |= arr1[i] == a;
            upper_limit |= arr1[i] == b;
        }
    }
    
    count += !lower_limit + !upper_limit;
    
    printf( "Number of elements between %d and %d is %zu\nv", a, b, count );
    
    a = 6; b = 15;
    
    count = 0;
    lower_limit = 0; upper_limit = 0;
    
    for ( size_t i = 0; i < n; i++ )
    {
        if ( a <= arr1[i] && arr1[i] <= b )
        {
            ++count;
            lower_limit |= arr1[i] == a;
            upper_limit |= arr1[i] == b;
        }
    }
    
    count += !lower_limit + !upper_limit;
    
    printf( "Number of elements between %d and %d is %zu\n", a, b, count );
    
    return 0;
}

The program output is

Number of elements between 2 and 5 is 4
Number of elements between 6 and 15 is 3

Answered By – Vlad from Moscow

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.