Today I was solving some array related problems on GeeksForGeeks,
Came across with solution How Hashset helped me to solve these problems for large amount of input within given time complexity.
Here is the problem statements I have solved:-
Problem Statement:- Pair with given sum in a sorted array
Solution :-
Problem Statement:- Triplet Sum in Array
Solution :-
Came across with solution How Hashset helped me to solve these problems for large amount of input within given time complexity.
Here is the problem statements I have solved:-
Problem Statement:- Pair with given sum in a sorted array
Solution :-
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T --> 0){
int N = Integer.parseInt(br.readLine());
String str = br.readLine();
String[] inputArr = str.trim().split("\\s+");
int A[] = new int[N];
for(int i=0;i<N;i++){
A[i] = Integer.parseInt(inputArr[i]);
}
int K = Integer.parseInt(br.readLine());
int low = 0;
int high = N-1;
boolean flag = true;
while(low<high){
if(A[low]+A[high] > K){
high--;
}else if(A[low]+A[high] < K){
low++;
}else{
System.out.println(A[low]+" "+A[high] + " "+ K);
low++;
flag = false;
}
}
if(flag){
System.out.println(-1);
}
}
}
}
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T --> 0){
int N = Integer.parseInt(br.readLine());
String str = br.readLine();
String[] inputArr = str.trim().split("\\s+");
int A[] = new int[N];
for(int i=0;i<N;i++){
A[i] = Integer.parseInt(inputArr[i]);
}
int K = Integer.parseInt(br.readLine());
int low = 0;
int high = N-1;
boolean flag = true;
while(low<high){
if(A[low]+A[high] > K){
high--;
}else if(A[low]+A[high] < K){
low++;
}else{
System.out.println(A[low]+" "+A[high] + " "+ K);
low++;
flag = false;
}
}
if(flag){
System.out.println(-1);
}
}
}
}
Problem Statement:- Triplet Sum in Array
Solution :-
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T --> 0){
String input = br.readLine();
String[] inputArry = input.trim().split("\\s+");
int N = Integer.parseInt(inputArry[0]);
int X = Integer.parseInt(inputArry[1]);
String arrInp = br.readLine();
String[] arr = arrInp.trim().split("\\s+");
int[] A = new int[N];
for(int i=0;i<N;i++){
A[i] = Integer.parseInt(arr[i]);
}
Set<Integer> sumSet = new HashSet<>();
boolean flag= true;
for(int i=0;i<N;i++){
if(sumSet.contains(X-A[i])){
System.out.println("1");
flag = false;
break;
}else{
for(int j=0;j<=i;j++){
sumSet.add(A[i]+A[j]);
}
}
}
if(flag){
System.out.println("0");
}
}
}
}
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
while(T --> 0){
String input = br.readLine();
String[] inputArry = input.trim().split("\\s+");
int N = Integer.parseInt(inputArry[0]);
int X = Integer.parseInt(inputArry[1]);
String arrInp = br.readLine();
String[] arr = arrInp.trim().split("\\s+");
int[] A = new int[N];
for(int i=0;i<N;i++){
A[i] = Integer.parseInt(arr[i]);
}
Set<Integer> sumSet = new HashSet<>();
boolean flag= true;
for(int i=0;i<N;i++){
if(sumSet.contains(X-A[i])){
System.out.println("1");
flag = false;
break;
}else{
for(int j=0;j<=i;j++){
sumSet.add(A[i]+A[j]);
}
}
}
if(flag){
System.out.println("0");
}
}
}
}