Tuesday, April 19, 2022

LeetCode





https://gitlab.com/perwaizali01/leetcode.git


  • git init

  • git add .
  • git commit -m “commit message”
  • git status
  • git push -u origin master
  • -----------------------------------------------------------------------------------------------------

1. Given an array of integers nums and an integer target, return indices of the two numbers 


such 


that they add up to target.


You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

 

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6 

Output: [1,2]


package com.example.LeetCode_Problems;


import java.util.Arrays;

import java.util.HashMap;

import java.util.Map;

import java.util.stream.Collectors;


public class Two_Sum_2 {


public static void main(String[] args) {


int arryas[] = {4,1,15,7,8, 20, 2};

int target =9;

int[] finalLocationArray = twoSum(arryas, target);

Arrays.stream(finalLocationArray).boxed().collect(Collectors.toList()).forEach(System.out::print);

}


public static int[]  twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

for(int i = 0; i<nums.length; i++) {

int cur = nums[i];

int x = target - cur;

if(map.containsKey(x)) {

return new int[] {map.get(x),i};

}

map.put(cur, i);

}

return null;

}


}



OUTPUT :_
14 






-----------------------------------------------------------------------------------------------------------------


package com.example.demo.java_8.Duplicate_Array;


import java.util.Arrays;


public class DuplicateInArray {


public static boolean checkDuplicate(int[] a) {

Arrays.sort(a);

for(int i=0;i<a.length-1;i++) {

if(a[i]==a[i+1]) {

return true;

}

}

return false;

}

public static void main(String[] args) {


int[] arrayA = new int[] {3,4,6,7,3,4,5};

int[] arrayB = new int[] {3,4,6,7};

System.out.println(checkDuplicate(arrayB));

}


}






OUTPUT:-

false


-------------------------------------------------------------------------------------------------

package com.example.demo.LeetCode;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Arrays_Duplicate {
public static int[] check_duplicate_Elemnts_in_Array(int[] arr) {
List<Integer> list = new ArrayList<>();
Arrays.sort(arr);
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1]) {
list.add(arr[i]);
}
}
int[] arr_with_duplicate_element = list.stream().mapToInt(Integer::intValue).toArray();
return arr_with_duplicate_element;
}
public static void main(String[] args) {
int[] array = new int[] { 3, 5, 4, 2, 3, 2, 1, 1 };
int[] array_1 = check_duplicate_Elemnts_in_Array(array);
System.out.println("Duplicate elements in array are :");
for (int element : array_1) {
System.out.print(element+",");
}
}
}


OUTPUT:-

Duplicate elements in array are :
1,2,3,


------------------------------------------------------------------------------------------------------
package com.example.LeetCode_pepcode;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class MapWithKeyCount {

public static void main(String[] args) {

List<Integer> list = Arrays.asList(12, 1, 23, 45, 6, 12, 12, 1, 23);
Map<Integer, Integer> map = new HashMap<>();
TreeSet<Integer> duplicateEle = new TreeSet<>();

for (int ele : list) {

map.put(ele, map.getOrDefault(ele, 0) + 1);

}
for (int ele : list) {
if (map.get(ele) > 1) {
duplicateEle.add(ele);
}
}
TreeSet reverseTreeSet = (TreeSet) duplicateEle.descendingSet();
System.out.println(map);
System.out.println("----------------------------");
System.out.println(duplicateEle);
System.out.println("----------------------------");
System.out.println(reverseTreeSet);
}

}


OUTPUT :-
{1=2, 6=1, 23=2, 12=3, 45=1}
----------------------------
[1, 12, 23]
----------------------------
[23, 12, 1]


-----------------------------------------------------------------------------------------------------------------

package com.example.LeetCode_pepcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LonelyNumber {

public static void main(String[] args) {

int[] array = { 10, 6, 5, 8 };

HashMap<Integer, Integer> map = new HashMap<>();
List<Integer> ans = new ArrayList<>();

for (int ele : array) {
map.put(ele, map.getOrDefault(ele ,0)+ 1);
}

for (int ele : array) {
if (map.get(ele) > 1 || map.containsKey(ele - 1) || map.containsKey(ele + 1))
continue;
ans.add(ele);
}
System.out.println(map);
                System.out.println(ans);
}

}
OUTPUT :-
{5=1, 6=1, 8=1, 10=1}
[10, 8]

----------------------------------------------------------------------------------------------------------