Repository: TheAlgorithms/Java
Pull request #2943 (2022-02-16 05:21:35)

Fixes(#2873)
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


siddhant2002: @siriak please check the updated pr as I've added the tests.

Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java
new file mode 100644
index 00000000..508227f0
--- /dev/null
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java
@@ -0,0 +1,67 @@
+/** Author : Siddhant Swarup Mallick
+ * Github : https://github.com/siddhant2002
+ */
+
+
+/**
+ * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
+ * The robot can only move either down or right at any point in time.
+ * The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
+ * How many possible unique paths are there?
+ */
+
+/** Program description - To find the number of unique paths possible */
+
+package com.thealgorithms.dynamicprogramming;
+
+import java.util.*;
+
+public class UniquePaths {
+    public static boolean uniquePaths(int m , int n , int ans) {
+        int []dp = new int[n];
+        Arrays.fill(dp,1);
+        for (int i=1; i<m; i++)
+        {
+            for (int j=1; j<n; j++)
+            {
+                dp[j] += dp[j-1];
+
+            }
+        }
+        return dp[n-1]==ans;
+        // return true if predicted answer matches with expected answer
+    }
+    // The above method runs in O(n) time
+    public static boolean uniquePaths2(int m , int n , int ans) {
+        int dp[][] = new int[m][n];
+        for (int i=0; i<m; i++)
+        {
+            dp[i][0] = 1;
+        }
+        for (int j=0; j<n; j++)
+        {
+            dp[0][j] = 1;
+        }
+        for (int i=1; i<m; i++)
+        {
+            for (int j=1; j<n; j++)
+            {
+                dp[i][j]=dp[i-1][j]+dp[i][j-1];
+            }
+        }
+        return dp[m-1][n-1]==ans;
+        // return true if predicted answer matches with expected answer
+    }
+    // The above mthod takes O(m*n) time
+}
+/**
+     * OUTPUT :
+     * Input - m = 3, n = 7
+     * Output: it returns either true if expected answer matches with the predicted answer else it returns false
+     * 1st approach Time Complexity : O(n)
+     * Auxiliary Space Complexity : O(n)
+     * Input - m = 3, n = 7
+     * Output: it returns either true if expected answer matches with the predicted answer else it returns false
+     * 2nd approach Time Complexity : O(m*n)
+     * Auxiliary Space Complexity : O(m*n)
+     */


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/UniquePathsTests.java b/src/test/java/com/thealgorithms/others/UniquePathsTests.java
new file mode 100644
index 00000000..5b147581
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/UniquePathsTests.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.others;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+import com.thealgorithms.dynamicprogramming.UniquePaths;
+
+
+public class UniquePathsTests {
+    @Test
+	void testForOneElement() 
+	{
+		assertTrue(UniquePaths.uniquePaths(3,7,28));
+	}
+
+	@Test
+	void testForTwoElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths(3,2,3));
+	}
+
+	@Test
+	void testForThreeElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths(3,3,6));
+	}
+
+	@Test
+	void testForFourElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths(4,6,56));
+	}
+
+	@Test
+	void testForFiveElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths2(3,5,15));
+	}
+
+	@Test
+	void testForSixElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths2(6,2,6));
+	}
+
+	@Test
+	void testForSevenElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths2(5,9,495));
+    }
+
+	@Test
+	void testForEightElements() 
+	{
+		assertTrue(UniquePaths.uniquePaths2(4,8,120));
+	}
+}
\ No newline at end of file