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;
public class DuplicateInArray {
public static boolean checkDuplicate(int[] a) {
for(int i=0;i<a.length-1;i++) {
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
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:-
1,2,3,