Showing posts with label MUM. Show all posts
Showing posts with label MUM. Show all posts

Sunday, 24 December 2017

Write a function named largestDifferenceOfEvens which returns the largest difference between even valued elements of its array argument. For example largestDifferenceOfEvens(new int[ ]{-2, 3, 4, 9}) returns 6 = (4 - (-2)). If there are fewer than 2 even numbers in the array, largestDifferenceOfEvens should return -1.

If you are programming in Java or C#, the function signature is
int largestDifferenceOfEvens(int[ ] a)

If you are programming in C or C++, the function signature is
int largestDifferenceOfEvens(int a[ ], int len) where len is the number of elements in the array a.

Examples

a is
then function returns
reason
{1, 3, 5, 9}
-1
because there are no even numbers
{1, 18, 5, 7, 33}
-1
because there is only one even number (18)
{[2, 2, 2, 2]}
0
because 2-2 == 0
{1, 2, 1, 2, 1, 4, 1, 6, 4}
4
because 6 - 2 == 4

   
public static int largestDifferenceOfEvens(int[] a)
        {
            int largestDiff = 0;

            int maxevent = 0, minevent = 0, eventcount = 0;
            
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] % 2 == 0)
                {
                    if (eventcount == 0)
                    {
                        maxevent = a[i];
                        minevent = a[i];
                    }
                    eventcount++;
                    if (a[i] > maxevent)
                    {
                        maxevent = a[i];
                    }
                    if (a[i] < minevent)
                    {
                        minevent = a[i];
                    }


                }
            }
            largestDiff = maxevent - minevent;

            if (eventcount < 2)
            {
                largestDiff = -1;
            }

            return largestDiff;



        }
Write a function named factorTwoCount that returns the number of times that 2 divides the argument.

For example, factorTwoCount(48) returns 4 because
48/2 = 24
24/2 = 12
12/2 = 6
6/2 = 3
2 does not divide 3 evenly.

Another example: factorTwoCount(27) returns 0 because 2 does not divide 27.

The function signature is

   int factorTwoCount(int n);

First Way:
public static int factorTwoCount(int n)
        {
            int count = 0;
            while (n > 1)
            {
                if (n % 2 == 0)
                {
                    n = n / 2;
                    count++;
                }
                else
                {
                    break;
                }
            }
            return count;
        }
       Second way :
       
public static int factorTwoCount(int n)
        {
            int count = 0;
            bool flag = false;
            double temp = n;
            do
            {
                temp = temp / 2;
                flag = (temp % 1 == 0);
                if (flag)
                {
                    count++;
                }
            } while (flag);
            return count;
        }
Write a function called goodSpread that returns 1 if no value in its array argument occurs more than 3 times in the array.

For example, goodSpread(new int[] {2, 1, 2, 5, 2, 1, 5, 9} returns 1 because no value occurs more than three times.

But goodSpread(new int[ ] {3, 1, 3 ,1, 3, 5, 5, 3} ) returns 0 because the value 3 occurs four times.

If you are writing in Java or C#, the function signature is
   int goodSpread (int[ ] a)

If you are writing in C or C++, the function signature is
   int goodSpread  (int a[ ], int len) where len is the number of elements in the array.
public static int goodSpread(int[] a)
        {
            int[] freq = new int[a.Length];
            int result = 1;
            for (int i = 0; i < a.Length; i++)
            {
                freq[i] = -1;
            }
            for (int i = 0; i < a.Length; i++)
            {
                int count = 1;
                for (int j = i + 1; j < a.Length; j++)
                {
                    if (a[i] == a[j])
                    {
                        count++;
                        freq[j] = 0;
                    }
                }
                if (freq[i] != 0)
                {
                    freq[i] = count;
                }
            }
            for (int i = 0; i < freq.Length; i++)
            {
                if (freq[i] != 0)
                {
                   
                    if (freq[i] > 3)
                    {
                        result = 0;
                        break;
                    }
                }
            }
            return result;

        }
Meera array is defined to be an array that contains at least one odd number and  begins and ends with the same number of even numbers.

So {4, 8, 6, 3, 2, 9, 8,11, 8, 13, 12, 12, 6} is a Meera array because it begins with three even numbers and ends with three even numbers and it contains at least one odd number

The array {2, 4, 6, 8, 6} is not a Meera array because it does not contain an odd number.

The array {2, 8, 7, 10, -4, 6} is not a Meera array because it begins with two even numbers but ends with three even numbers.

Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise, it returns 0.

If you are writing in Java or C#, the function signature is
   int isMeera (int[ ] a)

If you are writing in C or C++, the function signature is
   int isMeera (int a[ ], int len) where len is the number of elements in the array.
 public static int isMeera(int[] a)
        {
            bool isOdd = false, firstCountEnd = false, lastCountEnd = false;
            int firstCount = 0, lastCount = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] % 2 != 0)
                {
                    isOdd = true;
                    break;
                }
            }
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] % 2 != 0)
                    firstCountEnd = true;
                if (!firstCountEnd && a[i] % 2 == 0)
                    firstCount++;
                if (a[(a.Length - 1) - i] % 2 != 0)
                    lastCountEnd = true;
                if (!lastCountEnd && a[(a.Length - 1) - i] % 2 == 0)
                    lastCount++;
            }
            if (isOdd && firstCount == lastCount)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

A Meera array is defined to be an array such that for all values n in the array, the value 2*n is not in the array. So {3, 5, -2} is a Meera array because 3*2, 5*2 and -2*2 are not in the array. But {8, 3, 4} is not a Meera array because for n=4,2*n=8 is in the array.

Write a function named isMeera that returns 1 if its array argument is a Meera array. Otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isMeera(int [ ] a)

If you are programming in C or C++, the function signature is
   int isMeera(int a[ ], int len) where len is the number of elements in the array.

 Solution :
public static int isMeera(int[] a)
        {
            int isMeera = 1;

            for (int i = 0; i < a.Length && isMeera == 1; i++)
            {
                int isValid = 0;
                for (int j = 0; j < a.Length; j++)
                {
                    if (a[i] == 2 * a[j])
                    {
                        isValid = 1;
                        break;
                    }
                }

                if (isValid == 1)
                    isMeera = 0;
            }

            return isMeera ;
        }
A Bean array is defined to be an integer array where for every value n in the array, there is also an element 2n, 2n+1 or n/2 in the array.

For example, {4, 9, 8} is a Bean array because
For 4, 8 is present; for 9, 4 is present; for 8, 4 is present.

Other Bean arrays include {2, 2, 5, 11, 23}, {7, 7, 3, 6} and {0}.

The array {3, 8, 4} is not a Bean array because of the value 3 which requires that the array contains either the value 6, 7 or 1 and none of these values are in the array.

Write a function named isBean that returns 1 if its array argument is a Bean array. Otherwise it returns a 0.

If you are programming in Java or C#, the function signature is
   int isBean(int[ ] a)

If you are programming in C or C++, the function signature is
   int isBean(int a[ ], int len) where len is the number of elements in the array.

solution :
first Way
public static int isBean(int[] a)
        {
            int isBean = 1;

            for (int i = 0; i < a.Length; i++)
            {
                int isValid = 0;
                for (int j = 0; j < a.Length; j++)
                {
                    if (a[i] == 2 * a[j] || a[i] == 2 * a[j] + 1 || a[i] == a[j] / 2)
                    {
                        isValid = 1;
                        break;
                    }
                }
                if (isValid == 0)
                    isBean = 0;
            }
            return isBean;
        }
       Second Way
  
 public static int isBean(int [] a)
        {
            int i, j,x,y,z;
            bool isMeera = true, step = false;
            for(i=0;i<a.Length;i++)
            {
                step = false;
                for(j=0;j<a.Length;j++)
                {
                    x = 2 *a[j];
                    y = 2 * a[j] + 1;
                    z = a[j]/2;
                    if (a[i] == x || a[i]==y || a[i]==z)
                        step = true;
                }
                if(j==a.Length && step==false)
                {
                    isMeera =false;
                    break;
                }
            }
            return isMeera ==true ? 1 : 0;

        }
