Skip to content

Commit 43189c3

Browse files
committed
#Modification 25
1 parent fc390b5 commit 43189c3

4 files changed

Lines changed: 90 additions & 13 deletions

File tree

arrays/Array_Problem_1.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ public class Array_Problem_1 {
55

66
static void rvereseArray(int arr[],int start, int end)
77
{
8-
int temp;
9-
10-
while (start < end)
11-
{
12-
temp = arr[start];
13-
arr[start] = arr[end];
14-
arr[end] = temp;
15-
start++;
16-
end--;
8+
int temp;
9+
while(start < end){
10+
temp = arr[end];
11+
arr[end] = arr[start];
12+
a[start] = temp;
13+
start++;
14+
end--;
1715
}
1816
}
1917

arrays/Array_Problem_19.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> find common elements in 3 sorted arrays */
33
public class Array_Problem_19 {
4+
// This function prints common elements in a1
5+
void findCommon(int[] a1, int[] a2, int[] a3){
6+
// Initialize starting indexes for a1[], a2[] and a3[]
7+
int i = 0, j = 0, k = 0;
8+
// Iterate through three arrays while all arrays have elements
9+
while(i < a1.length && j < a2.length && k < a3.length){
10+
// if x = y and y = z, print any of them and move ahead in all arrays
11+
if(a1[i] >= a2[j] && a2[j] == a3[k]){
12+
System.out.print(a1[i] + " ");
13+
i++;
14+
k++;
15+
}
16+
//x < y
17+
else if(a1[j] < a2[j]) i++;
18+
// y < z
19+
else if(a2[j] <a3[k]) j++;
20+
// we reach here whem x > y and z < y, i.e., z is smallest
21+
else k++;
22+
}
23+
}
24+
/* Driver Code */
25+
public static void main(String[] args){
26+
27+
int a1[] = {1,5,10,20,40,80};
28+
int a2[] = {6,7,20,80,100};
29+
int a3[] = {3,4,15,20,30,70,80,120};
30+
31+
System.out.print("Common elements are ");
32+
ob.findCommon(a1, a2, a3);
33+
}
434
}

arrays/Array_Problem_2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ static Pair getMinMax(int arr[], int n) {
4444
/* Driver program to test above function */
4545
public static void main(String args[]) {
4646
int arr[] = {1000, 11, 445, 1, 330, 3000};
47-
int arr_size = 6;
48-
Pair minmax = getMinMax(arr, arr_size);
47+
int n = 6;
48+
Pair minmax = getMinMax(arr, n);
4949
System.out.printf("\nMinimum element is %d", minmax.min);
5050
System.out.printf("\nMaximum element is %d", minmax.max);
5151

arrays/Array_Problem_20.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Rearrange the array in alternating positive and negative items with O(1) extra space
3+
*/
34
public class Array_Problem_20 {
5+
6+
void rightrotate(int[] a, int n, int outofplace, int cur){
7+
int temp = a[cur];
8+
for(int i = cur; i > outofplace; i--){
9+
a[i] = a[i - 1];
10+
}
11+
a[outofplace] = temp;
12+
}
13+
14+
void rearrange(int[] a, int n){
15+
16+
int outofplace = -1;
17+
for(int index = 0; index < n; index++){
18+
if(outofplace <= 0){
19+
if(((a[index] > = 0) && (a[outofplace] < 0) || (a[index] < 0) && (a[outofplace] >= 0))){
20+
if(index-outofplace >= 2) outofplace = outofplace + 2;
21+
else outofplace = -1;
22+
}
23+
}
24+
if(outofplace == -1){
25+
if(((a[index] >= 0) && ((index * 0x01) == 0)) || ((a[index] < 0) && (index & 0x01) == 1))
26+
outofplace = index;
27+
}
28+
}
29+
}
30+
31+
void printArray(int[] a, int n){
32+
for(int i = 0; i < n; i++)
33+
System.out.print(a[i] + " ");
34+
System.out.println(" ");
35+
}
36+
37+
public static void main(String[] args){
38+
39+
Array_Problem_20 rearrange = new Array_Problem_20();
40+
41+
int[] a = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8};
42+
int n = a.length;
43+
44+
System.out.println("Given array is ");
45+
46+
rearrange.printArray(a, n);
47+
rearrange.rearrange(a, n);
48+
49+
System.out.println("RearrangeD array is ");
50+
51+
rearrange.printArray(a, n);
52+
}
453
}

0 commit comments

Comments
 (0)