├── README.md ├── Year2007 └── 2007.java ├── Year2009 └── August2009a83 │ └── Poly │ ├── A.java │ ├── B.java │ └── DriverandTester.java ├── Year2011 └── 2011a.java ├── Year2012 ├── February2012a82 ├── July2012a96 ├── June2012a81 └── May2012a92 ├── Year2013 ├── August2013a92 ├── July2013a34 └── March2013a33 ├── Year2014 └── February2014a85 ├── Year2015 ├── 2015a83 └── 2015a85 ├── Year2016 ├── February2016a83 └── June2016a82 ├── Year2017 ├── August2017a87 ├── August2017a93 └── March2017a86 ├── Year2018 ├── 2018a87 ├── February2018a85 │ ├── February2018a85.pdf │ └── RecursionComplexity.java ├── June2018b83 │ ├── June2018b83.pdf │ └── RecursionComplexity.java └── March2018a91 ├── Year2019 ├── 2018a84 │ ├── Poly │ │ ├── A.java │ │ ├── B.java │ │ └── Driver.java │ └── RecursionComplexity │ │ └── RecursionComplexity.java ├── 2019b83 │ └── ReucrsionComplexity.java ├── 2019b86 │ ├── ComplexityRecursion.java │ └── July2019b86.pdf ├── 2019b93 │ ├── August2019b93.pdf │ ├── Polymorphism │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── E.java │ │ └── main.java │ └── RecursionComplexity.java └── August2019a83 ├── Year2020 ├── July2020a96 ├── July2020b81 │ ├── July2020b81.pdf │ └── RecursioComplexity.java ├── March2020a85 │ ├── Augus2020a85.pdf │ └── RecurionComplexity.java └── March2020a87 │ ├── March2020a87.pdf │ └── RecursionComplexity.java ├── Year2021 ├── Augugst2021a85 ├── August2021 ├── August2021a70 ├── August2021b92 │ ├── August2021b92.pdf │ ├── LinkedLists │ │ ├── Driver.java │ │ ├── Driver2.java │ │ ├── IntNode.java │ │ └── IntNode2.java │ ├── Polymorphism │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── Driver.java │ ├── RecursionComplexity.java │ └── Trees │ │ ├── Driver.java │ │ └── Node.java ├── July2021b62 │ ├── 2021b62.pdf │ └── RecursionComplexity.java └── June2021b60 │ ├── June2021b60.pdf │ └── RecursionComplexity.java ├── Year2022 ├── April2022a96 │ ├── August2022a96.pdf │ ├── Polymorphism │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── Driver.java │ │ └── Tester.java │ └── RecursionComplexity.java ├── August2022a87 ├── July2022b91 │ ├── July2022b91.pdf │ └── RecursionComplexity.java ├── June2022a86 ├── March2022a74 │ ├── LinkedList │ │ └── IntNodee.java │ ├── March2022a74.pdf │ ├── Polymorphism │ │ ├── AAA.java │ │ ├── BBB.java │ │ └── Driver.java │ ├── RecursionComplexity.java │ └── Trees │ │ ├── Driver.java │ │ └── Node.java └── March2022a89 └── Year2023 └── February2023a65 ├── BinaryTree ├── Driver.java └── Node.java ├── Feburary2023a64.pdf ├── Lists ├── Driver.java └── IntNode.java └── RecursionComplexity.java /README.md: -------------------------------------------------------------------------------- 1 | # OpenUJava20441 2 | Java programming intro course (Open University). 3 | All years solutions of recursion and complexity. 4 | -------------------------------------------------------------------------------- /Year2007/2007.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Contributed 3 | *@author:Refael Camus 4 | *@version 22-10-2021 5 | */ 6 | public class a2007b1 7 | { 8 | public static void main (String [] args) 9 | { 10 | int [] b = {2,1,6,5,4}; 11 | System.out.println("currect: 2"); 12 | System.out.println(minDiff(b)); 13 | } 14 | 15 | public static int minDiff(int [] a) 16 | { 17 | return minDiff(a,0); 18 | } 19 | 20 | public static int minDiff(int[]a,int i) 21 | { 22 | if(i==a.length) 23 | return -1; 24 | return Math.max(minDiff(a,0,i,0,0),minDiff(a,i+1)); 25 | } 26 | 27 | public static int minDiff(int[]a,int i,int devided,int sum1,int sum2) 28 | { 29 | if (i==a.length) 30 | { 31 | if(sum1==sum2) 32 | return devided; 33 | else 34 | return -1; 35 | } 36 | if(i<=devided) 37 | sum1+=a[i]; 38 | else 39 | sum2+=a[i]; 40 | return minDiff(a,i+1,devided,sum1,sum2); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Year2009/August2009a83/Poly/A.java: -------------------------------------------------------------------------------- 1 | package Poly3; 2 | 3 | public class A { 4 | 5 | public A() { 6 | System.out.println("A"); 7 | } 8 | 9 | public void arik() { 10 | System.out.println("Arik_A"); 11 | } 12 | 13 | public void yosef() { 14 | arik(); 15 | } 16 | 17 | 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Year2009/August2009a83/Poly/B.java: -------------------------------------------------------------------------------- 1 | package Poly3; 2 | 3 | public class B extends A{ 4 | 5 | public B() { 6 | System.out.println("B"); 7 | } 8 | 9 | public void arik() { 10 | System.out.println("Arik_B"); 11 | } 12 | 13 | public void yosef() { 14 | System.out.println("Yosef"); 15 | } 16 | 17 | public void superYosef() { 18 | super.yosef(); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Year2009/August2009a83/Poly/DriverandTester.java: -------------------------------------------------------------------------------- 1 | package Poly3; 2 | 3 | public class Driver { 4 | 5 | public static void main(String [] args) { 6 | System.out.println("Question 1:"); 7 | A a = (A) new B(); 8 | // System.out.println(a); 9 | 10 | System.out.println("Question 2:"); 11 | A aa = new A(); 12 | // System.out.println(aa); 13 | 14 | System.out.println("Question 3:"); 15 | A ab = new B(); 16 | // System.out.println(ab); 17 | 18 | System.out.println("Question 4:"); 19 | // B ba = new A(); 20 | // System.out.println("Comp Error"); 21 | 22 | System.out.println("Question 5:"); 23 | B bb = new B(); 24 | System.out.println(bb); 25 | 26 | // System.out.println("Question 6:"); 27 | // aa.yosef(); 28 | // 29 | // System.out.println("Question 7:"); 30 | // ab.yosef(); 31 | // 32 | // System.out.println("Question 8:"); 33 | // bb.yosef(); 34 | // 35 | // System.out.println("Question 9:"); 36 | // ((A)aa).yosef(); 37 | 38 | System.out.println("Question 10:"); 39 | ((A)bb).yosef(); 40 | 41 | System.out.println("Question 11:"); 42 | // ((A)bb).superYosef(); 43 | System.out.println("Comp Error"); 44 | 45 | System.out.println("Question 12:"); 46 | System.out.println("Runtime Error"); 47 | // ((B)aa).yosef(); 48 | 49 | System.out.println("Question 13:"); 50 | System.out.println("Runtime Error"); 51 | // ((B)aa).superYosef(); 52 | 53 | System.out.println("Question 14:"); 54 | ((B)ab).superYosef(); 55 | 56 | System.out.println("Question 15:"); 57 | ((B)bb).superYosef(); 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Year2011/2011a.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | *@author:Refael Camus 4 | *@version 22-10-2021 5 | */ 6 | public class a2011 7 | { 8 | public static void main (String [] args) 9 | { 10 | int [][] mat = { 11 | {8,9,5,1,7,8}, 12 | {9,5,5,16,17,18}, 13 | {10,11,6,15,2,19}, 14 | {7,12,13,14,4,20}, 15 | {9,13,4,15,22,21}, 16 | {10,11,12,12,23,22}, 17 | }; 18 | 19 | System.out.println(isPath(mat)); 20 | int [] a ={10,5,1,2,7,4,2,6}; 21 | System.out.println(what(a,2)); 22 | } 23 | 24 | public static boolean isPath(int[][]mat) 25 | { 26 | if(mat.length==0) 27 | return false; 28 | if(mat.length==1) 29 | return true; 30 | return isPath(mat,0,0,mat[0][0]-1); 31 | } 32 | 33 | private static boolean isPath(int[][]mat,int i,int j,int prev) 34 | { 35 | if(i<0||j<0||i>mat.length-1||j>mat[0].length-1||mat[i][j]!=prev+1) 36 | return false; 37 | if(i==mat.length-1&&j==mat[0].length-1) 38 | return true; 39 | prev=mat[i][j]; 40 | return (isPath(mat,i+1,j,prev)||isPath(mat,i-1,j,prev)||isPath(mat,i,j+1,prev)||isPath(mat,i,j-1,prev)); 41 | } 42 | 43 | public static int what(int[]a,int num) 44 | { 45 | int b[]=new int[a.length]; 46 | int size=0; 47 | for(int i=0;i= s.length || sum < 0) { 33 | return false; 34 | } 35 | boolean option1 = isSumOf(s, n, i+1, sum,str); 36 | boolean option2 = isSumOf(s, n, i, sum - s[i],str +s[i] +" "); 37 | return option1 || option2; 38 | } 39 | 40 | /** 41 | * לולאה ראשונה בודקת האם מספר כלשהו במערך שווה לאיקס. לאחר מכן במידה ונמצא את איקס נשבור את הלולאה 42 | * נעבור לאיף ראשון שיבדוק האם הגענו לסוף המערך. במידה וכן נחזיר שיש רק מספר אחד כזה. 43 | * נבצע שוב בדיקה לבדוק מי זה מיד , ולאחר מכן נרוץ בלולאה נוספת ונבדוק האם מצד ימין או צצד שמאל האם קיים אותו איקס שוב ושוב 44 | * במידה וכן, נסכום אותם ונחזיר אותם בסוף. 45 | * במידה ולא נשבור את הלולאה ונגיע לאיף האחרון שיבדוק האם אין מכל צדדיו שוב איקס , במידהו אין תחזיר -1 כי אין את איקס בכלל, אחרת תחזיר את כמות האיקס שסכמנו. 46 | * @param a 47 | * @param x 48 | * @return 49 | */ 50 | public static int count (int[] a, int x) { 51 | int low = 0; 52 | int high = a.length-1; 53 | int mid; 54 | int i = 1; // go right 55 | int j = 1; // go left 56 | while(low <= high) { 57 | mid = (low + high) /2; 58 | if(a[mid] == x) 59 | break; 60 | if (a[mid] < x) 61 | low = mid+1; 62 | else 63 | high = mid-1; 64 | } 65 | if(low == high) // if we reached the last index in the array. 66 | return 1; 67 | mid = (low+high) /2; 68 | while(a[mid+i] == x || a[mid-j] == x && mid+1 >= j) { 69 | if(a[mid-j] == x && j < mid+1) 70 | j++; 71 | if(a[mid+i] == x) 72 | i++; 73 | else break; 74 | } 75 | if (low > high || low == high && a[high] != x && a[low] != x && a[high-low] != x) // if the number didnt found return -1 76 | return -1; 77 | return (i + j)-1; 78 | 79 | } // public close 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /Year2012/July2012a96: -------------------------------------------------------------------------------- 1 | package Year2012; 2 | 3 | public class July2012a96 { 4 | 5 | public static void main(String[] args) 6 | { 7 | // int[][] m = {{8,4,2,4,3},{6,3,8,4,5},{1,4,9,9,7},{2,1,7,6,5}}; 8 | // System.out.println("Array:"); 9 | // print(m); 10 | // System.out.println("Results:"); 11 | // printPathWeights(m); 12 | int [] a = { 1,9,2,8,4,7,7,4,12 } ; 13 | // System.out.println("\narray before: "); 14 | // print1(a); 15 | // crossSort(a); 16 | // System.out.println("\narray after: "); 17 | // print1(a); 18 | // 19 | System.out.println(crossSearch(a, 9)); 20 | } 21 | 22 | public static void print(int[][] m) 23 | { 24 | for(int i = 0; i < m.length; i++) 25 | for(int j = 0; j < m[i].length; j++) 26 | { 27 | System.out.print(m[i][j]); 28 | if(j == m[i].length-1) 29 | System.out.print("\n"); 30 | else 31 | System.out.print(" "); 32 | } 33 | } 34 | 35 | public static void print1(int[] arr) 36 | { 37 | for(int i =0; i < arr.length; i++) 38 | System.out.print(arr[i]+" "); 39 | } 40 | 41 | public static void printPathWeights(int [][] m) { 42 | printPathWeights(m,0,0,0); 43 | } 44 | 45 | private static void printPathWeights(int [][] m,int i,int j,int sum) { 46 | if(i >= m.length || j >= m[0].length || i < 0 || j < 0 || m[i][j] == -1) 47 | return; 48 | if(i == m.length-1 && j == m[0].length-1) 49 | System.out.println(sum+m[i][j]); 50 | int k = m[i][j]; 51 | m[i][j] = -1; 52 | printPathWeights(m,i+1,j,sum+k); 53 | printPathWeights(m,i-1,j,sum+k); 54 | printPathWeights(m,i,j+1,sum+k); 55 | printPathWeights(m,i,j-1,sum+k); 56 | m[i][j] = k; 57 | } 58 | 59 | //{ 1,9,2,8,4,7,7,4,12 } 60 | public static void crossSort(int[] arr) { 61 | int[] temp = new int[arr.length]; 62 | int i = 0; 63 | int j; 64 | int x = 0; 65 | if(arr.length%2 == 0) 66 | j = arr.length-1; 67 | else 68 | j = arr.length-2; 69 | 70 | while(i < arr.length || j >= 0){ // exception 71 | if(i < arr.length && j >= 0 ){ 72 | if(arr[i] < arr[j]){ 73 | temp[x] = arr[i]; 74 | x++; 75 | i = i+2; 76 | }else{ 77 | temp[x] = arr[j]; 78 | x++; 79 | j = j-2; 80 | } 81 | }else if(i < arr.length){ 82 | temp[x] = arr[i]; 83 | x++; 84 | i = i+2; 85 | }else{ 86 | temp[x] = arr[j]; 87 | x++; 88 | j = j-2; 89 | } 90 | } 91 | for(i = 0; i < arr.length; i++) // switchs all the original arr to the temp arr values. 92 | { 93 | arr[i] = temp[i]; 94 | } 95 | } 96 | 97 | // 9 98 | public static int crossSearch(int [] arr, int x) { 99 | int high; 100 | if (arr.length%2 == 0) 101 | high = arr.length - 2; 102 | else 103 | high = arr.length - 1; 104 | high = high/2; // dividing by 2 as the final result would be the last even index in the array 105 | int low = 0; 106 | int mid = 0; 107 | while(low <= high) { 108 | mid = low + (high-low)/2; 109 | if(arr[mid*2] == x) 110 | return mid*2; 111 | if(arr[mid*2] < x) 112 | low = mid + 1; 113 | else 114 | high = mid - 1; 115 | } 116 | if (arr.length%2 == 0) 117 | low = arr.length - 2; 118 | else 119 | low = arr.length - 1; 120 | low = low/2; 121 | high = 0; 122 | 123 | while( low >= high) { // notice low >= high 124 | mid = low + (high-low)/2; 125 | if(arr[mid*2 + 1] == x) 126 | return mid*2 + 1; 127 | if(arr[mid*2 + 1] < x) 128 | low = mid - 1; 129 | else 130 | high = mid + 1; 131 | } 132 | return -1; 133 | 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /Year2012/June2012a81: -------------------------------------------------------------------------------- 1 | package Year2012; 2 | 3 | public class June2012a81 { 4 | public static void main(String[] args) 5 | { 6 | int [] a = {2,8,3,4,2,5}; 7 | int [] b = {2,4,6,2,3,4}; 8 | System.out.println(splitEqualSum(a)); 9 | System.out.println(splitEqualSum(b)); 10 | } 11 | public static boolean splitEqualSum(int [] a) { 12 | return splitEqualSum(a,0,0,0); 13 | } 14 | 15 | private static boolean splitEqualSum(int [] a,int i,int sum1,int sum2) { 16 | if (i == a.length) 17 | if (sum1 == sum2) 18 | return true; 19 | else 20 | return false; 21 | return splitEqualSum(a,i+1,sum1+a[i],sum2) || splitEqualSum(a,i+1,sum1,sum2+a[i]); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Year2012/May2012a92: -------------------------------------------------------------------------------- 1 | package Year2012; 2 | 3 | public class May2012a92 { 4 | 5 | public static void main(String[] args) 6 | { 7 | // int[] arr = {5, 3, 1, 4}; 8 | // mirror(arr); 9 | // int[] arr1 = {5, 3, 1, 4, 5}; 10 | // mirror(arr1); 11 | // 12 | // String str = "aaabbcccc"; 13 | // System.out.println(findBC(str)); 14 | String str1 = "aaacccccc"; 15 | System.out.println(firstB(str1)); 16 | // System.out.println(firstB(str)); 17 | } 18 | 19 | 20 | public static void mirror(int[] arr) { 21 | mirror(arr, 0, arr.length - 1,0); 22 | } 23 | 24 | private static void mirror(int[] arr, int start, int end,int index) { 25 | if (arr.length%2 == 1 || arr.length%2 == 2) { // if the array is not even , we cant mirror it. 26 | System.out.println("-1"); 27 | return; 28 | } 29 | if (start > end) { 30 | printArray(arr,index); 31 | return; 32 | } 33 | // swap the elements at the start and end index's 34 | int temp = arr[start]; 35 | arr[start] = arr[end]; 36 | arr[end] = temp; 37 | // recursively check the remaining elements 38 | mirror(arr, start + 1, end - 1,index); 39 | // restore the original array 40 | arr[end] = arr[start]; 41 | arr[start] = temp; 42 | // check the remaining elements without swapping the start and end index's 43 | mirror(arr, start + 1, end - 1,index); 44 | } 45 | 46 | private static void printArray(int[] arr, int index) { 47 | if (index == arr.length) { 48 | System.out.println(); 49 | return; 50 | } 51 | System.out.print(arr[index] + " "); 52 | printArray(arr, index + 1); 53 | } 54 | 55 | 56 | public static int findBC (String s) { 57 | int i = 0; 58 | while(s.charAt(i) == 'a') { 59 | i++; 60 | } 61 | return i; 62 | } 63 | 64 | public static int firstB (String s) { 65 | int mid; 66 | int low=0; 67 | int high=findBC(s); 68 | while(high > low+1) { 69 | mid = (low+high)/2; 70 | if ((s.charAt(mid) == 'b') || (s.charAt(mid) == 'c')) 71 | high = mid; 72 | else 73 | low = mid; 74 | } 75 | if ((high == low+1) && (s.charAt(low) == 'a') && (s.charAt(high) == 'b')) 76 | return high; 77 | return -1; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Year2013/August2013a92: -------------------------------------------------------------------------------- 1 | package Year2013; 2 | 3 | import java.util.Arrays; 4 | 5 | public class August2013a92 { 6 | 7 | public static void main(String[] args) { 8 | int [] arr = {1,1,1,1}; 9 | int [] arr3 = {2,2,2,2,2}; 10 | int arr2 [] = {4,5,6,5,4,3}; //i = 1 , j = 2 11 | int arr4 [] = {4,6,7,6,4,3}; 12 | int arr5 [] = {4,6,7,6,7,4,3}; 13 | // System.out.println(longestFlatSequence(arr)); 14 | // System.out.println(longestFlatSequence(arr3)); 15 | // System.out.println(longestFlatSequence(arr2)); 16 | // System.out.println(longestFlatSequence(arr4)); 17 | // System.out.println(longestFlatSequence(arr5)); 18 | int[] a = {35,17,13,252,4,128,7,3,81}; 19 | int[] expected = {81,252,13,3,4,35,17,7,128}; 20 | sortMod(a,10); 21 | System.out.println("expected: " + Arrays.toString(expected)); 22 | System.out.println("received: " + Arrays.toString(a)); 23 | } 24 | public static int longestFlatSequence(int[] arr) 25 | { 26 | return longestFlatSequence(arr, 0, 0); // Main method which uses the private method longestFlatSequence with loading. 27 | } 28 | 29 | private static int longestFlatSequence(int[] arr, int i, int max) 30 | { 31 | if (i >= arr.length) // if the Array is 0 Characters. so it will stop immediately. Handling an abnormal error. 32 | { 33 | return max; 34 | } 35 | int length = lengthFlat(arr, i); // Using the Helping Method. 36 | max = Math.max(max, length); // Perform placement by comparing the max parameter to the length parameter to find which one is bigger. 37 | return longestFlatSequence(arr, i + 1, max); 38 | } 39 | 40 | public static int lengthFlat(int[] arr, int i) 41 | { 42 | return lengthFlat(arr, i, arr[i], arr[i], 0); // Helping method which uses the lengthFlat method with loading. 43 | } 44 | 45 | private static int lengthFlat(int[] arr, int i, int arrayNum, int arrayNum2, int lengthOfArray) 46 | { 47 | if (i >= arr.length) //if the Array is 0 Characters. so it will stop immediately. Handling an abnormal error. 48 | { 49 | return lengthOfArray; 50 | } 51 | if (arr[i] == arrayNum || arr[i] == arrayNum2) // Checks if the index 0 position is different, and then starts running recursively on the whole array. 52 | { 53 | return lengthFlat(arr, i+1, arrayNum, arrayNum2, lengthOfArray+1); 54 | } 55 | if (arrayNum != arrayNum2) // Checks if the arrayNum is not equals to arrayNum2 56 | { 57 | return lengthOfArray; 58 | } 59 | if (Math.abs(arr[i] - arrayNum2) == 1) // Checks if the index in the array is exactly 1 higher or lower. 60 | { 61 | return lengthFlat(arr, i+1, arrayNum, arr[i], lengthOfArray+1); // if true , index+1 , and set arrayNum2 as arr[index]. 62 | } 63 | return lengthOfArray; 64 | } 65 | 66 | 67 | 68 | public static void sortMod(int a[],int k) { 69 | int jj = 0; 70 | for(int i=0; i mid) 23 | right = mid -1; 24 | if(a[mid] < mid) 25 | left = mid+1; 26 | } 27 | return -1; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Year2013/March2013a33: -------------------------------------------------------------------------------- 1 | package Year2013; 2 | 3 | public class March2013a33 { 4 | 5 | public static void main(String[] args) { 6 | int a [] = {2,3,57}; 7 | int aa [] = {5,39,67}; 8 | int aaa [] = {2,3,573,4,34,35}; 9 | int aaaa [] = {2,3,573,4,324,35}; 10 | int aaaaa [] = {2,3}; 11 | int z [] = {2,3,573,4,324,35}; 12 | int b [] = {1,0,2}; 13 | int nulll [] = {}; 14 | System.out.println(match(a, b)); 15 | System.out.println(match(aa, b)); 16 | System.out.println(match(aaa, b)); 17 | System.out.println(match(aaaa, nulll)); 18 | System.out.println(match(aaaaa, b)); 19 | System.out.println(match(z, b)); 20 | } 21 | public static boolean match(int[] a, int[] pattern) { 22 | return match(a, pattern, 0, 0, 0); 23 | } 24 | 25 | private static boolean match(int[] a, int[] pattern, int beginning, int i, int counter) { 26 | if (pattern.length == 0 || counter == pattern.length) 27 | return true; 28 | 29 | if (beginning + i > a.length - 1) 30 | return false; 31 | 32 | if (a[beginning + i] > 9 && a[beginning + i] < 100 && pattern[i] == 2 33 | || a[beginning + i] < 10 && pattern[i] == 1 34 | || a[beginning + i] < 100 && pattern[i] == 0) 35 | return match(a, pattern, beginning, i + 1, counter + 1); 36 | 37 | return match(a, pattern, beginning + 1, 0, 0); 38 | } 39 | 40 | 41 | // public int what (char start, char end) { 42 | 43 | //} // TO DO !!! 44 | } 45 | -------------------------------------------------------------------------------- /Year2014/February2014a85: -------------------------------------------------------------------------------- 1 | package Year2014; 2 | 3 | public class February2014a85 { 4 | public static void main(String[] args) 5 | { 6 | int [][] a = {{3,8,7,1}, 7 | {5,15,2,4}, 8 | {12,14,-13,22}, 9 | {13,16,17,52}}; 10 | 11 | // printPath(a); 12 | int [] b = {65,70,-5,3,48,49,52}; 13 | System.out.println(findMax(b)); 14 | } 15 | 16 | public static void printPath (int[][] mat) { 17 | printPath(mat,0,0,mat[0][0],mat[0][0],""); 18 | } 19 | 20 | private static void printPath (int[][] mat,int i,int j,int foundMax,int currentNum,String str) { 21 | if(i < 0 || j < 0 || i >= mat.length || j >= mat[0].length) 22 | return; 23 | if(i == mat.length-1 && j == mat[0].length-1) { 24 | int finish = 0; 25 | int finish2 = 0; 26 | if(j <= mat[0].length) { 27 | int a = Math.max(foundMax,mat[i][j]); 28 | int b = Math.max(foundMax,mat[i][j-1]); 29 | finish = Math.max(a, b); 30 | str += "(" + i + "," + j + ")" ; 31 | } 32 | if(i <= mat.length) { 33 | int c = Math.max(foundMax,mat[i][j]); 34 | int d = Math.max(foundMax,mat[i-1][j]); 35 | finish2 = Math.max(c, d); 36 | str += "(" + i + "," + j + ")" ; 37 | } 38 | int finish3 = Math.max(finish2, finish); 39 | if(finish3 > foundMax) { 40 | System.out.println(str + "" + finish3); 41 | }else { 42 | System.out.println(str + "" + foundMax); 43 | } 44 | } 45 | if(currentNum <= mat[i][j]) { 46 | printPath(mat,i+1,j,mat[i][j],mat[i][j],str + "(" + i + "," + j + ")"); 47 | printPath(mat,i,j+1,mat[i][j],mat[i][j],str + "(" + i + "," + j + ")"); 48 | printPath(mat,i-1,j,mat[i][j],mat[i][j],str + "(" + i + "," + j + ")"); 49 | printPath(mat,i,j-1,mat[i][j],mat[i][j],str + "(" + i + "," + j + ")"); 50 | }else { 51 | currentNum = foundMax; 52 | } 53 | } 54 | 55 | public static int findMax(int[] arr) { 56 | int low = 0; 57 | int high = arr.length-1; 58 | int mid; 59 | while(low <= high) { 60 | if(low == high) 61 | return low; 62 | mid = (low+high)/2; 63 | if(mid < high && arr[mid] > arr[mid + 1]) 64 | return mid; 65 | if(mid > low && arr[mid] < arr[mid - 1]) 66 | return mid-1; 67 | if(arr[mid] < arr[low]) 68 | high = mid-1; 69 | else 70 | low = mid+1; 71 | } 72 | return 0; 73 | } 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Year2015/2015a83: -------------------------------------------------------------------------------- 1 | package Year2015; 2 | 3 | public class a2015a83 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [][] aa = {{3,13,15,28,30}, 8 | {40,51,52,29,30}, 9 | {28,10,53,54,53}, 10 | {53,12,55,53,60}, 11 | {70,62,56,20,80}, 12 | {80,81,90,95,100} 13 | }; 14 | System.out.println(shortestPath(aa)); 15 | 16 | } 17 | 18 | 19 | public static int shortestPath(int[][] mat) 20 | { 21 | return shortestPath(mat,0,0,0); 22 | } 23 | 24 | private static int shortestPath(int [][]mat, int i, int j, int sum) 25 | { 26 | if (i < 0 || i >= mat.length || j < 0 || j >= mat[0].length || mat[i][j] < 0) // exception matrix 27 | return 0; 28 | 29 | if (i == mat.length-1 && j == mat[0].length-1) // finished the road 30 | { 31 | return sum+1; 32 | } 33 | 34 | int k = mat [i][j]; // who is k ? 35 | mat[i][j] = -k; // so we wont get to this place again 36 | 37 | int options = shortestPath(mat,i+1,j,sum+1)+ 38 | shortestPath(mat,i,j+1,sum+1)+ 39 | shortestPath(mat,i-1,j,sum+1)+ 40 | shortestPath(mat,i,j-1,sum+1); 41 | 42 | int way1 = shortestPath(mat,i+1,j,sum+k) + shortestPath(mat,i,j+1,sum+k); 43 | int way2 = shortestPath(mat,i-1,j,sum+k)+ shortestPath(mat,i,j-1,sum+k); 44 | mat [i][j] = k; // flip it back to k 45 | // System.out.println(sum); 46 | return Math.min(way1,way2); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Year2015/2015a85: -------------------------------------------------------------------------------- 1 | package Year2015; 2 | 3 | public class a2015a85 { 4 | 5 | public static void main(String[] args) 6 | { 7 | 8 | int [][] aaa = { 9 | {1, 0, 0, 10, 0, 0}, 10 | {0, 0, 8, 0, 9, 0}, 11 | {0, 6, 0, 0, 0, 7}, 12 | {3, 0, 4, 0, 0, 5}, 13 | {1, 2, 0, 0, 0, 2} 14 | }; 15 | 16 | System.out.println(countRopes(aaa)); 17 | } 18 | 19 | 20 | public static int countRopes(int [][] mat) 21 | { 22 | return countRopes(mat,0,0,0); 23 | } 24 | 25 | private static int countRopes(int mat[][],int i,int j,int sum) 26 | { 27 | if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length) // exception 28 | { 29 | return 0; 30 | } 31 | 32 | if (i == mat.length-1 && j == mat[0].length-1) // finished the road 33 | { 34 | return 1; 35 | } 36 | 37 | int k = mat[i][j]; 38 | mat[i][j] = -k; // so we wont get to this place again 39 | 40 | if (mat[i][j] < mat[i+1][j-1] || mat[i][j] < mat[i+1][j+1] || mat[i][j] < mat[i][j+1]) 41 | { 42 | int ropes = countRopes(mat,i+1,j+1,sum + 1)+ 43 | countRopes(mat,i+1,j-1,sum + 1)+ 44 | countRopes(mat,i,j+1,sum + 1); 45 | } 46 | 47 | mat[i][j] = k; 48 | 49 | return sum; 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Year2016/February2016a83: -------------------------------------------------------------------------------- 1 | package Year2016; 2 | 3 | public class Feburary2016a83 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [][] a = { 8 | {-2, -3, 3}, 9 | {-5, -10, 1}, 10 | {10, 30, -5} 11 | }; 12 | 13 | System.out.println(minPoints(a)); 14 | 15 | int [] b = {1,2,5,3,6,10,9 }; 16 | int [] c = {1}; // alone - false 17 | System.out.println(findX(b, 9)); // true 18 | System.out.println(findX(b, 19)); // true 19 | System.out.println(findX(b, 16)); // true 20 | System.out.println(findX(b, 8)); // true 21 | System.out.println(findX(b, 7)); // true 22 | System.out.println(findX(b, 3)); // true 23 | System.out.println(findX(b, 10)); // false 24 | System.out.println(findX(b, 20)); // false 25 | System.out.println(findX(b, 17)); // false 26 | System.out.println(findX(b, 6)); // false 27 | System.out.println(findX(b, 2)); // false 28 | } 29 | /* 30 | * 31 | * RECURSION 32 | */ 33 | /** 34 | * The method returns the minimum number that should be in the 0,0 position in the array in order to end the route with a positive number 35 | * @param m - the array 36 | * @return returns the minimum number that should be in the 0,0 position in the array in order to end the route with a positive number 37 | */ 38 | public static int minPoints(int [][] m) 39 | { 40 | return minPoints(m,0,0); 41 | } 42 | 43 | private static int minPoints(int [][] m, int i, int j) 44 | { 45 | if (i == m.length || j == m[0].length) // out of the road 46 | { 47 | return Integer.MAX_VALUE; 48 | } 49 | 50 | if (i == m.length-1 && j == m[0].length-1) // finished the road 51 | { 52 | if(m[i][j] <= 0) 53 | return -m[i][j] + 1; // stay positive and add 1. 54 | else 55 | return m[i][j]; 56 | } 57 | 58 | int right = minPoints(m,i,j+1); // go right 59 | int down = minPoints(m,i+1,j); // go down 60 | 61 | int options = Math.min(right,down) -m[i][j]; // get the minimum 62 | 63 | if(options <= 0) 64 | return 1; 65 | else 66 | return options; 67 | } 68 | 69 | 70 | /** 71 | * @param a - the array 72 | * @param x - The number we will need to reach through a sum of 2 adjacent index's in the array 73 | * Example - array = 1,2,5,3,6,10,9 , x = 9 so it will be x=3+6=9 | index places = index 4 + index 5. 74 | * @return 75 | */ 76 | public static boolean findX (int [] a , int x) 77 | { 78 | int mid = 0; 79 | int low = 0; 80 | int high = a.length-1; 81 | 82 | if (a.length <= 1) 83 | return false; 84 | while (low < high) 85 | { 86 | mid = (low + high)/2; // get the mid 87 | if (a[mid] + a[mid+1] == x) // if mid + next index is equals to x return true 88 | return true; 89 | else if (a[mid] + a[mid+1] < x) // if mid + next index smaller than x , low = mid+1 90 | low = mid+1; 91 | else high = mid; 92 | } 93 | return false; 94 | } 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /Year2016/June2016a82: -------------------------------------------------------------------------------- 1 | package Year2016; 2 | 3 | import java.awt.font.NumericShaper.Range; 4 | 5 | public class June2016a82 { 6 | 7 | public static void main(String[] args) 8 | { 9 | 10 | int [] a = {5, 6, 1, 2, 8}; 11 | System.out.println(where(a)); 12 | } 13 | 14 | /* 15 | * Recursion 16 | */ 17 | /** 18 | * 1 A . 19 | * 20 | *The method goes through the whole array and with the help of the sum checker method each cell will move if the sum of the organs before it to the sum of the organs after it. 21 | If not / we have reached the end of the array, the method will return -1. 22 | If so, the method will return the position of the number. 23 | * @param vec - the Array 24 | * @return P - the sum of the indexs in the array up to its location is equal to the sum of the following indexs in the array. 25 | */ 26 | public int where (int [] vec) 27 | { 28 | return where(vec,0,1,vec.length-1); 29 | } 30 | 31 | private int where (int [] vec, int left, int p, int right) 32 | { 33 | //right - the last index in the array. 34 | //left - first index in the array. 35 | // p is 1 because everytime the method starts over , it will go inside the second if and will subtract 1 again so it will be all the index's in the array. 36 | if (p > right) 37 | return -1; 38 | if (sum(vec,left,p-1) == sum(vec,p,right)) 39 | return p; 40 | return where (vec,left,p+1,right); 41 | } 42 | 43 | private int sum (int [] vec, int lo, int hi) 44 | { 45 | // returns the sum of low to high . 46 | // no need to write it ,at the test it was gaven built in. 47 | } 48 | 49 | /* 50 | * 1 B . 51 | */ 52 | 53 | /** 54 | * Checks if from the first horizontal and checks all the next sub arrays is also horizontal until we check K horizontals. 55 | * @param vec - the array. 56 | * @param k - horizontal times 57 | * @return true if all sub arrays are horizontal . 58 | */ 59 | public static boolean isBalanced (int [] vec, int k) 60 | { 61 | return isBalanced(vec,0,vec.length-1,k); 62 | } 63 | 64 | public static boolean isBalanced(int [] vec , int left, int right , int k) 65 | { 66 | if (k == 0) // if the array is empty 67 | return true; 68 | int p = where(vec,left,left+1,right); 69 | 70 | if (p == -1) // if the p is negative the Array is not horizontal. 71 | return false; 72 | 73 | return isBalanced(vec,left,p-1,k-1) && isBalanced(vec,p,right,k-1); 74 | } 75 | 76 | 77 | /* 78 | * Complexity 79 | */ 80 | /** The algorithm relies on the fact that between every 2 cells in the Range array there is such a regularity that any number can be reached between the connection of the maximum and the connection of the minimum. 81 | And so you can sum all the numbers between them. 82 | * @param a - the array 83 | * @param x - the number we need to get to. 84 | * @return true of false If possible connect two cells in an array and get the sum of the number as the number x 85 | */ 86 | public static boolean isSum (Range a[], int x) 87 | { 88 | int low=0; 89 | int high = a.length-1; 90 | 91 | while (low < high) 92 | { 93 | if (x > a[low].getLargest() + a[high].getLargest()) 94 | low++; 95 | else if (x < a[low].getSmallest() + a[high].getSmallest()) 96 | high--; 97 | else return true; 98 | } 99 | 100 | if (a[low].getLargest() == a[low].getSmallest()) 101 | return false; // duplicate numbers 102 | 103 | if (x < (a[low].getLargest() * 2)-1 && x > (a[low].getSmallest() * 2) -1) 104 | return true; // single cell 105 | return false; 106 | } 107 | 108 | 109 | 110 | 111 | 112 | 113 | } 114 | -------------------------------------------------------------------------------- /Year2017/August2017a87: -------------------------------------------------------------------------------- 1 | package Year2017; 2 | 3 | public class August2017a87 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [] a = {1,3,4,5,6}; 8 | // int [] b = {4,5,6,10,12}; 9 | System.out.println(isPythagorean(a)); 10 | // System.out.println(isPythagorean(b)); 11 | } 12 | 13 | 14 | /**copy , allZero, xZero is built in the test. 15 | * We will go over each row in the matrix and split into 2 cases. 16 | Take it or not take it. 17 | If we take it we will subtract the common numbers for the row and array until we get that the array is empty or full at the end of the process. 18 | * @param mat - the matrix. 19 | * @param arr - the array 20 | * @param k - the amount of rows we can use to find all the values in the arr from the mat. 21 | * @return 22 | */ 23 | public static boolean covers (int [] [] mat, int [] arr, int k) 24 | { 25 | int [] arr2 = new int [arr.length]; 26 | copy (arr2,arr); // copying the values from the original array 27 | return covers (mat,arr2,k,0); 28 | } 29 | 30 | private static boolean covers (int [] [] mat, int [] arr, int k, int row) 31 | { 32 | if (allZero(arr)) 33 | return true; // all the values has been found so its true. 34 | 35 | if (k == 0 || row == mat.length) 36 | return false; // if we used the maximum of rows or got out of the array and we didnt find all the values return false. 37 | 38 | boolean notUsed = covers(mat,arr,k,row+1); // what happens if we wont use the current row. 39 | 40 | int [] currRow = new int [mat[row].length]; 41 | copy (currRow,mat[row]); // copy the current row 42 | xZero(arr,currRow); // delete the values were already found in the current row. 43 | boolean used = covers(mat,arr,k-1,row+1); // what happens if we use the current row. 44 | return used || notUsed; // return what happend if we will use the row or will not use the row. 45 | } 46 | 47 | 48 | /** 49 | * Selected c, running on the array from end to beginning. 50 | Therefore a, b are necessarily smaller than it. 51 | If a ^ 2 + b ^ 2 are equal to c ^ 2, we get a truth. 52 | If a ^ 2 + b ^ 2 is less than c ^ 2, e a will move one step to the right. 53 | If a ^ 2 + b ^ 2 is greater than c ^ 2, the b will advance one step to the left. 54 | If no solution is found for the array, the answer will return False. 55 | * @param arr - the array 56 | * @return true or false 57 | */ 58 | public static boolean isPythagorean (int [] arr) 59 | { 60 | for(int c=arr.length-1; c > 1; c--) // no need to run more than 2 places. 61 | { 62 | int a = 0; // the index of a 63 | int b = c-1; // the index of b 64 | while (b > a) // b is bigger than a 65 | { 66 | int aPow = arr[a] * arr[a]; // Pow on a 67 | int bPow = arr[b] * arr[b]; // Pow on b 68 | int cPow = arr[c] * arr[c]; // Pow on c 69 | int sum = aPow + bPow; // sum = a^2 + b^2 70 | if (sum == cPow) // if there is a^2 + b^2 = c^2 it is true. 71 | return true; 72 | if (sum < cPow) // if a^2 + b^2 < c^2 , go 1 step right. 73 | a++; 74 | else // if a^2 + b^2 > c^2 , go 1 step left. 75 | b--; 76 | } // close of while 77 | } // close of for 78 | return false; 79 | } // close of main 80 | } 81 | -------------------------------------------------------------------------------- /Year2017/August2017a93: -------------------------------------------------------------------------------- 1 | package Year2017; 2 | 3 | public class August2017a93 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int aa [] = {3,4,5,12,19,20,21,100,101,102,103,104}; 8 | 9 | } 10 | 11 | public static int cntTrueReg(boolean[][] mat) { 12 | return cntTrueReg(mat, 0, 0, mat.length); // square matrix so size of nxn 13 | } 14 | 15 | private static int cntTrueReg(boolean[][] mat, int i, int j, int maxIndex) { 16 | // checking bounds and iterating through the matrix 17 | if(j >= maxIndex && i < maxIndex) { 18 | return cntTrueReg(mat, i + 1, 0, mat.length); 19 | }else if(j >= maxIndex && i >= maxIndex) 20 | return 0; 21 | 22 | // if false, continue , else change the region to false and count 1 and repeat until we reach the end of the matrix 23 | if(!mat[i][j]) { 24 | return cntTrueReg(mat, i, j + 1, mat.length); 25 | }else { 26 | changeArray(mat, i, j, maxIndex); 27 | return 1 + cntTrueReg(mat, i, j + 1, mat.length); 28 | } 29 | 30 | } 31 | 32 | private static void changeArray(boolean[][] arr, int i, int j, int maxIndex) { // trace the true path and change it 33 | if(j >= maxIndex || i >= maxIndex || i < 0 || j < 0) 34 | return; 35 | 36 | if(arr[i][j]) { 37 | 38 | arr[i][j] = false; 39 | 40 | if(j < maxIndex - 1 && arr[i][j + 1]) 41 | changeArray(arr, i, j + 1, maxIndex); 42 | if(i < maxIndex - 1 && arr[i + 1][j]) 43 | changeArray(arr, i + 1, j, maxIndex); 44 | if(j >= 1 && arr[i][j - 1]) 45 | changeArray(arr, i, j - 1, maxIndex); 46 | if(i >= 1 && arr[i - 1][j]) 47 | changeArray(arr, i - 1, j, maxIndex); 48 | 49 | } 50 | } 51 | 52 | 53 | public class Range 54 | { 55 | private int _center, _radius; 56 | public Range (int c, int r) 57 | { 58 | _center = c; 59 | _radius = r; 60 | } 61 | public int getCenter() { 62 | return _center; 63 | } 64 | public int getRadius() { 65 | return _radius; 66 | } 67 | } 68 | 69 | public static int isInRange (Range[] a, int num) { 70 | int left=0; 71 | int right=a.length-1; 72 | int mid; 73 | int leftBound; 74 | int rightBound; 75 | while (left <= right) { 76 | mid = (left+right)/2; 77 | leftBound = a[mid].getCenter() - a[mid].getRadius(); 78 | rightBound = a[mid].getCenter() + a[mid].getRadius(); 79 | if (num >=leftBound && num<=rightBound) 80 | return mid; 81 | if (num < leftBound) 82 | right = mid-1; 83 | else 84 | left = mid+1; 85 | } 86 | return -1; 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /Year2017/March2017a86: -------------------------------------------------------------------------------- 1 | package Year2017; 2 | 3 | public class March2017a86 { 4 | 5 | public static void main(String[] args) 6 | { 7 | // String str1 = "moriel"; 8 | // String str2 = "morie"; 9 | // System.out.println(edit(str1, str2)); 10 | 11 | int [] arr = { -2 , 0 , 1 , 3 }; 12 | System.out.println(countTriplets(arr, 2)); 13 | // int [] arr1 = { 1, 3, 4, 5, 7 }; 14 | // System.out.println(countTriplets(arr1, 12)); 15 | } 16 | 17 | 18 | public static int edit (String str1, String str2) 19 | { 20 | return edit(str1,str2,0); 21 | } 22 | 23 | private static int edit (String str1, String str2, int count) 24 | { 25 | if(str1 == null || str2 == null) 26 | return 0; 27 | if (str1.length() == 0 & str2.length() == 0) // two empty strings 28 | return count; 29 | if (str1.length() == 0) // if str1 is empty first , return the str2 length left 30 | return (count + str2.length()); 31 | if (str2.length() == 0) // if str2 is empty first , return the str1 length left 32 | return (count + str1.length()); 33 | if (str1.charAt(0) == str2.charAt(0)) // main Recursion which is cutting the char's when they are the same . 34 | return edit(str1.substring(1),str2.substring(1),count); 35 | return Math.min(edit(str1.substring(1),str2,count+1),edit(str1,str2.substring(1),count+1)); 36 | } 37 | 38 | 39 | public static int countTriplets(int[] arr, int num) { 40 | // the array is sorted in ascending order ! 41 | int arrLength = arr.length; 42 | int counter = 0; 43 | for(int i = 0; i < arrLength - 2 ; ++i) { 44 | 45 | int leftIndex = i + 1; 46 | int rightIndex = arrLength - 1; 47 | 48 | while(leftIndex < rightIndex) { 49 | // if the interval meets the requirement , add all triplet solutions in that interval and increase the left pointer and repeat the proccess 50 | if(arr[i] + arr[leftIndex] + arr[rightIndex] < num) { 51 | counter += rightIndex - leftIndex; 52 | leftIndex++; 53 | }else 54 | rightIndex--; // in case the sum is not less than num , we decrease the right pointer therefore the value in that index will be lower 55 | } 56 | } 57 | return counter; 58 | 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Year2018/2018a87: -------------------------------------------------------------------------------- 1 | package Year2018; 2 | 3 | public class None2018a87 { 4 | // {1,1,1} , {1,1,2} 5 | public static void main(String[] args) 6 | { 7 | System.out.println(howManySorted(3, 2)); 8 | } 9 | 10 | public static int howManySorted(int n, int max) 11 | { 12 | return howManySorted(n, 1, max,","); 13 | } 14 | 15 | private static int howManySorted(int n, int min, int max, String str) 16 | { 17 | if (n == 1) { 18 | if (min == max) { 19 | System.out.println("{" + min + str + "}"); 20 | return 1; 21 | } 22 | else { 23 | System.out.println("{" + min + str + "}"); 24 | System.out.println("{" + str + max + "}"); 25 | return 2; 26 | } 27 | } 28 | 29 | if (min == max) { 30 | System.out.println("{" + min + str + min + "}"); 31 | return 1; 32 | } 33 | 34 | 35 | int two = howManySorted(n-1, min, max-1, "," + min + str); 36 | int one = howManySorted(n-1, min, max, "," + max + str); 37 | return two + one; 38 | 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Year2018/February2018a85/February2018a85.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2018/February2018a85/February2018a85.pdf -------------------------------------------------------------------------------- /Year2018/February2018a85/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2018; 2 | 3 | public class Feburary2018a85 { 4 | 5 | public static void main(String[] args) 6 | { 7 | 8 | int [][] aaa = { 9 | {3, 13, 15, 28, 30}, 10 | {55, 54, 53, 27, 26}, 11 | {54, 12, 52, 51, 50}, 12 | {50, 10, 8, 53, 11} 13 | }; 14 | 15 | int [] a = {2,2,2,3,3,2,2,2,2,2}; 16 | // System.out.println(what(a)); 17 | System.out.println(longestSlope(aaa,1)); 18 | } 19 | 20 | public static int longestSlope (int [][] mat, int num) 21 | { 22 | return longestSlope (mat,num,0,0,1); 23 | } 24 | 25 | private static int longestSlope (int [][] mat,int num, int i, int j, int max) 26 | { 27 | if (j == mat[0].length) { 28 | return longestSlope(mat,num,i+1,0,max); 29 | }else if(i == mat.length) { 30 | return max; 31 | }else { 32 | int newPath = findPath(mat,num,i,j,mat[i][j]+num); 33 | return longestSlope(mat,num,i,j+1,Math.max(max, newPath)); 34 | } 35 | } 36 | private static int findPath (int [][] mat,int num, int i, int j, int prev) 37 | { 38 | if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] == -1) 39 | return 0; 40 | 41 | if(prev - mat[i][j] != num) 42 | return 0; 43 | 44 | int up = findPath(mat,num,i-1,j,mat[i][j])+1; 45 | int down = findPath(mat,num,i+1,j,mat[i][j])+1; 46 | int right = findPath(mat,num,i,j+1,mat[i][j])+1; 47 | int left = findPath(mat,num,i,j-1,mat[i][j])+1; 48 | int max1 = Math.max(right, left); 49 | int max2 = Math.max(up,down); 50 | return Math.max(max1, max2); 51 | } 52 | 53 | 54 | public static int what(int [] a) { 55 | int low = 0; 56 | int high = a.length-1; 57 | int len = a.length; 58 | int sum = 0; 59 | 60 | for(int i=0; i< high; i++) { 61 | sum+=a[i]; 62 | } 63 | if(sum%2 == 1) { 64 | return len; 65 | } 66 | len--; 67 | while(low <= high) { 68 | if(sum%2 == 1) { 69 | return len; 70 | } 71 | if(a[low]%2 == 1 || a[high]%2 == 1) { 72 | return len; 73 | } 74 | low++; 75 | high--; 76 | len--; 77 | } 78 | return 0; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Year2018/June2018b83/June2018b83.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2018/June2018b83/June2018b83.pdf -------------------------------------------------------------------------------- /Year2018/June2018b83/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2018; 2 | 3 | public class June2018b83 { 4 | 5 | public static void main(String[] args) 6 | { 7 | 8 | // int [][] aaa = { 9 | // {2, 0, 1, 2, 3}, 10 | // {2, 3, 5, 5, 4}, 11 | // {8, -1, 6, 8, 7}, 12 | // {3, 4, 7, 2, 4}, 13 | // {2, 4, 3, 1, 2} 14 | // 15 | // }; 16 | // 17 | // System.out.println(prince(aaa, 0, 0)); 18 | 19 | int b [] = {6,6,18,18,-4,-4,12,9,9}; 20 | int bb[] = {5}; 21 | int cc [] = {8,8,-7,-7,3,3,0,0,10,10,5,5,4}; 22 | // System.out.println(findSingle(b)); 23 | // System.out.println(findSingle(bb)); 24 | System.out.println(findSingle(cc)); 25 | } 26 | 27 | public static int prince(int [] []drm,int i, int j) { 28 | int total = prince(drm,i,j,drm[i][j],0); 29 | if(total == Integer.MAX_VALUE) 30 | return -1; 31 | return total; 32 | 33 | } 34 | 35 | private static int prince(int [][] drm,int i, int j,int prev,int sum) { 36 | if (i < 0 || j < 0 || i >= drm.length || j >= drm[0].length) 37 | return Integer.MAX_VALUE; 38 | 39 | 40 | if(drm[i][j] == -1) 41 | return sum+1; 42 | 43 | if(drm[i][j] == Integer.MAX_VALUE) 44 | return Integer.MAX_VALUE; 45 | 46 | prev = drm[i][j]; 47 | drm[i][j] = Integer.MAX_VALUE; 48 | 49 | int down = prince(drm, i+1,j,prev,sum+1); 50 | int up =prince(drm, i-1,j,prev,sum+1); 51 | int left = prince(drm, i,j-1,prev,sum+1); 52 | int right =prince(drm, i,j+1,prev,sum+1); 53 | 54 | drm[i][j] = prev; 55 | 56 | int max1 =Math.min(left, right); 57 | int max2 = Math.min(down,up); 58 | 59 | return Math.min(max1, max2); 60 | 61 | } 62 | 63 | 64 | public static int findSingle(int [] a) { 65 | if (a.length == 1) 66 | return a[0]; 67 | int low=0; 68 | int high=a.length-1; 69 | int mid; 70 | while(low < high) { 71 | mid = (low + high)/2; 72 | if(a[mid] == a[mid+1]){ 73 | if((mid-low)%2 == 0) { 74 | low=mid; 75 | }else{ 76 | high = mid-1; 77 | } 78 | }else if(a[mid] == a[mid-1]){ 79 | if((high-mid)%2 == 0){ 80 | high=mid; 81 | }else{ 82 | low=mid+1; 83 | } 84 | }else{ 85 | return a[mid]; 86 | } 87 | } 88 | return a[low]; 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /Year2018/March2018a91: -------------------------------------------------------------------------------- 1 | package Year2018; 2 | 3 | import java.util.Arrays; 4 | 5 | public class March2018a91 { 6 | 7 | public static void main(String[] args) 8 | { 9 | int [] b = {2,8,3,4,7,1,3,2}; 10 | System.out.println(cheapestRoute(b)); 11 | int[] a = {35,17,13,252,4,128,7,3,81}; 12 | int[] expected = {81,252,13,3,4,35,17,7,128}; 13 | sortmod(a,10); 14 | System.out.println("expected: " + Arrays.toString(expected)); 15 | System.out.println("received: " + Arrays.toString(a)); 16 | } 17 | 18 | 19 | 20 | public static int cheapestRoute(int [] stations) { 21 | return cheapestRoute (stations,0, 0); 22 | } 23 | 24 | private static int cheapestRoute(int [] stations,int i, int sum) { 25 | 26 | if(stations.length == 1) 27 | return stations[0]; 28 | if(i == stations.length-1) 29 | return sum + stations[i]; 30 | if(i == stations.length-2) 31 | return sum + stations[i] + stations[i+1]; 32 | int firstWay = cheapestRoute(stations,i+1,sum+stations[i]); 33 | int secondWay = cheapestRoute(stations,i+2,sum+stations[i]); 34 | return Math.min(firstWay, secondWay); 35 | } 36 | 37 | public static void sortmod(int[] a, int k){ 38 | int index = 0; 39 | int temp; 40 | for(int i = 0; i < k; i++){ 41 | for (int j = index; j < a.length; j++) { 42 | if(a[j] % k == i){ 43 | temp = a[index]; 44 | a[index] = a[j]; 45 | a[j] = temp; 46 | index++; 47 | } 48 | } 49 | } 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Year2019/2018a84/Poly/A.java: -------------------------------------------------------------------------------- 1 | package Poly3; 2 | 3 | public class A { 4 | 5 | public A() { 6 | System.out.println("A"); 7 | } 8 | 9 | public void arik() { 10 | System.out.println("Arik_A"); 11 | } 12 | 13 | public void yosef() { 14 | arik(); 15 | } 16 | 17 | 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Year2019/2018a84/Poly/B.java: -------------------------------------------------------------------------------- 1 | package Poly3; 2 | 3 | public class B extends A{ 4 | 5 | public B() { 6 | System.out.println("B"); 7 | } 8 | 9 | public void arik() { 10 | System.out.println("Arik_B"); 11 | } 12 | 13 | public void yosef() { 14 | System.out.println("Yosef"); 15 | } 16 | 17 | public void superYosef() { 18 | super.yosef(); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Year2019/2018a84/Poly/Driver.java: -------------------------------------------------------------------------------- 1 | package Poly3; 2 | 3 | public class Driver { 4 | 5 | public static void main(String [] args) { 6 | System.out.println("Question 1:"); 7 | A a = (A) new B(); 8 | // System.out.println(a); 9 | 10 | System.out.println("Question 2:"); 11 | A aa = new A(); 12 | // System.out.println(aa); 13 | 14 | System.out.println("Question 3:"); 15 | A ab = new B(); 16 | // System.out.println(ab); 17 | 18 | System.out.println("Question 4:"); 19 | // B ba = new A(); 20 | // System.out.println("Comp Error"); 21 | 22 | System.out.println("Question 5:"); 23 | B bb = new B(); 24 | System.out.println(bb); 25 | 26 | // System.out.println("Question 6:"); 27 | // aa.yosef(); 28 | // 29 | // System.out.println("Question 7:"); 30 | // ab.yosef(); 31 | // 32 | // System.out.println("Question 8:"); 33 | // bb.yosef(); 34 | // 35 | // System.out.println("Question 9:"); 36 | // ((A)aa).yosef(); 37 | 38 | System.out.println("Question 10:"); 39 | ((A)bb).yosef(); 40 | 41 | System.out.println("Question 11:"); 42 | // ((A)bb).superYosef(); 43 | System.out.println("Comp Error"); 44 | 45 | System.out.println("Question 12:"); 46 | System.out.println("Runtime Error"); 47 | // ((B)aa).yosef(); 48 | 49 | System.out.println("Question 13:"); 50 | System.out.println("Runtime Error"); 51 | // ((B)aa).superYosef(); 52 | 53 | System.out.println("Question 14:"); 54 | ((B)ab).superYosef(); 55 | 56 | System.out.println("Question 15:"); 57 | ((B)bb).superYosef(); 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Year2019/2018a84/RecursionComplexity/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2019; 2 | 3 | public class Feburary2019a84 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [] a = {5, 7, -2, 10 }; 8 | System.out.println(sumPower3(67)); 9 | System.out.println(sumPower3(128)); 10 | System.out.println(sumPower3(37)); 11 | System.out.println(sumPower3(38)); 12 | // System.out.println(average(a)); 13 | } 14 | 15 | 16 | /**In this method we will average the differences between the organs from a particular index from beginning to end, 17 | * we divide the array into two sides, rightsum and leftsum. 18 | In fact, 19 | we calculate the difference between the right side of the array from the particular index and also 20 | from the left side of the array from the particular index. 21 | Then, perform a calculation between all the differences we received for all the indexes and 22 | return the difference between the averages 23 | * @param arr - the array. 24 | * @return - the average between. 25 | */ 26 | public static int average (int [] arr) 27 | { 28 | 29 | if(arr.length == 1) 30 | return arr[0]; 31 | if(arr.length == 0) 32 | return 0; 33 | 34 | int start = 0; 35 | int end =arr.length - 1; 36 | int divider = 0; // the "divider" that moves along the array 37 | 38 | double maxDif = 0; // don't need to set as Integer.MIN_VALUE since we use the absolute difference so 0 will do as a dummy 39 | double leftSum = arr[0]; 40 | double rightSum = 0; 41 | 42 | for(int i = 1; i <= end ; ++i) { 43 | rightSum += arr[i]; 44 | } 45 | 46 | for(int i = 0 ; i < arr.length -1 ; ++i) { 47 | //get the temporary absolute difference 48 | double tempDif = leftSum/(start + 1) - rightSum/end; 49 | tempDif = (tempDif < 0) ? -tempDif : tempDif ; 50 | 51 | if(tempDif > maxDif) { 52 | maxDif = tempDif; 53 | divider = i; 54 | } 55 | 56 | // here we move the divider to the right by 1 spot so we increase and decrease values as we make the left window bigger and the right one smaller 57 | leftSum += arr[i+1]; 58 | start++; // for the average 59 | rightSum-= arr[i+1]; 60 | end--; // for the average 61 | 62 | } 63 | return divider; 64 | } 65 | 66 | 67 | 68 | /** 2019a84 February - RECURSION . 69 | * In this method we get some number, and the method should check if the number can make a strong third. 70 | For example: 3 * 0 + 3 * 1 + 3 * 2 = number 71 | If so, truth must be returned. 72 | If not, a lie must be returned. 73 | The method performs a modular arithmetic operation on the number and checks if it has a remainder. 74 | If its remainder is 2, we will automatically get a lie. 75 | If the remainder is 1, we will continue with the method and perform another mathematical operation which 76 | is the number less the remainder divided by 3, 77 | and so we will divide non-stop until we reach 0 and thus we get a truth 78 | * @param num - the number. 79 | * @return - True or False. 80 | */ 81 | public static boolean sumPower3 (int num) 82 | { 83 | int rest = num%3; // leaves the rest of the number. 84 | if(rest == 2 || rest == 0) // the solution will be 1 || 2 || 3 . if it is 0,2 , it cannot be used by Pow. 85 | return false; 86 | if(rest == 1) 87 | return true; 88 | return sumPower3(num); 89 | } // main close 90 | 91 | 92 | 93 | 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /Year2019/2019b83/ReucrsionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2019; 2 | 3 | public class June2019b83 { 4 | public static void main(String[] args) 5 | { 6 | int mat [] [] = {{1,1,1,1,1,1,1}, 7 | {1,1,0,1,0,0,1}, 8 | {1,1,1,1,0,1,1}}; 9 | int mat1 [] [] = {{-99,-72,-64,-55,-28,-10,-5}, 10 | {-72,-53,-46,-38,11,13,22}, 11 | {-63,-48,-27,-12,14,16,23}, 12 | {-44,-29,-10,0,18,20,28}, 13 | {0,12,14,20,28,30,35}}; 14 | System.out.println(howManyNegativeNumbers(mat1)); 15 | // System.out.println(longestPath(mat, 2, 5)); 16 | 17 | } 18 | 19 | public static int longestPath(int mat [][], int x, int y) { 20 | return longestPath(mat,0,0,0,x,y); 21 | } 22 | 23 | private static int longestPath(int mat [][],int i,int j, int sum,int x,int y) { 24 | 25 | if(i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] == 0) 26 | return sum; 27 | 28 | int z = mat[i][j]; 29 | mat[i][j] = 0; 30 | 31 | int right = longestPath(mat,i,j+1,sum+z,x,y); 32 | int down = longestPath(mat,i+1,j,sum+z,x,y); 33 | int left = longestPath(mat,i,j-1,sum+z,x,y); 34 | int up = longestPath(mat,i-1,j,sum+z,x,y); 35 | mat[i][j] = z; 36 | 37 | int max1 = Math.max(right, left); 38 | int max2 = Math.max(up, down); 39 | return Math.max(max1, max2); 40 | 41 | } 42 | 43 | public static int howManyNegativeNumbers(int [][] arr) { 44 | int sum = 0; 45 | int m = arr.length; 46 | int n = arr[0].length; 47 | for (int i = 0; i < m; i++) { 48 | for (int j = 0; j < n; j++) { 49 | if (arr[i][j] < 0) { 50 | sum++; 51 | } 52 | } 53 | } 54 | return sum; 55 | } 56 | } 57 | 58 | 59 | -------------------------------------------------------------------------------- /Year2019/2019b86/ComplexityRecursion.java: -------------------------------------------------------------------------------- 1 | package Year2019; 2 | 3 | public class July2019b86 { 4 | 5 | public static void main(String[] args) 6 | { 7 | 8 | // int [][] aaa = { 9 | // {1, 3, 1, 6}, 10 | // {2, 8, 1, 2}, 11 | // {6, 2, 7, 5}, 12 | // {2, 4, 1, 3} 13 | // }; 14 | // 15 | // System.out.println(howManyPaths(aaa)); 16 | 17 | int [] a = {19,19,16,15,15,15,15,13,5}; 18 | int [] b = {0,12,13,14,14,15,15,19,25,30,35}; 19 | System.out.println(mettingPoint(a, b)); 20 | } 21 | 22 | public static int mettingPoint (int[] a, int[] b) 23 | { 24 | int high = Math.max(a.length-1, b.length-1); 25 | if (high == -1) return -1; 26 | int low = 0, smallest = Integer.MAX_VALUE; // if it will stay smallest return -1. 27 | int mid; 28 | 29 | while (low <= high) 30 | { 31 | mid = (low + high)/2; 32 | if (a[mid] == b[mid]) 33 | { 34 | if (mid < smallest) 35 | { 36 | smallest = mid; 37 | } 38 | high = mid-1; 39 | } 40 | else if (a[mid] > b[mid]) 41 | low = mid+1; 42 | else if (a[mid] < b[mid]) 43 | high = mid-1; 44 | 45 | if (smallest == Integer.MAX_VALUE) 46 | return -1; 47 | } 48 | return smallest; 49 | } 50 | 51 | 52 | 53 | /** In this method we will look for all the routes that can be followed from the beginning of the array to the end of the array. 54 | * @param mat - the array. 55 | * @return options - amount of routes to the end of the matrix. 56 | */ 57 | public static int howManyPaths(int[][] mat) 58 | { 59 | return howManyPaths(mat,0,0); 60 | } 61 | 62 | private static int howManyPaths(int [][]mat, int i, int j) 63 | { 64 | if (i < 0 || i >= mat.length || j < 0 || j >= mat[0].length || mat[i][j] < 0) // exception matrix 65 | return 0; 66 | 67 | if (i == mat.length-1 && j == mat[0].length-1) // finished the road 68 | { 69 | return 1; 70 | } 71 | 72 | int k = mat [i][j]; // who is k ? 73 | mat[i][j] = -k; // so we wont get to this place again 74 | 75 | int options = howManyPaths(mat,i+k,j)+ 76 | howManyPaths(mat,i-k,j)+ 77 | howManyPaths(mat,i,j+k)+ 78 | howManyPaths(mat,i,j-k); 79 | mat [i][j] = k; // flip it back to k 80 | return options; 81 | 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Year2019/2019b86/July2019b86.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2019/2019b86/July2019b86.pdf -------------------------------------------------------------------------------- /Year2019/2019b93/August2019b93.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2019/2019b93/August2019b93.pdf -------------------------------------------------------------------------------- /Year2019/2019b93/Polymorphism/A.java: -------------------------------------------------------------------------------- 1 | package Poly2019b93; 2 | 3 | public class A extends B{ 4 | public void aaa() { 5 | System.out.println("A - aaa()"); 6 | super.aaa(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Year2019/2019b93/Polymorphism/B.java: -------------------------------------------------------------------------------- 1 | package Poly2019b93; 2 | 3 | public class B { 4 | public void aaa() { 5 | System.out.println("B - aaa()"); 6 | ccc(); 7 | } 8 | public void ccc() { 9 | System.out.println("B - ccc()"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Year2019/2019b93/Polymorphism/C.java: -------------------------------------------------------------------------------- 1 | package Poly2019b93; 2 | 3 | public class C extends A{ 4 | 5 | public void bbb() { 6 | System.out.println("C - bbb()"); 7 | super.ccc(); 8 | } 9 | public void ccc() { 10 | System.out.println("C - ccc()"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Year2019/2019b93/Polymorphism/E.java: -------------------------------------------------------------------------------- 1 | package Poly2019b93; 2 | 3 | public class E extends C{ 4 | 5 | public void bbb() { 6 | aaa(); 7 | System.out.println("E - bbb()"); 8 | } 9 | public void ccc() { 10 | System.out.println("E - ccc()"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Year2019/2019b93/Polymorphism/main.java: -------------------------------------------------------------------------------- 1 | package Poly2019b93; 2 | 3 | public class main { 4 | 5 | public static void main(String[] args) 6 | { 7 | 8 | B b1 = new A(); 9 | B b2 = new C(); 10 | A a1 = (C) b2; 11 | // D d1 = new C(); 12 | B b3 = new B(); 13 | // D d2 = new A(); 14 | // D d3 = new B(); 15 | E e1 = new E(); 16 | // D e2 = new E(); 17 | B e3 = new E(); 18 | C c1 = new C(); 19 | 20 | B var1 = new A(); 21 | C var2 = new C(); 22 | B var3 = new E(); 23 | B var4 = new C(); 24 | Object var5 = new A(); 25 | 26 | System.out.println("1:"); 27 | var1.aaa(); 28 | System.out.println("2:"); 29 | var1.ccc(); 30 | System.out.println("3:"); 31 | var2.aaa(); 32 | System.out.println("4:"); 33 | var2.bbb(); 34 | System.out.println("5:"); 35 | var3.aaa(); 36 | System.out.println("6:"); 37 | System.out.println("Compilation Error"); 38 | // var3.bbb(); 39 | System.out.println("7:"); 40 | var4.aaa(); 41 | System.out.println("8:"); 42 | System.out.println("Compilation Error"); 43 | // var5.aaa(); 44 | System.out.println("9:"); 45 | System.out.println("Runtime Error."); 46 | // ((C)var5).aaa(); 47 | System.out.println("10:"); 48 | ((E)var3).bbb(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Year2019/2019b93/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2019; 2 | 3 | public class August2019a93 { 4 | public static void main (String [] args) 5 | { 6 | int a[] = {5,4,2,1,3}; 7 | // System.out.println(isSum(a, 0)); 8 | // System.out.println(isSum(a, 8)); 9 | // System.out.println(isSum(a, 9)); 10 | // System.out.println(isSum(a, 2)); 11 | // System.out.println(isSum(a, 11)); // Still has bug here. 12 | // System.out.println(isSum(a, 17)); 13 | int road1[] = {5,4,5,8,12,9,9,3}; 14 | int road2[] = {7,3,3,12,10,2,10,7}; 15 | System.out.println(shortestRoad(road1, road2)); 16 | } 17 | 18 | public static boolean isSum (int[] a, int num) { 19 | return isSum(a, num, 0, 0, 0); 20 | } 21 | 22 | private static boolean isSum (int[] a, int num, int i, int sum, int count) { 23 | 24 | if (count <= 3 && sum == num || num == 0) 25 | return true; 26 | if(count > 3 ) 27 | return false; 28 | if(sum > num && i == a.length || i == a.length) 29 | return false; 30 | if(a[i] == num) 31 | return true; 32 | if(i == a.length-1 && count < 3) { 33 | sum+= a[i]; 34 | count+=1; 35 | } 36 | return isSum(a, num, i+1, sum+a[i], count+1) || isSum(a, num, i+2, sum+a[i], count+1) ; 37 | } 38 | 39 | public static int shortestRoad (int [] road1, int [] road2) { 40 | int i=0; 41 | int sum = 0; 42 | while(i < road1.length-2 || i < road2.length-2) { 43 | if(road1[i] + road1[i+1] + road1[i+2] < road2[i] + road2[i+1] + road2[i+2]) { 44 | sum +=road1[i] + road1[i+1] + road1[i+2]; 45 | i = i+3; 46 | }else { 47 | sum+=road2[i]+road2[i+1]+road2[i+2]; 48 | i=i+3; 49 | } 50 | } 51 | if(road1[i]+road1[i+1] < road2[i]+road2[i+1]) { 52 | sum+=road1[i]+road1[i+1]; 53 | }else { 54 | sum+=road2[i]+road2[i+1]; 55 | } 56 | return sum; 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Year2019/August2019a83: -------------------------------------------------------------------------------- 1 | package Year2019; 2 | 3 | public class August2019a93 { 4 | public static void main (String [] args) 5 | { 6 | int a[] = {5,4,2,1,3}; 7 | // System.out.println(isSum(a, 0)); 8 | // System.out.println(isSum(a, 8)); 9 | // System.out.println(isSum(a, 9)); 10 | // System.out.println(isSum(a, 2)); 11 | // System.out.println(isSum(a, 11)); 12 | // System.out.println(isSum(a, 17)); 13 | int road1[] = {5,4,5,8,12,9,9,3}; 14 | int road2[] = {7,3,3,12,10,2,10,7}; 15 | System.out.println(shortestRoad(road1, road2)); 16 | } 17 | 18 | public static boolean isSum (int[] a, int num) { 19 | return isSum(a, num, 0, 0); 20 | } 21 | 22 | private static boolean isSum (int[] a, int num, int i,int count) { 23 | if (count == 3) 24 | return false; 25 | if(num == 0) 26 | return true; 27 | if(i > a.length-1 || num < 0) 28 | return false; 29 | return isSum(a, num - a[i], i+1, count+1) || isSum(a, num, i+1,0) ; 30 | } 31 | 32 | public static int shortestRoad (int [] road1, int [] road2) { 33 | int sum1 = 0; 34 | int sum2 = 0; 35 | int min1; 36 | int min2; 37 | for (int i = 0; i < road1.length; i++) { 38 | sum1 += road1[i]; 39 | sum2 += road2[i]; 40 | } 41 | min1 = sum1; 42 | min2 = sum2; 43 | int segment1 = road1[0]; 44 | int segment2 = road2[0]; 45 | for (int i = 1; i < road1.length; i++) { 46 | if (min1 > (sum2 - segment2) + segment1) { 47 | min1 = (sum2 - segment2) + segment1; 48 | } 49 | if (min2 > (sum1 - segment1) + segment2) { 50 | min2 = (sum1 - segment1) + segment2; 51 | } 52 | segment1 += road1[i]; 53 | segment2 += road2[i]; 54 | } 55 | return Math.min(min1, min2); 56 | } 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /Year2020/July2020a96: -------------------------------------------------------------------------------- 1 | package Year2020; 2 | 3 | public class July202096 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [][] aaa = { 8 | {4, 5, 6, 7, 1}, 9 | {3, 5, 1, 7, 4}, 10 | {4, 5, 6, 5, 8}, 11 | {3, 4, 7, 7, 9}, 12 | {6, 2, 2, 7, 6} 13 | }; 14 | System.out.println(maxSumKnight(aaa)); 15 | } 16 | public static int maxSumKnight(int[][]mat) { 17 | return maxSumKnight(mat,0,0,0,mat[0][0]); 18 | } 19 | 20 | private static int maxSumKnight(int[][]mat,int i, int j, int sum, int done) 21 | { 22 | if(i < 0 || j < 0 || i > mat.length-1 || j > mat[0].length-1) 23 | { 24 | return 0; 25 | } 26 | if(Math.abs(done - mat[i][j]) > 1) { 27 | return 0; 28 | } 29 | if(i == mat.length-1 && j == mat.length-1) { 30 | return sum+mat[i][j]; 31 | } 32 | int z = mat[i][j]; 33 | mat[i][j] = -1; 34 | int up1 = maxSumKnight(mat,i-1,j+2,sum+z,z); 35 | int up2 = maxSumKnight(mat,i-2,j+1,sum+z,z); 36 | int up3 = maxSumKnight(mat,i-2,j-1,sum+z,z); 37 | int up4 = maxSumKnight(mat,i-1,j-2,sum+z,z); 38 | int down1 = maxSumKnight(mat,i+1,j+2,sum+z,z); 39 | int down2 = maxSumKnight(mat,i+2,j+1,sum+z,z); 40 | int down3 = maxSumKnight(mat,i+2,j-1,sum+z,z); 41 | int down4 = maxSumKnight(mat,i+1,j-2,sum+z,z); 42 | mat[i][j] = z; 43 | int maximumOne = Math.max(up1, up2); 44 | int maximumTwo = Math.max(up3, up4); 45 | int maximumThree = Math.max(down1, down2); 46 | int maximumFour = Math.max(down3, down4); 47 | int maximumFive = Math.max(maximumOne, maximumTwo); 48 | int maximumSix = Math.max(maximumThree, maximumFour); 49 | return Math.max(maximumFive, maximumSix); 50 | } 51 | 52 | public static int minimalPositive (Range [] rangeA) { 53 | int left=0; 54 | int right=rangeA.length-1; 55 | int mid; 56 | while(left < right) { 57 | mid = (left+right)/2; 58 | if(rangeA[mid].getBig() <= 0) { 59 | left = mid+1; 60 | }else{ 61 | right = mid; 62 | } 63 | } 64 | int smallNumber = rangeA[right].getSmall(); 65 | int bigNumber = rangeA[right].getBig(); 66 | if(bigNumber <= 0) 67 | return -1; 68 | if(smallNumber <= 0) 69 | return 1; 70 | return smallNumber; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Year2020/July2020b81/July2020b81.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2020/July2020b81/July2020b81.pdf -------------------------------------------------------------------------------- /Year2020/July2020b81/RecursioComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2020; 2 | 3 | public class July2020b81 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [] lengths = {2,5,10,20,50}; 8 | System.out.println(makeSum(lengths,40,4)); 9 | // int aa [] = {10,4,2,5,6,3,8,1,5,9}; 10 | // minimumSubK(aa, 3); 11 | } 12 | 13 | public static int makeSum(int [] lengths, int k, int num) { 14 | return makeSum(lengths,k,num,0); 15 | } 16 | 17 | private static int makeSum(int [] lengths, int k, int num, int i) { 18 | if(i == lengths.length-1) 19 | return 0; 20 | if(k == 0 && num >= 0) 21 | return 1; 22 | if(num < 0) 23 | return 0; 24 | return makeSum(lengths,k-lengths[i],num-1,i) + makeSum(lengths,k,num,i+1); 25 | } 26 | 27 | public static void minimumSubK(int [] arr, int k) 28 | { 29 | int start=0; 30 | int end=k-1; 31 | int sum=0; 32 | int sumMin; 33 | int i; 34 | int len=arr.length; 35 | for(i=0; i= mat.length || col < 0 || col >= mat[row].length || mat[row][col] == -1) // exception condition 45 | { 46 | return sum; 47 | } 48 | 49 | int moveDown = findMaximum(mat, row + 1, col, sum + mat[row][col]); // go down 50 | int moveRightOrLeft; 51 | if (row % 2 == 0) 52 | { 53 | moveRightOrLeft = findMaximum(mat, row, col + 1, sum + mat[row][col]); // go only right 54 | } 55 | else 56 | { 57 | moveRightOrLeft = findMaximum(mat, row, col - 1, sum + mat[row][col]); // go only left 58 | } 59 | 60 | return Math.max(moveDown, moveRightOrLeft); 61 | } 62 | 63 | 64 | 65 | 66 | 67 | /** 68 | * @param a - The array 69 | * @return - Returns the number of sub-arrays within it that are arranged is rising series. 70 | * Time Complexity - O(n) 71 | * Space Complexity - O(n) 72 | */ 73 | public static int strictlyIncreasing (int[] a) 74 | { 75 | int counter = 0; 76 | int sum = 0; 77 | for (int i = 0; i < a.length-1; i++) 78 | { 79 | if (a[i] < a[i+1]) 80 | { 81 | counter++; 82 | sum += counter; 83 | 84 | } // if close 85 | else // if there is no up going array 86 | { 87 | counter = 0; 88 | } 89 | } // for close 90 | return sum; 91 | } // Main close 92 | 93 | } 94 | -------------------------------------------------------------------------------- /Year2020/March2020a87/March2020a87.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2020/March2020a87/March2020a87.pdf -------------------------------------------------------------------------------- /Year2020/March2020a87/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2020; 2 | 3 | public class March2020a87 { 4 | /* 5 | * Tester 6 | */ 7 | public static void main(String[] args) 8 | { 9 | int[] a ={ 1, 3, 6, 8 }; // nothing 10 | int [] b = {1,2,3,4,5,6,7,8,9}; // YES 11 | March2020a87.printTriplets(a, 40); 12 | March2020a87.printTriplets(b, 40); 13 | } 14 | 15 | 16 | /** 17 | Moed 2020a - 470 , 87 Complexity 18 | /**A method that calculates the product of three different members in a one-dimensional array. 19 | Count three different members at a time when there is i, i + 1, i-2 and so we will be able to calculate three 20 | different members at a time and compare them to Num. 21 | * 22 | * @param a - The array. 23 | * @param num - the number which suppose to be calculated. 24 | */ 25 | 26 | public static void printTriplets (int[] a, int num) 27 | { 28 | for(int i = 0 ; i < a.length-2; ++i) { 29 | int rightIndex = a.length-1; 30 | int leftIndex = i + 1; 31 | 32 | while(leftIndex < rightIndex) { 33 | if(a[i] * a[leftIndex] * a[rightIndex] == num) 34 | System.out.println(a[i] + " " + a[leftIndex] + " " + a[rightIndex] ); 35 | if(a[i] * a[leftIndex] * a[rightIndex] < num) 36 | leftIndex++; 37 | else 38 | rightIndex--; 39 | } // while close 40 | 41 | } // for close 42 | 43 | } // main close 44 | 45 | 46 | /** 47 | * Moed 2020a - 470 , 87 RECURSION 48 | * A method that calculates the amount of references that can be made in a matrix. 49 | The method works so you can only go right or down. 50 | So we will use 2 recursive readings. 51 | The first will go right and down and make sure we did not come from above with the help of a Boolean response. 52 | The second will go down and right and make sure we came from above with the help of a Boolean response. 53 | * @param mat - the Array 54 | * @param k - Turns amount 55 | * @return - the amount of turns can be made in the same two dimensional array. 56 | */ 57 | 58 | public static int totalWays(int [][] mat,int k ) { 59 | return totalWays(mat,k,0,0,0); 60 | } 61 | 62 | private static int totalWays(int [][] mat,int k ,int i,int j,int lastI) { 63 | if(i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || k < 0) 64 | return 0; 65 | if (i == mat.length-1 && j == mat.length-1 && k == 0) 66 | return 1; 67 | 68 | if(i == 0 && j == 0) 69 | return totalWays(mat,k,i+1,j,i); 70 | if(lastI == i) { 71 | return totalWays(mat,k,i+1,j,i) + totalWays(mat,k,i,j+1,i); 72 | }else { 73 | return totalWays(mat,k,i+1,j,i) + totalWays(mat,k-1,i,j+1,i); 74 | } 75 | 76 | } 77 | 78 | 79 | } 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /Year2021/Augugst2021a85: -------------------------------------------------------------------------------- 1 | package Year2021; 2 | 3 | public class August2021a85 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int bb[] []= { 8 | {0, 15, 80, 90}, 9 | {-1, 0, 40, 50}, 10 | {-1, -1, 0, 70}, 11 | {-1, -1, -1, 0} 12 | }; 13 | System.out.println(minPrice(bb)); 14 | } 15 | public static int minPrice(int [][] mat) { 16 | return minPrice(mat,0,0,0); 17 | } 18 | 19 | private static int minPrice(int [][] mat, int i, int j,int sum) { 20 | if(j == mat[0].length-1) 21 | return mat[i][j]+sum; 22 | if(i == j) // if we are in the same index's so you are at a 0 price station , so we will go 1 right 23 | return minPrice(mat,i,j+1,sum); 24 | 25 | int firstWay = minPrice(mat,i,j+1,sum); 26 | int secondWay = minPrice(mat,j,j,sum+mat[i][j]); 27 | return Math.min(firstWay, secondWay); 28 | } 29 | 30 | public static boolean findAverage(int [] arr, double x) { 31 | int i; 32 | for(i =0; i= 0 && right < len) { 39 | avg = sum/(right-left+1); 40 | if(avg > x) { 41 | left--; 42 | if(left >= 0) // exception 43 | sum+= arr[left]; 44 | } else if(avg < x) { 45 | right++; 46 | if(right < len) // exception 47 | sum += arr[right]; 48 | } else { 49 | System.out.println(left+"---"+right); 50 | return true; 51 | } 52 | } 53 | return false; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Year2021/August2021: -------------------------------------------------------------------------------- 1 | package Year2021; 2 | 3 | public class August2021 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int[] arr ={ 1, 3, 6, 2 }; 8 | System.out.println(printExpr(arr, 9)); 9 | int [] a = { 1 , 1 , 0 , 1 ,1 ,0 , 1 , 1 , 1 , 1 , 0 , 0 }; 10 | // System.out.println(longestSequence(a, 0)); 11 | // System.out.println(longestSequence(a, 1)); 12 | // System.out.println(longestSequence(a, 2)); 13 | // System.out.println(longestSequence(a, 3)); 14 | // System.out.println(longestSequence(a, 4)); 15 | 16 | 17 | } 18 | 19 | 20 | /** 21 | * Linear Search - O(n) 22 | * Moed 2022b - 477 , 92 Complexity 23 | * @param a - the Array. 24 | * @param k - the amount of 0's in the array. 25 | * @return the longest array which includes 1's between the 0's. 26 | * 27 | */ 28 | public static int longestSequence(int[] a , int k) { 29 | int start = 0; 30 | int max = 0; 31 | int sum = k; 32 | while (start + max < a.length) 33 | { 34 | sum += (a[max + start] -1); 35 | System.out.println(a[max + start] -1); 36 | if (sum >= 0) 37 | max++; 38 | else 39 | { 40 | sum -= (a[start] -1); 41 | start++; 42 | } 43 | } 44 | return max; 45 | } // main close 46 | 47 | 48 | 49 | 50 | 51 | public static int printExpr(int [] arr,int num) { 52 | return printExpr(arr,0,arr.length-1,num,0,""); 53 | } 54 | 55 | private static int printExpr(int [] arr,int i,int max,int num,int sum,String test) { 56 | if (num == sum) { 57 | System.out.println(test); 58 | return 1; 59 | } 60 | if (i > max) 61 | return 0; 62 | int r1 = printExpr(arr,i+1,max,num,sum,test); 63 | int r2 = printExpr(arr,i+1,max,num,sum+arr[i],test+ "+" + arr[i] + " "); 64 | int r3 = printExpr(arr,i+1,max,num,sum-arr[i],test+ "-" + arr[i] + " "); 65 | return r1+r2+r3; 66 | } 67 | } 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /Year2021/August2021a70: -------------------------------------------------------------------------------- 1 | package Year2021; 2 | 3 | public class August2021a70 { 4 | 5 | public static void main(String[] args) 6 | { 7 | // int prices [] = {0,1,3,10,9,10,17,17,20}; 8 | // System.out.println(findMaxPrice(prices, 8)); 9 | int a [] = {1,4,5,7,20}; 10 | int b [] = {1,4,5,7,20,28}; 11 | System.out.println(getMedian(a, b)); 12 | } 13 | 14 | public static int findMaxPrice(int [] prices,int n) { 15 | return findMaxPrice (prices,n,1); 16 | } 17 | 18 | private static int findMaxPrice(int [] prices,int n,int i) { 19 | if (n <= 0 || i > prices.length-1 || i > n) 20 | return 0; 21 | int firstWay = prices[i] + findMaxPrice(prices, n-i, i); 22 | int secondWay = findMaxPrice(prices, n, i+1); 23 | return Math.max(firstWay,secondWay); 24 | } 25 | 26 | public static int getMedian (int a[], int [] b) { 27 | int i=0; 28 | int j=0; 29 | int sum=0; 30 | int count=0; 31 | while(count < a.length-1) { 32 | if(a[i] < b[j]) { 33 | i++; 34 | }else { 35 | j++; 36 | } 37 | count++; 38 | } 39 | if (i < a.length-1 && a[i+1] < b[j]) { 40 | sum +=a[i]+a[i+1]; 41 | }else if(j < b.length-1 && b[j+1] < a[i]) { 42 | sum+=b[j]+b[j+1]; 43 | }else { 44 | sum+=a[i]+b[j]; 45 | } 46 | return sum/2; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Year2021/August2021b92/August2021b92.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2021/August2021b92/August2021b92.pdf -------------------------------------------------------------------------------- /Year2021/August2021b92/LinkedLists/Driver.java: -------------------------------------------------------------------------------- 1 | package Lists2021b92; 2 | 3 | public class Driver { 4 | 5 | public static void main(String[] args) { 6 | IntNode node5 = new IntNode(5,null); 7 | IntNode node4 = new IntNode(4, node5); 8 | IntNode node3 = new IntNode(3, node4); 9 | IntNode node2 = new IntNode(2, node3); 10 | IntNode node1 = new IntNode(1, node2); 11 | 12 | // Create a list 13 | IntNode.IntList testList = node1.new IntList(); 14 | 15 | // IntNode node10 = new IntNode(5,null); 16 | IntNode node9 = new IntNode(3, null); 17 | IntNode node8 = new IntNode(4, node9); 18 | IntNode node7 = new IntNode(2, node8); 19 | IntNode node6 = new IntNode(1, node7); 20 | 21 | // Create Second List 22 | IntNode.IntList list = node6.new IntList(); 23 | 24 | // Check 25 | boolean result = testList.what1(list); 26 | System.out.println("First Result:" + result); 27 | boolean result2 = testList.what2(list); 28 | System.out.println("Second Result:" + result2); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Year2021/August2021b92/LinkedLists/Driver2.java: -------------------------------------------------------------------------------- 1 | package Lists2021b92; 2 | 3 | public class Driver2 { 4 | 5 | public static void main(String[] args) { 6 | IntNode node5 = new IntNode(5,null); 7 | IntNode node4 = new IntNode(4, node5); 8 | IntNode node3 = new IntNode(3, node4); 9 | IntNode node2 = new IntNode(2, node3); 10 | IntNode node1 = new IntNode(1, node2); 11 | 12 | // Create a list 13 | IntNode.IntList testList = node1.new IntList(node1); 14 | 15 | // IntNode node10 = new IntNode(5,null); 16 | IntNode node9 = new IntNode(3, null); 17 | IntNode node8 = new IntNode(4, node9); 18 | IntNode node7 = new IntNode(2, node8); 19 | IntNode node6 = new IntNode(1, node7); 20 | 21 | // Create Second List 22 | IntNode.IntList list = node6.new IntList(node6); 23 | 24 | // Check 25 | boolean result = testList.what1(list); 26 | System.out.println("First Result:" + result); 27 | boolean result2 = testList.what2(list); 28 | System.out.println("Second Result:" + result2); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Year2021/August2021b92/LinkedLists/IntNode.java: -------------------------------------------------------------------------------- 1 | package Lists2021b92; 2 | 3 | public class IntNode { 4 | 5 | public int _value; 6 | public IntNode _next; 7 | 8 | public IntNode(int val, IntNode n) { 9 | _value = val; 10 | _next = n; 11 | } 12 | public int getValue() { 13 | return _value; 14 | } 15 | 16 | public IntNode getNext() { 17 | return _next; 18 | } 19 | 20 | public void setValue(int v) { 21 | _value = v; 22 | } 23 | 24 | public void setNext(IntNode node) { 25 | _next = node; 26 | } 27 | 28 | public class IntList{ 29 | 30 | public IntNode _head; 31 | 32 | public IntList() { 33 | _head = null; 34 | } 35 | 36 | public boolean what1(IntList list) { 37 | IntNode h1 = _head; 38 | IntNode h2 = list._head; 39 | while((h1 != null) && (h2 != null)) { 40 | if(h1.getValue() != h2.getValue()) 41 | return false; 42 | h1 = h1.getNext(); 43 | h2 = h2.getNext(); 44 | } 45 | 46 | return true; 47 | } 48 | 49 | public boolean what2(IntList list) { 50 | IntNode h1 = _head; 51 | 52 | while(h1 != null) { 53 | boolean found = false; 54 | IntNode h2 = list._head; 55 | while((h2 != null) && (!found)) { 56 | if (h1.getValue() == h2.getValue()) 57 | found = true; 58 | h2 = h2.getNext(); 59 | } 60 | if(!found) 61 | return false; 62 | h1 = h1.getNext(); 63 | } 64 | return true; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Year2021/August2021b92/LinkedLists/IntNode2.java: -------------------------------------------------------------------------------- 1 | package Lists2021b92; 2 | 3 | 4 | // This IntNode2 is the fixed code . i dont know if that use a trap or it was a mistake. 5 | public class IntNode2 { 6 | 7 | public int _value; 8 | public IntNode _next; 9 | 10 | public IntNode(int val, IntNode n) { 11 | _value = val; 12 | _next = n; 13 | } 14 | public int getValue() { 15 | return _value; 16 | } 17 | 18 | public IntNode getNext() { 19 | return _next; 20 | } 21 | 22 | public void setValue(int v) { 23 | _value = v; 24 | } 25 | 26 | public void setNext(IntNode node) { 27 | _next = node; 28 | } 29 | 30 | public class IntList{ 31 | 32 | public IntNode _head; 33 | 34 | public IntList(IntNode node) { 35 | _head = node; 36 | } 37 | 38 | public boolean what1(IntList list) { 39 | IntNode h1 = _head; 40 | IntNode h2 = list._head; 41 | while((h1 != null) && (h2 != null)) { 42 | if(h1.getValue() != h2.getValue()) 43 | return false; 44 | h1 = h1.getNext(); 45 | h2 = h2.getNext(); 46 | } 47 | 48 | return true; 49 | } 50 | 51 | public boolean what2(IntList list) { 52 | IntNode h1 = _head; 53 | 54 | while(h1 != null) { 55 | boolean found = false; 56 | IntNode h2 = list._head; 57 | while((h2 != null) && (!found)) { 58 | if (h1.getValue() == h2.getValue()) 59 | found = true; 60 | h2 = h2.getNext(); 61 | } 62 | if(!found) 63 | return false; 64 | h1 = h1.getNext(); 65 | } 66 | return true; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Year2021/August2021b92/Polymorphism/A.java: -------------------------------------------------------------------------------- 1 | package Polymorphism2021b92; 2 | 3 | public abstract class A { 4 | 5 | protected int _x; 6 | 7 | public A() { 8 | _x=1; 9 | } 10 | 11 | public abstract int foo(int x); 12 | 13 | public void foo(A a) { 14 | _x = a._x; 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Year2021/August2021b92/Polymorphism/B.java: -------------------------------------------------------------------------------- 1 | package Polymorphism2021b92; 2 | 3 | public class B extends A{ 4 | 5 | public B() { 6 | super(); 7 | } 8 | public B(int val){ 9 | _x = val; 10 | } 11 | 12 | public B(A val) { 13 | _x = foo(val._x); 14 | } 15 | 16 | public int foo(int x) { 17 | return _x + x; 18 | } 19 | 20 | public void foo(B b) { 21 | _x = _x * b._x; 22 | } 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Year2021/August2021b92/Polymorphism/C.java: -------------------------------------------------------------------------------- 1 | package Polymorphism2021b92; 2 | 3 | public class C extends B{ 4 | 5 | public C() { 6 | super(); 7 | } 8 | 9 | public C(int val) { 10 | super(val); 11 | } 12 | 13 | public int foo(int x) { 14 | return _x - x; 15 | } 16 | 17 | public void foo(A a) { 18 | if (a instanceof C) { 19 | _x = _x - a._x; 20 | // System.out.println(a._x); 21 | }else { 22 | super.foo(a); 23 | } 24 | } 25 | 26 | public void foo(B b) { 27 | super.foo(b); 28 | _x++; 29 | } 30 | 31 | public void foo(C c) { 32 | _x = c._x -1; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Year2021/August2021b92/Polymorphism/Driver.java: -------------------------------------------------------------------------------- 1 | package Polymorphism2021b92; 2 | 3 | public class Driver { 4 | 5 | public static void main(String [] args) { 6 | A a; 7 | B b; 8 | C c; 9 | 10 | // System.out.println("Question1 : "); 11 | // System.out.println("Compilation Error."); 12 | // a = new A(); 13 | // a.foo(2); 14 | 15 | // System.out.println("Question2: "); 16 | // a= new B(); 17 | // b = new C(); 18 | // b.foo(a); 19 | // System.out.println(a._x); 20 | 21 | 22 | // System.out.println("Question3: "); 23 | // a = new C(); 24 | // b = new C(); 25 | // ((C)b).foo(a); 26 | // System.out.println(a._x); 27 | 28 | // System.out.println("Question4: "); 29 | // C c1 = new C(); 30 | // b = new C(); 31 | // b.foo(c1); 32 | // System.out.println(b._x); 33 | 34 | 35 | // System.out.println("Question5: "); 36 | // c = new C(); 37 | // c.foo(c); 38 | // System.out.println(c._x); 39 | 40 | // System.out.println("Question6: "); 41 | // C c1 = new C(2); 42 | // a = new C(3); 43 | // c1.foo((B)a); 44 | // System.out.println(a._x); 45 | 46 | System.out.println("Question7: "); 47 | System.out.println("Compliation Error - No Override for Object at A/B/C"); 48 | // b = new B(2); 49 | // Object o =b; 50 | // o.foo(4); 51 | 52 | System.out.println("Question8: "); 53 | C c1 = new C(2); 54 | a = new C(3); 55 | c1.foo(a); 56 | System.out.println(a._x); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Year2021/August2021b92/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package shadi; 2 | 3 | public class liveTest9 { 4 | public static void main(String[] args) 5 | { 6 | int [] a = {1,3,6,2}; 7 | // System.out.println(printExpr(a,4)); 8 | int [] aa = { 1 , 1 , 0 , 1 ,1 ,0 , 1 , 1 , 1 , 1 , 0 , 0 }; 9 | // System.out.println(longestSequence(aa, 0)); 10 | System.out.println(longestSequence(aa, 1)); 11 | System.out.println(longestSequence(aa, 2)); 12 | // System.out.println(longestSequence(aa, 3)); 13 | // System.out.println(longestSequence(aa, 4)); 14 | } 15 | 16 | public static int printExpr(int [] arr,int num) { 17 | return printExpr(arr,num,0,0,""); 18 | } 19 | 20 | private static int printExpr(int [] arr,int num,int i,int sum,String str) { 21 | 22 | if(sum == num) { 23 | System.out.println(str); 24 | return 1; 25 | } 26 | if(i > arr.length-1) { 27 | return 0; 28 | } 29 | 30 | int way1 = printExpr(arr,num,i+1,sum,str); 31 | int way2 = printExpr(arr,num,i+1,sum+arr[i],str + "+" + arr[i]); 32 | int way3 = printExpr(arr,num,i+1,sum-arr[i],str + "-" + arr[i]); 33 | return way1+way2+way3; 34 | } 35 | 36 | 37 | 38 | 39 | public static int longestSequence(int [] a, int k) { 40 | int left = 0; 41 | int counter = 0; 42 | int sum = k; 43 | while (left + counter < a.length) 44 | { 45 | sum += (a[left + counter] -1); 46 | // System.out.println(a[max + start] -1); 47 | if (sum >= 0) 48 | counter++; 49 | else 50 | { 51 | sum -= (a[left] -1); // so it will ignore the current place 52 | left++; 53 | } 54 | } 55 | return counter; 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Year2021/August2021b92/Trees/Driver.java: -------------------------------------------------------------------------------- 1 | package Tree2021b92; 2 | 3 | 4 | public class Driver { 5 | public static void main (String[] args){ 6 | 7 | Node bt = new Node(60); 8 | 9 | bt._rightSon = new Node(70); 10 | 11 | bt._leftSon = new Node(40); 12 | bt._leftSon._leftSon = new Node(30); 13 | bt._leftSon._leftSon._leftSon = new Node(20); 14 | 15 | bt._leftSon._rightSon = new Node(50); 16 | bt._leftSon._rightSon._rightSon = new Node(55); 17 | 18 | bt._leftSon._rightSon._leftSon = new Node(47); 19 | bt._leftSon._rightSon._leftSon._leftSon = new Node(43); 20 | 21 | System.out.println(Node.find(bt, 43)); 22 | System.out.println(Node.find(bt, 59)); 23 | System.out.println(Node.find(bt, 15)); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Year2021/August2021b92/Trees/Node.java: -------------------------------------------------------------------------------- 1 | package Tree2021b92; 2 | 3 | public class Node { 4 | public int _number; 5 | public Node _leftSon, _rightSon; 6 | 7 | 8 | public Node (int number){ 9 | _number = number; 10 | _leftSon = null; 11 | _rightSon = null; 12 | } 13 | 14 | public int getNumber(){return _number;} 15 | public Node getLeftSon(){return _leftSon;} 16 | public Node getRightSon(){return _rightSon;} 17 | 18 | public static int find(Node root, int num) { 19 | if (root== null) 20 | return -1; 21 | if (root.getNumber() == num) 22 | return num; 23 | else if(root.getNumber() < num) { 24 | int k = find(root.getRightSon(),num); 25 | if(k== -1) 26 | return root.getNumber(); 27 | else 28 | return k; 29 | 30 | } 31 | else if(root.getNumber() > num) 32 | return find(root.getLeftSon(), num); 33 | return -1; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Year2021/July2021b62/2021b62.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2021/July2021b62/2021b62.pdf -------------------------------------------------------------------------------- /Year2021/July2021b62/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2021; 2 | 3 | public class July2021b62 { 4 | 5 | public static void main(String[] args) 6 | { 7 | int [] a = {-3,5,12,14,-9,13}; 8 | System.out.println(equalSplit(a)); 9 | int [] aa = {-3,5,-12,14,-9,13}; 10 | System.out.println(equalSplit(aa)); 11 | int [][] aaa = 12 | {{1,3,7,9}, 13 | {6,4,15,11}, 14 | {36,50,21,22}, 15 | {60,55,30,26}}; 16 | System.out.println(search(aaa, 11)); 17 | } 18 | 19 | public static boolean equalSplit(int[] arr) 20 | { 21 | return equalSplit(arr,0,0,0,0,arr.length,0); 22 | } 23 | 24 | private static boolean equalSplit(int[] arr,int sum1, int sum2, int cnt1, int cnt2,int left, int i) { 25 | if (arr.length % 2 != 0) // if arr length is not double ( 3/3 2/2 ZUGI ) 26 | return false; 27 | if (left == 0 && cnt1 == cnt2 && sum1 == sum2) 28 | return true; 29 | if (left == 0 && (cnt1 != cnt2 || sum1 != sum2)) 30 | return false; 31 | return equalSplit(arr,sum1+arr[i],sum2,cnt1+1,cnt2,left-1,i+1) 32 | || equalSplit(arr,sum1,sum2+arr[i],cnt1,cnt2+1,left-1,i+1); 33 | } 34 | 35 | //option1 36 | 37 | public static boolean search (int [][]mat,int num) 38 | { 39 | int n = mat.length; 40 | int i = 0; 41 | int j = 0; 42 | while(n > 1) { 43 | if(num <= mat[(n/2)-1+i][j]){ // [1][0] // Left Top Quarter // 6 44 | System.out.println(mat[(n/2)-1+i][j]); 45 | i = i; 46 | j = j; 47 | }else if(num <= mat[(n/2) -1 + i][(n/2) + j]){ // [1][2] // Right Top Quarter // 15 48 | // System.out.println(mat[(n/2) -1 + i][(n/2) + j]); 49 | j += n/2; 50 | }else if(num <= mat[(n-1) + i][(n/2) +j]) { // [1][3] // Right Low Quarter // 11 51 | // System.out.println(mat[(n-1) + i][(n/2) +j]); 52 | i += n/2; 53 | j += n/2; 54 | }else if(num <= mat[(n-1)+i][j]){ // [3][1] // Left Low Quarter // 60 55 | // System.out.println(mat[(n-1)+i][j]); 56 | i += n/2; 57 | } 58 | if(mat[i][j] == num) { 59 | System.out.println("row= " + i); 60 | System.out.println("col= " + j); 61 | return true; 62 | }else { 63 | n = n/2; 64 | } 65 | } 66 | return false; 67 | } 68 | 69 | 70 | //option2 71 | public static boolean searchh (int [][]mat,int num) 72 | { 73 | int len = mat.length; 74 | int row = len-1; 75 | int col = 0; 76 | int value = num-1; 77 | int firstQuarter,secondQuarter,thirdQuarter,fourthQuarter; 78 | int halfSize = len/2; 79 | if(row == 0) { 80 | if(num == mat[row][col]) { 81 | System.out.println("row=0\ncol=0"); 82 | return true; 83 | } 84 | return false; 85 | } 86 | 87 | while(num != value && halfSize > 0){ 88 | firstQuarter=mat[row-halfSize][col]; 89 | secondQuarter=mat[row-halfSize][col+halfSize]; 90 | thirdQuarter=mat[row][col+halfSize]; 91 | fourthQuarter=mat[row][col]; 92 | 93 | if(firstQuarter >= num) { 94 | value=firstQuarter; 95 | row-=halfSize; 96 | } 97 | else if(secondQuarter >= num) { 98 | value=secondQuarter; 99 | row-=halfSize; 100 | col+=halfSize; 101 | } 102 | else if(thirdQuarter >= num) { 103 | value=thirdQuarter; 104 | col+=halfSize; 105 | } 106 | else if(fourthQuarter >= num) { 107 | value=fourthQuarter; 108 | }else{ 109 | return false; 110 | } 111 | halfSize/=2; 112 | } // while 113 | if(value == num) { 114 | System.out.println("row=" + row+ "\ncol= " +col); 115 | return true; 116 | } 117 | return false; 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Year2021/June2021b60/June2021b60.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2021/June2021b60/June2021b60.pdf -------------------------------------------------------------------------------- /Year2021/June2021b60/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2021; 2 | 3 | import java.util.Arrays; 4 | 5 | public class June2021b60 { 6 | 7 | public static void main(String[] args) 8 | { 9 | // int[] a = {8,4,7,1,2,3,5}; 10 | // System.out.println(split3(a)); 11 | // int[] aa = {4,7,1,2,3,5}; 12 | // System.out.println(split3(aa)); 13 | int[] aaa = {1,4,13,6,0,19}; 14 | System.out.println(smallestSub(aaa, 22)); 15 | } 16 | 17 | public static boolean split3 (int [] arr) 18 | { 19 | return split3 (arr,0,0,0,0); 20 | 21 | } 22 | private static boolean split3 (int [] arr,int s1,int s2,int s3,int i) 23 | { 24 | if (arr.length < 3) 25 | return false; 26 | if (i == arr.length) 27 | { 28 | if(s1 == s2 && s2 == s3 && s1 == s3) 29 | { 30 | return true; 31 | } 32 | return false; 33 | } 34 | return split3(arr,s1+arr[i],s2,s3,i+1) 35 | || split3(arr,s1,s2+arr[i],s3,i+1) 36 | || split3(arr,s1,s2,s3+arr[i],i+1); 37 | } // private close 38 | 39 | public static int smallestSub (int [] a, int k) 40 | { 41 | int i=0; 42 | int j=0; 43 | int sum=0; 44 | int smallest = a.length-1; 45 | 46 | while(i < a.length) 47 | { 48 | sum+=a[i]; // count all the index's in the array. 49 | while (sum > k) // if sum is bigger than k 50 | { 51 | smallest = i-j+1; 52 | sum -=a[j]; 53 | j++; 54 | } 55 | i++; 56 | } 57 | return smallest; 58 | } 59 | 60 | } // main 61 | -------------------------------------------------------------------------------- /Year2022/April2022a96/August2022a96.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2022/April2022a96/August2022a96.pdf -------------------------------------------------------------------------------- /Year2022/April2022a96/Polymorphism/A.java: -------------------------------------------------------------------------------- 1 | package Poly2; 2 | 3 | public class A { 4 | 5 | private int _num; 6 | 7 | public A(int num) { 8 | _num = num; 9 | } 10 | 11 | public int getNum() { 12 | return _num; 13 | } 14 | 15 | public boolean foo(A a) { 16 | // System.out.println("Afoo"); 17 | return _num>=2*a._num; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Year2022/April2022a96/Polymorphism/B.java: -------------------------------------------------------------------------------- 1 | package Poly2; 2 | public class B extends A { 3 | 4 | public B(int num) { 5 | super(num); 6 | } 7 | 8 | public boolean foo(B b) { 9 | System.out.println("BBfoo"); 10 | return getNum() == b.getNum(); 11 | } 12 | 13 | public boolean foo(C c) { 14 | System.out.println("BCfoo"); 15 | return getNum() == c.getNum(); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Year2022/April2022a96/Polymorphism/C.java: -------------------------------------------------------------------------------- 1 | package Poly2; 2 | public class C extends A { 3 | 4 | private int _num2; 5 | public C() { 6 | super(20); 7 | _num2 =20; 8 | } 9 | 10 | public boolean foo(A a) { 11 | System.out.println("Cfoo"); 12 | if(!(a instanceof C)) 13 | return false; 14 | C c =(C)a; 15 | return _num2 == c._num2; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Year2022/April2022a96/Polymorphism/Driver.java: -------------------------------------------------------------------------------- 1 | package Poly2; 2 | 3 | public class Driver { 4 | 5 | public static void main(String [] args) { 6 | A y1 = new B(100); 7 | B y2 = new B(100); 8 | B y3 = new B(20); 9 | A z1 = new C(); 10 | C z2 = new C(); 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Year2022/April2022a96/Polymorphism/Tester.java: -------------------------------------------------------------------------------- 1 | public class Tester { 2 | 3 | public static void main(String [] args) { 4 | System.out.println("Question 1: "); 5 | System.out.println("Expected : YES"); 6 | if(y1.foo(y2) == (y2.foo(y1))){ 7 | System.out.println("Received: YES"); 8 | }else { 9 | System.out.println("Received: NO"); 10 | } 11 | 12 | System.out.println("Question 2: "); 13 | System.out.println("Expected : NO"); 14 | if(y1.foo(y3) == (y3.foo(y1))){ 15 | System.out.println("Received: YES"); 16 | }else { 17 | System.out.println("Received: NO"); 18 | } 19 | 20 | System.out.println("Question 3: "); 21 | System.out.println("Expected : YES"); 22 | if(y1.foo(z1) == (y1.foo(z2))){ 23 | System.out.println("Received: YES"); 24 | }else { 25 | System.out.println("Received: NO"); 26 | } 27 | 28 | System.out.println("Question 4: "); 29 | System.out.println("Expected:"); 30 | System.out.println("Bbfoo"); 31 | System.out.println("False"); 32 | System.out.println("**Received:"); 33 | if((y2.foo(y3) == true)) { 34 | System.out.println("True"); 35 | }else { 36 | System.out.println("False"); 37 | } 38 | 39 | System.out.println("Question 5: "); 40 | System.out.println("Expected:"); 41 | System.out.println("Afoo"); 42 | System.out.println("True"); 43 | System.out.println("**Received:"); 44 | if((y2.foo(z1) == false)) { 45 | System.out.println("False"); 46 | }else { 47 | System.out.println("True"); 48 | } 49 | 50 | System.out.println("Question 6: "); 51 | System.out.println("Expected:"); 52 | System.out.println("Bcfoo"); 53 | System.out.println("False"); 54 | System.out.println("**Received:"); 55 | if((y2.foo(z2) == false)) { 56 | System.out.println("False"); 57 | }else { 58 | System.out.println("True"); 59 | } 60 | 61 | System.out.println("Question 7: "); 62 | System.out.println("Expected:"); 63 | System.out.println("Bcfoo"); 64 | System.out.println("True"); 65 | System.out.println("**Received:"); 66 | if((y3.foo(z2) == false)) { 67 | System.out.println("False"); 68 | }else { 69 | System.out.println("True"); 70 | } 71 | 72 | System.out.println("Question 8: "); 73 | System.out.println("Expected 3 TIMES !!!:"); 74 | System.out.println("Cfoo"); 75 | System.out.println("False"); 76 | System.out.println("**Received:"); 77 | if((z1.foo(y1) == false )) { 78 | System.out.println("False"); 79 | }else { 80 | System.out.println("True"); 81 | } 82 | if((z1.foo(y2) == false )){ 83 | System.out.println("False"); 84 | }else { 85 | System.out.println("True"); 86 | } 87 | if((z1.foo(y3) == false )){ 88 | System.out.println("False"); 89 | }else { 90 | System.out.println("True"); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Year2022/April2022a96/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2022; 2 | 3 | public class April2022a96 { 4 | 5 | public static void main(String[] args) { 6 | int a [] = {3,0,0,4,7,9,0,0,0,0,11,15,0,19,20,0,0,31,40,0}; // 19 7 | // System.out.println(kAlmostSearch(a,9)); 8 | // System.out.println(kAlmostSearch(a,31)); 9 | // System.out.println(kAlmostSearch(a,30)); 10 | // System.out.println(kAlmostSearch(a,20)); 11 | // System.out.println(kAlmostSearch(a,40)); 12 | // System.out.println(kAlmostSearch(a,3)); 13 | // System.out.println(kAlmostSearch(a,4)); 14 | // System.out.println(kAlmostSearch(a,7)); 15 | // System.out.println(kAlmostSearchh(a,9)); 16 | // System.out.println(kAlmostSearchh(a,31)); 17 | // System.out.println(kAlmostSearchh(a,30)); 18 | // System.out.println(kAlmostSearchh(a,20)); 19 | // System.out.println(kAlmostSearchh(a,40)); 20 | // System.out.println(kAlmostSearchh(a,3)); 21 | // System.out.println(kAlmostSearchh(a,4)); 22 | // System.out.println(kAlmostSearchh(a,7)); 23 | // 24 | System.out.println(calc(3,36,4)); 25 | } 26 | 27 | public static int kAlmostSearch(int [] a, int num) { 28 | int low = 0; 29 | int high = a.length-1; 30 | while(low <= high) { 31 | int mid = (low + high)/2; 32 | while (mid <= high && a[mid] == 0) { 33 | mid++; 34 | } 35 | if (mid > high) 36 | { 37 | mid--; 38 | while (mid >= low && a[mid] == 0) 39 | { 40 | mid--; 41 | } 42 | if (mid < low) 43 | { 44 | return -1; 45 | } 46 | } 47 | if (a[mid] == num) 48 | return mid; 49 | if (a[mid] < num) 50 | low = (low+high)/2 + 1; 51 | else 52 | high = (low+high)/2 - 1; 53 | } 54 | return -1; 55 | } 56 | 57 | public static int kAlmostSearchh(int [] a, int num) { 58 | int low = 0; 59 | int high = a.length-1; 60 | while(low <= high) { 61 | int mid = (low + high)/2; 62 | if (mid <= high && a[mid] == 0) 63 | mid++; 64 | if (mid >= high && a[mid] == 0) 65 | mid--; 66 | if (a[mid] == num) 67 | return mid; 68 | if (a[mid] < num) { 69 | low = (low+high)/2 + 1; 70 | if(a[low] == num) 71 | return low; 72 | } 73 | else 74 | high = (low+high)/2 - 1; 75 | } 76 | return -1; 77 | } 78 | 79 | 80 | public static int calc(int num, int result, int maxOp) { 81 | return calc(num,result,maxOp,num+"",num); 82 | } 83 | 84 | private static int calc(int num, int result, int maxOp,String string, int currentResult) { 85 | if (maxOp < 0) 86 | return 0; 87 | if (currentResult == result) { 88 | System.out.println(string + "=" + result); 89 | return 1; 90 | } 91 | int r0 = calc(num, result, maxOp-1,string + "+" + num, currentResult + num); 92 | int r1 = calc(num, result, maxOp-1,string + "-" + num, currentResult - num); 93 | int r2 = calc(num, result, maxOp-1,string + "*" + num, currentResult * num); 94 | int r3 = calc(num, result, maxOp-1,string + "/" + num, currentResult / num); 95 | 96 | return r0+r1+r2+r3; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Year2022/August2022a87: -------------------------------------------------------------------------------- 1 | package Year2022; 2 | 3 | public class August2022a87 { 4 | 5 | public static void main(String [] args) { 6 | System.out.println(findMissingIndex(new int [] {22,24,26,32,34,36,38,40})); 7 | System.out.println(findMissingIndex(new int [] {22,26,28})); 8 | System.out.println(findMissingIndex(new int [] {2,4,6,8,10})); 9 | } 10 | /** 11 | * Recursion , A 12 | * @param mat 13 | * @param x 14 | * @param size 15 | * @return 16 | */ 17 | public static boolean isIdentity(int [][] mat, int x, int size) 18 | { 19 | return isIdentity(mat,x,size,x,x); 20 | } 21 | 22 | private static boolean isIdentity(int [][] mat, int x, int size,int i,int j) 23 | { 24 | if (i==j) 25 | { 26 | if(mat[i][j] == 1) 27 | { 28 | return isIdentity(mat,x,size,i+1,j) && isIdentity(mat,x,size,i,j+1); 29 | } 30 | else { 31 | return false; 32 | } 33 | } 34 | if(i == x+size || j == x+size) { 35 | return true; 36 | } 37 | if (mat[i][j] == 0) { 38 | return isIdentity(mat,x,size,i+1,j) && isIdentity(mat,x,size,i,j+1); 39 | } else { 40 | return false; 41 | } 42 | } 43 | 44 | /** 45 | * REcursion B 46 | * @param mat 47 | * @return 48 | */ 49 | public static int maxMatrix(int [][] mat) { 50 | return maxMatrix(mat, 0, mat.length); 51 | } 52 | private static int maxMatrix(int [][] mat, int start, int size) { 53 | if (size <= 0) { 54 | return 0; 55 | } 56 | if (isIdentity(mat,start,size)) { 57 | return size; 58 | } 59 | return maxMatrix(mat, start+1, size-2); 60 | } 61 | 62 | 63 | public static int findMissingIndex(int [] a) { 64 | int diff = Math.min(a[1] - a[0], a[2] - a[1]); 65 | int left = 0; 66 | int right = a.length-1; 67 | int mid; 68 | while (left < right-1) { 69 | mid = (left+right) /2; 70 | if(a[left] + diff *(mid-left) != a[mid]) { 71 | right = mid; 72 | }else if (a[mid] + diff*(right-mid) != a[right]) { 73 | left = mid; 74 | } else { 75 | return a.length; 76 | } 77 | } 78 | return right; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /Year2022/July2022b91/July2022b91.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2022/July2022b91/July2022b91.pdf -------------------------------------------------------------------------------- /Year2022/July2022b91/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2022; 2 | 3 | public class July2022a91 { 4 | 5 | 6 | public static void main(String[] args) 7 | { 8 | int [] a = {2,4,8,3,10,1,12,3,2}; 9 | cheapRt(a,3,2,4); 10 | int [] aa = {-4,1,-8,9,6}; 11 | System.out.println(findTriplet(aa)); 12 | } 13 | 14 | public static int cheapRt (int [] stations,int step1,int step2, int limit) 15 | { 16 | return cheapRt(stations,step1,step2,limit,0,0,""); 17 | } 18 | 19 | private static int cheapRt (int [] stations,int step1,int step2, int limit,int i,int sum,String str) 20 | { 21 | if(limit < 0 || i > stations.length-1) 22 | return Integer.MAX_VALUE; 23 | if (i == stations.length-1) 24 | { 25 | sum += stations[i]; 26 | System.out.println(str + i + " " + "= " + sum); 27 | return sum; 28 | } 29 | int firstWay = cheapRt(stations,step1,step2,limit,i+step1,sum+stations[i],str + i + " "); 30 | int secondWay = cheapRt(stations,step1,step2,limit-1,i+step2,sum+stations[i],str + i + " "); 31 | 32 | return Math.min(firstWay, secondWay); 33 | } 34 | 35 | public static int findTriplet (int [] arr) 36 | { 37 | int max1 = Integer.MIN_VALUE; 38 | int max2 = Integer.MIN_VALUE; 39 | int max3 = Integer.MIN_VALUE; 40 | int min1 = Integer.MAX_VALUE; 41 | int min2 = Integer.MAX_VALUE; 42 | for(int i=0; i < arr.length; i++) 43 | { 44 | if(arr[i]>max1) { 45 | max3=max2; 46 | max2=max1; 47 | max1=arr[i]; 48 | }else if(arr[i]>max2) { 49 | max3=max2; 50 | max2=arr[i]; 51 | }else if(arr[i]>max3) { 52 | max3=arr[i]; 53 | } 54 | if(arr[i] max1 * max2 * max3) { 62 | System.out.println(min1 + " " + min2 + " " + max1); 63 | return min1 * min2 * max1; 64 | }else { 65 | System.out.println(max1 + " " + max2 + " " + max3); 66 | return max1 * max2 * max3; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Year2022/June2022a86: -------------------------------------------------------------------------------- 1 | package Year2022; 2 | 3 | import java.util.Arrays; 4 | 5 | public class June2022a86 { 6 | public static void main(String[] args) 7 | { 8 | // int [][] aa = {{12,22,23,54,11}, 9 | // {43,35,21,20,30}, 10 | // {34,23,43,22,30}, 11 | // {25,31,2,20,34}, 12 | // {10,22,10,11,10}, 13 | // {40,13,3,1,23} 14 | // }; 15 | // System.out.println(maxPath(aa)); 16 | 17 | int[] a = {35,17,13,252,4,128,7,3,81}; 18 | int[] expected = {81,252,13,3,4,35,17,7,128}; 19 | sortmod(a,10); 20 | System.out.println("expected: " + Arrays.toString(expected)); 21 | System.out.println("received: " + Arrays.toString(a)); 22 | } 23 | 24 | public static int maxPath (int [][] mat) 25 | { 26 | return maxPath(mat,0,0,0); 27 | } 28 | 29 | private static int maxPath (int [][] mat,int i , int j, int sum) 30 | { 31 | if (i >= mat.length || j >= mat[0].length || i < 0 || j < 0) 32 | return Integer.MIN_VALUE; 33 | if (i == mat.length-1 && j == mat[0].length-1) 34 | return sum+mat[i][j]; 35 | int way1 = maxPath(mat,i+mat[i][j]/10,j+mat[i][j]%10 ,sum+mat[i][j]); 36 | int way2 = maxPath(mat,i+mat[i][j]%10,j+mat[i][j]/10 ,sum+mat[i][j]); 37 | return Math.max(way1,way2); 38 | } 39 | 40 | 41 | public static void sortmod(int[] a, int k){ 42 | int index = 0; 43 | int temp; 44 | for(int i = 0; i < k; i++){ 45 | for (int j = index; j < a.length; j++) { 46 | if(a[j] % k == i){ 47 | temp = a[index]; 48 | a[index] = a[j]; 49 | a[j] = temp; 50 | index++; 51 | } 52 | } 53 | } 54 | } 55 | 56 | } // main close 57 | -------------------------------------------------------------------------------- /Year2022/March2022a74/LinkedList/IntNodee.java: -------------------------------------------------------------------------------- 1 | package NodeTester; 2 | 3 | // 2022a74 4 | public class IntNodee { 5 | public static void main(String[] args) { 6 | IntNodee intNodee = new IntNodee(); 7 | IntList intList = intNodee.new IntList(); 8 | 9 | int[] arr = new int[]{-6,-3,-2,0,1,3,7,8,10}; 10 | IntNode current = null; 11 | for(int i = arr.length - 1; i >= 0; i--){ 12 | current = intNodee.new IntNode(arr[i], current); 13 | } 14 | intList._head = current; 15 | 16 | int x = 5; 17 | int count = intList.secret(x); 18 | System.out.println("Count: " + count); 19 | } 20 | 21 | public class IntNode { 22 | private int _value; 23 | private IntNode _next; 24 | public IntNode(int val, IntNode n) { 25 | _value = val; 26 | _next = n; 27 | } 28 | public IntNode getNext( ) { 29 | return _next; 30 | } 31 | public int getValue() { 32 | return _value; 33 | } 34 | } 35 | 36 | public class IntList 37 | { 38 | private IntNode _head; 39 | public IntList() { 40 | _head = null; 41 | } 42 | public int secret (int x) 43 | { 44 | int count=0; 45 | IntNode p = _head, q = _head.getNext(); 46 | while (p!=null && q!=null) 47 | { 48 | if (p!=q && q.getValue()- p.getValue()== x) 49 | { 50 | System.out.println ("("+p.getValue()+","+q.getValue()+")"); 51 | p = p.getNext(); 52 | q = q.getNext(); 53 | count++; 54 | } 55 | else if(q.getValue()- p.getValue()< x) 56 | q = q.getNext(); 57 | else 58 | p = p.getNext(); 59 | } 60 | return count; 61 | } 62 | } // end of class IntList 63 | } -------------------------------------------------------------------------------- /Year2022/March2022a74/March2022a74.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2022/March2022a74/March2022a74.pdf -------------------------------------------------------------------------------- /Year2022/March2022a74/Polymorphism/AAA.java: -------------------------------------------------------------------------------- 1 | package Poly2022a74; 2 | 3 | public class AAA { 4 | private int _val; 5 | public AAA() { 6 | _val = 1; 7 | } 8 | public AAA(int val) { 9 | _val = val; 10 | } 11 | public int getVal() { 12 | return _val; 13 | } 14 | public void setVal (int val) { 15 | _val = val; 16 | } 17 | public String toString() { 18 | return "_val=" + _val; 19 | } 20 | 21 | public boolean equals (Object obj) { 22 | _val++; 23 | AAA a = (AAA) obj; 24 | return a._val == (_val - 1); 25 | } 26 | 27 | public boolean eq(AAA a) { 28 | return super.equals(a); 29 | } 30 | 31 | } //end of class AAA 32 | 33 | -------------------------------------------------------------------------------- /Year2022/March2022a74/Polymorphism/BBB.java: -------------------------------------------------------------------------------- 1 | package Poly2022a74; 2 | 3 | public class BBB extends AAA{ 4 | 5 | private String _st; 6 | public BBB() { 7 | _st = "bbb"; 8 | } 9 | public BBB(String st, int val) { 10 | super(val); 11 | _st = st; 12 | } 13 | public String getSt() { 14 | return _st; 15 | } 16 | 17 | public String toString(){ 18 | return "_val= " + getVal()+ " _st= "+_st; 19 | } 20 | 21 | public boolean equals (BBB other){ 22 | System.out.println("BBB equals"); 23 | return getVal() == other.getVal() && 24 | _st.equals(other.getSt()); 25 | } 26 | } //end of class BBB 27 | -------------------------------------------------------------------------------- /Year2022/March2022a74/Polymorphism/Driver.java: -------------------------------------------------------------------------------- 1 | package Poly2022a74; 2 | 3 | public class Driver { 4 | public static void main (String [] args) 5 | { 6 | AAA a = new AAA(5); 7 | BBB b = new BBB(); 8 | // System.out.println("Question 0:"); 9 | // System.out.println(a); 10 | // System.out.println(b); 11 | // System.out.println("Question 1 :"); 12 | // a = b; 13 | // System.out.println (a.equals(b)); 14 | 15 | // System.out.println("Question 2 :"); 16 | // System.out.println (b.equals(a)); 17 | 18 | // AAA a2 = new BBB(); 19 | // BBB b2 = new BBB("bbb", 2); 20 | // BBB b3 = new BBB(); 21 | 22 | // System.out.println("Question 3:"); 23 | // System.out.println (a2.equals(b3)); 24 | // System.out.println (b3.equals(a2)); 25 | // System.out.println (b2.equals(b3)); 26 | // System.out.println (b3.equals(b)); 27 | 28 | 29 | 30 | AAA a11 = new AAA(2); 31 | AAA a22 = new AAA(2); 32 | Object a33 = a11; 33 | 34 | // System.out.println (a11.equals(null)); 35 | System.out.println (a11.equals(a22)); 36 | System.out.println (a33.equals(a33)); 37 | System.out.println (a22.eq(a22)); 38 | // System.out.println (a11.eq(a33)); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Year2022/March2022a74/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2022; 2 | 3 | public class March2022b74 { 4 | 5 | 6 | public static void main(String[] args) { 7 | char[][] mat1 = {{'a','c','b','c','@','a'}, 8 | {'b','x','z','c','s','a'}, 9 | {'?','c','d','*','c','d'}, 10 | {'b','c','a','8','b','b'}, 11 | {'c','2','x','+','b','c'}}; 12 | System.out.println(lengthPath(mat1, "abc")); 13 | System.out.println(maxPath(mat1, "abc")); 14 | // int [] arr = {2,4,5,3,5,1}; 15 | // System.out.println(findDuplicate(arr)); 16 | // System.out.println(findDuplicateee(arr)); 17 | } 18 | 19 | 20 | 21 | public static int lengthPath (char[][] mat, String pattern) { 22 | return lengthPath(mat,pattern,0,0,0,0,1,0); 23 | } 24 | public static int lengthPath(char[][] mat, String pattern, int i, int j, int prevI,int privJ, int pathLength, int charIndex) { 25 | 26 | int maxPath = pathLength; 27 | char currentChar = pattern.charAt(charIndex); 28 | 29 | if (j > 0 && (j-1 != privJ || i != prevI) && mat[j-1][i] == currentChar) { 30 | int path = lengthPath(mat, pattern, i, j-1, i, j, pathLength+1, 0); 31 | maxPath = Math.max(maxPath, path); 32 | } 33 | if (i > 0 && (j != privJ && i-1 != prevI) && mat[j][i-1] == currentChar) { 34 | int path = lengthPath(mat, pattern, i-1, j, i, j, pathLength+1, 0); 35 | maxPath = Math.max(maxPath, path); 36 | } 37 | if (i < mat.length && (j+1 != privJ || i != prevI) && mat[j+1][i] == currentChar) { 38 | int path = lengthPath(mat, pattern, i, j+1, i, j, pathLength+1, 0); 39 | maxPath = Math.max(maxPath, path); 40 | } 41 | if (j < mat[j].length && (j != privJ || i+1 != prevI) && mat[j][i+1] == currentChar) { 42 | int path = lengthPath(mat, pattern, i+1, j, i, j, pathLength+1, 0); 43 | maxPath = Math.max(maxPath, path); 44 | } 45 | 46 | if (charIndex < pattern.length()-1) { 47 | int path = lengthPath(mat, pattern, i, j, prevI, privJ, pathLength,charIndex+1); 48 | maxPath = Math.max(maxPath, path); 49 | } 50 | return maxPath; 51 | } 52 | 53 | 54 | 55 | public static int maxPath(char[][] mat, String pattern) { 56 | return maxPath(mat, pattern, 0, 0, 0, 0); 57 | } 58 | 59 | private static int maxPath(char[][] mat, String pattern, int xIndex, int yIndex, int charIndex, int sum) { 60 | 61 | int pathSum = sum; 62 | char currentChar = pattern.charAt(charIndex); 63 | char element = mat[yIndex][xIndex]; 64 | 65 | if (currentChar == element) { 66 | int path = lengthPath(mat, pattern); 67 | pathSum = Math.max(pathSum, path); 68 | } 69 | if (charIndex < pattern.length() - 1) { 70 | int path = maxPath(mat, pattern, xIndex, yIndex, charIndex + 1, sum); 71 | pathSum = Math.max(pathSum, path); 72 | } else { 73 | if (xIndex < mat.length) { 74 | int path = maxPath(mat, pattern, xIndex + 1, yIndex, 0, sum); 75 | pathSum = Math.max(pathSum, path); 76 | } 77 | if (yIndex < mat[xIndex].length) { 78 | int path = maxPath(mat, pattern, 0, yIndex + 1, 0, sum); 79 | pathSum = Math.max(pathSum, path); 80 | } 81 | } 82 | 83 | return pathSum; 84 | } 85 | 86 | 87 | public static int findDuplicate(int[] a) 88 | { 89 | 90 | for (int i = 0; i < a.length; i++){ 91 | if (a[0] == a[a[0]]){ 92 | return a[0]; 93 | } 94 | // swap(0, a[0]); 95 | int temp = a[0]; 96 | a[0] = a[a[0]]; 97 | a[temp] = temp; 98 | } 99 | return 0; 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /Year2022/March2022a74/Trees/Driver.java: -------------------------------------------------------------------------------- 1 | package Trees2022a74; 2 | 3 | 4 | 5 | public class Driver { 6 | public static void main (String[] args){ 7 | Node bt = new Node(36); 8 | 9 | bt._rightSon = new Node(5); 10 | bt._rightSon._rightSon = new Node(35); 11 | 12 | bt._leftSon = new Node(7); 13 | bt._leftSon._leftSon = new Node(6); 14 | bt._leftSon._leftSon._leftSon = new Node(25); 15 | 16 | bt._leftSon._rightSon = new Node(29); 17 | Node t = bt._leftSon._rightSon._rightSon = new Node(10); 18 | 19 | Node t1 = bt._leftSon._rightSon._leftSon = new Node(8); 20 | bt._leftSon._rightSon._leftSon._rightSon = new Node(70); 21 | 22 | System.out.println(Node.why(bt, t)); 23 | System.out.println(Node.why(bt, t1)); 24 | System.out.println(Node.what(bt,t)); 25 | System.out.println(Node.what(bt,t1)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Year2022/March2022a74/Trees/Node.java: -------------------------------------------------------------------------------- 1 | package Trees2022a74; 2 | 3 | public class Node { 4 | 5 | public int _number; 6 | public Node _leftSon, _rightSon; 7 | public Node (int number) { 8 | _number = number; 9 | _leftSon = null; 10 | _rightSon = null; 11 | } 12 | public int getNumber() {return _number; } 13 | public Node getLeftSon() {return _leftSon; } 14 | public Node getRightSon() {return _rightSon; } 15 | 16 | public static Node why(Node root, Node t){ 17 | if (root==null || root.getLeftSon()==t || root.getRightSon()==t) 18 | return root; 19 | Node temp = why(root.getLeftSon(),t); 20 | if (temp==null) 21 | return why(root.getRightSon(),t); 22 | return temp; 23 | } 24 | 25 | public static int what(Node root, Node t) 26 | { 27 | if (root == null) 28 | return 0; 29 | if(root == why(root,t)) 30 | return 1; 31 | return 1+ what(root, why(root,t)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Year2022/March2022a89: -------------------------------------------------------------------------------- 1 | package Year2022; 2 | 3 | public class March2022a89 { 4 | public static void main(String[] args) { 5 | // System.out.println(isJump("adbrcfa","abc",2)); 6 | // System.out.println(isJump("cbdadbrcfa","abc",2)); 7 | // System.out.println(isJump("adcfbaagcxabcd","abc",4)); 8 | // System.out.println(isJump("adcfbaagcxabcd","abc",1)); 9 | // System.out.println(strStep("adbrcfa","abc")); 10 | // System.out.println(strStep("cbdadbrcfa","abc")); 11 | System.out.println(strStep("adcfbaagcxabcd","abc")); 12 | // System.out.println(strStep("abcfbaagcxabcd","abc")); 13 | // int [] a = {-1,1,-1,-5,2,2}; 14 | // int [] aa = {3,3,2,-7,2,1,1,-2,-2}; 15 | // int [] b = {1,2,3,4,5,4}; 16 | // int [] bb = {1,-2,3,-4,-5,4,2,-4,6,-2}; 17 | // System.out.println(longestSubArray(a)); 18 | //// System.out.println(longestSubArray(aa)); 19 | //// System.out.println(longestSubArray(b)); 20 | //// System.out.println(longestSubArray(bb)); 21 | } 22 | 23 | public static boolean isJump (String str1, String str2, int step) { 24 | if(str1.length() < str2.length()) 25 | return false; 26 | return isJump(str1,str2,step,0); 27 | } 28 | private static boolean isJump (String str1, String str2, int step,int stepsLeft) { 29 | if(str2.length() == 0) 30 | return true; 31 | if(str1.length() == 0) 32 | return false; 33 | if(stepsLeft == 0 && str1.charAt(0) != str2.charAt(0)) 34 | return false; 35 | if(stepsLeft == 0 && str1.charAt(0) == str2.charAt(0)) 36 | return isJump(str1.substring(1),str2.substring(1),step,step-1); 37 | return isJump(str1.substring(1),str2,step,stepsLeft-1); 38 | } 39 | 40 | public static int strStep(String str1,String str2) 41 | { 42 | return strStep(str1,str2,1); 43 | } 44 | private static int strStep(String str1,String str2,int step) { 45 | if(step > str1.length() || str1.charAt(0) != str2.charAt(0)) 46 | return -1; 47 | if (isJump(str1,str2,step)) 48 | return step; 49 | return strStep(str1,str2,step+1); 50 | } 51 | 52 | public static int longestSubArray(int[] a) { 53 | int start = 0; 54 | int end = 0; 55 | int startMax = 0; 56 | int endMax = 0; 57 | int arrMax = Integer.MIN_VALUE; 58 | boolean positive; 59 | if(a[0] > 0) 60 | positive=true; 61 | else 62 | positive=false; 63 | while(end < a.length-1) { 64 | if(a[end] > 0 && positive || a[end] < 0 && !positive) { // if number is positive or negative just move forward 65 | System.out.println(a[end]); 66 | end++; 67 | }else{ 68 | if(end-start > arrMax) { 69 | arrMax = end-start; 70 | startMax = start; 71 | endMax = end; 72 | } 73 | start = end; 74 | } 75 | positive =! positive; 76 | } 77 | if (end-start > arrMax) { 78 | System.out.println("Starting Index = " + start + " Ending Index = " + (end-1)); 79 | return end-start; 80 | } 81 | System.out.println("Starting Index = " + startMax + " Ending Index = " + (endMax-1)); 82 | return arrMax; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Year2023/February2023a65/BinaryTree/Driver.java: -------------------------------------------------------------------------------- 1 | package Trees2023; 2 | 3 | public class Driver { 4 | public static void main (String[] args){ 5 | 6 | Node bt = new Node(60); 7 | 8 | bt._rightSon = new Node(40); 9 | 10 | bt._leftSon = new Node(20); 11 | bt._leftSon._leftSon = new Node(10); 12 | bt._leftSon._leftSon._leftSon = new Node(15); 13 | 14 | bt._leftSon._rightSon = new Node(30); 15 | bt._leftSon._rightSon._rightSon = new Node(5); 16 | 17 | bt._leftSon._rightSon._leftSon = new Node(10); 18 | bt._leftSon._rightSon._leftSon._rightSon = new Node(30); 19 | 20 | // System.out.println(Node.what(bt)); 21 | System.out.println(Node.what1(bt)); 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Year2023/February2023a65/BinaryTree/Node.java: -------------------------------------------------------------------------------- 1 | package Trees2023; 2 | 3 | public class Node { 4 | private int _number; 5 | public Node _leftSon, _rightSon; 6 | public Node (int number) 7 | { 8 | _number = number; 9 | _leftSon = null; 10 | _rightSon = null; 11 | } 12 | public int getNumber() {return _number; } 13 | public Node getLeftSon() {return _leftSon; } 14 | public Node getRightSon() {return _rightSon; } 15 | public void setNumber(int num) { _number = num; } 16 | public void setLeftSon(Node node) { _leftSon = node; } 17 | public void setRightSon(Node node) { _rightSon = node; } 18 | 19 | 20 | public static int what(Node root) 21 | { 22 | return what(root, 1); 23 | } 24 | 25 | private static int what(Node root, int op){ 26 | if (root == null) 27 | if (op == 0) 28 | return Integer.MIN_VALUE; 29 | else 30 | return Integer.MAX_VALUE; 31 | if (root.getLeftSon() == null && root.getRightSon() == null) 32 | return root.getNumber(); 33 | if (op == 1) 34 | return Math.max(what(root.getLeftSon(), 0),what(root.getRightSon(), 0)); 35 | else 36 | return Math.min(what(root.getLeftSon(), 1),what(root.getRightSon(), 1)); 37 | } 38 | 39 | public static int what1(Node root) 40 | { 41 | return what1(root, 1); 42 | } 43 | private static int what1(Node root, int op){ 44 | if (root == null) 45 | if (op == 0) 46 | return Integer.MAX_VALUE; 47 | else 48 | return Integer.MIN_VALUE; 49 | if (root.getLeftSon() == null && root.getRightSon() == null) 50 | return root.getNumber(); 51 | if (op == 1) { 52 | return Math.min(what1(root.getLeftSon(), 0),what1(root.getRightSon(), 0)); 53 | } 54 | else { 55 | return Math.max(what1(root.getLeftSon(), 1),what1(root.getRightSon(), 1)); 56 | } 57 | } 58 | 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /Year2023/February2023a65/Feburary2023a64.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MorielHarush/OpenUJava20441/c11c54f94d3842a36c8413d49b07119c6dd79614/Year2023/February2023a65/Feburary2023a64.pdf -------------------------------------------------------------------------------- /Year2023/February2023a65/Lists/Driver.java: -------------------------------------------------------------------------------- 1 | package Lists; 2 | 3 | public class Driver { 4 | 5 | public static void main(String[] args) { 6 | // IntNode node6 = new IntNode(6); 7 | // IntNode node5 = new IntNode(5, node6); 8 | // IntNode node4 = new IntNode(4, node5); 9 | // IntNode node3 = new IntNode(3, node4); 10 | // IntNode node2 = new IntNode(2, node3); 11 | // IntNode node1 = new IntNode(1, node2); 12 | // 13 | IntNode node4 = new IntNode(1); 14 | IntNode node3 = new IntNode(2, node4); 15 | IntNode node2 = new IntNode(1, node3); 16 | IntNode node1 = new IntNode(2, node2); 17 | IntNode.IntList list = node1.new IntList(node1); 18 | IntNode first = list._head; 19 | while (first != null) { 20 | System.out.print(first.getValue() + " "); 21 | first = first.getNext(); 22 | } 23 | 24 | System.out.println(""); 25 | IntNode.IntList secretList = list.secret(); 26 | 27 | IntNode current = secretList._head; 28 | while (current != null) { 29 | System.out.print(current.getValue() + " "); 30 | current = current.getNext(); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Year2023/February2023a65/Lists/IntNode.java: -------------------------------------------------------------------------------- 1 | package Lists; 2 | 3 | public class IntNode 4 | { 5 | public int _value; 6 | public IntNode _next; 7 | public IntNode(int val, IntNode n) { 8 | _value = val; 9 | _next = n; 10 | } 11 | public IntNode(int val) { 12 | _value = val; 13 | _next = null; 14 | } 15 | public IntNode getNext( ) { return _next; } 16 | public void setNext(IntNode node) { _next = node; } 17 | public int getValue() { return _value; } 18 | public void setValue(int v) { _value = v; } 19 | 20 | public class IntList 21 | { 22 | public IntNode _head; 23 | public IntList(IntNode node) { 24 | _head = node; 25 | } 26 | 27 | public IntList secret(){ 28 | IntNode p; 29 | IntNode temp, pTemp; 30 | IntNode i, j, k, l, ptr=null; 31 | 32 | temp = _head.getNext(); 33 | pTemp = _head; 34 | 35 | while (temp != null){ 36 | IntNode x = temp.getNext(); 37 | if (temp.getValue() %2 != 0){ 38 | pTemp.setNext(x); 39 | temp.setNext(_head); 40 | _head = temp; 41 | } 42 | else 43 | pTemp = temp; 44 | temp = x; 45 | } 46 | 47 | temp = _head.getNext(); 48 | pTemp = _head; 49 | while (temp != null && temp.getValue() % 2 != 0) 50 | { 51 | pTemp = temp; 52 | temp = temp.getNext(); 53 | } 54 | p = temp; 55 | pTemp.setNext(null); 56 | i = _head; 57 | j = p; 58 | while (j != null && i != null) 59 | { 60 | k = i.getNext(); 61 | l = j.getNext(); 62 | i.setNext(j); 63 | j.setNext(k); 64 | ptr = j; 65 | i = k; 66 | j = l; 67 | } 68 | if (i == null) 69 | ptr.setNext(j); 70 | 71 | return new IntList(_head); 72 | } 73 | 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Year2023/February2023a65/RecursionComplexity.java: -------------------------------------------------------------------------------- 1 | package Year2023; 2 | 3 | public class February2023a65 { 4 | public static void main (String[] args){ 5 | // System.out.println(countPairs(3)); 6 | int a [] = {2,3,8,27}; 7 | System.out.println(superInc(a, 13)); 8 | System.out.println(superInc(a, 30)); 9 | System.out.println(superInc(a, 7)); 10 | System.out.println(superInc(a, 9)); 11 | System.out.println(superInc(a, 10)); 12 | } 13 | 14 | public static int countPairs (int n) { 15 | return countPairs(n, 0, 0, ""); 16 | } 17 | 18 | private static int countPairs (int n, int open,int close, String str) { 19 | if(open < close || n < 0 ) { 20 | return 0; 21 | } 22 | if(n == 0 && open == close) { 23 | System.out.println(str); 24 | return 1; 25 | } 26 | int openR = countPairs(n-1,open+1,close,str+"("); 27 | int closeR = countPairs(n,open,close+1,str+")"); 28 | return openR + closeR; 29 | } 30 | 31 | 32 | // 2,3,8,27. k = 30 33 | public static boolean superInc (int [] arr, int k) { 34 | for(int i = arr.length-1; i >= 0; i--) { 35 | if(arr[i] <= k) 36 | k-= arr[i]; 37 | if(k == 0) 38 | return true; 39 | } 40 | return false; 41 | } 42 | 43 | 44 | 45 | } 46 | 47 | --------------------------------------------------------------------------------