Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf19682

Browse files
leetcode weekly 312
1 parent d30caf5 commit bf19682

File tree

4 files changed

+283
-1
lines changed

4 files changed

+283
-1
lines changed

‎DSAPractice/recursion/.cph/.LockFreeStack.java_d74be5e2e686bf6a5861633c57fcbb5c.prob‎

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* import java.io.*;
3+
* import java.util.*;
4+
*
5+
* class LongestSubarrayWithMaximumBitwiseAND {
6+
* static BufferedReader br;
7+
* static StringTokenizer st;
8+
*
9+
* static int nextInt() throws IOException {
10+
* return Integer.parseInt(next());
11+
* }
12+
*
13+
* static String next() throws IOException {
14+
* while (st == null || !st.hasMoreTokens()) {
15+
* st = new StringTokenizer(br.readLine());
16+
* }
17+
* return st.nextToken();
18+
* }
19+
*
20+
* static long nextLong() throws IOException {
21+
* return Long.parseLong(next());
22+
* }
23+
*
24+
* public static void main(String args[]) throws IOException {
25+
* br = new BufferedReader(new InputStreamReader(System.in));
26+
* int t = nextInt();
27+
* while (t-- > 0) {
28+
* input();
29+
* }
30+
* br.close();
31+
* }
32+
*
33+
* public static void input() throws IOException {
34+
* }
35+
* }
36+
*
37+
*/
38+
39+
// the bitwise AND of two different numbers will always be strictly less
40+
// than the maximum of those two numbers
41+
// so the longest subarray with max bitwise AND would be the subarray
42+
// containing only the max numbers
43+
// count length of the subarray where only maximum number is included
44+
45+
// example : [1,3,4,5,5,5,6,7,8,8,8,8]
46+
47+
// Ans : 4
48+
49+
import java.io.*;
50+
import java.util.*;
51+
52+
class LongestSubarrayWithMaximumBitwiseAND {
53+
static BufferedReader br;
54+
static StringTokenizer st;
55+
56+
static int nextInt() throws IOException {
57+
return Integer.parseInt(next());
58+
}
59+
60+
static String next() throws IOException {
61+
while (st == null || !st.hasMoreTokens()) {
62+
st = new StringTokenizer(br.readLine());
63+
}
64+
return st.nextToken();
65+
}
66+
67+
static long nextLong() throws IOException {
68+
return Long.parseLong(next());
69+
}
70+
71+
public static void main(String args[]) throws IOException {
72+
br = new BufferedReader(new InputStreamReader(System.in));
73+
int t = nextInt();
74+
while (t-- > 0) {
75+
input();
76+
}
77+
br.close();
78+
}
79+
80+
public static void input() throws IOException {
81+
int n = nextInt();
82+
int nums[] = new int[n];
83+
for (int i = 0; i < n; i++) {
84+
nums[i] = nextInt();
85+
86+
}
87+
88+
System.out.println(longestSubarray(nums));
89+
}
90+
91+
static int longestSubarray(int[] nums) {
92+
int result = 0, length = 0, max = 0;
93+
for (int ele : nums) {
94+
max = Math.max(max, ele);
95+
}
96+
97+
for (int ele : nums) {
98+
if (ele == max)
99+
length++;
100+
else
101+
length = 0;
102+
result = Math.max(result, length);
103+
}
104+
105+
return result;
106+
}
107+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class SortthePeople {
5+
static BufferedReader br;
6+
static StringTokenizer st;
7+
8+
static int nextInt() throws IOException {
9+
return Integer.parseInt(next());
10+
}
11+
12+
static String next() throws IOException {
13+
while (st == null || !st.hasMoreTokens()) {
14+
st = new StringTokenizer(br.readLine());
15+
}
16+
return st.nextToken();
17+
}
18+
19+
static long nextLong() throws IOException {
20+
return Long.parseLong(next());
21+
}
22+
23+
public static void main(String args[]) throws IOException {
24+
br = new BufferedReader(new InputStreamReader(System.in));
25+
int t = nextInt();
26+
while (t-- > 0) {
27+
input();
28+
}
29+
br.close();
30+
}
31+
32+
public static void input() throws IOException {
33+
int n = nextInt();
34+
int heights[] = new int[n];
35+
String names[] = new String[n];
36+
37+
for (int i = 0; i < n; i++) {
38+
heights[i] = nextInt();
39+
}
40+
41+
for (int i = 0; i < n; i++) {
42+
names[i] = next();
43+
}
44+
45+
System.out.println(Arrays.toString(sortPeople(names, heights)));
46+
47+
}
48+
49+
static String[] sortPeople(String[] names, int[] heights) {
50+
quickSort(heights, names, 0, heights.length - 1);
51+
return names;
52+
53+
}
54+
55+
static void quickSort(int[] heights, String[] names, int low, int high) {
56+
if (low < high) {
57+
58+
int pi = partition(heights, names, low, high);
59+
60+
quickSort(heights, names, low, pi - 1);
61+
quickSort(heights, names, pi + 1, high);
62+
}
63+
}
64+
65+
static int partition(int[] heights, String[] names, int low, int high) {
66+
67+
int pivot = heights[high];
68+
69+
int i = (low - 1);
70+
71+
for (int j = low; j <= high - 1; j++) {
72+
73+
if (heights[j] > pivot) {
74+
75+
i++;
76+
swap(heights, i, j);
77+
swap(names, i, j);
78+
79+
}
80+
}
81+
swap(heights, i + 1, high);
82+
swap(names, i + 1, high);
83+
return (i + 1);
84+
}
85+
86+
static void swap(int[] heights, int i, int j) {
87+
int temp = heights[i];
88+
heights[i] = heights[j];
89+
heights[j] = temp;
90+
}
91+
92+
static void swap(String[] names, int i, int j) {
93+
String temp = names[i];
94+
names[i] = names[j];
95+
names[j] = temp;
96+
}
97+
98+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
/** */
3+
import java.util.*;
4+
5+
public class maximumbitwiseor {
6+
7+
// Function to convert bit array to
8+
// decimal number
9+
static int build_num(int bit[]) {
10+
int ans = 0;
11+
12+
for (int i = 0; i < 32; i++)
13+
if (bit[i] > 0)
14+
ans += (1 << i);
15+
16+
return ans;
17+
}
18+
19+
// Function to find maximum values of
20+
// each bitwise OR operation on
21+
// element of subarray of size K
22+
static int maximumOR(int arr[], int n, int k) {
23+
// Maintain an integer array bit[]
24+
// of size 32 all initialized to 0
25+
int bit[] = new int[32];
26+
27+
// Create a sliding window of size k
28+
for (int i = 0; i < k; i++) {
29+
for (int j = 0; j < 32; j++) {
30+
System.out.println("i=" + i + ", j=" + j);
31+
if ((arr[i] & (1 << j)) > 0) {
32+
bit[j]++;
33+
System.out.println(bit);
34+
}
35+
}
36+
}
37+
38+
System.out.println(Arrays.toString(bit));
39+
// Function call
40+
int max_or = build_num(bit);
41+
42+
for (int i = k; i < n; i++) {
43+
44+
// Perform operation for
45+
// removed element
46+
for (int j = 0; j < 32; j++) {
47+
if ((arr[i - k] & (1 << j)) > 0)
48+
bit[j]--;
49+
}
50+
51+
// Perform operation for
52+
// added_element
53+
for (int j = 0; j < 32; j++) {
54+
if ((arr[i] & (1 << j)) > 0)
55+
bit[j]++;
56+
}
57+
58+
// Taking maximum value
59+
max_or = Math.max(build_num(bit), max_or);
60+
}
61+
62+
// Return the result
63+
return max_or;
64+
}
65+
66+
// Driver Code
67+
public static void main(String[] args) {
68+
// Given array arr[]
69+
int arr[] = { 2, 5, 3, 6, 11, 13 };
70+
71+
// Given subarray size K
72+
int k = 3;
73+
int n = arr.length;
74+
75+
// Function Call
76+
System.out.print(maximumOR(arr, n, k));
77+
}
78+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /