Skip to content

Commit 389d31d

Browse files
committed
updated contest and stack
1 parent 989c3ee commit 389d31d

8 files changed

+396
-0
lines changed

Contest/KSum.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package SummerTrainingGFG.Contest;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.StringTokenizer;
9+
10+
/**
11+
* @author Vishal Singh
12+
*/
13+
public class KSum {
14+
static class FastReader{
15+
BufferedReader br;
16+
StringTokenizer st;
17+
public FastReader(){
18+
br = new BufferedReader(new InputStreamReader(System.in));
19+
}
20+
String next(){
21+
while (st == null || !st.hasMoreElements()){
22+
try {
23+
st = new StringTokenizer(br.readLine());
24+
}catch (IOException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
return st.nextToken();
29+
}
30+
int nextInt(){
31+
return Integer.parseInt(next());
32+
}
33+
String nextLine(){
34+
String str = "";
35+
try {
36+
str = br.readLine();
37+
} catch (IOException e) {
38+
e.printStackTrace();
39+
}
40+
return str;
41+
}
42+
}
43+
public static void main(String[] args) {
44+
FastReader fr = new FastReader();
45+
int t = fr.nextInt();
46+
while (t-->0){
47+
int n = fr.nextInt();
48+
int k = fr.nextInt();
49+
int[] arr = new int[n];
50+
for (int i = 0; i < n; i++) {
51+
arr[i] = fr.nextInt();
52+
}
53+
Arrays.sort(arr);
54+
long sum = 0;
55+
for (int i = 0; i < k; i++) {
56+
sum+=arr[i];
57+
}
58+
System.out.println(sum);
59+
}
60+
}
61+
}

Contest/KthSmallestDifference.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package SummerTrainingGFG.Contest;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
9+
/**
10+
* @author Vishal Singh
11+
*/
12+
public class KthSmallestDifference {
13+
static class FastReader{
14+
BufferedReader br;
15+
StringTokenizer st;
16+
public FastReader(){
17+
br = new BufferedReader(new InputStreamReader(System.in));
18+
}
19+
String next(){
20+
while (st == null || !st.hasMoreElements()){
21+
try {
22+
st = new StringTokenizer(br.readLine());
23+
}catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
return st.nextToken();
28+
}
29+
int nextInt(){
30+
return Integer.parseInt(next());
31+
}
32+
String nextLine(){
33+
String str = "";
34+
try {
35+
str = br.readLine();
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
return str;
40+
}
41+
}
42+
static int smallestDistancePair(int[] arr, int k) {
43+
Arrays.sort(arr);
44+
int low = 0;
45+
int high = arr[arr.length - 1] - arr[0];
46+
while (low < high) {
47+
int mi = (low + high) / 2;
48+
int count = 0, left = 0;
49+
for (int right = 0; right < arr.length; ++right) {
50+
while (arr[right] - arr[left] > mi) left++;
51+
count += right - left;
52+
}
53+
if (count >= k) high = mi;
54+
else low = mi + 1;
55+
}
56+
return low;
57+
}
58+
public static void main(String[] args) {
59+
FastReader fr = new FastReader();
60+
int t = fr.nextInt();
61+
while (t-->0){
62+
int n = fr.nextInt();
63+
int k = fr.nextInt();
64+
int[] arr = new int[n];
65+
for (int i = 0; i < n; i++) {
66+
arr[i] = fr.nextInt();
67+
}
68+
System.out.println(smallestDistancePair(arr,k));
69+
}
70+
}
71+
}

Contest/XORPair.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package SummerTrainingGFG.Contest;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.StringTokenizer;
9+
10+
/**
11+
* @author Vishal Singh
12+
*/
13+
public class XORPair {
14+
static class FastReader{
15+
BufferedReader br;
16+
StringTokenizer st;
17+
public FastReader(){
18+
br = new BufferedReader(new InputStreamReader(System.in));
19+
}
20+
String next(){
21+
while (st == null || !st.hasMoreElements()){
22+
try {
23+
st = new StringTokenizer(br.readLine());
24+
}catch (IOException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
return st.nextToken();
29+
}
30+
int nextInt(){
31+
return Integer.parseInt(next());
32+
}
33+
String nextLine(){
34+
String str = "";
35+
try {
36+
str = br.readLine();
37+
} catch (IOException e) {
38+
e.printStackTrace();
39+
}
40+
return str;
41+
}
42+
}
43+
public static void main(String[] args) {
44+
FastReader fr = new FastReader();
45+
int t = fr.nextInt();
46+
while (t-->0){
47+
int n = fr.nextInt();
48+
int c = fr.nextInt();
49+
int[] arr = new int[n];
50+
for (int i = 0; i < n; i++) {
51+
arr[i] = fr.nextInt();
52+
}
53+
HashSet<Integer> set = new HashSet<>();
54+
int r = 0;
55+
for (int i = 0; i < n; i++) {
56+
if (set.contains(arr[i]^c)){
57+
r+=1;
58+
}
59+
set.add(arr[i]);
60+
}
61+
if (r>0){
62+
System.out.println("Yes");
63+
}else {
64+
System.out.println("No");
65+
}
66+
}
67+
}
68+
}

Stack/KStackInArray.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package SummerTrainingGFG.Stack;
2+
3+
/**
4+
* @author Vishal Singh
5+
*/
6+
public class KStackInArray {
7+
static class KStack{
8+
int[] arr;
9+
int[] top;
10+
int[] next;
11+
int k, capacity, freeTop;
12+
KStack(int k1, int n){
13+
k = k1;
14+
capacity = n;
15+
arr = new int[capacity];
16+
top = new int[k];
17+
next = new int[capacity];
18+
freeTop = 0;
19+
for (int i = 0; i < capacity-1; i++) {
20+
next[i] = i+1;
21+
}
22+
next[capacity-1] = -1;
23+
}
24+
void push(int x, int stackNumber){
25+
int i = freeTop;
26+
freeTop = next[i];
27+
next[i] = top[stackNumber];
28+
top[stackNumber] = i;
29+
arr[i] = x;
30+
}
31+
int pop(int stackNumber){
32+
int i = top[stackNumber];
33+
top[stackNumber] = next[i];
34+
next[i] = freeTop;
35+
freeTop = i;
36+
return arr[i];
37+
}
38+
}
39+
public static void main(String[] args) {
40+
KStack kStack = new KStack(4,4);
41+
kStack.push(101,0);
42+
kStack.push(211,1);
43+
kStack.push(325,2);
44+
kStack.push(444,3);
45+
for (int i = 0; i < 4; i++) {
46+
System.out.println(kStack.pop(i));
47+
}
48+
}
49+
}

Stack/NextGreater.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package SummerTrainingGFG.Stack;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Stack;
6+
7+
/**
8+
* @author Vishal Singh
9+
*/
10+
public class NextGreater {
11+
public static void main(String[] args) {
12+
int[] arr = {5,15,10,8,6,12,9,18};
13+
// {15,18,12,12,12,18,18,-1}
14+
ArrayList<Integer> list = new ArrayList<>();
15+
Stack<Integer> stack = new Stack<>();
16+
stack.push(arr[arr.length-1]);
17+
for (int i = arr.length-1; i >=0; i--) {
18+
while (!stack.isEmpty() && stack.peek()<= arr[i]){
19+
stack.pop();
20+
}
21+
int ng = stack.empty()?-1: stack.peek();
22+
list.add(ng);
23+
stack.push(arr[i]);
24+
}
25+
Collections.reverse(list);
26+
System.out.println(list);
27+
}
28+
}

Stack/PreviousGreater.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package SummerTrainingGFG.Stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author Vishal Singh
7+
*
8+
* : -> To find the closest previous greater
9+
*/
10+
public class PreviousGreater {
11+
public static void main(String[] args) {
12+
int[] arr = {15, 10, 18, 12, 4, 6, 2, 8};
13+
// = {-1, 15 -1 18 12 12 6 12}
14+
Stack<Integer> previousGreat = new Stack<>();
15+
previousGreat.push(arr[0]);
16+
System.out.print("-1 ");
17+
for (int i = 1; i < 8; i++) {
18+
while (!previousGreat.isEmpty() && previousGreat.peek() <= arr[i]){
19+
previousGreat.pop();
20+
}
21+
int pg = previousGreat.empty() ? -1 : previousGreat.peek();
22+
System.out.print(pg+" ");
23+
previousGreat.push(arr[i]);
24+
}
25+
}
26+
}

Stack/StockSpan.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package SummerTrainingGFG.Stack;
2+
3+
import java.util.Arrays;
4+
import java.util.Stack;
5+
6+
/**
7+
* @author Vishal Singh
8+
*/
9+
public class StockSpan {
10+
public static void main(String[] args) {
11+
int[] arr = {15,13,12,14,16,8,6,4,10,30};
12+
//Span arr= {1, 1, 1, 3, 5, 1,1,1, 1, 10}
13+
System.out.println(Arrays.toString(arr));
14+
Stack<Integer> span = new Stack<Integer>();
15+
span.push(0);
16+
for (int i = 1; i < 10; i++) {
17+
while (!span.isEmpty() && arr[span.peek()] <= arr[i]){
18+
span.pop();
19+
}
20+
int spanSize = span.empty()? (i+1) : (i - span.peek());
21+
System.out.print(spanSize+" ");
22+
span.push(i);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)