diff --git a/DSA/JAVA/day 07/ArrayListExample.java b/DSA/JAVA/day 07/ArrayListExample.java new file mode 100644 index 0000000..6bbccff --- /dev/null +++ b/DSA/JAVA/day 07/ArrayListExample.java @@ -0,0 +1,51 @@ +// ArrayList is a part of Java's Collection Framework and is dynamic in size +// It's found in the java.util package + +import java.util.ArrayList; + +public class ArrayListExample { + public static void main(String[] args) { + + // Create an ArrayList of strings + ArrayList list = new ArrayList(); + + // Add elements to the ArrayList + list.add("apple"); + list.add("banana"); + list.add("cherry"); + + // Add element at specific index + list.add(1, "mango"); // Inserts "mango" at index 1 + + // Print the ArrayList + System.out.println("ArrayList after adding elements: " + list); + + // Get an element at a specific index + String fruit = list.get(2); // Gets element at index 2 + System.out.println("Element at index 2: " + fruit); + + // Modify an element + list.set(0, "orange"); // Replaces "apple" with "orange" + System.out.println("ArrayList after modifying index 0: " + list); + + // Remove an element by index + list.remove(3); // Removes the element at index 3 + System.out.println("ArrayList after removing index 3: " + list); + + // Remove an element by value + list.remove("banana"); // Removes "banana" from the list + System.out.println("ArrayList after removing 'banana': " + list); + + // Check if the list contains an element + boolean containsCherry = list.contains("cherry"); + System.out.println("Does the list contain 'cherry'? " + containsCherry); + + // Size of the ArrayList + int size = list.size(); + System.out.println("Size of ArrayList: " + size); + + // Clear all elements + list.clear(); + System.out.println("ArrayList after clearing all elements: " + list); + } +} diff --git a/DSA/JAVA/day 07/QuickSort.java b/DSA/JAVA/day 07/QuickSort.java index 09300cc..765d8af 100644 --- a/DSA/JAVA/day 07/QuickSort.java +++ b/DSA/JAVA/day 07/QuickSort.java @@ -1,3 +1,5 @@ +import java.util.Arrays; + class Quick { // Method to perform QuickSort on a given array within the specified range (low to high) public static void qSort(int[] arr, int low, int high) { @@ -58,8 +60,6 @@ public static void main(String[] args) { Quick.qSort(arr, 0, arr.length - 1); // Print the sorted array - for (int i = 0; i < arr.length; i++) { - System.out.print(arr[i] + " "); - } + System.out.println(Arrays.toString(arr)); } } diff --git a/DSA/JAVA/day 08/Even.java b/DSA/JAVA/day 08/Even.java new file mode 100644 index 0000000..18da9d8 --- /dev/null +++ b/DSA/JAVA/day 08/Even.java @@ -0,0 +1,21 @@ +//create a arraylist of natural numbers and then create a method to return the arraylist of even numbers +import java.util.ArrayList; +public class Even { + static ArrayList even(ArrayList list){ + ArrayList evenList = new ArrayList(); + for(int i = 0; i < list.size(); i++){ + if(list.get(i) % 2 == 0){ + evenList.add(list.get(i)); + } + } + return evenList; + } + public static void main(String[] args) { + ArrayList list = new ArrayList(); + for(int i = 1; i<= 20; i++){ + list.add(i); + } + System.out.println(list); + System.out.println(even(list)); + } +} diff --git a/DSA/JAVA/day 08/Friend.java b/DSA/JAVA/day 08/Friend.java new file mode 100644 index 0000000..393c476 --- /dev/null +++ b/DSA/JAVA/day 08/Friend.java @@ -0,0 +1,28 @@ +import java.util.ArrayList; +import java.util.Scanner; +public class Friend { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + ArrayList Friends = new ArrayList(); + + System.out.println("Enter the name of friends: "); + boolean isContinue = true; + while (isContinue) { + String name = sc.nextLine(); + if (name.equals("exit")) { + isContinue = false; + } else { + Friends.add(name); + System.out.println("Enter new friend name(type exit to end process): "); + } + } + + System.out.println(Friends); + //removing last element + Friends.remove(Friends.size()-1); + System.out.println(Friends); + + //finding best friend whoever comes at 0th index + System.out.println(Friends.get(0)); + } +} diff --git a/DSA/JAVA/day 08/HashMapExample.java b/DSA/JAVA/day 08/HashMapExample.java new file mode 100644 index 0000000..a8dd3ba --- /dev/null +++ b/DSA/JAVA/day 08/HashMapExample.java @@ -0,0 +1,73 @@ +/* +- stores data in the key value pair + +- syntax: + HashMap map = new HashMap<>(); + +- methods: +mapName.put(key, value): adds a new key-value pair to the map or updates the value associated with the given key + +mapName.get(key): returns the value associated with the given key + +mapName.remove(key): removes the key-value pair associated with the given key + +mapName.size(): returns the number of key-value pairs in the map + +mapName.isEmpty(): returns true if the map is empty, false otherwise + +mapName.clear(): removes all key-value pairs from the map + +mapName.containsKey(key): returns true if the map contains the given key, false otherwise + +mapName.containsValue(value): returns true if the map contains the given value, false otherwise + +mapName.keySet(): returns a set of all the keys in the map + +mapName.values(): returns a set of all the values in the map + +mapName.entrySet(): returns a set of all the key-value pairs in the map + +mapName.toString(): returns a string representation of the map +*/ + +import java.util.*; + +public class HashMapExample { // Renamed class + public static void main(String[] args) { + + HashMap map = new HashMap<>(); + map.put("John", 30); + map.put("Jane", 25); + map.put("Bob", 40); + map.put("Alice", 35); + System.out.println(map); + + System.out.println(map.get("John")); + + map.remove("Bob"); + System.out.println(map); + + System.out.println(map.size()); + + System.out.println(map.isEmpty()); + + System.out.println(map); + + System.out.println(map.containsKey("John")); + + System.out.println(map.containsValue(25)); + + System.out.println(map.keySet()); + + System.out.println(map.values()); + + System.out.println(map.entrySet()); + + System.out.println(map.toString()); + + map.clear(); + + System.out.println(map); + } +} + diff --git a/DSA/JAVA/day 08/MostFreq.java b/DSA/JAVA/day 08/MostFreq.java new file mode 100644 index 0000000..e7e6c3c --- /dev/null +++ b/DSA/JAVA/day 08/MostFreq.java @@ -0,0 +1,24 @@ +import java.util.HashMap; + +//given a array of name s find which name is coming most frequently +public class MostFreq { + public static void main(String[] args) { + String[] names ={"yug" , "om", "anvita", "yug", "kanishka", "yug", "yug", "anvita"}; + HashMap map = new HashMap<>(); + for(String i:names){ + System.out.println(map); + map.put(i, map.getOrDefault(i, 0) + 1); + System.out.println(map); + } + System.out.println(map); + int max = 0; + String ans = ""; + for(String i:map.keySet()){ + if(map.get(i) > max){ + max = map.get(i); + ans = i; + } + } + System.out.println(ans); + } +} diff --git a/DSA/JAVA/day 08/Num.java b/DSA/JAVA/day 08/Num.java new file mode 100644 index 0000000..0c22e4d --- /dev/null +++ b/DSA/JAVA/day 08/Num.java @@ -0,0 +1,24 @@ +import java.util.Arrays; +import java.util.HashSet; + +public class Num { + public static void main(String[] args) { + int[] arr = {1,1,2,3,2,2,1,1,3}; + HashSet unique = new HashSet<>(); + for(int i:arr){ + unique.add(i); + } + int res[] = new int[unique.size()]; + int ip=0; + for(int i:unique){ + res[ip++] = i; + } + System.out.println(Arrays.toString(res)); + } +} + +//you given a 2D array of integers that have multiple natural numbers now your task is to find an 1D array which has the max frequent element from 2D array. +//Example: +//Input: +//2D array = {{1,2,3,1},{2,3,1,2},{3,1,2,3}} +//Output: {3,1,2,3} diff --git a/DSA/JAVA/day 09/StringMethod.java b/DSA/JAVA/day 09/StringMethod.java new file mode 100644 index 0000000..068dd2d --- /dev/null +++ b/DSA/JAVA/day 09/StringMethod.java @@ -0,0 +1,55 @@ +//String pool in java: +//if you make many strings with the same value then the memory is allocated only once and it is stored in the string pool. +//multiple variable with the same input string will not take the new memory location. + +//string builder: +/* +mutable sequence of characters. +syntax: +StringBuilder str = new StringBuilder(""); +*/ + +//string buffer: +/* +mutable sequence of characters. +syntax: +StringBuffer str = new StringBuffer(""); +*/ +//string methods: +/* +.length()->returns the length of the string +.charAt(index)->returns the character at the specified index +.toUpperCase()->converts the string to uppercase +.toLowerCase()->converts the string to lowercase +.equals()->compares two strings +.concat()->concatenates two strings +.toCharArray()->converts the string to a char array +*/ + +//string builder methods: +/* +.toString()->convert the string builder to string +.append()->appends the string, adds string to last +.delete()->deletes the character at the specified index +.reverse()->reverses the string +*/ +public class StringMethod { + public static void main(String[] args) { + String str = "Hello"; + System.out.println(str); + // str.concat("world"); + System.out.println(str.concat(" world")); + System.out.println(); + + StringBuilder sb = new StringBuilder("Hello"); + System.out.println(sb); + sb.append(" world"); + System.out.println(sb); + System.out.println(); + + StringBuffer sf = new StringBuffer("Hello"); + System.out.println(sf); + sf.append(" world"); + System.out.println(sf); + } +} diff --git a/DSA/JAVA/day 09/StringQuestion.java b/DSA/JAVA/day 09/StringQuestion.java new file mode 100644 index 0000000..9a2f49d --- /dev/null +++ b/DSA/JAVA/day 09/StringQuestion.java @@ -0,0 +1,25 @@ +//question1: create a string which stores a names of a person and create a method which adds last name in original string +//question2: write a java program to reverse a string + +class AddName{ + static String add(String Fname,String LName){ + return Fname+" "+LName; + } +} + +class RevStr{ + static String rev(String str){ + return new StringBuilder(str).reverse().toString(); + } +} + +public class StringQuestion { + public static void main(String[] args) { + String Fname="Yug"; + String LName="Ramoliya"; + System.out.println(AddName.add(Fname,LName)); + + String str="Yug Ramoliya"; + System.out.println(RevStr.rev(str)); + } +} diff --git a/LeetCode.java b/LeetCode.java index 491d4d1..fa8499b 100644 --- a/LeetCode.java +++ b/LeetCode.java @@ -1,35 +1,24 @@ -//Relative Sort Array +class Solution { + public int maxSubArray(int[] nums) { + int maxSum = nums[0]; + int currentSum = nums[0]; -class RelSort { - public static int[] RelativeSort(int[] arr1, int[] arr2) { - int[] fr = new int[1001]; - int index = 0; - for(int i = 0; i < arr1.length; i++) { - fr[arr1[i]]++; - } - for(int a:arr2) { - while(fr[a] > 0) { - arr1[index++] = a; - fr[a]--; - } - } - for (int i = 0; i < fr.length; i++) { - while (fr[i] > 0) { - arr1[index++] = i; - fr[i]--; - } - } - return arr1; + for (int i = 1; i < nums.length; i++) { + currentSum = Math.max(nums[i], currentSum + nums[i]); + + maxSum = Math.max(maxSum, currentSum); } + return maxSum; + } } -public class LeetCode { - public static void main(String[] args) { - int[] arr1 = {2,3,1,3,2,4,6,7,9,2,19}; - int[] arr2 = {2,1,4,3,9,6}; +public class LeetCode{ + public static void main(String[] args){ + int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; - int[] res = RelSort.RelativeSort(arr1, arr2); - for(int i = 0; i < res.length; i++) { - System.out.print(res[i] + " "); - } + Solution solution = new Solution(); + + int maxSum = solution.maxSubArray(nums); + + System.out.println("Maximum subarray sum is: " + maxSum); } } \ No newline at end of file