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.
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,...
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
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...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convert2(int & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as a 2's complement number and return true. For example, convertU(n, "10011") should set n to -13...
I'm trying to convert between different number representations in C++ , I have the prototype but...
I'm trying to convert between different number representations in C++ , I have the prototype but im not sure what do do from here bool convertU(unsigned & n, const string & bits); bits should have size exactly 5, and each char of bits should be '0' or '1'; otherwise return false. If bits is ok, set n to the number that bits represents as an unsigned and return true. For example, convertU(n, "0101") and convertU(n, "10210") should return false; convertU(n,...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form 1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345 2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45". 3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45". Please write as simple as possible because I am a freshman in C! Thanks!
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal...
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form 1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345 2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45". 3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45". Please write as simple as possible because I am a freshman in C! And please give me some clues! Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT