C program to double the first element and move zero to end for given array

In this blog post, we learn how to write a C program to double the first element and move zero to end for a given array? Here we are supposing that for a given array of n integers ‘0’ is an invalid number and all others as a valid number.

We need to convert the array in such a way that if both the current and next element is valid and same then double the current value and replace the next number with 0. After the modification, rearrange the array such that all 0’s shifted to the end.

Example,

Input array : int arr[] = {2, 2, 0, 4, 0, 8};

Output array: 4 4 8 0 0 0



Input array: int arr[] = {0, 2, 2, 2, 0, 6, 6, 0, 0, 8};

Output array:  4 2 12 8 0 0 0 0 0 0

 

Algorithm for double the first element and move zero to the end:

So let’s see the logic to double the first element and move zero to end for a given array. Suppose arr is a given input integer array of size N (arr[N] ).

1. Traverse the array from 0 to n-1(inclusively).

2. Check the current and next values using the if condition. If arr[i] is not equal to ‘0’ and (arr[i]==arr[i+1]), where arr[i] is current value.

  1. If the condition is true, then make the current value twice of the self.
  2. Update next element as 0 and do i++.

3. After converting the array you need to shift all zero to the end. We have already written an article on “how to shift all zero at the end”, you can check. Read the article.

4.  In the last now print the array.

If you want to learn more about the C language, you can check this course, Free Trial Available.

C program to double the first element and move zero to the end

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


//swap function not handle null pointer scenario
//not much safe to use
void swap(int *s1, int *s2)
{
    int temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}


// Function to move all zeros present in the array to the end
void moveAllZeroesAtArrayEnd(int arr[], int n)
{
    int i =0,j = 0;
    // each time we encounter a non-zero, j is incremented and
    // the element is placed before the pivot
    for (i = 0; i < n; i++)
    {
        if (arr[i] != 0)//non-zero
        {
            swap(&arr[i],&arr[j]);
            j++;
        }
    }
}

// function to rearrange the array elements
// after modification
int arrayModification(int arr[], int n)
{
    int i =0;

    const int status  = (n>1)? 0: -1;

    // traverse the array
    for (i = 0; i < n - 1; i++)
    {
        // if current element valid and equal to next
        if ((arr[i] != 0) && (arr[i] == arr[i + 1]))
        {
            // double current index value
            arr[i] = 2 * arr[i];

            // put 0 in the next index
            arr[i + 1] = 0;
            // increment by 1 so as to move two
            // indexes ahead during loop iteration
            i++;
        }
    }
    //call function when input array is valid
    if(status ==0)
    {
        //move all the zeros at the end of the array
        moveAllZeroesAtArrayEnd(arr, n);
    }

    return status;
}


int main()
{
    int arr[] = { 0, 2, 2, 2, 0, 6, 6, 0, 0, 8 };
    int i = 0;

    //get array size
    int arr_size = ARRAY_SIZE(arr);

    //modify and rearrange the array
    const int status = arrayModification(arr, arr_size);

    if(status == 0)
    {
        //print array element
        for (i = 0; i < arr_size; i++)
        {
            printf("%d ",arr[i]);
        }
    }
    else
    {
        printf("Enter valid array !");
    }
    
    return 0;
}

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *