From b140d3cec7051251c76c7db1307da575e2cbcdcd Mon Sep 17 00:00:00 2001 From: UvFlint <107190312+UvFlint@users.noreply.github.com> Date: Sat, 18 Feb 2023 11:08:35 +0100 Subject: [PATCH 1/2] Create 2022a-87-Qs question 1 in 2022a-87 --- Exams/2022/2022a-87-Qs | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Exams/2022/2022a-87-Qs diff --git a/Exams/2022/2022a-87-Qs b/Exams/2022/2022a-87-Qs new file mode 100644 index 0000000..53c481d --- /dev/null +++ b/Exams/2022/2022a-87-Qs @@ -0,0 +1,45 @@ +public class 2022a-87-Qs{ + + + //Q1 + public static boolean isIdentity(int[][] mat, int x, int size){ + int endIndex = x+size-1; //End index of the identity Matrix + if (size == 1 && mat[x][x]==1) //If size 1 and only cell value is 1 + return true; + + return isIdentity(mat, x, endIndex, x); + } + + public static boolean isIdentity(int[][] mat, int x, int endIndex, int checkIndex){ + if (checkIndex>endIndex) //if end of identity matrix + return true; + + if (checkRow(mat[checkIndex], checkIndex, x, endIndex)) //check if the row of the identity matrix is valid + return isIdentity(mat, x, endIndex, checkIndex+1); //proceed to the next row in the identity matrix + + return false; //if any row is not valid, isIdentity will return false + } + + + /** + * Assisting function to check if a row in the identity matrix is valid + * @param arr - a row in the matrix + * @param oneIndex - The location of the one in the arr + * @param x - the starting point of the check + * @param endIndex - End index of the row to check + * */ + public static Boolean checkRow(int[] arr, int oneIndex, int x, int endIndex){ + return checkRow(arr, oneIndex, x, x, endIndex); + } + + public static Boolean checkRow(int[] arr, int oneIndex, int checkIndex, int x, int endIndex){ + if (checkIndex>endIndex) //if end of row of the identity matrix + return true; + + if ((checkIndex==oneIndex && arr[checkIndex] != 1) || (checkIndex!=oneIndex && arr[checkIndex] != 0)) //checks if in the index should be 1 or 0. Stopping argument as false + return false; + + return checkRow(arr, oneIndex, checkIndex+1, endIndex); //proceed to the next index in the identity row +} + +} From e74a372095ffa2006d8385ff490479dd4954b2cc Mon Sep 17 00:00:00 2001 From: UvFlint <107190312+UvFlint@users.noreply.github.com> Date: Sat, 18 Feb 2023 13:14:58 +0100 Subject: [PATCH 2/2] Update and rename 2022a-87-Qs to 2022a_87_Qs --- Exams/2022/{2022a-87-Qs => 2022a_87_Qs} | 29 +++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) rename Exams/2022/{2022a-87-Qs => 2022a_87_Qs} (67%) diff --git a/Exams/2022/2022a-87-Qs b/Exams/2022/2022a_87_Qs similarity index 67% rename from Exams/2022/2022a-87-Qs rename to Exams/2022/2022a_87_Qs index 53c481d..60f3b23 100644 --- a/Exams/2022/2022a-87-Qs +++ b/Exams/2022/2022a_87_Qs @@ -1,7 +1,7 @@ public class 2022a-87-Qs{ - //Q1 + //Q1a public static boolean isIdentity(int[][] mat, int x, int size){ int endIndex = x+size-1; //End index of the identity Matrix if (size == 1 && mat[x][x]==1) //If size 1 and only cell value is 1 @@ -21,7 +21,7 @@ public class 2022a-87-Qs{ } - /** + /**Q1b * Assisting function to check if a row in the identity matrix is valid * @param arr - a row in the matrix * @param oneIndex - The location of the one in the arr @@ -42,4 +42,29 @@ public class 2022a-87-Qs{ return checkRow(arr, oneIndex, checkIndex+1, endIndex); //proceed to the next index in the identity row } + /** + Q2 + Time complexity - O(n) + Space complexity - O(1) + Takes the difference between the first two elements in the array. Checks if its the same difference for all elements in the array. If it encounters a non consisting difference + it will return the index that is missing according to the conditions. + If its consisting, than the array is valid and the missing index is the length of the array. + */ + public static int findMissingIndex(int[] a){ + int change; + int firstChange = a[1] - a[0]; + for (int i = 0 ;ifirstChange){ + return i+1; + } + return i; + + } + } + return a.length; + } + + }