Given a matrix A[][] of size N*M, the task is to check if the given matrix satisfies the following two conditions or not:
- Sum of all elements is prime
- Element A[i][j] from the matrix should also be a prime if (i + j) is a prime.
Examples:
Input: N = 4, M = 5
A[][] = {{ 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 }}
Output: YES
Explanation:
Sum of all elements = 107 (prime)
All possible (i, j) such that (i + j) is prime are:
(0+2) = 2 (prime) and A[0][2] = 3 (prime)
(0+3) = 3 (prime) and A[0][3] = 2 (prime)
(1+1) = 2 (prime) and A[1][1] = 2 (prime)
(1+2) = 3 (prime) and A[1][2] = 7 (prime)
(1+4) = 5 (prime) and A[1][4] = 7 (prime)
(2+0) = 2 (prime) and A[2][0] = 7 (prime)
(2+1) = 3 (prime) and A[2][1] = 7 (prime)
(2+3) = 5 (prime) and A[2][3] = 7 (prime)
(3+0) = 3 (prime) and A[3][0] = 2 (prime)
(3+2) = 5 (prime) and A[3][2] = 3 (prime)
(3+4) = 7 (prime) and A[3][4] = 7 (prime)
Hence, both conditions are satisfied and answer is YES.Input: N = 3, M = 3
A[][] = {{7, 3, 4}, {11, 0, 6}, {12, 16, 3}}
Output: NO
Explanation:
Sum of all elements = 62 (not prime)
Hence, the first condition is not satisfied.
Naive Approach:
Calculate the sum of all elements in the matrix and check if it is prime. If not, print NO. Otherwise, traverse the whole matrix and for every (i, j) such that (i + j) is prime, check if A[ i ][ j ] is prime. If both conditions are satisfied for all such values of (i, j), print YES as output. Otherwise, print NO. Primality test for a number K can be performed using simple brute force in O(√K).
Below is the implementation of the above approach:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function checks if // n is prime or not bool isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to sqrt(n) for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } // Function returns sum of // all elements of matrix int takeSum(int a[4][5]) { // Stores the sum of the matrix int sum = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) sum += a[i][j]; return sum; } // Function to check if all a[i][j] // with prime (i+j) are prime bool checkIndex(int n, int m, int a[4][5]) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // If index is prime if (isPrime(i + j)) { // If element not prime if (!isPrime(a[i][j])) return false; } } } return true; } // Driver code int main() { int n = 4, m = 5; int a[4][5] = { { 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 } }; int sum = takeSum(a); // Check for both conditions if (isPrime(sum) && checkIndex(n, m, a)) { cout << "YES" << endl; } else cout << "NO" << endl; return 0; } |
Java
// Java implementation of // the above approach class GFG{ // Function checks if // n is prime or not static boolean isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to Math.sqrt(n) for(int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } // Function returns sum of // all elements of matrix static int takeSum(int a[][]) { // Stores the sum of the matrix int sum = 0; for(int i = 0; i < 4; i++) for(int j = 0; j < 5; j++) sum += a[i][j]; return sum; } // Function to check if all a[i][j] // with prime (i+j) are prime static boolean checkIndex(int n, int m, int a[][]) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // If index is prime if (isPrime(i + j)) { // If element not prime if (!isPrime(a[i][j])) return false; } } } return true; } // Driver code public static void main(String[] args) { int n = 4, m = 5; int a[][] = { { 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 } }; int sum = takeSum(a); // Check for both conditions if (isPrime(sum) && checkIndex(n, m, a)) { System.out.print("YES" + "\n"); } else { System.out.print("NO" + "\n"); } } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of # the above approach import math # Function checks if # n is prime or not def isPrime(n): # Corner case if (n <= 1): return False # Check from 2 to sqrt(n) for i in range(2, int(math.sqrt(n)) + 1): if (n % i == 0): return False return True # Function returns sum of # all elements of matrix def takeSum(a, n, m): # Stores the sum of the matrix sum = 0 for i in range(0, n): for j in range(0, m): sum += a[i][j] return sum # Function to check if all a[i][j] # with prime (i+j) are prime def checkIndex(n, m, a): for i in range(0, n): for j in range(0, m): # If index is prime if (isPrime(i + j)): # If element not prime if (isPrime(a[i][j]) != True): return False return True # Driver code n = 4m = 5 a = [ [ 1, 2, 3, 2, 2 ] , [ 2, 2, 7, 7, 7 ], [ 7, 7, 21, 7, 10 ], [ 2, 2, 3, 6, 7 ] ] sum = takeSum(a, n, m) # Check for both conditions if (isPrime(sum) and checkIndex(n, m, a)): print("YES") else: print("NO") # This code is contributed by sanjoy_62 |
C#
// C# implementation of // the above approach using System; class GFG{ // Function checks if // n is prime or not static bool isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to Math.Sqrt(n) for(int i = 2; i <= Math.Sqrt(n); i++) if (n % i == 0) return false; return true; } // Function returns sum of // all elements of matrix static int takeSum(int[,]a) { // Stores the sum of the matrix int sum = 0; for(int i = 0; i < 4; i++) for(int j = 0; j < 5; j++) sum += a[i, j]; return sum; } // Function to check if all a[i,j] // with prime (i+j) are prime static bool checkIndex(int n, int m, int[,]a) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // If index is prime if (isPrime(i + j)) { // If element not prime if (!isPrime(a[i, j])) return false; } } } return true; } // Driver code public static void Main(String[] args) { int n = 4, m = 5; int[,]a = { { 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 } }; int sum = takeSum(a); // Check for both conditions if (isPrime(sum) && checkIndex(n, m, a)) { Console.Write("YES" + "\n"); } else { Console.Write("NO" + "\n"); } } } // This code is contributed by PrinciRaj1992 |
YES
Time complexity: O(N * M * √K)
Auxiliary Space: O(1)
Efficient Approach:
In order to optimize the above approach, we can perform primality test using Sieve of Eratosthenes. Store the prime numbers upto sum, denoting sum of matrix elements. This reduces the primality test computational complexity to O(1) and precomputing the sieve requires O(log(log(sum))).
Below is the implementation of the above approach:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Stores true at prime // indices vector<bool> prime; // Function to generate // the prime numbers // using Sieve of Eratosthenes void buildSieve(int sum) { prime = vector<bool>(sum + 1, true); prime[0] = false; prime[1] = false; for (int p = 2; p * p < (sum + 1); p++) { // If p is still true if (prime[p] == true) { // Mark all multiples of p for (int i = p * 2; i < (sum + 1); i += p) prime[i] = false; } } } // Function returns sum of // all elements of matrix int getSum(int a[4][5]) { int s = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) s += a[i][j]; return s; } // Function to check if for all // prime (i+j), a[i][j] is prime bool checkIndex(int n, int m, int a[4][5]) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { // If index is prime if (prime[i + j] && !prime[a[i][j]]) { return false; } } return true; } // Driver Code int main() { int n = 4, m = 5; int a[4][5] = { { 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 } }; int sum = getSum(a); buildSieve(sum); // Check for both conditions if (prime[sum] && checkIndex(n, m, a)) { cout << "YES" << endl; } else cout << "NO" << endl; return 0; } |
Java
// Java implementation of // the above approach import java.util.*; class GFG{ // Stores true at prime // indices static boolean []prime; // Function to generate // the prime numbers // using Sieve of Eratosthenes static void buildSieve(int sum) { prime = new boolean[sum + 1]; Arrays.fill(prime, true); prime[0] = false; prime[1] = false; for(int p = 2; p * p < (sum + 1); p++) { // If p is still true if (prime[p] == true) { // Mark all multiples of p for(int i = p * 2; i < (sum + 1); i += p) prime[i] = false; } } } // Function returns sum of // all elements of matrix static int getSum(int a[][]) { int s = 0; for(int i = 0; i < 4; i++) for(int j = 0; j < 5; j++) s += a[i][j]; return s; } // Function to check if for all // prime (i+j), a[i][j] is prime static boolean checkIndex(int n, int m, int a[][]) { for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { // If index is prime if (prime[i + j] && !prime[a[i][j]]) { return false; } } return true; } // Driver Code public static void main(String[] args) { int n = 4, m = 5; int a[][] = { { 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 } }; int sum = getSum(a); buildSieve(sum); // Check for both conditions if (prime[sum] && checkIndex(n, m, a)) { System.out.print("YES" + "\n"); } else System.out.print("NO" + "\n"); } } // This code is contributed by gauravrajput1 |
C#
// C# implementation of // the above approach using System; class GFG{ // Stores true at prime // indices static bool []prime; // Function to generate // the prime numbers // using Sieve of Eratosthenes static void buildSieve(int sum) { prime = new bool[sum + 1]; for(int i = 0; i < prime.Length; i++) prime[i] = true; prime[0] = false; prime[1] = false; for(int p = 2; p * p < (sum + 1); p++) { // If p is still true if (prime[p] == true) { // Mark all multiples of p for(int i = p * 2; i < (sum + 1); i += p) prime[i] = false; } } } // Function returns sum of // all elements of matrix static int getSum(int[,]a) { int s = 0; for(int i = 0; i < 4; i++) for(int j = 0; j < 5; j++) s += a[i, j]; return s; } // Function to check if for all // prime (i+j), a[i,j] is prime static bool checkIndex(int n, int m, int[,]a) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { // If index is prime if (prime[i + j] && !prime[a[i, j]]) { return false; } } } return true; } // Driver Code public static void Main(String[] args) { int n = 4, m = 5; int[,]a = { { 1, 2, 3, 2, 2 }, { 2, 2, 7, 7, 7 }, { 7, 7, 21, 7, 10 }, { 2, 2, 3, 6, 7 } }; int sum = getSum(a); buildSieve(sum); // Check for both conditions if (prime[sum] && checkIndex(n, m, a)) { Console.Write("YES" + "\n"); } else Console.Write("NO" + "\n"); } } // This code is contributed by gauravrajput1 |
YES
Time complexity: O(log(log(sum)) + (N*M)), where sum denotes the sum of the matrix.
Auxiliary Space: O(N)
Recommended Posts:
- Check if given Binary string follows then given condition or not
- Find N in the given matrix that follows a pattern
- Find the longest path in a matrix with given constraints
- Maximum number of ones in a N*N matrix with given constraints
- Check if it is possible to make the given matrix increasing matrix or not
- Check if it is possible to convert one string into another with given constraints
- Find smallest number with given number of digits and sum of digits under given constraints
- Program to check if a matrix is Binary matrix or not
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Forming smallest array with given constraints
- Construct a binary string following the given constraints
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count of elements which form a loop in an Array according to given constraints
- Remove minimum characters from string to split it into three substrings under given constraints
- Check if a given matrix is sparse or not
- Check whether a given matrix is orthogonal or not
- Check given matrix is magic square or not
- Check if a given matrix is Hankel or not
- Check whether the given Matrix is balanced or not
- C Program To Check whether Matrix is Skew Symmetric or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


