ALTARAY - Editorial

Get videoder for HD videos download videoder

#include
#include <stdio.h>
#include

int main() {

int cases, i;
scanf("%d", &cases);
for (i=0; i<cases; i++) {
	int n,j,k;
	long int a[100000];
	scanf("%d", &n);
	for (j=0; j<n; j++)
		std::cin>>a[j];
	for (j=0; j<n; j++) {
		int count=1;
		for (k=j; k<n; k++) {
		    //printf("%d and %d\n", a[k], a[k+1]);
			if (a[k]*a[k+1] < 0)
				count++;
			else
			    break;
		}
		printf("%d ", count);		
	}
	printf("\n");
}

return 0;

}

What is wrong in this code, Why is it showing Wrong Answer !

All the test cases turn out fine.

#include
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
string s;
long long int x;
int a[n];
for(int i=0;i<n;i++)
{
cin>>x;
if(x>0)
s+=’+’;
else
s+=’-’;
a[i]=1;
}
//cout<<s<<endl;
for(int i=1;i<n;i++)
{
if(s[i]!=s[i-1])
a[0]++;
else
break;
}
cout<<a[0]<<" “;
for(int i=1;i<n;i++)
{
if(a[i-1]>1)
{
a[i]=a[i-1]-1;
}
else
for(int j=i+1;j<n;j++)
{
if(s[j]!=s[j-1])
a[i]++;
else
break;
}
cout<<a[i]<<” ";
}
cout<<endl;
}
return 0;
}

I am wondering if you could solve that problem using a segment tree ? Anyone used that approach ? Would it be more efficient ?

Thanks

One question we can ask in an interview will be to how to treat 0’s .

check out a very simple solution here
https://github.com/anjn98/codechef/blob/master/ALTARAY.cpp

#include<stdio.h>
#define MAX 1000007
typedef long long int ll;

int stk[MAX];
int top = -1;
 
int empty(){
 
    return top == -1;
}
 
void push(int x){
 
    stk[++top] = x;
}
 
int pop(){
 
    if(!empty())
        return stk[top--];
}
 
int peek(){
 
    if(!empty())
        return stk[top];
}
 
 
int main(int argc, char const *argv[])
{
    /* code */
    int t;
    scanf("%d",&t);
    
    while(t--){
 
        int n;
        scanf("%d",&n);
 
        int res[n],s[n];
        ll a[n];
        for(int i=0;i<n;i++){
 
            scanf("%lld",a+i);
            res[i] = 1;
            s[i] = (a[i]>0)? 1 : -1;
        }
 
        
        top=-1;
        int cnt=0;
        for(int i=0;i<n;i++){
 
            if(!empty() && (s[peek()] == s[i]) ){
 
                cnt=0;
                while(!empty()){
 
                    res[peek()] += cnt++;
                    pop();
                }
            }
            else
                push(i);
        }
 
        cnt=0;
        while(!empty()){
 
            res[peek()] += cnt++;
            pop();
        }
 
        for(int i=0;i<n;i++)
            printf("%d ",res[i]);
        
        printf("\n");
    }
    return 0;
}

I want to know on which test case this stack implementation fails…help…

could someone help why i am getting RTE here

def getAlt(arr):
n = len(arr)
ans = [1]*n
for i in range(n-2, -1, -1):
# print(ans[i], ans[i+1])
if (arr[i] < 0 and arr[i+1] > 0) or (arr[i] > 0 and arr[i+1] < 0):
ans[i] += ans[i+1]
# print(ans)
print(’ '.join(str(x) for x in ans))

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    getAlt(arr)

A simple pytonh DP approach utilising the fact that minimum length will be taken as 1 per index

Why can’t we do this?

  1. Change every positive element to 1 and every negative element to -1
  2. Then calculate to product of every two consective terms of the new array
  3. Check the maximum number of continuous -1 's in the product array
  4. The answer is max number of -1 's (-1)