|
| 1 | +//Array contains only one type of DATA TYPE of Elements |
| 2 | +// arr[] = {1,2,3,4,5}; |
| 3 | +// inr arr[] = new int[size]; |
| 4 | + |
| 5 | +// 1.Large Element in an Array |
| 6 | +/* |
| 7 | +public class Array_One_Short { |
| 8 | + public static void is_Large(int arr[]){ |
| 9 | + int largest = arr[0]; |
| 10 | + for (int i =1; i <=arr.length-1; i++) { |
| 11 | + if(arr[i] > largest) |
| 12 | + largest = arr[i]; |
| 13 | + } |
| 14 | + System.out.println("largest Element: "+largest); |
| 15 | + } |
| 16 | +
|
| 17 | + public static void main(String[] args) { |
| 18 | + int arr[] = {3,2,1,5,2}; |
| 19 | + is_Large(arr); |
| 20 | + } |
| 21 | +} |
| 22 | +*/ |
| 23 | +// largest Element: 5 |
| 24 | + |
| 25 | +// 2. 2nd Large element |
| 26 | +/* |
| 27 | +public class Array_One_Short { |
| 28 | + public static int second_largest(int arr[]){ |
| 29 | + int largest = arr[0]; |
| 30 | + int sec_large = -1; |
| 31 | + for (int i =1; i < arr.length; i++) { |
| 32 | + if(arr[i] > largest){ |
| 33 | + sec_large = largest; |
| 34 | + largest = arr[i]; |
| 35 | + } |
| 36 | + else if(arr[i] < largest && arr[i] > sec_large){ |
| 37 | + sec_large = arr[i]; |
| 38 | + } |
| 39 | + } |
| 40 | + System.out.println("Second large element: "+sec_large); |
| 41 | + return sec_large; |
| 42 | + } |
| 43 | +
|
| 44 | + public static void main(String[] args) { |
| 45 | + int arr[] = {3,2,1,7,7,5}; |
| 46 | + second_largest(arr); |
| 47 | + } |
| 48 | +} |
| 49 | + */ |
| 50 | +// Second large element: 5 |
| 51 | + |
| 52 | + |
| 53 | +// 3.Second Smallest element |
| 54 | +/* |
| 55 | +public class Array_One_Short { |
| 56 | + public static int sec_small(int arr[]) { |
| 57 | + int smallest = arr[0]; |
| 58 | + int sec_smallest = Integer.MAX_VALUE; |
| 59 | + for (int i = 0; i < arr.length; i++) { |
| 60 | + if (arr[i] < smallest) { |
| 61 | + sec_smallest = smallest; |
| 62 | + smallest = arr[i]; |
| 63 | + } else if (arr[i] != smallest && arr[i] < sec_smallest) { |
| 64 | + sec_smallest = arr[i]; |
| 65 | + } |
| 66 | + } |
| 67 | + System.out.println("Second Smallest: " + sec_smallest); |
| 68 | + return sec_smallest; |
| 69 | + } |
| 70 | +
|
| 71 | + public static void main(String[] args) { |
| 72 | + int arr[] = {2, 3, 5, 6, 6, 8, 2}; |
| 73 | + sec_small(arr); |
| 74 | + } |
| 75 | +} |
| 76 | + */ |
| 77 | + |
| 78 | + |
| 79 | +// 4. Check Array is sorted or Not |
| 80 | +/* |
| 81 | +public class Array_One_Short { |
| 82 | + public static boolean sort_or_not(int arr[]){ |
| 83 | + for (int i =1; i <arr.length; i++) { |
| 84 | + if(arr[i] < arr[i-1]){ |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + return true; |
| 89 | + } |
| 90 | +
|
| 91 | + public static void main(String[] args) { |
| 92 | + int arr[] = {1,2,3,45}; |
| 93 | + if (sort_or_not(arr)){ |
| 94 | + System.out.println("Sorted"); |
| 95 | + }else{ |
| 96 | + System.out.println("Not"); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +*/ |
| 101 | + |
| 102 | + |
| 103 | +// 4. Remove duplicate from sorted Array |
| 104 | +/* |
| 105 | +class Solution { |
| 106 | + public static int removeDuplicates(int[] nums) { |
| 107 | + int i=0; |
| 108 | + for(int j=0; j<nums.length; j++){ |
| 109 | + if(nums[j] != nums[i]) |
| 110 | + { |
| 111 | + nums[i+1] = nums[j]; |
| 112 | + i++; |
| 113 | + } |
| 114 | + } |
| 115 | + return (i+1); |
| 116 | + } |
| 117 | +} |
| 118 | +*/ |
| 119 | + |
| 120 | +// 5.Left Rotate by 1 Element |
| 121 | +/* |
| 122 | +public class Array_One_Short { |
| 123 | + public static void one_ele(int arr[]){ |
| 124 | + int temp = arr[0]; |
| 125 | + for (int i =1; i < arr.length; i++) { |
| 126 | + arr[i-1] = arr[i]; |
| 127 | + } |
| 128 | + arr[arr.length-1] = temp; |
| 129 | + } |
| 130 | +
|
| 131 | + public static void main(String[] args) { |
| 132 | + int arr[] = {1,2,3,4,5}; |
| 133 | + one_ele(arr); |
| 134 | + for (int i =0; i < arr.length ; i++) { |
| 135 | + System.out.print(arr[i]+" "); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +*/ |
| 140 | + |
| 141 | +// 6.Left Rotate by N Element TC = O(2N) SC= O(1) |
| 142 | +/* |
| 143 | +public class Array_One_Short { |
| 144 | + public static void rotate_n_num(int arr[], int k){ |
| 145 | + int n = arr.length; |
| 146 | + k = k % n; |
| 147 | +
|
| 148 | +// declare temp array |
| 149 | + int temp[] = new int[k]; |
| 150 | +
|
| 151 | + for (int i = 0; i < k; i++) { |
| 152 | + temp[i] = arr[i]; |
| 153 | + } |
| 154 | + for (int i = k; i < n; i++) { |
| 155 | + arr[i-k] = arr[i]; |
| 156 | + } |
| 157 | + for (int i = n - k; i < n; i++) { |
| 158 | + arr[i] = temp[i - (n - k)]; |
| 159 | + } |
| 160 | + } |
| 161 | +
|
| 162 | + public static void main(String[] args) { |
| 163 | + int k=3; |
| 164 | + int arr[] = {1,2,3,4,5,6}; // 4 5 6 1 2 3 |
| 165 | + rotate_n_num(arr,k); |
| 166 | + for (int i = 0; i < arr.length; i++) { |
| 167 | + System.out.print(arr[i]+" "); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | +*/ |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +// 6.Left Rotate by N Element |
| 176 | +/* |
| 177 | +class Solution { |
| 178 | + public void rotate(int[] nums, int k) { |
| 179 | + k = k % nums.length; |
| 180 | + reverse(nums, 0, nums.length - 1); |
| 181 | + reverse(nums, 0, k - 1); |
| 182 | + reverse(nums, k, nums.length - 1); |
| 183 | + } |
| 184 | +
|
| 185 | + public void reverse(int[] nums, int start, int end){ |
| 186 | + while( start < end){ |
| 187 | + int temp = nums[start]; |
| 188 | + nums[start] = nums[end]; |
| 189 | + nums[end] = temp; |
| 190 | + start++; |
| 191 | + end--; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + */ |
| 196 | + |
| 197 | + |
| 198 | +// 7.MOVE ALL ZERO TO END |
| 199 | +/* |
| 200 | +public class Array_One_Short { |
| 201 | + public static void move_zero_last(int arr[]){ |
| 202 | + int i = 0; |
| 203 | + for(int num : arr){ |
| 204 | + if(num != 0){ |
| 205 | + arr[i++] = num; |
| 206 | + } |
| 207 | + } |
| 208 | + while(i < arr.length){ |
| 209 | + arr[i++] = 0; |
| 210 | + } |
| 211 | + } |
| 212 | + public static void main(String[] args) { |
| 213 | + int arr[] = {1,0,2,4,5,0,0,3,0,4}; |
| 214 | + move_zero_last(arr); |
| 215 | + for (int i = 0; i < arr.length ; i++) { // 1 2 4 5 3 4 0 0 0 0 |
| 216 | + System.out.print(arr[i]+" "); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + */ |
| 221 | + |
| 222 | + |
| 223 | +// 8.Linear Search |
| 224 | +/* |
| 225 | +public class Array_One_Short { |
| 226 | + public static int linear_search(int arr[], int key){ |
| 227 | + for (int i = 0; i < arr.length; i++) { |
| 228 | + if(arr[i] == key){ |
| 229 | + return i; |
| 230 | + } |
| 231 | + } |
| 232 | + return -1; |
| 233 | + } |
| 234 | +
|
| 235 | + public static void main(String[] args) { |
| 236 | + int arr[] = {1,2,3,4,5,6,7,10}; |
| 237 | + int key = 6; |
| 238 | + int output = linear_search(arr,key); |
| 239 | +
|
| 240 | + if(output != -1){ |
| 241 | + System.out.println("Idx: "+output); |
| 242 | + }else{ |
| 243 | + System.out.println("Not Found"); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
| 247 | +// Idx: 5 |
| 248 | + */ |
| 249 | + |
| 250 | + |
| 251 | +// 9. Union of 2 sorted array |
| 252 | + |
| 253 | +import java.util.*; |
| 254 | + |
| 255 | +public class Basic { |
| 256 | + public static void main(String[] args) { |
| 257 | + int arr1[] = {1, 2, 3, 4, 5}; |
| 258 | + int arr2[] = {4, 5, 6, 7, 8}; |
| 259 | + |
| 260 | + Set<Integer> set = new HashSet<>(); |
| 261 | + for (int num : arr1) { |
| 262 | + set.add(num); |
| 263 | + } |
| 264 | + for (int num : arr2) { |
| 265 | + set.add(num); |
| 266 | + } |
| 267 | + |
| 268 | + System.out.println("Union of two arrays is: " + set); // Union of two arrays is: [1, 2, 3, 4, 5, 6, 7, 8] |
| 269 | + } |
| 270 | +} |
| 271 | + |
0 commit comments