Skip to content

Commit

Permalink
Problem Solving
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaspundpal committed Jul 17, 2023
1 parent 1ff4e4a commit ff0e3c3
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 14 deletions.
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/Java-DSA.iml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Java-DSA.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="Java_DSA" />
<sourceFolder url="file://$MODULE_DIR$/Collection Framework" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Recursion" isTestSource="false" packagePrefix="Java_DSA.Searching" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
34 changes: 34 additions & 0 deletions Problems/Arrays/FirstAndLastPositionOfElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Java_DSA.Problems.Arrays;

import java.util.* ;
import java.io.*;
public class FirstAndLastPositionOfElement {

public static int[] firstAndLastPosition(ArrayList<Integer> arr, int n, int k) {
// Write your code here.
ArrayList<Integer>list = new ArrayList<>();
for(int i = 0;i < n;i++){
if(arr.get(i) == k){
list.add(i);
}
}
int[] ans = new int[2];
if(list.size()==0){
ans[0] = -1;
ans[1] = -1;
return ans;
}
ans[0] = Collections.min(list);//list.get(0);
ans[1] = Collections.max(list);//list.get(list.size()-1);
return ans;
}

public static void main(String[] args) {
ArrayList<Integer>l = new ArrayList<>(List.of(0,0,1,1,2,3,2,2,2));
int[] result = firstAndLastPosition(l,l.size(),2);
for (int j : result) {
System.out.print(j + " ");
}
}

};
48 changes: 48 additions & 0 deletions Problems/Arrays/IntersectionOfArrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Java_DSA.Problems.Arrays;

import java.util.* ;
import java.io.*;
public class IntersectionOfArrays
{
public static ArrayList<Integer> findArrayIntersection(ArrayList<Integer> arr1, int n, ArrayList<Integer> arr2, int m)
{

// Write Your Code Here.
ArrayList<Integer>list = new ArrayList<>();
//initializing pointers to 0
int i = 0;
int j = 0;
while(i < n && j < m){
if(arr1.get(i).equals(arr2.get(j)))//use equals to check same value bcz if u use == it checks object.
{
list.add(arr1.get(i));
i++;
j++;
}
else if(arr1.get(i) < arr2.get(j))//since array is sorted so increment first array pointer if value is less than value in 2nd array
{
i++;
}
else{
j++;
}
}
if(list.size()== 0)
list.add(-1);

return list;
}

public static void main(String[] args) {

ArrayList<Integer> ans = new ArrayList<>();

ArrayList<Integer>l1 = new ArrayList<>(List.of(1,2,2,2,3,4));
ArrayList<Integer>l2 = new ArrayList<>(List.of(2,2,3,3));
// ArrayList<Integer>l1 = new ArrayList<>(List.of(3,4));
// ArrayList<Integer>l2 = new ArrayList<>(List.of(2,2));
ans = findArrayIntersection(l1,l1.size(),l2,l2.size());

System.out.println(ans);
}
}
37 changes: 37 additions & 0 deletions Problems/Arrays/RotateArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//Sample Input 1:
// 8
// 7 5 2 11 2 43 1 1
// 2
//Sample Output 1:
// 2 11 2 43 1 1 7 5
//Explanation Of Sample Input 1:
//Rotate 1 steps to the left: 5 2 11 2 43 1 1 7
//Rotate 2 steps to the left: 2 11 2 43 1 1 7 5
//

package Java_DSA.Problems.Arrays;

import java.util.* ;
import java.io.*;
class RotateArray {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int arr[]=new int[n];
for(int i=0 ; i<n ; i++){
arr[i]=in.nextInt();
}
int r=in.nextInt();
for(int i=0 ; i<r ; i++){
int temp=arr[0];
for(int j=1 ; j<n ; j++){
arr[j-1]=arr[j];
}
arr[n-1]=temp;
}
for(int i=0 ; i<n ; i++){
System.out.print(arr[i]+" ");
}
}
}

49 changes: 49 additions & 0 deletions Problems/Arrays/Sort012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//Given an array which only contains 0,1 and 2 and we have to sort it.
//Sample Input 1 :
// 2
// 6
// 0 1 2 2 1 0
// 7
// 0 1 2 1 2 1 2
// Sample Output 1 :
// 0 0 1 1 2 2
// 0 1 1 1 2 2 2
// Sample Input 2 :
// 2
// 7
// 2 2 2 1 1 1 0
// 6
// 2 1 2 0 1 0
// Sample Output 2 :
// 0 1 1 1 2 2 2
// 0 0 1 1 2 2

package Java_DSA.Problems.Arrays;
import java.util.* ;
import java.io.*;
public class Sort012
{
public static void sort012(int[] arr)
{
int count0=0;
int count1=0;
int count2=0;

for(int num:arr){
if(num==0)
count0++;
else if(num==1)
count1++;
else
count2++;
}
for(int i=0;i<arr.length;i++){
if(i<count0)
arr[i]=0;
else if(i<count0+count1)
arr[i]=1;
else
arr[i]=2;
}
}
}
57 changes: 57 additions & 0 deletions Problems/Arrays/TwoSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//Sample Input 1 :
// 2
// 4 9
// 2 7 11 13
// 5 1
// 1 -1 -1 2 2
//Sample Output 1:
// 2 7
// -1 2
// -1 2
//Explanation For Sample 1:
//For the first test case, we can see that the sum of 2 and 7 is equal to 9 and it is the only valid pair.
//For the second test case, there are two valid pairs (-1,2) and (-1,2), which add up to 1.
//
//Sample Input 2 :
// 1
// 4 16
// 2 7 11 13
//Sample Output 2 :
// -1 -1
//
//package Java_DSA.Problems.Arrays;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.*;
//



//public class TwoSum {
// static ArrayList<Pair<Integer, Integer>> twoSum(ArrayList<Integer> arr, int target, int n) {
// // Write your code here.
//
// ArrayList<Pair<Integer,Integer>>list = new ArrayList<>();
// int i = 0;
// int j = n-1;
// Collections.sort(arr);
//
// while(i < j){
// if((arr.get(i)+arr.get(j))== target){
// list.add(new Pair(arr.get(i),arr.get(j)));
// i++;
// j--;
// }
// else if((arr.get(i)+arr.get(j)) < target){
// i++;
// }
// else{
// j--;
// }
// }
// if(list.isEmpty())
// list.add(new Pair(-1,-1));
//
// return list;
// }
//}
Loading

0 comments on commit ff0e3c3

Please sign in to comment.