-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlib.txt
More file actions
138 lines (76 loc) · 2.38 KB
/
Copy pathlib.txt
File metadata and controls
138 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
1....Input formats
static Scanner sc = new Scanner(System.in);
static long l(){
return sc.nextLong();
}
static int i(){
return sc.nextInt();
}
static String s(){
return sc.nextLine();
}
static int[] ia(int n){ //integer array urf ia()
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i] = i();
return arr;
}
2...prime--returnns true if no. is prime--
boolean prime(long num) //returns true for prime numbers and false for non-primes
{
long i,limit =num/2 ;
if(num%2==0)
return false;
for(i=3;i<=limit;i+=2){
if(num%i==0)
return false;
}
return true;
}
3...
throws IOException
try{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
int p = Integer.parseInt(input.readline()); //for int
String y = input.readLine(); //for array
String[] ar = y.split(" "); //for array
}
catch(IOException e){}
4...Hashmap
HashMap<String, Integer> map = new HashMap<String, Integer>();
Iterating hashmap
for (Map.Entry<Integer, Integer> entry : map.entrySet())
{
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Iterating over keys only
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
Iterating over values only
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
5.ArrayList
ArrayList<String> obj = new ArrayList<String>();
Iterating arraylist
for (String temp : obj) {
System.out.println(temp);
}
6...exor...Find exor of all numbers in the range [a,b]
long long f(long long a) {
long long res[] = {a,1,a+1,0};
return res[a%4];
}
long long getXor(long long a, long long b) {
return f(b)^f(a-1);
}
7... decimal to binary conversion
integer to binary number
String s = Integer.toBinaryString(int);
binary to integer
int n = Integer.parseInt(BinaryString,2)
Long to binary
String s = Long.toBinaryString(long);
binary to integer
long n = Long.parseLong(BinaryString,2)