├── Exams ├── 2013 │ └── empty.txt ├── 2014 │ ├── 2014a #85 (Efficiency: findMax).java │ └── 2014a #85 (Recursion: printPath).java ├── 2015 │ ├── 2015b #8 6 (Recursion: printAllSum).java │ ├── 2015a #83 (Recursion: shortestPath).java │ ├── 2015_A84_Qs.java │ └── 2015_B94_Qs.java ├── 2016 │ ├── Space.java │ └── UFOSearch.java ├── 2017 │ ├── 2017a #86 (Efficiency: countTriplets).java │ ├── 2017_B93_Tst.java │ └── 2017_B85_Qs.java ├── 2018 │ ├── 2018a #87 (Recursion: howManySorted) │ ├── 2018_A85_Qs2 │ └── 2018_A87_Qs.java ├── 2019 │ ├── 2019b #83 (Efficiency: howManyNegativeNumbers).java │ ├── 2019b #84 (Recursion: howManyPaths).java │ └── Exam2019B85.java ├── 2020 │ ├── 2020_b81_Qs.java │ └── 2020_A87_Qs.java ├── 2021 │ ├── 2021b #93 (Recursion: printExpr).java │ ├── 2021_B62_Qs.java │ ├── 2021_B91_Qs.java │ └── 2021_B50_Qs.java ├── 2022 │ ├── 2022b (Efficiency: sortMod).java │ └── 2022_B86.java └── 2023 │ ├── Q1_81.java │ └── T23Aֹֹ_A2_Q1 └── README.md /Exams/2013/empty.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Exams/2022/2022b (Efficiency: sortMod).java: -------------------------------------------------------------------------------- 1 | // Time Complexity : O(nk) - k is constant 2 | // Space Complexity: O(4) = O(1) 3 | 4 | public static void sortMod(int[] a, int k){ 5 | 6 | int i,j,temp,count = 0; 7 | for(i = 0; i < k; i++){ 8 | for(j = count; j < a.length; j++){ 9 | if(a[j] % k == i){ 10 | temp = a[j]; 11 | a[j] = a[count]; 12 | a[count] = temp; 13 | count++; 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Exams/2014/2014a #85 (Efficiency: findMax).java: -------------------------------------------------------------------------------- 1 | public static int findMax(int[] a) { 2 | 3 | int high = a.length - 1; 4 | int low = 0; 5 | int mid; 6 | int maxIndex = 0; 7 | 8 | while(low < high){ 9 | 10 | mid = (low + high) / 2; 11 | 12 | if(a[mid] > a[maxIndex]){ 13 | maxIndex = mid; 14 | } 15 | 16 | if(a[low] > a[mid]){ 17 | high = mid; 18 | } 19 | else low = mid + 1; 20 | 21 | } 22 | 23 | return maxIndex; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Exams/2019/2019b #83 (Efficiency: howManyNegativeNumbers).java: -------------------------------------------------------------------------------- 1 | // Time Complexity: O(n + m) - Linear 2 | // Space Complexity: O(1) 3 | 4 | 5 | public static int howManyNegativeNumber(int[][] a){ 6 | 7 | int i = 0; 8 | int j = a[i].length - 1; 9 | int counter = 0; 10 | 11 | while(i != a.length -1 && j != 0){ 12 | 13 | if(a[i][j] >= 0){ 14 | j--; 15 | } 16 | 17 | if(a[i][j] < 0){ 18 | counter += j + 1; 19 | i++; 20 | } 21 | } 22 | 23 | return counter; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # introToJavaOpenUni 2 | * Sulotions and tests for solutions for course 'Intro To Java 20441'. 3 | 4 | ## Format 5 | * Upload the files in this format, make sure to format them before uploading them or edit them before commit them. 6 | * The file name should be in the following format: ```[year]_[semester + number]_[Question / test]_[Question number]```. 7 | * For example: ```"2022_A96_Qs1.java" / "2022_C84_Tst2.java"``` 8 | 2022_A96_Qs1.java 9 | 10 | * Each file should be under the relevant year! 11 | 12 | 13 | 14 | 15 | 16 | ## Useful links 17 | * Link to [Mega ]( https://mega.nz/folder/0Sg0iD4B#0OPF1JJgFjtYoJuStlsCtA ) 18 | ## 19 | * Feel free to add your solutions! 🔥💻🤘🏻 20 | -------------------------------------------------------------------------------- /Exams/2015/2015b #8 6 (Recursion: printAllSum).java: -------------------------------------------------------------------------------- 1 | public static void printAllSum(int[] a,int sum) { 2 | 3 | printAllSum(a,sum,0,""); 4 | 5 | } 6 | 7 | private static void printAllSum(int[] a,int sum,int i, String s){ 8 | 9 | if (sum == 0 && i == a.length) { // reached sum and end of array of numbers 10 | System.out.println(s); // printing the binary string 11 | return; 12 | } 13 | else if (i == a.length) {// didn't reach sum - cannot create a binary string with the sum number with this array 14 | return; 15 | } 16 | printAllSum(a, sum, i+1,s +'0'); 17 | printAllSum(a,sum-a[i], i+1, s + '1'); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Exams/2018/2018a #87 (Recursion: howManySorted): -------------------------------------------------------------------------------- 1 | public static int howManySorted(int n, int max) { 2 | return howManySorted(n, max, 1); 3 | } 4 | 5 | 6 | public static int howManySorted(int n, int max, int i) { 7 | 8 | if(i > max){ 9 | return 0; // move passed the maximum number 10 | } 11 | 12 | if(n == 0){ 13 | return 1; // a combination of an array has been made 14 | } 15 | 16 | // each recursive call will be split into 2 17 | int use = howManySorted(n - 1,max,i); // use the number and subtract from the length of n "array" 18 | int hold = howManySorted(n,max, i + 1); // hold the length of n "array" and add the next number 19 | 20 | return use + hold; 21 | } 22 | -------------------------------------------------------------------------------- /Exams/2016/Space.java: -------------------------------------------------------------------------------- 1 | public class Space { 2 | int _len ; 3 | int[] _UFOPos = new int[2]; 4 | public Space(int n,int i, int j){ 5 | _len = n; 6 | _UFOPos[0] = i; 7 | _UFOPos[1] = j; 8 | } 9 | public int getSize(){ 10 | return _len; 11 | } 12 | public int[] ask(int i, int j){ 13 | int x, y; 14 | if(i == _UFOPos[0]) 15 | x = 0; 16 | else if(_UFOPos[0] > i) 17 | x = 1; 18 | else 19 | x = -1; 20 | 21 | if(j == _UFOPos[1]) 22 | y = 0; 23 | else if(_UFOPos[1] > j) 24 | y = 1; 25 | else 26 | y = -1; 27 | 28 | int[] a = {x,y}; 29 | 30 | return a; 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Exams/2021/2021b #93 (Recursion: printExpr).java: -------------------------------------------------------------------------------- 1 | public static int printExpr(int[] arr, int num) { 2 | if (arr == null || arr.length == 0) { 3 | return 0; 4 | } 5 | 6 | return printExpr(arr, num, 0, ""); 7 | } 8 | 9 | 10 | private static int printExpr(int[] a, int num, int i, String str) { 11 | 12 | if (num == 0) { 13 | System.out.println(str); 14 | return 1; 15 | } 16 | 17 | if (i == a.length) { 18 | return 0; 19 | } 20 | 21 | 22 | int take = printExpr(a, num + a[i], i + 1, str.concat("+" + a[i])); 23 | int leave = printExpr(a, num - a[i], i + 1, str.concat("-" + a[i])); 24 | int hold = printExpr(a, num, i + 1, str); 25 | 26 | 27 | return take + hold + leave; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Exams/2019/2019b #84 (Recursion: howManyPaths).java: -------------------------------------------------------------------------------- 1 | public static int howManyPaths(int[][] mat){ 2 | return howManyPaths(mat,mat[0][0],0,0); 3 | } 4 | 5 | private static int howManyPaths(int[][] mat, int k, int i ,int j){ 6 | 7 | if(i > mat.length - 1 || i < 0 || j > mat[0].length - 1 || j < 0 || mat[i][j] == 0){ 8 | return 0; 9 | } 10 | 11 | if(i == mat.length - 1 && j == mat[0].length - 1){ 12 | return 1; 13 | } 14 | 15 | k = mat[i][j]; 16 | mat[i][j] = 0; 17 | 18 | int up = howManyPaths(mat,k,i - k, j); 19 | int down = howManyPaths(mat,k,i + k, j); 20 | int left = howManyPaths(mat,k,i, j - k); 21 | int right = howManyPaths(mat,k,i, j + k); 22 | 23 | mat[i][j] = k; 24 | 25 | return up+down+left+right; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Exams/2017/2017a #86 (Efficiency: countTriplets).java: -------------------------------------------------------------------------------- 1 | // Time Complexity: O(n^2) - as requested 2 | // Space Complexity: O(1) 3 | 4 | public static int countTriplets(int[] a , int num){ 5 | int high = a.length - 1; 6 | int low = 0; 7 | int mid = high - 1; 8 | int counter = 0; 9 | 10 | while (low <= mid){ 11 | 12 | if(low == mid){ 13 | high = high - 1; 14 | mid = high - 1; 15 | if(low == high - 1){ 16 | break; 17 | } 18 | } 19 | if(a[high] + a[mid] + a[low] < num){ 20 | counter++; 21 | mid--; 22 | } 23 | else if(a[high] + a[mid] + a[low] >= num){ 24 | mid--; 25 | } 26 | } 27 | 28 | return counter; 29 | } 30 | -------------------------------------------------------------------------------- /Exams/2018/2018_A85_Qs2: -------------------------------------------------------------------------------- 1 | //2.1 The mathod what is finding the length of the longest subarray in the given array a such that the sum of its elements is an odd number 2 | //2.2 O(n^3) 3 | //2.3: 4 | public static int what(int[] a) { 5 | int temp = 0; 6 | int currentSum = 0; 7 | int maxTemp = 0; 8 | 9 | for (int i = 0; i < a.length; i++) { 10 | currentSum += a[i]; 11 | 12 | if (currentSum % 2 == 1) { 13 | temp = i + 1; 14 | } else if (currentSum % 2 == 0 && temp > 0) { 15 | temp--; 16 | } 17 | 18 | if (temp > maxTemp) { 19 | maxTemp = temp; 20 | } 21 | } 22 | 23 | return maxTemp; 24 | } 25 | 26 | 27 | //2.4 This optimized version achieves a time complexity of O(n), where n is the length of the input array a. It traverses the array a only once, updating the relevant variables based on the current element's sum. By eliminating the nested loops, we significantly improve the efficiency of the algorithm. 28 | -------------------------------------------------------------------------------- /Exams/2015/2015a #83 (Recursion: shortestPath).java: -------------------------------------------------------------------------------- 1 | public static int shortestPath(int[][] mat) { 2 | return shortestPath(mat, 0, 0, 0); 3 | } 4 | 5 | public static int shortestPath(int[][] mat, int prev, int i, int j) { 6 | 7 | if (i < 0 || i > mat.length - 1 || j < 0 || j > mat[0].length - 1 || mat[i][j] <= prev || mat[i][j] == 0) { 8 | return Integer.MAX_VALUE; 9 | } 10 | 11 | if (i == mat.length - 1 && j == mat[0].length - 1) { 12 | return 1; 13 | } 14 | 15 | int value = mat[i][j]; 16 | mat[i][j] = 0; 17 | 18 | int up = shortestPath(mat, value, i - 1, j); 19 | int down = shortestPath(mat, value, i + 1, j); 20 | int right = shortestPath(mat, value, i, j + 1); 21 | int left = shortestPath(mat, value, i, j - 1); 22 | 23 | mat[i][j] = value; 24 | 25 | 26 | int shortest = Math.min(Math.min(up, down), Math.min(left, right)); 27 | if (shortest == Integer.MAX_VALUE) 28 | return shortest; 29 | 30 | return 1 + shortest; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Exams/2014/2014a #85 (Recursion: printPath).java: -------------------------------------------------------------------------------- 1 | public static void printPath(int[][] mat){ 2 | 3 | if(mat == null){ 4 | return; 5 | } 6 | String path = printPath(mat, 0, 0, mat[0][0] - 1, ""); 7 | System.out.println(path); 8 | 9 | } 10 | 11 | private static String printPath(int[][] mat, int i, int j, int prev, String path){ 12 | 13 | if(i < 0 || i > mat.length - 1 || j < 0 || j > mat[0].length - 1 || mat[i][j] <= prev){ 14 | return path; 15 | } 16 | 17 | 18 | path += "(" + i + "," + j + ")"; 19 | 20 | 21 | 22 | String up = printPath(mat, i - 1, j, mat[i][j], path); 23 | String right = printPath(mat, i, j + 1, mat[i][j], path); 24 | String down = printPath(mat, i + 1, j, mat[i][j], path); 25 | String left = printPath(mat, i, j - 1, mat[i][j], path); 26 | 27 | return maxString(up,down,right,left); 28 | 29 | } 30 | 31 | private static String maxString(String a, String b, String c, String d) { 32 | 33 | if (a.length() < b.length()) 34 | a = b; 35 | 36 | if (c.length() < d.length()) 37 | c = d; 38 | 39 | if (a.length() < c.length()) 40 | a = c; 41 | 42 | return a; 43 | } 44 | -------------------------------------------------------------------------------- /Exams/2023/Q1_81.java: -------------------------------------------------------------------------------- 1 | // By Ziv Laifer 2 | public static int test(int[][] mat){ 3 | int[][] counter = new int[mat.length][mat[0].length]; 4 | return test(mat,0,0,0, counter, ""); 5 | } 6 | private static int test(int[][]m, int i, int j, int sum, int[][] counter, String str){ 7 | if(i == m.length-1 && j == m[i].length-1){ 8 | sum += m[i][j]; 9 | str += "(" + i + "," + j + ") = " + sum; 10 | System.out.println(str); 11 | return sum; 12 | } 13 | if(i < 0 || j < 0 || i >= m.length || j >= m[i].length || counter[i][j] >= m[i][j]){ 14 | return Integer.MIN_VALUE; 15 | } 16 | counter[i][j]++; 17 | str += "(" + i + "," + j + ") --> "; 18 | int left = test(m,i,j-1,sum+m[i][j], counter, str); 19 | int right = test(m,i,j+1,sum+m[i][j], counter, str); 20 | int up = test(m,i-1,j,sum+m[i][j], counter, str); 21 | int down = test(m,i+1,j,sum+m[i][j], counter, str); 22 | counter[i][j]--; 23 | return max(left,right,up,down); 24 | } 25 | private static int max(int a, int b, int c, int d) { 26 | int max1 = a > b ? a : b; 27 | int max2 = c > d ? c : d; 28 | return max1 > max2 ? max1 : max2; 29 | } 30 | -------------------------------------------------------------------------------- /Exams/2023/T23Aֹֹ_A2_Q1: -------------------------------------------------------------------------------- 1 | public static void main(String[] args) { 2 | int[][] mat = {{0, 1, 1, 1}, 3 | {1, 0, 1, 1}, 4 | {1, 1, 0, 1}, 5 | {1, 1, 1, 0}}; 6 | int x = friend3(mat); 7 | System.out.println(x); 8 | char c= 'a'; 9 | c++; 10 | System.out.println(c); 11 | 12 | } 13 | 14 | 15 | public static int friend3(int[][] mat) { 16 | if (mat.length < 2) { 17 | return 0; 18 | } 19 | boolean[][][] visited = new boolean[mat.length][mat.length][mat.length]; 20 | return friend3(mat, 0, 1, 2, visited); 21 | } 22 | 23 | public static int friend3(int[][] mat, int i, int j, int k, boolean[][][] visited) { 24 | if (i >= mat.length - 2 || j >= mat.length - 1 || k >= mat.length) { 25 | return 0; 26 | } 27 | int count = 0; 28 | if (!visited[i][j][k] && mat[i][j] == 1 && mat[j][k] == 1 && mat[i][k] == 1) { 29 | visited[i][j][k] = true; 30 | visited[j][k][i] = true; 31 | visited[i][k][j] = true; 32 | 33 | count++;} 34 | count += friend3(mat, i, j, k + 1, visited) + 35 | friend3(mat, i, j + 1, k, visited) + 36 | friend3(mat, i + 1, j, k, visited); 37 | return count; 38 | } 39 | -------------------------------------------------------------------------------- /Exams/2015/2015_A84_Qs.java: -------------------------------------------------------------------------------- 1 | public class E2015_A84_Qs { 2 | public static int countRope(int[][]mat){ 3 | return countRope(mat,0,0,-1); 4 | } 5 | 6 | private static int countRope(int[][]mat, int i, int j ,int pre){ 7 | if(i == mat.length || j == mat[0].length || i <0 || j < 0) 8 | return 0; 9 | if(mat[i][j] >= pre && pre != -1) 10 | return 0; 11 | if(i == mat.length-1) 12 | return 1; 13 | int count = countRope(mat,i+1,j,mat[i][j]) + countRope(mat,i+1,j-1,mat[i][j]) + countRope(mat,i+1,j+1,mat[i][j]); 14 | 15 | if(i == 0) 16 | return count + countRope(mat,i,j+1,-1); 17 | return count; 18 | } 19 | 20 | 21 | public static boolean splitT03(int[] arr){ 22 | int sum = 0; 23 | for(int i = 0; i< arr.length;i++){ 24 | sum += arr[i]; 25 | } 26 | 27 | int lim1 = 0, lim2 = arr.length-1, sum1 = 0, sum2 = 0, sum3 = 0; 28 | while(lim1 < lim2){ 29 | sum3 = sum - (sum1 + sum2); 30 | if(sum3 == 0 && sum1 <= 0 && sum2 >= 0) 31 | return true; 32 | else if(sum3 > 0) 33 | sum2 += arr[lim2--]; 34 | else if(sum3 < 0) 35 | sum1 += arr[lim1++]; 36 | else{ 37 | sum2 += arr[lim2--]; 38 | sum1 += arr[lim1++]; 39 | } 40 | 41 | 42 | } 43 | return false; 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Exams/2021/2021_B62_Qs.java: -------------------------------------------------------------------------------- 1 | public class E2021_B62_Qs{ 2 | //Q1 3 | public static boolean equalsSplit(int[] arr){ 4 | if(arr.length <1 || !(arr.length / 2 != 0)) 5 | return false; 6 | return equalsSplit(arr,0,0,0,0,0); 7 | } 8 | 9 | private static boolean equalsSplit(int[] arr, int i, int sum1, int sum2, int l1, int l2) { 10 | if(i == arr.length){ 11 | if(sum1 == sum2 && l1 == l2 && l1 == arr.length/2) 12 | return true; 13 | return false; 14 | } 15 | return equalsSplit(arr, i+1, sum1+arr[i], sum2, l1+1, l2) || equalsSplit(arr, i+1, sum1, sum2+arr[i], l1, l2+1); 16 | } 17 | public static int maxPath(int [][] mat){ 18 | return maxPath(mat,0,0,0); 19 | } 20 | private static int maxPath(int[][] mat, int i, int j, int sum){ 21 | if(i == mat.length || j == mat[0].length) 22 | return Integer.MAX_VALUE; 23 | if(i == mat.length-1 && j == mat[0].length -1) 24 | return mat[i][j] + sum; 25 | int stp1 = mat[i][j] / 10, stp2 = mat[i][j]%10; 26 | 27 | int opt1 = maxPath(mat, i+stp1, j+stp2, sum + mat[i][j]); 28 | int opt2= maxPath(mat, i+stp2, j+stp1, sum + mat[i][j]); 29 | 30 | return Math.max(opt1, opt2); 31 | 32 | } 33 | public static void main(String[] args) { 34 | int[] arr = {-3,5,-12,14,-9,13}; 35 | System.out.println(equalsSplit(arr)); 36 | System.out.println(2); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Exams/2019/Exam2019B85.java: -------------------------------------------------------------------------------- 1 | package Exams; 2 | public class Exam2019B85 { 3 | public static int howManyPaths(int[][] mat) { 4 | return howManyPaths(mat, 0, 0); 5 | } 6 | 7 | private static int howManyPaths(int[][] mat, int i, int j) { 8 | 9 | if (i >= mat.length || i < 0 || j < 0 || j >= mat[0].length) { 10 | return 0; 11 | } 12 | 13 | if (i == mat.length - 1 && j == mat[0].length - 1) { 14 | return 1; 15 | } 16 | 17 | int k = mat[i][j]; 18 | mat[i][j] = -k; 19 | int up = howManyPaths(mat, i + k, j) + howManyPaths(mat, i - k, j) + howManyPaths(mat, i, j + k) 20 | + howManyPaths(mat, i, j - k); 21 | mat[i][j] = k; 22 | return up; 23 | 24 | } 25 | 26 | public static int meetingPoint(int[] a, int[] b) { 27 | int smallest = Integer.MAX_VALUE; 28 | int high = Math.min(a.length, b.length); 29 | int low = 0; 30 | int mid; 31 | while (low <= high) { 32 | mid = (low + high) / 2; 33 | if (a[mid] == b[mid]) { 34 | if (mid < smallest) { 35 | smallest = mid; 36 | } 37 | high = mid - 1; 38 | } else if (a[mid] > b[mid]) { 39 | low = mid + 1; 40 | } else if (a[mid] < b[mid]) { 41 | high = mid - 1; 42 | } 43 | } 44 | if (smallest == Integer.MAX_VALUE) { 45 | return -1; 46 | } 47 | return smallest; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Exams/2018/2018_A87_Qs.java: -------------------------------------------------------------------------------- 1 | public class 2018_A87_Qs{ 2 | 3 | //Q1 4 | public static int longSlope(int[][] mat, int num){ 5 | return longSlope(mat,num,0,0,0); 6 | } 7 | 8 | private static int longSlope(int[][] mat, int num, int i, int j, int count) { 9 | if(i == mat.length) 10 | return count; 11 | if(j == mat[0].length) 12 | return longSlope(mat, num, i+1, 0, count); 13 | int tmpCount = longSlope(num,mat,i,j,(mat[i][j] + num)); 14 | if(tmpCount > count) 15 | count = tmpCount; 16 | return longSlope(mat, num, i, j+1, count); 17 | } 18 | 19 | private static int longSlope(int num, int[][] mat, int i, int j, int pre) { 20 | if(i == mat.length || j == mat[0].length || i < 0 || j < 0 || mat[i][j] == Integer.MAX_VALUE) 21 | return 0; 22 | if(pre - mat[i][j] == num){ 23 | int tmp = mat[i][j]; 24 | mat[i][j] = Integer.MAX_VALUE; 25 | 26 | int down = longSlope(num, mat, i+1, j, tmp) + 1; 27 | int up = longSlope(num, mat, i-1, j, tmp) + 1; 28 | int right = longSlope(num, mat, i, j+1, tmp) + 1; 29 | int left = longSlope(num, mat, i, j-1, tmp) + 1; 30 | 31 | mat[i][j] = tmp; 32 | 33 | return Math.max(Math.max(up, down), Math.max(right, left)); 34 | } 35 | return 0; 36 | } 37 | public static void main(String[] args) { 38 | int[][] mat = { 39 | {3,13,15,28,30}, 40 | {55,54,53,27,26}, 41 | {54,12,52,51,50}, 42 | {50,10,8,53,11}}; 43 | 44 | System.out.println(longSlope(mat, 1)); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Exams/2016/UFOSearch.java: -------------------------------------------------------------------------------- 1 | public class UFOSerach { 2 | public static int[] findUFO(Space sp){ 3 | int butC =0 , butR = 0, topC = sp.getSize()-1, topR = sp.getSize() -1, midC = 0,midR =0; 4 | 5 | while(butC <= topC && butR <= topR){ 6 | midC = (butC + topC)/2; 7 | midR = (butR + topR)/2; 8 | int[] tempAsk = sp.ask(midC, midR); 9 | 10 | 11 | if(tempAsk[0] == 0 && tempAsk[1] ==0){ 12 | butC = topC+1; 13 | butR = topR+1; 14 | } else { 15 | if(tempAsk[0] == 1) 16 | butC = midC +1; 17 | else if(tempAsk[0] == -1) 18 | topC = midC -1; 19 | 20 | if(tempAsk[1] == 1) 21 | butR = midR +1; 22 | else if( tempAsk[1] == -1) 23 | topR = midR -1; 24 | } 25 | 26 | } 27 | int[] loc = {midC,midR}; 28 | return loc; 29 | } 30 | 31 | public static boolean isPy(int[] arr){ 32 | int a,b ,c = arr.length-1, sumAB=0; 33 | 34 | for(int i = arr.length-2; i > 0; i--){ 35 | a = 0; 36 | b = i; 37 | sumAB = arr[a] + arr[b]; 38 | while(a < b && sumAB <= arr[c]){ 39 | if(sumAB == arr[c]) 40 | return true; 41 | else 42 | a++; 43 | sumAB = arr[a] + arr[b]; 44 | } 45 | c--; 46 | 47 | } 48 | return false; 49 | } 50 | 51 | public static void main(String[] args) { 52 | int[] arr = {4,16,25,36,81,144,167,196}; 53 | System.out.println(isPy(arr)); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Exams/2022/2022_B86.java: -------------------------------------------------------------------------------- 1 | public class E2022_B86_Qs{ 2 | //Q1 3 | public static int maxPath(int [][] mat){ 4 | return maxPath(mat,0,0,0); 5 | } 6 | private static int maxPath(int[][] mat, int i, int j, int sum){ 7 | if(i >= mat.length || j >= mat[0].length) 8 | return Integer.MIN_VALUE; //The equivalent to "False" in this question is Integer.MIN_VALUE and not Integer.MAX_VALUE. 9 | if(i == mat.length-1 && j == mat[0].length -1) 10 | return mat[i][j] + sum; 11 | int stp1 = mat[i][j] / 10, stp2 = mat[i][j]%10; 12 | 13 | int opt1 = maxPath(mat, i+stp1, j+stp2, sum + mat[i][j]); 14 | int opt2= maxPath(mat, i+stp2, j+stp1, sum + mat[i][j]); 15 | 16 | return Math.max(opt1, opt2); 17 | 18 | } 19 | 20 | //Q2 21 | // Time complexity: O(n) 22 | // Space complexity: O(1) 23 | 24 | // Time complexity is actually O(n*k), but k is constant. 25 | public static void sortmod(int[] a, int k){ 26 | 27 | int newIndex = 0; 28 | 29 | for(int mod = 0; mod < k; mod++){ 30 | 31 | for (int i = newIndex; i < a.length ; i++) { 32 | 33 | if(a[i] % k == mod){ 34 | 35 | // Swaps between both elements. 36 | int tempVal = a[newIndex]; 37 | a[newIndex] = a[i]; 38 | a[i] = tempVal; 39 | 40 | newIndex++; 41 | } 42 | } 43 | } 44 | } 45 | 46 | 47 | public static void main(String[] args){ 48 | 49 | int[] a = {35,17,13,252,4,128,7,3,81}; 50 | int[] expected = {81,252,13,3,4,35,17,7,128}; 51 | 52 | sortmod(a,10); 53 | System.out.println("expected: " + Arrays.toString(expected)); 54 | System.out.println("received: " + Arrays.toString(a)); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Exams/2020/2020_b81_Qs.java: -------------------------------------------------------------------------------- 1 | public class E2020_b81_Qs{ 2 | 3 | //Q1 4 | public static int makeSum(int[] lens, int k, int num){ 5 | if(k == 0 || num == 0) 6 | return 0; 7 | return makeSum(lens,k,num,0); 8 | } 9 | 10 | private static int makeSum(int[] lens, int k, int num, int i){ 11 | if(i == lens.length || num < 0 || k < 0) 12 | return 0; 13 | 14 | if(k == 0 && num >= 0) 15 | return 1; 16 | //Try to add lens[i] to sum Or try take the next lens(i+1) 17 | int count = makeSum(lens, k - lens[i], num-1, i) + makeSum(lens, k, num, i+1); 18 | 19 | return count; 20 | } 21 | 22 | //Q2 - O(n) 23 | public static void minimumSubK(int[] arr, int k){ 24 | int[] minSub = new int[3]; // [0] - min sum, [1] - start of subArr index, [2] end of subArr index 25 | int i = 0, j = 0; 26 | 27 | for(; j <= k-1 ;j++){ 28 | minSub[0] += arr[j]; 29 | } 30 | 31 | minSub[1] = i; 32 | minSub[2] = --j; 33 | 34 | int tempSum = minSub[0]; 35 | 36 | while(j < arr.length-1){ 37 | tempSum -= arr[i]; 38 | i++; 39 | j++; 40 | tempSum += arr[j]; 41 | 42 | if(tempSum < minSub[0]){ 43 | minSub[0] = tempSum; 44 | minSub[1] = i; 45 | minSub[2] = j; 46 | } 47 | } 48 | System.out.println("Minimum sum sub-arry is (" + minSub[1] + "," + minSub[2] +")"); 49 | 50 | 51 | } 52 | 53 | //Tester 54 | public static void main(String[] args) { 55 | 56 | //Test Q1 57 | int[] lens = {5,10,20,50}; 58 | int k = 40, num = 4; 59 | System.out.println(makeSum(lens, k, num)); // Should be 4 60 | 61 | //Test Q2 62 | int[] arr = {10,4,2,5,6,3,8,1,5,9}; 63 | minimumSubK(arr, 3); // Should be (1,3) 64 | minimumSubK(arr, 2); // Should be (1,2) / (7,8) 65 | 66 | 67 | 68 | } 69 | 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Exams/2021/2021_B91_Qs.java: -------------------------------------------------------------------------------- 1 | public class E2021_B91_Qs { 2 | //Q1 3 | public static int printExpr(int[] arr, int num){ 4 | return printExpr(arr, num,0,"",0); 5 | } 6 | 7 | private static int printExpr(int[] arr, int num, int i, String s, int curSum) { 8 | 9 | if(curSum == num){ 10 | System.out.println(s); 11 | return 1; 12 | } 13 | if(i == arr.length) 14 | return 0; 15 | return printExpr(arr, num, i+1, s + "+" + arr[i], curSum + arr[i]) + printExpr(arr, num, i+1, s + "-" + arr[i], curSum - arr[i]) + printExpr(arr, num, i+1, s, curSum); 16 | } 17 | public static int longestSequence(int[] a, int k){ 18 | int i = 0, maxCount = 0, curCount = 0,zeroFound = k; 19 | int[] zeros = new int[a.length]; 20 | int ZeroI = 0, zeroused = 0;; 21 | 22 | while(i < a.length){ 23 | if(a[i] == 0){ 24 | zeros[ZeroI++] = i; 25 | if(zeroFound > 0){ 26 | zeroFound--; 27 | curCount++; 28 | i++; 29 | 30 | }else{ 31 | zeroFound = k; 32 | if(maxCount < curCount) 33 | maxCount = curCount; 34 | curCount = 0; 35 | i = zeros[zeroused++]+1; 36 | 37 | } 38 | } else { 39 | curCount++; 40 | i++; 41 | } 42 | } 43 | return curCount > maxCount ? curCount:maxCount; 44 | } 45 | 46 | public static void main(String[] args) { 47 | int[] arr1 = {1,3,6,2}; 48 | System.out.println(printExpr(arr1, 3)); 49 | 50 | int[] arr = {1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0}; 51 | System.out.println("expected 4: " + longestSequence(arr, 0)); 52 | System.out.println("expected 7: " + longestSequence(arr, 1)); 53 | System.out.println("expected 10: " + longestSequence(arr, 2)); 54 | System.out.println("expected 11: " + longestSequence(arr, 3)); 55 | 56 | 57 | } 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Exams/2021/2021_B50_Qs.java: -------------------------------------------------------------------------------- 1 | 2 | public class E2021_B50_Qs{ 3 | 4 | //Q1 5 | public static boolean split3(int[] arr){ 6 | return split3(arr,0,0,0,0); 7 | } 8 | 9 | private static boolean split3(int[] arr, int i, int sum1, int sum2, int sum3) { 10 | if(i == arr.length){ 11 | if(sum1 == sum2 && sum2 == sum3) 12 | return true; 13 | return false; 14 | } 15 | return split3(arr,i+1, sum1 + arr[i], sum2, sum3) || 16 | split3(arr,i+1, sum1 ,arr[i] + sum2, sum3) || 17 | split3(arr,i+1, sum1, sum2, sum3+arr[i]); 18 | 19 | } 20 | 21 | //Q2 22 | public static int smallestSub(int[] a, int k){ 23 | int low = 0, high = 0, curCount = 0, minCount = a.length+1, curSum = 0; 24 | 25 | while(low < a.length){ 26 | if(curSum <= k){ 27 | if(high < a.length){ 28 | curCount++; 29 | curSum += a[high]; 30 | high++; 31 | } 32 | else 33 | low = a.length; 34 | } 35 | else if(curSum > k){ 36 | if(curCount < minCount) 37 | minCount = curCount; 38 | curCount--; 39 | curSum -= a[low]; 40 | low++; 41 | } 42 | } 43 | return minCount; 44 | } 45 | public static void main(String[] args) { 46 | System.out.println("expected true: " + split3(new int[]{8,4,7,1,2,3,5})); 47 | System.out.println("expected false: " + split3(new int[]{4,7,1,2,3,5})); 48 | 49 | 50 | 51 | int[] arr = {1, 4, 13, 6, 0, 19}; 52 | System.out.println("expected 3: " + smallestSub(arr, 22)); 53 | System.out.println("expected 6: " + smallestSub(arr, 42)); 54 | System.out.println("expected 7: " + smallestSub(arr, 43)); 55 | System.out.println("expected 4: " + smallestSub(arr, 26)); 56 | System.out.println("expected 1: " + smallestSub(arr, 2)); 57 | System.out.println("expected 1: " + smallestSub(arr, 0)); 58 | 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Exams/2020/2020_A87_Qs.java: -------------------------------------------------------------------------------- 1 | public class 2020_A87_Qs{ 2 | //Q1 3 | public static int totalWays(int[][] mat, int k){ 4 | return totalWays(mat,k,0,0,'E'); 5 | } 6 | 7 | private static int totalWays(int[][] mat, int k, int i, int j, char c) { 8 | if(i == mat.length || j == mat[0].length || k < 0){ 9 | return 0;} 10 | if(i == mat.length-1 && j == mat[0].length-1){ 11 | if(k == 0) 12 | return 1; 13 | return 0; 14 | } 15 | int spt1; 16 | if(c == 'D') 17 | spt1 = totalWays(mat, k, i+1, j, 'D') + totalWays(mat, k-1, i, j+1, 'L'); 18 | else if(c == 'L') 19 | spt1 = totalWays(mat, k-1, i+1, j, 'D') + totalWays(mat, k, i, j+1, 'L'); 20 | else // for the first step 21 | spt1 = totalWays(mat, k, i+1, j, 'D') + totalWays(mat, k, i, j+1, 'L'); 22 | return spt1; 23 | } 24 | 25 | //Q2 26 | public static void printTripels(int[] arr, int num){ 27 | if(arr.length < 3) 28 | return; 29 | 30 | int a = 0, b = 1, c = arr.length-1; 31 | int sum = 0; 32 | for(;b< arr.length-2;a++){ 33 | b = a+1; 34 | c=arr.length-1; 35 | while(b '9'; 19 | if (notANumber) 20 | return 0; 21 | 22 | boolean invalidSequence = prevChar >= s.charAt(0); 23 | if (invalidSequence) 24 | return 0; 25 | 26 | return 1 + longOrdNum_Koren(s.substring(1), s.charAt(0)); 27 | } 28 | 29 | 30 | public static int longOrdNum(String s) { 31 | if (s == null || s.length() == 0) 32 | return 0; 33 | return longOrdNum(s, 0, 0, 0, ' '); 34 | } 35 | private static boolean isNumber(char c){ 36 | return c >= '0' || c <= '9'; 37 | } 38 | 39 | private static int longOrdNum(String s, int i, int count, int max, char prev) { 40 | if (i == s.length()) 41 | return max; 42 | char curr = s.charAt(i); 43 | if (i == 0) { 44 | if (isNumber(curr)) 45 | return longOrdNum(s, i + 1, count + 1, count + 1, curr); 46 | return longOrdNum(s, i + 1, count, count, curr); 47 | } 48 | if (isNumber(curr)) { 49 | if (isNumber(prev) && curr > prev) { 50 | count++; 51 | if (count > max) 52 | max = count; 53 | return longOrdNum(s, i + 1, count, max, curr); 54 | } 55 | return longOrdNum(s, i + 1, 1, Math.max(1, max), curr); 56 | } 57 | return longOrdNum(s, i + 1, count, max, curr); 58 | } 59 | 60 | public static void main(String[] args) { 61 | 62 | System.out.println("expected 3: " + longOrdNum("x12y3348")); 63 | System.out.println("expected 1: " + longOrdNum("321")); 64 | System.out.println("expected 0: " + longOrdNum("xyz")); 65 | System.out.println("expected 0: " + longOrdNum("")); 66 | System.out.println("expected 4: " + longOrdNum("23256812")); 67 | System.out.println("expected 4: " + longOrdNum("1234")); 68 | System.out.println("expected 1: " + longOrdNum("9")); 69 | } 70 | 71 | } 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Exams/2017/2017_B93_Tst.java: -------------------------------------------------------------------------------- 1 | public class O2017B93 { 2 | 3 | public static int cntTrueReg(boolean[][] mat) { 4 | return cntTrueReg(mat, 0, 0); 5 | } 6 | 7 | private static int cntTrueReg(boolean[][] mat, int i, int j){ 8 | if (i>=mat.length || j>=mat[0].length || j<0 || i<0){ 9 | return 0; 10 | } 11 | if (mat[i][j]==true){ 12 | cleaner(mat, i, j); 13 | return 1 + cntTrueReg(mat,i+1,j) + cntTrueReg(mat,i,j+1); 14 | } 15 | return cntTrueReg(mat,i+1,j) + cntTrueReg(mat,i,j+1); 16 | } 17 | 18 | public static void cleaner(boolean[][] mat, int i, int j) { 19 | if (i >= mat.length || j >= mat[0].length || j < 0 || i < 0) { 20 | return; 21 | } 22 | if (mat[i][j] == true) { 23 | mat[i][j] = false; 24 | cleaner(mat, i + 1, j); 25 | cleaner(mat, i - 1, j); 26 | cleaner(mat, i, j - 1); 27 | cleaner(mat, i, j + 1); 28 | } 29 | 30 | } 31 | 32 | public static int findNum(Range rangeA[], int num){ 33 | int high = rangeA.length-1, low = 0; 34 | int mid; 35 | while (low<=high){ 36 | mid = (low+high)/2; 37 | if (num<=rangeA[mid].getCenter()+rangeA[mid].getRadius() && num>=rangeA[mid].getCenter()-rangeA[mid].getRadius()){ 38 | return mid; 39 | } 40 | else if (num>rangeA[mid].getCenter()+rangeA[mid].getRadius()){ 41 | low = mid+1; 42 | } 43 | else if (num -1){ 26 | sum = a[iA] + b[iB]; 27 | if(Math.abs(sum - x) < minSum[0]){ 28 | minSum[0] = Math.abs(sum - x); 29 | minSum[1] = iA; 30 | minSum[2] = iB; 31 | } 32 | if(sum == x){ 33 | System.out.println(a[iA] + " and " + b[iB]); 34 | return; 35 | } 36 | if(sum > x){ 37 | iB--; 38 | } else 39 | iA++; 40 | 41 | } 42 | System.out.println(a[minSum[1]] + " ~and~ " + b[minSum[2]]); 43 | 44 | 45 | } 46 | 47 | //O(log-n) 48 | public static void printClosest1(int[] a, int[] b, int x) { 49 | 50 | 51 | int lowA = 0; 52 | int highA = a.length - 1; 53 | 54 | int lowB = 0; 55 | int highB = b.length - 1; 56 | 57 | int numA = 0, numB = 0, savedDiff = Integer.MAX_VALUE; 58 | 59 | int sum = 0; 60 | 61 | while (lowA <= highA || lowB <= highB) { 62 | 63 | int midA = (lowA + highA) / 2; 64 | int midB = (lowB + highB) / 2; 65 | 66 | sum = a[midA] + b[midB]; 67 | 68 | if (sum == x) { 69 | System.out.println(a[midA] + " and " + a[midB]); 70 | return; 71 | } 72 | 73 | 74 | int currDiff = sum - x; 75 | if (currDiff < 0) currDiff = -currDiff; 76 | 77 | if (currDiff < savedDiff) { 78 | numA = a[midA]; 79 | numB = b[midB]; 80 | savedDiff = currDiff; 81 | } 82 | 83 | 84 | if (sum < x) { 85 | 86 | int intendedMidA = (midA + 1 + highA) / 2; 87 | int intendedMidB = (midB + 1 + highB) / 2; 88 | 89 | if ((lowA < highA && a[intendedMidA] < b[intendedMidB]) || lowB == highB) 90 | lowA = intendedMidA; 91 | else 92 | lowB = intendedMidB; 93 | } else { 94 | int intendedMidA = (lowA + midA) / 2; 95 | int intendedMidB = (lowB + midB) / 2; 96 | 97 | if ((lowB < highB && a[intendedMidA] < b[intendedMidB]) || lowA == highA) 98 | highB = intendedMidB; 99 | else 100 | highA = intendedMidA; 101 | } 102 | } 103 | int lastDiff = a[lowA] + b[lowB] - x; 104 | if (lastDiff < 0) lastDiff = -lastDiff; 105 | 106 | if (lastDiff < savedDiff) 107 | System.out.println(a[lowA] + " and " + b[lowB]); 108 | else 109 | System.out.println(numA + " and " + numB); 110 | 111 | } 112 | 113 | public static void main(String[] args) { 114 | for(int i = 1; i < 20; i++){ 115 | System.out.println(oneFiveSeven(i)); 116 | } 117 | 118 | int[] a = {0, 4, 6, 11, 11}; 119 | int[] b = {10, 20, 30, 40}; 120 | System.out.print("expected \"0 and 10\": "); 121 | printClosest(a, b, 11); 122 | 123 | System.out.print("expected \"4 and 10\": "); 124 | printClosest(a, b, 13); 125 | 126 | System.out.print("expected \"10 and 6\" or \"0 and 20\" : "); 127 | printClosest(a, b, 18); 128 | 129 | System.out.print("expected \"40 and 11\": "); 130 | printClosest(a, b, 70); 131 | 132 | char c = '5'; 133 | char c1 = '6'; 134 | System.out.println(c < c1); 135 | 136 | } 137 | } 138 | --------------------------------------------------------------------------------