You are given a string S of length M. It consists of only two
characters;
S is a balanced bracket sequence. Formally, a sequence of brackets is balanced-if the following conditions are met:
You are required to insert the character '+' in S such that there exists at least one '+' enclosed within each pair of matched
bracket sequence. Find the minimum number of ' characters to be inserted in S to satisfy the provided condition.
Sample Input Sample Output
1 2
6
()(())
You are given the following:
For each (1 ≤ i ≤ n), perform the following operation exactly one
time:
Determine the maximum length of the subsequence of array a, such that all numbers in that subsequence are equal after applying the given operation.
Applying one operation on indices 1, 2, and 4, with x = 0 to get
array a as [2, 5, 1, 2].
Applying one operation on index 3, with x = 1 to get array a as [2, 5, 2, 2]
Therefore, the maximum length of the subsequence with equal numbers is 3.
Complete the Maximize qualNumbers function provided in the editor. This function takes the following 3 parameters and returns the maximum length of the subsequence:
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
0 ≤ k ≤ 109
1 ≤ ai ≤ 109
Sample Input Sample Output
2 2
4 0 2
2 2 5 6
3 2
1 5 6
The first line represents the number of test cases
For test case 1:
Applying one operation with x - 0 on index 1, 2, 3, and 4 to get array
as [2, 2,5, 6]
Consider a square matrix of size N sorted diagonally (bottom left to the top right). The diagonals in the matrix are numbered and filled with integer values from 1 to N2 as given below.
For example, consider a square matrix of size N = 3 then we have
the following matrix. You are given Q queries
and each query has an Integer x.
Determine the diagonal number to which x belongs.
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
• The first line contains an integer N denoting the size of the matrix.
• The next line contains an integer Q denoting the number queries.
• The next line contains Q space-separated integers denoting x for each query.
For each query, print a single integer in a new line representing
the diagonal number to which x belongs.
Sample Input Sample Output
5 1
3 6
1 16 25 9
Consider a square matrix of size N = 5
For Query 1,1 occurs in the diagonal numbered 1
For Query 2,16 occurs in the diagonal numbered 6
For Query 3,25 occurs in the diagonal numbered 9
Since every element can be from a{i}-k to a{i}+k we will calculate the range of numbers an element can be and then calculate the maximum overlapping range.
Then
void solve() {
int n, k;
cin >> n >> k;
int ans = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
/*since every element can be from a{i}-k to a{i}+k we will
calculate the range of numbers an element can be and then
calculate the maximum overlapping range */
vector<pair<int, char>> v;
for (int i = 0; i < n; i++) {
v.push_back({a[i] - k, 'x'});
v.push_back({a[i] + k, 'y'});
}
sort(v.begin(), v.end());
int count = 0;
for (int i = 0; i < v.size(); i++) {
// if x occur it means a new range
// is added so we increase count
if (v[i].second == 'x')
count++;
// if y occur it means a range
// is ended so we decrease count
if (v[i].second == 'y')
count--;
// updating the value of ans
// after every traversal
ans = max(ans, count);
}
cout << ans << endl;
return;
}
O(n log n) n is the size of the array.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
Arrays.sort(arr);
int idx=1;
int ans=1;
int l=0, r=1;
while(r<n){
while(idx<n&&arr[l]==arr[idx]){
idx++;
}
if(r<n&&arr[l]+k>=arr[r]-k){
r++;
ans=Math.max(ans,r-l);
}
else{
l=idx;
}
}
System.out.println(ans);
}
}
Problem 2
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n,k;
cin>>n>>k;
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr.begin(),arr.end());
int ans = 1;
for(int i=0;i<n;i++)
{
int count = 0;
int it = (arr.begin()+i) - upper_bound(arr.begin(),arr.begin()+i,arr[i]-k-1);
count += it;
it = upper_bound(arr.begin()+i,arr.end(),arr[i]+k) - (arr.begin()+i);
count += it;
ans = max(ans,count);
}
cout<<ans<<endl;
return 0;
}
Getting Wrong answer on 15th test case
A vector of pair is not required I believe, You can just sort the given array and use a sliding window approach to maximise the overlapping intervals.
# problem description : https://thejoboverflow.com/p/p1198/#p1199
[n, k] = [int(i) for i in input().split(" ")]
a = [ int(i) for i in input().split(" ")]
l = []
for i in range(n):
l.append([a[i]-k, 'S'])
l.append([a[i]+k, 'E'])
l.sort()
ans = 0
current = 0
for i in range(len(l)) :
if l[i][1] == "S" :
current += 1
else :
current -= 1
ans = max(ans, current)
print(ans)
Hi I am getting Wrong answer in test case 1 itself. Can you help in find out the error. Thanks in advance.