That not the problem I can also take it as a[10000]
main problem in the complexity of this code
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(a[i]!=a[j] && a[a[i]] == a[a[j]]){
flag=1;
break;
}
}
}
please help in optimizing this
I am trying to compare whether in a given array I can found this sequence or not
A1,A2,…,AN. we need to determine if it is possible to choose two indices i and j such that Ai≠Aj, but Asub(Ai) = Asub(Aj).
sub==subscript
No Let me give you the example
for this sequence
1 1 2 3
it is true because Asub(A3)=Asub(A1) and A3≠A1
and for this the answer is no
2 1 3 3
because Asub(A3)=Asub(A4) but A3=A4,
I am starting from 1 index in all the example
I couldn't think of anything.
Did you try adding the flag test to the outer loop?
Is that not good enough?
If you're trying to solve a programming problem, it's best to give a link to it so I can see the details.
Chef has a sequence A1,A2,…,AN; each element of this sequence is either 0 or 1. Appy gave him a string S with length Q describing a sequence of queries. There are two types of queries:
'!': right-shift the sequence A, i.e. replace A by another sequence B1,B2,…,BN satisfying Bi+1=Ai for each valid i and B1=AN
'?': find the length of the longest contiguous subsequence of A with length ≤K such that each element of this subsequence is equal to 1
Answer all queries of the second type.
Input
The first line of the input contains three space-separated integers N, Q and K.
The second line contains N space-separated integers A1,A2,…,AN.
The third line contains a string with length Q describing queries. Each character of this string is either '?', denoting a query of the second type, or '!', denoting a query of the first type.
Output
For each query of the second type, print a single line containing one integer — the length of the longest required subsequence.
Constraints
1≤K≤N≤105
1≤Q≤3⋅105
0≤Ai≤1 for each valid i
S contains only characters '?' and '!'
Subtasks
Subtask #1 (30 points):
1≤N≤103
1≤Q≤3⋅103
Subtask #2 (70 points): original constraints
Example Input
5 5 3
1 1 0 1 1
?!?!?
Example Output
2
3
3
Explanation
In the first query, there are two longest contiguous subsequences containing only 1-s: A1,A2 and A4,A5. Each has length 2.
After the second query, the sequence A is [1,1,1,0,1].
In the third query, the longest contiguous subsequence containing only 1-s is A1,A2,A3.
After the fourth query, A=[1,1,1,1,0].
In the fifth query, the longest contiguous subsequence containing only 1-s is A1,A2,A3,A4 with length 4. However, we only want subsequences with lengths ≤K. One of the longest such subsequences is A2,A3,A4, with length 3.
@yoyohoney what concept are you using for this I have got 100 pts I could optimize your code
Or can you just share the code I will optimize that code itself
@yoyohoney I have got 100 by using brute force itself ,It is not a completely a brute force but still its work for all cases
I cann't share the same ans as judge would banned both account so give me your brute force method I would optimize it with some specific conditions
I am using bitmasking this is not the complete code but you will get the idea
#include<bits/stdc++.h>
using namespace std;
#define MAX 100000
int main()
{
int n,q,k,count;
string str;
cin>>n>>q>>k;
bitset<MAX>s,c;
//inserting bits in array
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
s[i]=temp;
}
cin>>str;
for(int i=0;i<q;i++)
{
//making duplicate bitset
c=s;
if(str[i]=='?')
{
count=0;
while(c!=0)
{
//using bitmask to count maximum no of continuous 1's-O(1's bit)
c=(c&(c<<1));
count++;
}
if(count>k)
cout<<k<<"\n";
else
cout<<count<<"\n";
}
else
{
//shifting each bit to right and updating first bit with previous last bit
bool lb=s[n-1];
s=s>>1;
s[n-1]=lb;
}
}
return 0;
}
If you share your brute force code then it would be good as I would love to do by that method also
So please do share that as well I am willing to do that also