From e86f5c1057806849a339a42a5e493538c6412776 Mon Sep 17 00:00:00 2001 From: Vasu Kansal Date: Sun, 18 Sep 2022 18:35:19 +0530 Subject: [PATCH 1/2] Greater Alphabet from array --- src/pkg1/Vasu.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/pkg1/Vasu.java diff --git a/src/pkg1/Vasu.java b/src/pkg1/Vasu.java new file mode 100644 index 0000000..af1caba --- /dev/null +++ b/src/pkg1/Vasu.java @@ -0,0 +1,68 @@ +package pkg1; + +// Get the greater alphabat from the array than the one which is input and if it is +//the gratest as compared to array then pront the first indexed element of array +import java.util.Scanner; +import java.util.Arrays; + +public class Vasu { + public static char nextGreatestLetter(char[] arr, char target) { + int[] arrint = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + arrint[i] = (int) arr[i]; + } + sort(arrint); + char anschar; + if (target > arrint[arrint.length - 1]) { + anschar = (char) arrint[0]; + return anschar; + } + int middle; + int start = 0; + int end = arr.length; + while (start <= end) { + middle = start + ((end - start) / 2); + if (target < arr[middle]) { + end = middle - 1; + } else if (target > arr[middle]) { + start = middle + 1; + } + } + anschar = (char) arr[start]; + return anschar; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the size of the array - "); + int size; + size = sc.nextInt(); + + char[] letters = new char[size]; + System.out.print("Enter the elements Continously - "); + letters = sc.next().toCharArray(); + System.out.println(Arrays.toString(letters)); + + char target; + System.out.print("Enter the target - "); + target = sc.next().charAt(0); + + char ans = nextGreatestLetter(letters, target); + System.out.println("Answer - " + ans); + sc.close(); + + } + + static void sort(int[] arr) { + for (int i = 0; i < arr.length; i++) { + int temp = arr[i]; + for (int j = i + 1; j < arr.length; j++) { + if (temp > arr[j]) { + temp = arr[j]; + arr[j] = arr[i]; + arr[i] = temp; + } + } + } + } +} \ No newline at end of file From 4cb7511dae6f75004b2c8f05c949188b30c59250 Mon Sep 17 00:00:00 2001 From: Vasu Kansal Date: Sun, 18 Sep 2022 19:01:30 +0530 Subject: [PATCH 2/2] Vasu commit --- src/pkg1/MasterClass.java | 5 ++- src/pkg1/Vasu.java | 66 ++++++--------------------------------- 2 files changed, 14 insertions(+), 57 deletions(-) diff --git a/src/pkg1/MasterClass.java b/src/pkg1/MasterClass.java index f4b3586..a1bc017 100644 --- a/src/pkg1/MasterClass.java +++ b/src/pkg1/MasterClass.java @@ -1,9 +1,12 @@ package pkg1; + public class MasterClass { - public static void main(String[] args) { + public static void main(String[] args) { Hitesh_Class hks = new Hitesh_Class(); hks.sayHello(); abcd ob2 = new abcd(); ob2.f1(); + Vasu v1 = new Vasu(); + v1._500094156(); } } diff --git a/src/pkg1/Vasu.java b/src/pkg1/Vasu.java index af1caba..f9cbe50 100644 --- a/src/pkg1/Vasu.java +++ b/src/pkg1/Vasu.java @@ -1,68 +1,22 @@ package pkg1; -// Get the greater alphabat from the array than the one which is input and if it is -//the gratest as compared to array then pront the first indexed element of array -import java.util.Scanner; -import java.util.Arrays; - +//Searching in 2d array public class Vasu { - public static char nextGreatestLetter(char[] arr, char target) { - int[] arrint = new int[arr.length]; - for (int i = 0; i < arr.length; i++) { - arrint[i] = (int) arr[i]; - } - sort(arrint); - char anschar; - if (target > arrint[arrint.length - 1]) { - anschar = (char) arrint[0]; - return anschar; - } - int middle; - int start = 0; - int end = arr.length; - while (start <= end) { - middle = start + ((end - start) / 2); - if (target < arr[middle]) { - end = middle - 1; - } else if (target > arr[middle]) { - start = middle + 1; - } - } - anschar = (char) arr[start]; - return anschar; - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.print("Enter the size of the array - "); - int size; - size = sc.nextInt(); - - char[] letters = new char[size]; - System.out.print("Enter the elements Continously - "); - letters = sc.next().toCharArray(); - System.out.println(Arrays.toString(letters)); - - char target; - System.out.print("Enter the target - "); - target = sc.next().charAt(0); - - char ans = nextGreatestLetter(letters, target); + public void _500094156() { + int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; + int target = 5; + boolean ans = search(arr, target); System.out.println("Answer - " + ans); - sc.close(); - } - static void sort(int[] arr) { + static boolean search(int[][] arr, int target) { for (int i = 0; i < arr.length; i++) { - int temp = arr[i]; - for (int j = i + 1; j < arr.length; j++) { - if (temp > arr[j]) { - temp = arr[j]; - arr[j] = arr[i]; - arr[i] = temp; + for (int j = 0; j < arr[i].length; j++) { + if (target == arr[i][j]) { + return true; } } } + return false; } } \ No newline at end of file