File tree Expand file tree Collapse file tree 6 files changed +274
-0
lines changed Expand file tree Collapse file tree 6 files changed +274
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java.util.HashMap;
2
+
3
+ public class Solution {
4
+
5
+ public static String uniqueChar(String str){
6
+ /* Your class should be named Solution
7
+ * Don't write main().
8
+ * Don't read input, it is passed as function argument.
9
+ * Return output and don't print it.
10
+ * Taking input and printing output is handled automatically.
11
+ */
12
+ HashMap<Character,Boolean> map = new HashMap<>();
13
+ String ans="";
14
+ for(int i =0;i<str.length();i++)
15
+ {
16
+ char front = str.charAt(i);
17
+ if(!map.containsKey(front))
18
+ {
19
+ map.put(front,true);
20
+ ans+=front;
21
+ }
22
+ }
23
+
24
+
25
+ return ans;
26
+
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ import java.util.Map;
2
+ import java.util.HashMap;
3
+ import java.util.ArrayList;
4
+
5
+ public class Solution {
6
+ public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) {
7
+ /* Your class should be named Solution
8
+ * Don't write main().
9
+ * Don't read input, it is passed as function argument.
10
+ * Return output and don't print it.
11
+ * Taking input and printing output is handled automatically.
12
+ */
13
+
14
+ HashMap<Integer,Integer> map = new HashMap<>();
15
+ for(int i =0;i<arr.length;i++)
16
+ {
17
+ map.put(arr[i],1);
18
+ }
19
+
20
+ int overallcount=0,maxStart=Integer.MIN_VALUE;
21
+ for(int i=0;i<arr.length;i++)
22
+ {
23
+
24
+ if(!map.containsKey(arr[i]-1))
25
+ {
26
+ int currentcount = 1;
27
+ int currentstart = arr[i];
28
+
29
+ while(map.containsKey(currentstart+1))
30
+ {
31
+ currentcount++;
32
+ currentstart=currentstart+1;
33
+ }
34
+ if(currentcount>overallcount)
35
+ {
36
+ overallcount=currentcount;
37
+ maxStart = arr[i];
38
+ }
39
+ }
40
+ }
41
+ ArrayList<Integer> ans = new ArrayList<>();
42
+ ans.add(maxStart);
43
+ ans.add(maxStart+overallcount-1);
44
+
45
+ return ans;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ import java.util.*;
2
+ public class Solution {
3
+
4
+ public static int maxFrequencyNumber(int[] arr){
5
+ /* Your class should be named Solution
6
+ * Don't write main().
7
+ * Don't read input, it is passed as function argument.
8
+ * Return output and don't print it.
9
+ * Taking input and printing output is handled automatically.
10
+ */
11
+ // import java.util.*;
12
+ HashMap<Integer,Integer> map = new HashMap<>();
13
+
14
+ for(int i =0;i<arr.length;i++)
15
+ {
16
+ if(map.containsKey(arr[i]))
17
+ {
18
+ int value = map.get(arr[i]);
19
+ value++;
20
+ map.put(arr[i],value);
21
+ }
22
+ else
23
+ {
24
+ map.put(arr[i],1);
25
+ }
26
+ }
27
+
28
+ int maxFreq = 0;
29
+ int max = Integer.MIN_VALUE;
30
+ for(int i : arr) {
31
+ if( maxFreq < map.get(i) ) {
32
+ maxFreq = map.get(i);
33
+ max = i;
34
+ }
35
+ }
36
+
37
+
38
+ return max;
39
+ }
40
+
41
+ }
42
+
Original file line number Diff line number Diff line change
1
+ import java.util.*;
2
+
3
+ public class Solution {
4
+ public static int PairSum(int[] input, int size) {
5
+ /* Your class should be named Solution
6
+ * Don't write main().
7
+ * Don't read input, it is passed as function argument.
8
+ * Return output and don't print it.
9
+ * Taking input and printing output is handled automatically.
10
+ */
11
+
12
+ HashMap<Integer,Integer> map = new HashMap<>();
13
+
14
+ for(int i =0;i<size;i++)
15
+ {
16
+ if(map.containsKey(input[i]))
17
+ {
18
+ int value = map.get(input[i]);
19
+ value++;
20
+ map.put(input[i],value);
21
+ }
22
+ else
23
+ {
24
+ map.put(input[i],1);
25
+ }
26
+ }
27
+
28
+ int finalCount = 0;
29
+ for(int i=0; i < size; i++)
30
+ {
31
+ int key = input[i];
32
+ if(map.containsKey(-key) && map.get(key)!=0)
33
+ {
34
+ int times;
35
+ if (key == (-key)) { //True in case of zero
36
+ int occurences = map.get(key);
37
+ times = (occurences * (occurences - 1)) / 2;
38
+ finalCount = finalCount+times;
39
+ map.put(key, 0);
40
+ continue;
41
+ }
42
+ times = map.get(key) * map.get(-key);
43
+ finalCount = finalCount+times;
44
+ map.put(key, 0);
45
+ map.put(-key, 0);
46
+ }
47
+ }
48
+ return finalCount;
49
+ }
50
+ }
Original file line number Diff line number Diff line change
1
+ import java.util.*;
2
+ public class Solution {
3
+
4
+ public static int getPairsWithDifferenceK(int arr[], int k) {
5
+ //Write your code here
6
+ HashMap<Integer,Integer> map = new HashMap<>();
7
+ int count = 0;
8
+ if(k!=0)
9
+ {
10
+ for(int i =0;i<arr.length;i++)
11
+ {
12
+ int p1 = arr[i]+k;
13
+ int p2 = arr[i]-k;
14
+
15
+ if(map.containsKey(p1))
16
+ {
17
+ count+=map.get(p1);
18
+ }
19
+ if(map.containsKey(p2))
20
+ {
21
+ count+=map.get(p2);
22
+ }
23
+
24
+ if(!map.containsKey(arr[i]))
25
+ {
26
+ map.put(arr[i],1);
27
+ }
28
+ else if(map.containsKey(arr[i]))
29
+ {
30
+ int value = map.get(arr[i]);
31
+ value++;
32
+ map.put(arr[i],value);
33
+ }
34
+ }
35
+ }
36
+ if(k==0)
37
+ {
38
+ for(int i =0;i<arr.length;i++)
39
+ {
40
+ if(!map.containsKey(arr[i]))
41
+ {
42
+ map.put(arr[i],1);
43
+ }
44
+ else if(map.containsKey(arr[i]))
45
+ {
46
+ int value = map.get(arr[i]);
47
+ value++;
48
+ map.put(arr[i],value);
49
+ }
50
+
51
+ }
52
+
53
+ for(int i =0;i<arr.length;i++)
54
+ {
55
+ int n = map.get(arr[i]);
56
+ if(n>0){
57
+ count+=(n*(n-1)/2);
58
+ map.put(arr[i],0);
59
+ }
60
+ }
61
+
62
+
63
+
64
+ }
65
+
66
+ return count;
67
+ }
68
+ }
Original file line number Diff line number Diff line change
1
+ import java.util.*;
2
+ public class Solution {
3
+
4
+ public static void printIntersection(int[] arr1,int[] arr2){
5
+ /* Your class should be named Solution
6
+ * Don't write main().
7
+ * Don't read input, it is passed as function argument.
8
+ * Return output and don't print it.
9
+ * Taking input and printing output is handled automatically.
10
+ */
11
+
12
+ if(arr1.length==0 || arr2.length==0)
13
+ return ;
14
+ HashMap<Integer,Integer> harr1=new HashMap<>();
15
+ for(int i=0;i<arr1.length;i++)
16
+ { if(harr1.containsKey(arr1[i]))
17
+ harr1.put(arr1[i],harr1.get(arr1[i])+1);
18
+ else
19
+ harr1.put(arr1[i],1);
20
+ }
21
+
22
+ for(int i=0;i<arr2.length;i++)
23
+ {
24
+ if(harr1.containsKey(arr2[i])){
25
+ if(harr1.get(arr2[i])!=0){
26
+
27
+ System.out.println(arr2[i]);
28
+ harr1.put(arr2[i],harr1.get(arr2[i])-1);
29
+ }
30
+ }
31
+ else
32
+ continue;
33
+
34
+
35
+ }
36
+
37
+
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments