Skip to content

Commit

Permalink
day 9 update
Browse files Browse the repository at this point in the history
  • Loading branch information
ramoliyaYug committed Nov 7, 2024
1 parent 937483b commit 54bb040
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 32 deletions.
51 changes: 51 additions & 0 deletions DSA/JAVA/day 07/ArrayListExample.java
Original file line number Diff line number Diff line change
@@ -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<String> list = new ArrayList<String>();

// 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);
}
}
6 changes: 3 additions & 3 deletions DSA/JAVA/day 07/QuickSort.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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));
}
}
21 changes: 21 additions & 0 deletions DSA/JAVA/day 08/Even.java
Original file line number Diff line number Diff line change
@@ -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<Integer> even(ArrayList<Integer> list){
ArrayList<Integer> evenList = new ArrayList<Integer>();
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<Integer> list = new ArrayList<Integer>();
for(int i = 1; i<= 20; i++){
list.add(i);
}
System.out.println(list);
System.out.println(even(list));
}
}
28 changes: 28 additions & 0 deletions DSA/JAVA/day 08/Friend.java
Original file line number Diff line number Diff line change
@@ -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 <String> Friends = new ArrayList<String>();

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));
}
}
73 changes: 73 additions & 0 deletions DSA/JAVA/day 08/HashMapExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
- stores data in the key value pair
- syntax:
HashMap<key datatype, value datatype> 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<String, Integer> 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);
}
}

24 changes: 24 additions & 0 deletions DSA/JAVA/day 08/MostFreq.java
Original file line number Diff line number Diff line change
@@ -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<String, Integer> 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);
}
}
24 changes: 24 additions & 0 deletions DSA/JAVA/day 08/Num.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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}
55 changes: 55 additions & 0 deletions DSA/JAVA/day 09/StringMethod.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
25 changes: 25 additions & 0 deletions DSA/JAVA/day 09/StringQuestion.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
47 changes: 18 additions & 29 deletions LeetCode.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 54bb040

Please sign in to comment.