Write a function named minDistance that returns the smallest distance between two non-trivial factors of a number. For example, consider 63. Its non-trivialfactors are 3, 7, 9 and 21. Thus minDistance(63) would return 2 because the smallest distance between any two non-trivial factors is 2 (9 - 7 = 2). As another example, minDistance (25) would return 0 because 25 has only one non-trivial factor: 5. Thus the smallest distance between any two non-trivial factors is 0 (5 - 5 = 0). Note that minDistance(11) would return -1 since 11 has no non-trivial factors.

The function signature is
   int minDistance(int n)

 Solution :
    
public static int minDistance(int n)
        {
            int minDist = n, count = 0, f1 = 0;
            for (int i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    if (count == 0)
                        f1 = i;
                    count++;
                    if (count > 1)
                    {
                        if (minDist > i - f1)
                            minDist = i - f1;
                        f1 = i;
                    }

                }
                
                if (count == 1 && i == n - 1)
                    minDist = i - i;
            }
            if (count == 0)
            {
                return -1;
            }
            return minDist;

        }
Question:
An array is defined to be maxmin equal if it contains at least two different elements and the number of times the maximum value occur is the same as the number of times the minimum value occur. So {11, 4, 9, 11, 8, 5 , 4, 10} is maxmin equal, because the max value 11 and min value 4 both appear two times in the array.

Write a function called isMaxMinEqual that accepts an integer array and returns 1 if the array is maxmin equal; otherwise it returns 0.

If you are programming in Java or C#, the function signature is
                int isMaxMinEqual(int[ ] a)

If you are programming in C or C++, the function signature is
                int isMaxMinEqual(int a[ ], int len) where len is the number of elements in the array

Some other examples:
if the input array is
isMaxMinEqual should return
{}
0 (array must have at least two different elements)
{2}
0 (array must have at least two different elements)
{1, 1, 1, 1, 1, 1}
0 (array must have at least two different elements)
{2, 4, 6, 8, 11}
1 (Both max value (11) and min value 2 appear exactly one time)
{-2, -4, -6, -8, -11}
1 (Both max value (-2) and min value -11 appear exactly one time)
 
public static int isMaxMinEqual(int[] a)
        {
            int max = a[0],
            min = a[0],
            maxCount = 0,
            minCount = 0;
            if (a.Length < 2)
                return 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (max < a[i])
                {
                    max = a[i];
                    maxCount = 1;
                }
                else if (min > a[i])
                {
                    min = a[i];
                    minCount = 1;
                }
                else
                {
                    if (max == a[i])
                    {
                        maxCount++;
                    }
                    if (min == a[i])
                    {
                        minCount++;
                    }
                }
            }
            if (maxCount == minCount && max != min)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
Question :
An array is defined to be a Filter array if it meets the following conditions
   a. If it contains 9 then it also contains 11.
   b. If it contains 7 then it does not contain 13.

So {1, 2, 3, 9, 6, 11} and {3, 4, 6, 7, 14, 16}, {1, 2, 3, 4, 10, 11, 13} and {3, 6, 5, 5, 13, 6, 13} are Filter arrays. The following arrays are not Filter arrays: {9, 6, 18} (contains 9 but no 11), {4, 7, 13} (contains both 7 and 13)

Write a function named isFilter that returns 1 if its array argument is a Filter array, otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isFilter(int[ ] a)
If you are programming in C or C++, the function signature is
   int isFilter(int a[ ], int len) where len is the number of elements in the array.
public static int isFilter(int [] a)
        {
            int result = 1;
            for(int i=0;i<a.Length;i++)
            {
                if(a[i]==9)
                {
                    for(int j=0;j<a.Length;j++)
                    {
                        if(a[j]==11)
                        {
                            result = 1;
                            break;
                        }
                        else
                        {
                            result = 0;
                        }
                    }
                }
                if(a[i]==7)
                {
                    for(int j=0;j<a.Length;j++)
                    {
                        if(a[j]==13)
                        {
                            result = 0;
                            break;
                        }
                    }
                }
            }
            return result;

        }
Question :
A non-empty array a of length n is called an array of all possiblities if it contains all numbers between 0 and a.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isAllPossibilities(int[ ] a)
If you are programming in C or C++, the function signature is
int isAllPossibilities(int a[ ], int len) where len is the number of elements in the array
Examples
If the input array is
return
{1, 2, 0, 3}
1
{3, 2, 1, 0}
1
{1, 2, 4, 3}
0 (because 0 not included and 4 is too big)
{0, 2, 3}
0 (because 1 is not included)
{0}
1
{}
0

public static int allPossibilities(int[] a)
        {
            bool isNice = true, step = false;
            int i, j, x, y;
            for (i = 0; i < a.Length; i++)
            {
                step = false;
                for (j = 0; j < a.Length; j++)
                {
                    if (a[i] == j)
                        step = true;
                }
                if (j == a.Length && step == false)
                {
                    isNice = false;
                    break;
                }
            }
            return isNice == true ? 1 : 0;
        }
Question :
Define a cluster in an integer array to be a maximum sequence of elements that are all the same value. For example, in the array {3, 3, 3, 4, 4, 3, 2, 2, 2, 2, 4} there are 5 clusters, {3, 3, 3}, {4, 4}, {3}, {2, 2, 2, 2} and {4}. A cluster-compression of an array replaces each cluster with the number that is repeated in the cluster. So, the cluster compression of the previous array would be {3, 4, 3, 2, 4}. The first cluster {3, 3, 3} is replaced by a single 3, and so on.

Write a function named clusterCompression with the following signature

If you are programming in Java or C#, the function signature is
int[ ] clusterCompression(int[ ] a)

If you are programming in C++ or C, the function signature is
int *clusterCompression(int a[ ], int len) where len is the length of the array.

The function returns the cluster compression of the array a. The length of the returned array must be equal to the number of clusters in the original array! This means that someplace in your answer you must dynamically allocate the returned array.

In Java or C# you can use
int[ ] result = new int[numClusters];

In C or C++ you can use
int *result = (int *)calloc(numClusters, sizeof(int));

Examples
a is
then function returns
{0, 0, 0, 2, 0, 2, 0, 2, 0, 0}
{0, 2, 0, 2, 0, 2, 0}
{18}
{18}
{}
{}
{-5, -5, -5, -5, -5}
{-5}
{1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1}
{1, 2, 1}
{8, 8, 6, 6, -2, -2, -2}
{8, 6, -2}


public static int[] clusterCompression(int[] a)
        {
            int[] rtnarray = new int[] { };
            if (a.Length != 0)
            {
                rtnarray = new int[FindCompressedArrayCount(a)];
                int currentValue = a[0], j = 0;
                rtnarray[j] = currentValue;
                for (int i = 1; i < a.Length; i++)
                {
                    if (currentValue != a[i])
                    {
                        j++;
                        currentValue = a[i];
                        rtnarray[j] = currentValue;
                    }
                }
            }
            return rtnarray;
        }

        public static int FindCompressedArrayCount(int[] a)
        {
            int currentValue = a[0], count = 1;
            for (int i = 0; i < a.Length; i++)
            {
                if (currentValue != a[i])
                {
                    count++;
                    currentValue = a[i];
                }
            }
            return count;
        }

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page