Making Elements Distinct in a Sorted Array by Minimum Increments


The aim of this article here is to make the elements distinct in a sorted array by minimum increments. First, an array of integers that has been sorted is provided. By increasing the values and keeping the array sum as low as possible, we must make array elements discrete from one another. As the output, we must display the smallest amount as the sum that is achievable.

Problem Statement

Make elements distinct in a sorted array by minimum increments.

Approach

Given an integer array of sorted numbers.

We first check whether the elements in the given integer array are distinct or not. That is, we make sure no element in the given integer array repeats more than once. If all the elements are distinct we add all the elements in the given array and return the sum of the elements present in that array as the output. Suppose if any number repeats more than once, we ignore the repetitions and take the element only once, no matter how many times it gets repeated. We add the elements and return the sum as the result.

Example 1

Consider an integer array, a={1,2,3}

Since all the three elements 1, 2, and 3 in the given array are distinct here, that is, none of the elements in this integer array is repeating more than once, we simply add all the elements 1,2 and 3 and print the sum 6 as the output.

That is, the output we obtain here is 1+2+3=6.

Example 2

Consider an integer array, a={1,2,2,3}

In this integer array, the elements 1 and 3 occurred only once but element 2 repeats two times here. By the rule we have to ignore the repetitions and consider element 2 only once. That is we add 1,2 and 3 and print the sum 6 as the output.

That is, the output we obtain here is 1+2+3=6.

Algorithm

The algorithm to make elements distinct in a sorted array by minimum increments is given below.

Step 1: Start

Step 2: Define the integer array with values.

Step 3: Set the size of the array.

Step 4: Set sum and previous element to the first element of the array.

Step 5: Run the loop, set the current element to the second element of the array.

Step 6: If the previous element is greater than or equal to the current element, increment the current element to the previous element plus 1.

Step 7: Set sum to sum plus current element.

Step 8: Set previous element to current element.

Step 9: Repeat the loop.

Step 10: Print the sum.

STEP 10: Stop

C program to make elements distinct in a sorted integer array by minimum increments is given below.

Example

#include <stdio.h>
   int main(){
   int a[]={1,2,2,3}; //input the array elements
   int n = 4;       //set the size of the array
   int sum = a[0];   //set sum to 0
   int prev = a[0];   //set prev to 0
//iterate the array and check for the condition
   for( int i = 1; i < n; i++ ) {
      int curr = a[i];
      if( prev >= curr ) {
         curr = prev+1;
      }
      sum += curr;
      prev = curr;
   }
   printf("%d",sum); // print the result obtained
}

Output

On execution, it will produce the following output:

10

Conclusion

Likewise, we can make elements distinct in a sorted array by the minimum increments possible.

The challenge of making elements distinct in a sorted array by minimum increments

is resolved in this article. Here C programming codes as well as the algorithm to make the elements distinct in a sorted array by minimum increments are provided.

Updated on: 23-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements