Minimize given Number by swapping adjacent digits with odd difference


The aim of this article is to implement a program to minimize a given number by swapping adjacent digits with odd differences.

The goal is to determine the lowest amount that can be created from a string of size N indicating an integer using only the characters '1', '2', and '3' by exchanging adjacent characters any number of times.

As we all know, a string is a group of characters that ends with the null character "0" in C programming. Characters from the C String are kept in a character array. A C string differs from a character array in that it ends with the distinctive character "\0".

Example 1

Let the Input string be,
S = “211323”
Output obtained here is: 112233

Example 2

Let the Input string be,
S = “112323”
Output obtained here is: 112233

Example 3

Let the Input string be,
S = “231213”
Output obtained here is: 223113

Example 4

Let the Input string be,
S = “123123”
Output obtained here is: 122313

Problem Statement

Implement a program to minimize given Number by swapping adjacent digits with odd difference

Approach

The approach to solve this problem and obtain a program to minimize given Number by swapping adjacent digits with odd difference.

By making the following insight, the greedy technique can be used to address this issue −

What exactly is the greedy technique?

A greedy algorithm develops up a solution slice by slice, constantly selecting the component that provides the most evident and immediate benefit as the next step. Therefore, Greedy fits well with issues where selecting locally optimal also results in a global solution.

In this scenario, since the difference between 1 and 3 is 2 (an even number), they cannot be switched.

Since the difference between 1 and 2 equals 1 (an odd number), so may the difference between 2 and 3.

Since the difference between 1 and 2 equals 1 (an odd number), so may the difference between 2 and 3.

Algorithm

The algorithm to implement a program to minimize given Number by swapping adjacent digits with odd difference given below

  • Step 1− Define a Function to swap adjacent numbers whether the difference is odd in order to get the minimum number that matches the provided string.

  • Step 2− Find the length of the string provided

  • Step 3− Declare an integer variable to store the first occurrence of 3

  • Step 4− declare variables to keep track of the string's total number of 2s and the count of the 1 before the first 3

  • Step 5− Now, count the 1s before the first 3 and the total number of 2s when traversing the string.

  • Step 6− Combine all instances of 1 before the first appearance of 3 in the answer

  • Step 7− Add every instance of 2 in the answer

  • Step 8− Add the remaining instances of 3 and 1 to the response in the same sequence as the supplied string

  • Step 9− finally, print the output as result.

Example: C program

Here is the C program implementation of the above written algorithm to minimize given Number by swapping adjacent digits with odd difference

#include <stdio.h>
#include <string.h>
void findMinimumString(char S[]){
   int N = strlen(S);
   int pos = -1;
   int count1 = 0, count2 = 0;
   for (int i = 0; i < N; i++) {
      if (pos == -1 && S[i] == '1')
         count1++;
      else if (pos == -1 && S[i] == '3')
         pos = i;
      else if (S[i] == '2')
         count2++;
   }
   while (count1--)
      printf("1");
   while (count2--)
      printf("2");
   if (pos != -1) {
      while (pos < N) {
         if (S[pos] != '2')
            printf("%c", S[pos]);
         pos++;
      }
   }
}
int main(){
   char S[] = "211323";
   findMinimumString(S);
   return 0;
}

Output

112233

Conclusion

Likewise, we can obtain the program to minimize given Number by swapping adjacent digits with odd difference. The challenge of obtaining the "title" Minimize given Number by swapping adjacent digits with odd difference is resolved in this article.

Here C programming code as well as the algorithm and the methodology to implement a program to minimize a given Number by swapping adjacent digits with odd difference are provided.

Updated on: 30-Oct-2023

30 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements