Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

go goa gone


WA in MARCHA1?

$
0
0

The code I've written passes all sample test cases and i tried with many other too, but it is sstill giving me wrong answer? Can someone please help?

import java.util.Scanner;

class marcha1
{
    public static String to_pay(int a[],int n,int m)
    {
        if(m == 0)
            return "Yes";
        if(n<0)
            return "No";
        String ans =new String();
        for(int i=n;i>=0;i--)
        {
            if(m-a[i]<0)
                continue;

            ans = to_pay(a,i-1,m-a[i]);
            if(ans == "Yes")
                break;
        }

        return ans;
    }

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();

        if(t<=100)
            while(t>0)
            {
                int n = input.nextInt();
                int m = input.nextInt();

                int[] a = new int[n];
                if(n<=20 && m<=1000)
                {
                    for(int i=0;i<n;i++)
                    a[i] = input.nextInt();

                                    //Sorting
                    for(int i=0;i<n;i++)
                    for(int j=1;j<n-i;j++)
                        {
                            if(a[j-1]>a[j])
                            {
                                int temp = a[j];
                                a[j] = a[j-1];
                                a[j-1] = temp;
                            }
                        }
                    System.out.println(to_pay(a,n-1,m));

                    }

                t--;
            }

        input.close();
    }
}

why my solution of DESTROY problem wrong?

code optemization

$
0
0

Can any one help me with code optimizationRemove for loop

there is a array and i want to check if arr[i ]% h is 0 or not can any one tell me the better approach

problem in the code of "Download File"

$
0
0

What is the problem in the code,even though the answers are correct as per the examples given??(Problem : Download File (beginner))

//CC Download file (Beginner)

include <stdio.h>

include <stdlib.h>

int main() { int j,i,k,t,n,a; scanf(" %d",&t); for (j=0;j<t;j++) { scanf(" %d%d",&n,&k); int b[n]; int c[n];

    for (i=0;i<n;i++)
    {
        scanf("%d%d",&b[i],&c[i]);
    }
    int s=0;
    for (i=0;i<n;i++)
    {
        a=b[i]*c[i];
        s=s+a;

    }
    int m=0,l=0,f,g,v;
    for (i=0;i<n;i++)
    {
        m=m+b[i];
        if (m<=k)
        {
            v=c[i]*b[i];
            l=l+v;
        }
        if (m>k)
        {
            f=k-m+b[i];
            g=f*c[i];
            break;
        }
    }
    s=s-g-l;
    printf("%d\n",s);
}

return 0;

}

PSHTTR - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Ivan Fekete
Primary Tester:Misha Chorniy
Editorialist:Hussain Kara Fallah

DIFFICULTY:

Medium

PREREQUISITES:

Graph Theory,Sorting,Data Structures

PROBLEM:

Given a rooted weighted tree with N nodes and M queries of the form (u , v , K). For each query, you must report to xor sum of edges which has weight less than or equal to K on the path from u to v.

N,M ≤ 10^5

EXPLANATION:

First of all let's solve the easier version of this problem. Queries which asks for the xor sum of edges on the path between 2 fixed nodes (without the restriction of K). Let's choose an arbitrary root for our tree and call a Depth-First search starting from this node.Let's maintain a table S[N], such that Si denotes the xor sum of edges weights starting from the root and ending at node i. The answer of each query (u,v) would be (Su xor Sv). Edges of the chain starting from the root and ending at LCA(u,v) (lowest common ancestor) would be excluded because (V xor V = 0).

Now let's solve our problem. First thing we should take advantage of, is that we can answer our queries offline (reading then processing all queries, after that reporting the answer of each one). Each edge weighted W must be included in the answer of all queries with K ≥ W. For queries with K < W we can assume that this edge's weight is zero (so it won't affect the answers).

Let's sort our queries in ascending order according to their magic numbers K, and sort our edges in ascending order according to their weights W. Let's process our queries in ascending order, and maintain a pointer iterating on our sorted edges list. So before processing a query with magic number K, we add all edges with W ≤ K through our pointer.

Now Let's discuss adding edges, and how will we get our table S[].

Let's maintain a timer incremented by one every time we visit a new node in our Depth-First-Search, and keep for the ith node a variable sti (denoting the value of the timer once entering this node),and a variable eni denoting the value of the timer after finishing this node's subtree (before exiting). This is a famous technique called euler tour on tree (dfs order). So we can represent nodes located in the subtree of the ith node by interval [sti , eni]

Regarding adding edges, each edge will link between 2 nodes (u,v) such that u = par[v], this edge's weight will be added to the xor sum (cumulative xor sum from the root) of all nodes located in the subtree of v. That means we should apply the operation

Si = Si xor V

for each i : (stv≤ i ≤ env)

This can be done using a binary indexed tree, segment tree (with/without) lazy probagation. Since our modification query is done on a segment of array S and we are querying the value of only one element we can do the following:

Let's maintain an array X which is all zeros at the beginning. When handling an edge of weight W added to the results of nodes in node v subtree.

X[sti] = X[sti] xor V

This means that we are adding V to the xor sum of all nodes with enterance timer ≥ stv

X[eni + 1] = X[eni + 1] xor V

This means that we are excluding V from the xor sum of all elements with entrance timer > en[v] (since it was added in the first operation so doing xor again is equivalent to exclusion).

You can notice now that the value of Si is:

Si = X1 xor X2 xor X3 xor X4 xor X5 xor ... Xi

Now we can easily iterate through our queries, and for each one we should make sure that all edges with weight less than or equal to this query magic number are added. After that, each query can be solved in O(log N).

Total complexity O(N log N + M (log M + log N))

AUTHOR'S AND TESTER'S SOLUTIONS:

AUTHOR's solution: Will be found here
TESTER's solution: Will be found here
EDITORIALIST's solution: Will be found here

How to solve problem A from Kickstart Round E ?

$
0
0

Here's the link to the problem statement :
https://code.google.com/codejam/contest/12234486/dashboard#s=p0
My approach :
dp[i] = minimum number of operations to form the first i characters.

My Code:
https://ideone.com/DgynCm

For each position 'i' see if the substring formed by next 'j' (for all possible 'j') characters is present in the the first 'i' characters, and try to append it as many times as possbile, while updating the solution for the higher positions by taking minimum of existing solution and new solutions. Basically, build solutions for smaller lengths, and try to extend them to larger strings in every way possible.

If someone has solved this / can find out where my logic is going wrong, please help me. Thanks.

Problem with Pishty and tree (PSHTTR)...


What should I do if i want to add a question in practise section

$
0
0

What should I do if i want to add a question in practise section?

Regarding modulo greater than 10^9

$
0
0

How will you approach a problem if it requires product of 2 numbers modulo ${10}^{10}+11$ or anything more than ${10}^{9}$?

Cannot use simple modulo since the product can exceed the range of long long in C++. Any tips/tricks?

WEASELTX - Editorial

$
0
0

PROBLEM LINK:

Practice

Contest

Author:Bogdan Ciobanu

Tester:Jingbo Shang

Editorialist:Hanlin Ren

DIFFICULTY:

Medium-hard

PREREQUISITES:

binomial coefficients, Lucas's theorem, multidimensional prefix sum

PROBLEM:

You are given a rooted tree with root number $0$. Every node $u$ has a weight $X_{0,u}$. When $d>0$, define $X_{d,v}$ is the bitwise-xor sum of all $X_{d-1,u}$ where $u$ is in $v$'s subtree. You are also given $Q$ queries, each query is a number $\Delta$, and you need to output $X_{\Delta,0}$.

QUICK EXPLANATION:

Let $Y_d$ be the xor-sum of weights of all nodes whose depth is exactly $d$, then the answer to a query $\Delta$ is the xor-sum of all $Y_d$'s, where $(\Delta-1)\text{ and }d=0$. By using a dp technique similar to multidimensional prefix sum, one can compute $Z_d$, which means the xor-sum of all $Y_k$'s where $k\text{ and }d=0$, in $O(N\log N)$ time, and the query takes only constant time.

EXPLANATION:

subtask 1

Since $N,\Delta\le 500$, we can calculate $X_{i,u}$ for all $0\le i\le 500$, $0\le u < N$. Given $X_{i,0},X_{i,1},\dots,X_{i,N-1}$, we can compute $X_{i+1,0},X_{i+1,1},\dots,X_{i+1,N-1}$ by doing one dfs on tree. This gives an $O(N\cdot\max\Delta)$ algorithm.

other subtasks

Let's first consider, what if "xor" is changed to addition? i.e., what if $X_{i+1,u}$ is defined as the sum, rather than xor, of all $X_{i,v}$'s where $v$ is in $u$'s subtree? Obviously the answer is a linear combination of all weights, i.e., $X_{\Delta,0}=\sum_{u=0}^{N-1}f(\Delta,u)X_{0,u}$, where $f(\Delta,u)$ is something only dependent with $\Delta$ and $u$.

What is $f(\Delta,u)$?

TL;DR: $f(\Delta,u)=\binom{dep_u+\Delta-1}{\Delta-1}$ where $dep_u$ is $u$'s depth and $dep_0=0$.

Now let's consider how to solve $f(\Delta,u)$. Take the sample input as an example:

Sample

Then we have:

$$\begin{align*} X_{1,0}=&X_{0,0}+X_{0,1}+X_{0,2}+X_{0,3};\\ X_{2,0}=&X_{1,0}+X_{1,1}+X_{1,2}+X_{1,3}\\ =&(X_{0,0}+X_{0,1}+X_{0,2}+X_{0,3})+(X_{0,1}+X_{0,2})+X_{0,2}+X_{0,3}\\ =&X_{0,0}+2X_{0,1}+3X_{0,2}+2X_{0,3};\\ X_{3,0}=&X_{1,0}+2X_{1,1}+3X_{1,2}+2X_{1,3}\\ =&(X_{0,0}+X_{0,1}+X_{0,2}+X_{0,3})+2(X_{0,1}+X_{0,2})+3X_{0,2}+2X_{0,3}\\ =&X_{0,0}+3X_{0,1}+6X_{0,2}+3X_{0,3};\\&\dots \end{align*}$$

For example, $f(2,3)=3$ and $f(2,2)=6$ here.

A recurrence equation of $f$ is: $f(\Delta,u)=\sum_{v\text{ is }u\text{'s ancestor}}f(\Delta-1,v)$(note that $v$ could be $u$). Why? Note that when we calculate $X_{\Delta,0}$, we write $$\begin{align*} X_{\Delta,0}=&\sum_vf(\Delta-1,v)X_{1_v}\\ =&\sum_vf(\Delta-1,v)\sum_{u\text{ is }v\text{'s offspring}}X_{0,u}\\ =&\sum_uX_{0,u}\sum_{v\text{ is }u\text{'s ancestor}}f(\Delta-1,v);\\ \text{also, }X_{\Delta,0}=&\sum_uX_{0,u}f(\Delta,u). \end{align*}$$

This explains the above equation.

Let's do more on the equation: $$\begin{align*} f(\Delta,u)=&\sum_{v_1\uparrow u}f(\Delta-1,v_1)&\text{we use }a\uparrow b\text{ to represent that }a\text{ is }b\text{'s ancestor(possibly }a=b\text{)}\\ =&\sum_{v_1\uparrow u}\sum_{v_2\uparrow v_1}f(\Delta-2,v_2)\\ =&\dots\\ =&\sum_{v_1\uparrow u}\sum_{v_2\uparrow v_1}\dots\sum_{v_{\Delta}\uparrow v_{\Delta-1}}f(0,v_{\Delta}). \end{align*}$$

Thus, $f(\Delta,u)$ is the number of sequences $(v_0,v_1,v_2,\dots,v_{\Delta})$ such that:

  • $v_0=u$;
  • $v_{\Delta}=0$(note that $f(0,v)=[v=0]$);
  • For all $1\le i\le\Delta$, $v_i\uparrow v_{i-1}$.

Obviously all $v_i$'s appear on the path from $u$ to $0$. Let $dep_x$ be the depth of node $x$($dep_0=0$) and $d_i=dep_{v_{i-1}}-dep_{v_i}$, then $(d_1,d_2,\dots,d_{\Delta})$ is an array satisfying the following condition:

  • $d_i$'s are nonnegative integers;
  • $\sum_{i=1}^{\Delta}d_i=dep_u$.

We find that every array $d$ satisfying the above condition gives us a unique valid sequence $v$! So $f(\Delta,u)$ is just the number of such array $d$'s. Next is a classical lemma stating this number is just $\binom{dep_u+\Delta-1}{\Delta-1}$(refer to Wikipedia, the last line of "Definition and interpretations"). We omit the proof here.

Lemma 1: given $n,k$, the number of nonnegative solutions of $x_1+x_2+\dots+x_n=k$ is $\binom{n+k-1}{n-1}$.

Coming back to XOR

Now let's consider the xor case. Note that xoring the same number for even times does nothing, so for a query $\Delta$, we pick all nodes $u$ such that $f(\Delta,u)$ is odd, and xor them up. Next we'll use a lemma called Lucas's Theorem:

Lemma 2: given $n,m,p$, and $p$ is a prime. Let's write $n,m$ in base $p$: $$\begin{align*} n=&\overline{n_kn_{k-1}\dots n_1n_0};\\ m=&\overline{m_km_{k-1}\dots m_1m_0}, \end{align*}$$Then, $$\binom{n}{m}\equiv\prod_{i=0}^k\binom{n_i}{m_i}\pmod p.$$

Let me demonstrate the lemma by an example. Let $p=5$, $n=116$, $m=55$. Then $n=(\overline{431})_5,m=(\overline{210})_5$, so $\binom{n}{m}\equiv \binom{4}{2}\cdot\binom{3}{1}\cdot\binom{2}{0}\equiv 3\pmod 5$. Actually, you can check that $\binom{n}{m}=5265169722428127562717416598795968\equiv 3\pmod 5$.

How does the theorem help us? Note that we only need to know the reminder $\binom{a}{b}\bmod 2$ for some huge $a,b$. When $p=2$, Lucas's theorem becomes

Lemma 3: $\binom{n}{m}\equiv 1\pmod 2$ if and only if $n\text{ and }m=m$, where $\text{and}$ is the bitwise-and operation.

Thus, $f(\Delta,u)\equiv 1\pmod 2\iff \binom{\Delta-1+dep_u}{dep_u}\equiv 1\pmod 2\iff (\Delta-1+dep_u)\text{ and }dep_u=dep_u$.

subtask 2

To solve subtask 2, we preprocess $Y_d$ as the bitwise-xor sum of all nodes at depth exactly $d$, and for a query $\Delta$ we enumerate all $d$ from $0$ to $N$, if $(\Delta-1+d)\text{ and }d=d$, we xor the answer with $Y_d$.

Time complexity: $O(NQ)$.

subtask 3

If you print the values of $X_{i,0}$ and try to find patterns, you'll find that $X_{i,0}$ has a period of length $L\le 2N$. The solution for this subtask is: first find the length of that period $L$, then prepare all $X_{i,0}$'s for $i\le L$; for any query $\Delta$, we just print $X_{\Delta\bmod L,0}$.

In the solution of subtask 4 I'll show that, the answer only depends on the last $\lceil\log_2 N\rceil$ bits of $\Delta-1$, and that's why we have an $O(N)$ period.

subtask 4

Can the condition "$(\Delta-1+d)\text{ and }d=d$" be further simplified? Yes! Note that $x\text{ and }y=0$ is a sufficient condition for $(x+y)\text{ and }y=y$, since when adding $x$ and $y$ in binary, no carries would happen. Is it necessary? The answer turns out to be yes! This can be proved by contradiction: Suppose $i$ is the lowest bit that both $x$ and $y$ has $1$ on this bit. Then $(x+y)$'s $i$-th bit is $0$ and that violates $(x+y)\text{ and }y=y$. Thus "$(\Delta-1+d)\text{ and }d=d$" is equivalent to "$(\Delta-1)\text{ and }d=0$".

Let $Z_d$ be the bitwise-xor sum of $Y_f$'s such that $f\text{ and }d=0$. For a query $\Delta$ we directly output $Z_{(\Delta-1)\bmod 2^{18}}$, since $2^{18}>N$ and $((\Delta-1)\text{ and }d)$ is only related to $(\Delta-1)$'s last $18$ bits.

How to compute $Z_d$? We can do it by a dp that's similar to multidimensional prefix sum: Let $dp_{i,j}$ denote the bitwise-xor sum of all $Y_d$'s, such that:

  • For $0\le k < i$, $d$ and $j$'s $k$-th bit can't be both $1$;
  • For $i\le k < 18$, $d$ and $j$'s $k$-th bit are the same.

Then $dp_{i+1,j}=\begin{cases} dp_{i,j\text{ xor }2^i}&i\text{-th bit of }j\text{ is }1\\ dp_{i,j}\text{ xor }dp_{i,j\text{ xor }2^i}&i\text{-th bit of }j\text{ is }0\\ \end{cases}$. Note that $dp_{0,j}=Y_j$, and what we want is $Z_j=dp_{18,j}$.

The overall complexity is $O(N\log N+M)$.

ALTERNATIVE SOLUTION:

If your solution is different with ours, feel free to leave a comment.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.
Editorialist's solution can be found here.

spoj NthPRIME

PLease help me with this C program

$
0
0

Help me with is ------->The success of IEEE code-quest depends on number of times the factor is present in a given string.Help core members to find them Factor of string is defined as a substring.In this case it's '01'. Input First line of input contains an integer T, denoting the number of test cases. Then follows description of T cases.Each line of input is a binary form. Output Print the number of times the factor is present in a given string.

Constraints

1 <= S <= 100000

1 <= T <= 50

USE C PROGRAM

TEST CASE 1

INPUT

2

1001010100001

100101

OUTPUT

4

2

TEST CASE 2

INPUT

1

10001000101

OUTPUT

3

QGRID - Editorial

$
0
0

PROBLEM LINK:

Practice

Contest

Author:Hanlin Ren

Tester:Jingbo Shang

Editorialist:Hanlin Ren

DIFFICULTY:

Hard

PREREQUISITES:

shortest path tree, divide and conquer, heavy-light decomposition

PROBLEM:

You are given an $M\times N$ undirected grid graph. Every edge has a weight that's fixed and used to calculate shortest paths. Every vertex has a weight that's initially $0$. The problem asks you to support two types of operations:

  • Add $c$ to the weights of all vertices in the shortest path between $(i_1,j_1)$ and $(i_2,j_2)$
  • Query about the weight of $(i,j)$

It's guaranteed that for any operation of the first type, the shortest path is unique.

QUICK EXPLANATION:

We find $K=O(M\log N)$ spanning trees: first we find all shortest-path trees rooted at $(i,\frac{N}{2})$, and recursively find $K'=K-M$ trees $TL_1,\dots,TL_{K'}$ for the left grid, and $K'$ trees $TR_1,\dots,TR_{K'}$ for the right grid. Then we combine the $K'$ left trees and $K'$ right trees into $K'$ spanning trees of the whole grid. These trees have a property that every shortest path is described(see below) by some of them. The rest is very easy: for each modification find which tree describes its shortest path, and use heavy-light decomposition to maintain these trees.

EXPLANATION:

subtask 1

For any query of the first type, we can use Dijkstra's Algorithm to compute the shortest path, and iterate all vertices on it to update vertex weights. If we use heap to optimize the algorithm, its complexity becomes $O(|E|\log |V|)$. The time complexity is $O(MNQ\log MN)$.

subtask 2

In this subtask $M=1$, so the graph becomes a path. This problem becomes the classical data-structure exercise: add $c$ on one interval or output the value of one vertex. Segment Trees or Fenwick Trees can do the job on $O(N+Q\log N)$.

subtask 3-5

The crucial idea for this problem is that, there exists $K=O(M\log N)$ spanning trees $T_1,T_2,\dots,T_K$ of the graph such that, for any two vertices $u,v$, their shortest path is the path between them in $T_i$ for some $i\le K$. In other words, we can use $K$ spanning trees to describe all shortest paths.

Note that in this editorial, we say tree $T$ describes the shortest path from $(i_1,j_1)$ to $(i_2,j_2)$, if that shortest path coincides with the simple path from $(i_1,j_1)$ to $(i_2,j_2)$ in $T$.

Finding the spanning trees

We use $solve(l,r)$ to denote the procedure that, only consider vertices whose column number is in $[l,r]$, and finds $M\lceil\log_2(r-l+1)\rceil$ spanning trees that satisfies the above condition.

Let's consider an easier case: for every modification, $j_1\le mid$ and $j_2\ge mid$, where $mid=\lfloor\frac{l+r}{2}\rfloor$. The shortest path between such $(i_1,j_1)$ and $(i_2,j_2)$ must pass some vertex at $(u,mid)$ where $1\le u\le M$. We let $T_i$ be the shortest-path tree rooted at $(i,mid)$. Suppose the shortest path between $(i_1,j_1)$ and $(i_2,j_2)$ passes vertex $(u,mid)$, then this shortest path is described by $T_u$.

How about the case that both $j_1,j_2$ are less(greater) than $mid$? Note that in this case, it's still possible that the shortest path passes through some vertex at $(i_0,mid)$. See example below.

An example

In this example, blue edges have weight $1$ and all other edges have weight $10^{10}$.

However, if the shortest path passes through $(u,mid)$, then we can still use $T_u$ to describe this path. So we can assume that this path doesn't pass through $(*,mid)$.

Then we can do things recursively! Let's call $solve(l,mid-1)$ and $solve(mid+1,r)$. Suppose $solve(l,mid-1)$ returns $K'=M\lceil\log_2(mid-l)\rceil$ trees $TL_1,TL_2,\dots,TL_{K'}$; $solve(mid+1,r)$ returns $K'$ trees $TR_1,TR_2,\dots,TR_{K'}$. For any pair of vertices $(i_1,j_1)$ and $(i_2,j_2)$, if both $j_1,j_2$ are less than $mid$, then some tree $TL_i$ will contain their shortest path; if both $j_1,j_2$ are greater than $mid$, then some tree $TR_i$ will contain their shortest path. We can combine these trees together: for $TL_i$ and $TR_i$, we find some path in the original grid graph to connect them together, such that they become one tree. We number this tree $T_{i+M}$(note that we already had $M$ trees above), then $T_{i+M}$ can describe all shortest paths that $TL_i$ or $TR_i$ can describe.

Combination of two trees $TL_i$ and $TR_i$

Since all $TL_i$'s and all $TR_i$'s together can describe all shortest paths that doesn't pass through $(*,mid)$, all $T_i(i>M)$'s also can. And the paths that pass through $(*,mid)$ are described by $T_i(i\le M)$. Our method $solve(l,r)$ just returns all $T_i$'s.

The number of trees is the recursion depth multiply $M$, i.e., $O(M\log N)$. To find a shortest-path tree, we can use Dijkstra's Algorithm(use a heap to optimize) and it works on $O(M(r-l)\log(M(r-l)))$. The complexity for $solve(1,N)$ is $$T(N)=O(M^2N\log(MN))+2T(\frac{N}{2})\implies T(N)=O(M^2N\log^2 N).$$

Performing all queries

Once we find the trees $T_1,T_2,\dots,T_K(K=O(M\log N))$, the queries become very easy. We can use heavy-light decomposition to maintain these trees. We need to perform path-add operation(add $c$ to all weights of vertices on some path of some tree) and vertex-query operation(output the weight of some vertex on some tree). Also we need HLD to help us querying distances on these trees.

For a modification $1\ i_1\ j_1\ i_2\ j_2\ c$, we iterate all trees $T_1,\dots,T_K$ to find which tree describes the shortest path, and perform a path add operation on that tree.

For a query $2\ i\ j$, we output the sum of the weights of $(i,j)$ over all trees.

The complexity for this part is $O(QM\log^2 N)$.

ALTERNATIVE SOLUTION:

If your solution is different with ours, feel free to leave a comment.

Also, solutions for subtask 3($M=2$) and subtask 4(random data) are welcome!

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

RELATED PROBLEMS:

A bonus problem

Originally this problem was: you're given a grid graph, and support two operations:

  • Add weights of all vertices in some shortest path;
  • Query the sum of weights of all vertices in some shortest path.

However I couldn't get a satisfactory solution. My solution requires $O(\sqrt{N}\log^2 N)$ time per operation, and I'm not sure if it's faster than an optimized brute force, so I made a simpler version. This problem is left as a bouns.

SPOJ IOPALIN


NZEC Error

$
0
0

This is my code when I execute It on the ide it works well but while submitting it shows NZEC Runtime error. Please help.

import java.util.Scanner;

 class codechef {
static char[] coin;
static int n,i,q;
static void flipcoins(int side)
{
  if(side==1)
      for(int i1=0;i1<n;i1++)
          coin[i1]='H';
  else
      for(int i1=0;i1<n;i1++)
          coin[i1]='T';
}
static void changeside()
{
    for(int a=0;a<n;a++)
    {
     for(int j=0;j<=a;j++)
     {
         if(coin[j]=='H')
             coin[j]='T';
         else
             coin[j]='H';
     }
    }
}
 static void count()
 {
    int nt=0,nh=0;
    for(int i1=0;i1<n;i1++)
    {
       if(coin[i1]=='H')
           nh++;
       else
           nt++;
    }
    if(q==1)
        System.out.println(nh);
    else
        System.out.println(nt);
}
public static void main(String[] args) {
   int t;
   try{
   Scanner sc= new Scanner(System.in);
   t=sc.nextInt();
   while(t-->0)
   {
    int g=sc.nextInt();
    while(g-->0)
    {
      i=sc.nextInt();
      n=sc.nextInt();
      q=sc.nextInt();
      coin= new char[n];
      flipcoins(i);
      changeside();
      count();
    }

   }
}catch(Exception e)
{ System.out.println(e);}
}
 }

MKTHNUM - K-th Number

ANDROUND question

RAINBOW:Showing wrong answer even with correct output

$
0
0

import java.util.*; class RAINBOW { public static void main(String []args){ try{ Scanner in=new Scanner(System.in); int T=in.nextInt(); for(int g=0;g<T;g++){ int N=in.nextInt(); int op=0,t=0,l=0; int[] A=new int[N]; int[] num={1,2,3,4,5,6,7}; for(int a=0;a<N;a++){ A[a]=in.nextInt(); } if(N%2==0){ if(A[N/2]==7) op=0; else op++;

    }
    else {
        if(A[(N+1)/2]==7)
            op=0;
        else op++;
    }
    for(int x=0;x<N;x++){

            if((A[x]==8)||(A[x]==9)||(A[x]==10))
                t++;

        }
   if(t!=0){op++;}
    for(int i=0;i<N/2;i++){
        if((A[i+1]-A[i])!=0 || (A[i+1]-A[i])!=1)
            op=0;
        else op++;
    }
   for(int k=0;k<N/2;k++){
        if((A[k]<A[k+1]) || (A[k]==A[k+1]))
            l++;
        if(l==0)
            op++;
    }
    for(int w=0;w<N/2;w++){
        if((A[w]!=A[N-1-w]))
            op++;
    }
    if(op==0)
    System.out.println("yes");
    else System.out.println("no");
  }

    }catch(Exception e){return;}

}}

What's wrong with this CODE (SIMPSTAT)

Viewing all 39796 articles
Browse latest View live