Question

In: Computer Science

Do only in C# (Not in C++ or C ) Minimum number of operations to convert...

Do only in C# (Not in C++ or C )

Minimum number of operations to convert array M to array N by adding an integer into a subarray

Given two arrays M and N , the task is to find the minimum number of operations in which the array M can be converted into array N where each operation consists of adding an integer K into a subarray from L to R.

M= {3, 7, 1, 4, 1, 2}, N = {3, 7, 3, 6, 3, 2}
Output: 1

In the above given example only one operation is required to convert from M to N: L = 3, R = 5 and K = 2
Array after the following operation:
Index 0: M[0] = 3, N[0] = 3
Index 1: M[1] = 7, N[1] = 7
Index 2: M[2] = 1 + 2 = 3, N[2] = 3
Index 3: M[3] = 4 + 2 = 6, N[3] = 6
Index 4: M[4] = 1 + 2 = 3, N[4] = 3
Index 5: M[5] = 2, N[5] = 2

M= {1, 1, 1, 1, 1},N = {1, 2, 1, 3, 1}
Output: 2

In the above given example only one operation is required to convert from M to N –
Operation 1: Add 1 to L = 2 to R = 2
Operation 2: Add 2 to L = 4 to R = 4

Solutions

Expert Solution

CODE :-

OUTPUT :-

TEXT CODE :-

using System;

namespace NoOfOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Test Array 1
            int[] M = { 3, 7, 1, 4, 1, 2 };
            int[] N = { 3, 7, 3, 6, 3, 2 };

            // Test Array 2
            int[] M1 = { 1, 1, 1, 1, 1 };
            int[] N1 = { 1, 2, 1, 3, 1 };

            // Other Variables and Array
            int[] minIndex = { -1, -1, -1, -1, -1, -1 };
            int min = 0, steps = 0;
            bool equal = false, first = true;

            // Test With Array 1
            do {
                for (int i = 0; i < M.Length; i++)
                {
                    if (M[i] != N[i])
                    {
                        if (min == 0)
                        {
                            min = N[i] - M[i];
                            minIndex[i] = 1;
                        } else if ((N[i] - M[i]) < min)
                        {
                            min = N[i] - M[i];
                            minIndex[i] = 1;
                        }else
                        {
                            minIndex[i] = 1;
                        }
                    }
                }
                for (int i = 0; i < M.Length; i++)
                {
                    if(minIndex[i] == 1)
                    {
                        M[i] += min;
                        minIndex[i] = -1;
                    }
                }
              
                if(min == 0)
                {
                    equal = true;
                }
              
                if(first == false)
                {
                    steps += 1;
                }

                min = 0;
                first = false;

            } while (equal == false);
          
            // Output
            Console.WriteLine("Test With Array 1- \nOutput: {0}\n", steps);

            // Variable Reset
            steps = 0;
            equal = false;
            first = true;

            // Test With Array 2
            do
            {
                for (int i = 0; i < M1.Length; i++)
                {
                    if (M1[i] != N1[i])
                    {
                        if (min == 0)
                        {
                            min = N1[i] - M1[i];
                            minIndex[i] = 1;
                        }
                        else if ((N1[i] - M1[i]) < min)
                        {
                            min = N1[i] - M1[i];
                            minIndex[i] = 1;
                        }
                        else
                        {
                            minIndex[i] = 1;
                        }
                    }
                }
                for (int i = 0; i < M1.Length; i++)
                {
                    if (minIndex[i] == 1)
                    {
                        M1[i] += min;
                        minIndex[i] = -1;
                    }
                }

                if (min == 0)
                {
                    equal = true;
                }

                if (first == false)
                {
                    steps += 1;
                }

                min = 0;
                first = false;

            } while (equal == false);

            // Output
            Console.WriteLine("\nTest With Array 2- \nOutput: {0}\n", steps);
        }
    }
}


Related Solutions

Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Convert the hexadecimal number directly to base 4; then convert both the original number and your...
Convert the hexadecimal number directly to base 4; then convert both the original number and your answer to binary to check your result. Please show steps and explain
Do each of the following: a) What is the minimum number of people that must be...
Do each of the following: a) What is the minimum number of people that must be in a room to guarantee that at least 20 were born on the same day of the week? Assume all days of the week are equally likely. b) What is the coefficient of x 9 y 11in ( 3 x − 4 y ) 20? c) What is the probability that in a permutation of the letters {A, B, C, D, E, F, G,...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Use the ListInterface operations only to create a tackPunct routine in C++ that will take a...
Use the ListInterface operations only to create a tackPunct routine in C++ that will take a list of string (array or linked, your choice) as a reference. Use the list operations on the passed list to make all items that don’t have a ‘.’, ‘?’ or ‘!’ have a ‘.’ tacked on.
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature...
C language <stdio.h> ONLY USE double and const int Write a program to convert Celsius temperature to Fahrenheit temperature. The formula for converting Celsius temperature into Fahrenheit temperature is:    F = (9 / 5) * C + 32 Create integer constants for the 3 numbers in the formula (9, 5, and 32).  Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. Convert the 9 to a double when converting the temperature (use type casting). Have a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT