-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray-2-java
More file actions
206 lines (188 loc) · 4.1 KB
/
Array-2-java
File metadata and controls
206 lines (188 loc) · 4.1 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
Q1 - Given an unsorted array arr[] of size N having both negative and positive integers, place
all negative elements at the end of array without changing the order of positive elements
and negative elements
(Medium)
Assignment Solutions
Input :
N = 8
arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output :
1 3 2 11 6 -1 -7 -5
Code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println(“Enter the length of array: ”);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
int[] ans = new int[n];
int idx = 0;
for(int i = 0; i < n; i++){
if(arr[i] >= 0){
ans[idx] = arr[i];
idx++;
}
}
for(int i = 0; i < n; i++){
if(arr[i] < 0){
ans[idx] = arr[i];
idx++;
}
}
for(int i = 0; i < n; i++){
System.out.print(ans[i] + " ");
}
}
}
Cracking the Coding Interview in JAVA - Foundation
Q2 - Given two arrays a[] and b[] of size n and m respectively where m >= n. The task is to find
union between these two arrays and print the number of elements of the union set.
Union of the two arrays can be defined as the set containing distinct elements from both
the arrays. If there are repetitions, then only one occurrence of element should be printed
in the union.
(Medium)
Assignment Solutions
Input:
5 3
1 2 3
1 2 3 4 5
Output:
5
Code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println(“Enter the length of arrays: ”);
int n = scn.nextInt();
int m = scn.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i = 0; i < n; i++){
a[i] = scn.nextInt();
}
for(int i = 0; i < m; i++){
b[i] = scn.nextInt();
boolean check = false;
for(int j = 0; j < n; j++){
if(b[i] == a[j]){
check = true;
break;
}
}
if(!check){
System.out.println(b[i]);
}
}
}
}
Cracking the Coding Interview in JAVA - Foundation
Q3 - Given an array arr[] and an integer K where K is smaller than size of array, the task is to
find the Kth smallest element in the given array. It is given that all array elements are
distinct.
(Easy)
Assignment Solutions
Input:
N = 6
arr[] = 7 10 4 3 20 15
K = 3
Output:
7
Code:
import java.util.Scanner;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println(“Enter the length of array: ”);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
int k = scn.nextInt();
Arrays.sort(arr);
System.out.print(arr[k-1]);
}
}
Cracking the Coding Interview in JAVA - Foundation
Q4 - Given an unsorted array A of size N that contains only non-negative integers, find a
continuous sub-array which adds to a given number S.
In case of multiple subarrays, return the subarray which comes first on moving from left
to right.
You need to print the start and end index of answer subarray.
(Medium)
Assignment Solutions
Input:
N = 5, S = 12
A[] = {1,2,3,7,5}
Output:
1 3
Code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println(“Enter the length of array: ”);
int n = scn.nextInt();
int s = scn.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
int i = 0;
int j = 0;
int sum = 0;
while(j < n){
sum += arr[j];
if(sum > s){
while(sum > s && i < j){
sum -= arr[i];
i++;
}
}
if(sum == s){
System.out.print(i + " ");
System.out.print(j);
break;
}
j++;
}
}
}
Cracking the Coding Interview in JAVA - Foundation
Q5 - Write a Java program to test the equality of two arrays. (Easy)
Assignment Solutions
Input:
5
2 5 7 9 11
2 5 7 9 13
Output:
false
Code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println(“Enter the length of array: ”);
int n = scn.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[n];
for(int i = 0; i < n; i++){
arr1[i] = scn.nextInt();
}
boolean ans = true;
for(int i = 0; i < n; i++){
arr2[i] = scn.nextInt();
if(arr2[i] != arr1[i]){
ans = false;
break;
}
}
System.out.print(ans);
}
}