Check if it is possible to perform the given Grid Division
Given two integers N and M which represent the size of a grid. Also given an integer array arr[] of size P which represent that the given grid is divided into P parts each consisting of arr[i] cells from the grid. The task is to check whether it is possible to divide the grid in the given manner or not.
Examples:
Input: arr[] = {6, 3, 2, 1}, N = 3, M = 4
Output: Yes
Input: arr[] = {4, 2, 2}, N = 3, M = 2
Output: No
Approach: In order for the division to be possible, the sum of the cells of all the parts must be equal to the total number of cells in the given grid i.e. the sum of all the array elements must be equal to N * M.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if it is possible to // divide the grid satisfying the given conditions bool isPossible(int arr[], int p, int n, int m) { // To store the sum of all the // cells of the given parts int sum = 0; for (int i = 0; i < p; i++) sum += arr[i]; // If the sum is equal to the total number // of cells in the given grid if (sum == (n * m)) return true; return false; } // Driver code int main() { int n = 3, m = 4; int arr[] = { 6, 3, 2, 1 }; int p = sizeof(arr) / sizeof(arr[0]); if (isPossible(arr, p, n, m)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if it is possible to // divide the grid satisfying the given conditions static boolean isPossible(int arr[], int p, int n, int m) { // To store the sum of all the // cells of the given parts int sum = 0; for (int i = 0; i < p; i++) sum += arr[i]; // If the sum is equal to the total number // of cells in the given grid if (sum == (n * m)) return true; return false; } // Driver code public static void main(String[] args) { int n = 3, m = 4; int arr[] = { 6, 3, 2, 1 }; int p = arr.length; if (isPossible(arr, p, n, m)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach # Function that returns true if # it is possible to divide the grid # satisfying the given conditions def isPossible(arr, p, n, m): # To store the sum of all the # cells of the given parts sum = 0; for i in range(p): sum += arr[i]; # If the sum is equal to the total number # of cells in the given grid if (sum == (n * m)): return True; return False; # Driver code if __name__ == '__main__': n = 3; m = 4; arr = [6, 3, 2, 1]; p = len(arr); if (isPossible(arr, p, n, m)): print("Yes"); else: print("No"); # This code is contributed by Rajput-Ji |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if it is possible to // divide the grid satisfying the given conditions static bool isPossible(int []arr, int p, int n, int m) { // To store the sum of all the // cells of the given parts int sum = 0; for (int i = 0; i < p; i++) sum += arr[i]; // If the sum is equal to the total number // of cells in the given grid if (sum == (n * m)) return true; return false; } // Driver code public static void Main(String[] args) { int n = 3, m = 4; int []arr = { 6, 3, 2, 1 }; int p = arr.Length; if (isPossible(arr, p, n, m)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Rajput-Ji |
Yes
Recommended Posts:
- Write an Efficient Method to Check if a Number is Multiple of 3
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Write you own Power without using multiplication(*) and division(/) operators
- Check if a number is multiple of 5 without using / and % operators
- Check whether a given point lies inside a triangle or not
- DFA based division
- Check divisibility by 7
- Check if a number is Palindrome
- How to check if two given line segments intersect?
- How to check if a given point lies inside or outside a polygon?
- How to check if a given number is Fibonacci number?
- Check if binary representation of a number is palindrome
- How to check if an instance of 8 puzzle is solvable?
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a given number is Fancy
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.




