MCPcopy Create free account
hub / github.com/apna-college/Alpha / Classroom

Class Classroom

11_ArrayLists/Classroom.java:3–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.ArrayList;
2
3public class Classroom {
4 //Brute Force
5 // public static boolean pairSum1(ArrayList<Integer> list, int target) {
6
7 // for(int i=0; i<list.size(); i++) {
8 // for(int j=i+1; j<list.size(); j++) {
9 // if(list.get(i) + list.get(j) == target) {
10 // return true;
11 // }
12 // }
13 // }
14
15 // return false;
16 // }
17
18 //2 pointer approach
19 public static boolean pairSum1(ArrayList<Integer> list, int target) {
20 int lp = 0;
21 int rp = list.size()-1;
22
23 while(lp != rp) {
24 //case 1
25 if(list.get(lp)+list.get(rp) == target) {
26 return true;
27 }
28
29 //case 2
30 if(list.get(lp)+list.get(rp) < target) {
31 lp++;
32 } else {
33 //case 3
34 rp--;
35 }
36 }
37
38 return false;
39 }
40
41 //O(n)
42 public static boolean pairSum2(ArrayList<Integer> list, int target) {
43 int bp = -1;
44 int n = list.size();
45 for(int i=0; i<list.size(); i++) {
46 if(list.get(i) > list.get(i+1)) { //breaking point
47 bp = i;
48 break;
49 }
50 }
51
52 int lp = bp+1; //smallest
53 int rp = bp; //largest
54
55 while(lp != rp) {
56 //case1
57 if(list.get(lp) + list.get(rp) == target) {
58 return true;
59 }
60

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…