Magic Array:Tricky Question Solution in C#
Q.) An array is defined to be a Magic array if the sum of the primes in the array is equal to the first element of the array. If there are no primes in the array, the first element must be 0. So {21, 3, 7, 9, 11 4, 6} is a Magic array because 3, 7, 11 are theprimes in the array and they sum to 21 which is the first element of the array. {13, 4, 4, 4, 4} is also a Magic array because the sum of the primes is 13 which is also the first element. Other Magic arrays are {10, 5, 5}, {0, 6, 8, 20} and {3}. {8, 5, -5, 5, 3} is not a Magic array because the sum of the primes is 5+5+3 = 13. Note that -5 is not a prime because prime numbers are positive. Write a function named isMagicArraythat returns 1 if its integer array argument is a Magic array. Otherwise it returns 0.If you are writing in Java or C#, the function signature isint isMagicArray (int[ ])
a)If you are writing in C or C++, the function signature isint isMagicArray (int a[ ], int len) where len is the number of elements in the array.
You may assume that a function named isPrimeexists that returns 1 if its int argument is a prime, otherwise it returns 0. You do not have to write this function ! You are allowed to use it.
So, the code for the problem is provided below:
using System;
namespace MagicArray
{
class Program
{
//Function to check whether the array is Margic arrray
int isMagicArray(int[] arr, int l)
{
Console.WriteLine(l);
int sumPrime = 0;
int sumnotP = 0;
Console.WriteLine("The value of l is: {0}", l);
for (int i = 1; i < l; i++)
{
int s = arr[i];
Console.WriteLine("The number to be checked is {0}", s);
if (isPrime(s, l) == 1)
{
sumPrime = sumPrime + s;
Console.WriteLine("The number {0} is prime", s);
}
if (isPrime(s, l) == 0)
{
Console.WriteLine("The number {0} is not prime", s);
sumnotP = sumnotP + s;
}
}
if (arr[0] == sumPrime)
{
Console.WriteLine("The sumPrime is {0}", sumPrime);
return 1;
}
else
{
Console.WriteLine("The notsumP is {0}", sumnotP);
return 0;
}
}
//To check whether the array is prime or not......
int isPrime(int s, int l)
{
int ctr = 0;
for (int i = 1; i <= s; i++)
{
if ((s % i) == 0)
{
ctr++;
if (ctr == 3)
{
break;
}
}
}
if (ctr == 2 && s != 1)
{
return 1;
}
else
{
return 0;
}
}
static void Main(string[] args)
{
int[] arr = new int[10];
Console.WriteLine("how many numbers do you wanna enter:");
int l = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the numbers:");
for (int i = 0; i < l; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Program p = new Program();
//calling the magic array function..............
int k = p.isMagicArray(arr, l);
if (k == 1)
{
Console.WriteLine("The array is magic array");
}
else
{
Console.WriteLine("The array is not magic array");
}
Console.ReadKey();
}
}
}
The output for the problem is provided below:
So this is how I have solved the problem of checking whether the given array is magic array. But you can also do it in any other way. Hope this will be helpful for new bies in C# and this kind of problems are even asked in the interviews.
If you have different solution for the problem comment it down below:
Also Read: Biggest Issues in IT today