Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the solution of LC-523 #165

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions CodeForces/Coprime/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//Author : Surjendu Pal
//Problem Link : https://codeforces.com/contest/1742/problem/D
#include <bits/stdc++.h>
#include<string>
#include <iostream>
using namespace std;
using ll=long long;
const ll MOD = 1000000007;

int gcd(int a, int b)
{
return a?gcd(b%a,a):b;
}

void solve()
{
int n;
cin>>n;
vector<int>v(200010),mp(200010,0);
for(int i=1;i<=n;i++) {
cin>>v[i];
mp[v[i]] = i;
}

int ans = -1;
for(int i=1;i<=1000;i++) {
for(int j=1;j<=1000;j++) {
if(gcd(i,j)==1 and mp[i]!=0 and mp[j]!=0) {
ans = max(ans,mp[i]+mp[j]);
}
}
}
cout<<ans<<endl;
}



int main()
{
int tc=1;
cin>>tc;
while(tc--){
solve();
}
}



5 changes: 5 additions & 0 deletions CodeForces/Coprime/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
As the test case have 10^5 order we may have get TLE in brut force so we go for some more optimal one.
Here it is...
1. First of all we take the array elements and its index in a map
2. Itearate upto 1000 as it is the highest value of array element and check for coprime through gcd and also check the array elements present in map or not.
3. If condition satisfied calculate maximum value of (i+j)
55 changes: 55 additions & 0 deletions CodeForces/Coprime/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
**Link to problem:** https://codeforces.com/contest/1742/problem/D

**Description:**

time limit per test : 3 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

Given an array of n positive integers a1,a2,…,an (1≤ai≤1000). Find the maximum value of i+j such that ai and aj are coprime,† or −1 if no such i, j exist.

For example consider the array [1,3,5,2,4,7,7]. The maximum value of i+j that can be obtained is 5+7, since a5=4 and a7=7 are coprime.

† Two integers p and q are [coprime](https://en.wikipedia.org/wiki/Coprime_integers) if the only positive integer that is a divisor of both of them is 1 (that is, their [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) is 1).

### Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤10) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n (2≤n≤2⋅105) — the length of the array.

The following line contains n space-separated positive integers a1, a2,..., an (1≤ai≤1000) — the elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

### Output

For each test case, output a single integer — the maximum value of i+j such that i and j satisfy the condition that ai and aj are coprime, or output −1 in case no i, j satisfy the condition.

### Input

6
3
3 2 1
7
1 3 5 2 4 7 7
5
1 2 3 4 5
3
2 2 4
6
5 4 3 15 12 16
5
1 2 2 3 6
8


### Output

6
12
9
-1
10
7
35 changes: 35 additions & 0 deletions CodeForces/Increasing/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Author : Surjendu Pal
//Problem Link : https://codeforces.com/contest/1742/problem/B
#include <bits/stdc++.h>
#include<string>
#include <iostream>
using namespace std;
using ll=long long;
const ll MOD = 1000000007;



void solve()
{
int n;cin>>n;
vector<int>v(n);
set<int>st;
for(auto &x:v){
cin>>x;
st.insert(x);
}

cout<<((st.size()==n)?"YES":"NO")<<endl;
}



int main()
{
int tc=1;
cin>>tc;
while(tc--){
solve();
}
}

1 change: 1 addition & 0 deletions CodeForces/Increasing/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
First take all the array element in a set and then check if the size of the set is equal to size of array or not, by means we are checking the all the elements are distinct or not
46 changes: 46 additions & 0 deletions CodeForces/Increasing/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
**Link to problem:** https://codeforces.com/contest/1742/problem/B

**Description:**

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

You are given an array a of n positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that a1<a2<⋯<an holds.


### Input

The first line contains a single integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤100) — the length of the array.

The second line of each test case contains n integers ai (1≤ai≤109) — the elements of the array.

### Output

For each test case, output "YES" (without quotes) if the array satisfies the condition, and "NO" (without quotes) otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

### Input

3
4
1 1 1 1
5
8 7 1 3 4
1
5



### Output

NO
YES
YES



61 changes: 61 additions & 0 deletions CodeForces/Scuza/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//Author : Surjendu Pal
//Problem Link : https://codeforces.com/contest/1742/problem/E
#include <bits/stdc++.h>
#include<string>
#include <iostream>
using namespace std;
using ll=long long;
const ll MOD = 1000000007;



void solve()
{
int n, q;
cin >> n >> q;
vector<int> v(n);
vector<long long> preSum(n+1);
for (int i = 0; i < n; i++) {
cin >> v[i];
preSum[i+1] = preSum[i]+v[i];
}
for (int i = 1; i < n; i++) v[i] = max(v[i-1], v[i]);
while (q--) {
int x;
cin >> x;
int l = -1;
int h = n;
while (h-l > 1) {
int mid = (l+h)/2;
if (v[mid] > x) h = mid;
else l = mid;
}
cout << preSum[h] << ' ';
}
cout << '\n';
}



int main()
{
int tc=1;
cin>>tc;
while(tc--){
solve();
}
}



int main()
{
int tc=1;
cin>>tc;
while(tc--){
solve();
}
}



3 changes: 3 additions & 0 deletions CodeForces/Scuza/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Here it is...
1. First of all calculate the maximum prefix sum of the given array
2. Then for each number in second array (query) do binary search in the array for the value in each query/array.
Binary file added CodeForces/Scuza/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions CodeForces/Scuza/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
**Link to problem:** https://codeforces.com/contest/1742/problem/E

**Description:**

time limit per test : 3 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

Timur has a stairway with n steps. The i-th step is ai meters higher than its predecessor. The first step is a1 meters higher than the ground, and the ground starts at 0 meters.

![](image.png)

Timur has q questions, each denoted by an integer k1,…,kq. For each question ki, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length ki. Timur can only climb the j-th step if his legs are of length at least aj. In other words, ki≥aj for each step j climbed.

Note that you should answer each question independently.

### Input

The first line contains a single integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains two integers n,q (1≤n,q≤2⋅105) — the number of steps and the number of questions, respectively.

The second line of each test case contains n integers (1≤ai≤109) — the height of the steps.

The third line of each test case contains q integers (0≤ki≤109) — the numbers for each question.

It is guaranteed that the sum of n does not exceed 2⋅105, and the sum of q does not exceed 2⋅105.

### Output

For each test case, output a single line containing q integers, the answer for each question.

Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

### Input

3
4 5
1 2 1 5
1 2 4 9 10
2 2
1 1
0 1
3 1
1000000000 1000000000 1000000000
1000000000



### Output

1 4 4 9 9
0 2
3000000000

40 changes: 40 additions & 0 deletions CodeForces/Stripes/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Author : Surjendu Pal
//Problem Link : https://codeforces.com/contest/1742/problem/C
#include <bits/stdc++.h>
#include<string>
#include <iostream>
using namespace std;
using ll=long long;
const ll MOD = 1000000007;



void solve()
{
char v[8][8];

char ans = 'B';
for(int i=0;i<8;i++) {
int cr = 0;
for(int j=0;j<8;j++) {
cin>>v[i][j];
if(v[i][j]=='R')cr++;
if(cr==8){
ans='R';
}
}
}

cout<<ans<<endl;
}



int main()
{
int tc=1;
cin>>tc;
while(tc--){
solve();
}
}
1 change: 1 addition & 0 deletions CodeForces/Stripes/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Iterate over the grid matrix and check for "R" count is 8 or not,means if Red strip painted last so the must be one strip with 8 cell contiguously filled. If not then "B" will be the answer
Binary file added CodeForces/Stripes/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading