304 North Cardinal St.
Dorchester Center, MA 02124

# Buggy algorithm CodeChef Solution

## Buggy algorithm CodeChef Solution in C++17

``````
#include <bits/stdc++.h>
using namespace std;

char getchar_() {
#ifdef LOCAL
return getchar();
#else
static char buf[1 << 20], *b = buf;
static int cnt = 0;

if (cnt == 0) {
if ((cnt = fread(buf, 1, 1 << 20, stdin)) == 0)
return EOF;
b = buf;
}
return cnt--, *b++;
#endif
}

void scan(char *cc) {
int i = 0;
char c;

while (isspace(c = getchar_()))
;
cc[i++] = c;
while (!isspace(c = getchar_()) && c != EOF)
cc[i++] = c;
}

template<class T> void scan(T *x) {
int sign = 1;
char c;

while (!isdigit(c = getchar_()))
if (c == '-')
sign = -1;
*x = c - '0';
while (isdigit(c = getchar_()))
*x = (*x << 1) + (*x << 3) + (c - '0');
*x *= sign;
}

template<class T, class... Ts> void scan(T t, Ts... ts) {
scan(t), scan(ts...);
}

#define N	30

int main() {
int t;

scan(&t);
while (t--) {
static long long dp[N][N];
static int aa[N], pp[N];
long long ans;
int n, m, i, j, k;

scan(&n, &m);
for (i = 0; i < m; i++) {
scan(&aa[i]), aa[i]--;
pp[i] = aa[i];
for (j = 0; j < i; j++)
if (aa[j] < aa[i])
pp[i]--;
}
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < n; j++)
if (i < n - m) {
if (j == 0)
for (k = 0; k <= i; k++) {
if (k == 0)
dp[i][0] += dp[i - 1][j];
else
dp[i][k] += dp[i - 1][j];
}
else
for (k = 0; k <= j; k++) {
if (k == 0)
dp[i][j + 1] += dp[i - 1][j];
else
dp[i][k] += dp[i - 1][j];
}
} else {
k = pp[n - 1 - i];
if (j == 0) {
if (k == 0)
dp[i][0] += dp[i - 1][j];
else if (k <= i)
dp[i][k] += dp[i - 1][j];
} else {
if (k == 0)
dp[i][j+1] += dp[i - 1][j];
else if (k <= j)
dp[i][k] += dp[i - 1][j];
}

}
ans = 0;
for (i = 0; i < n; i++)
ans += dp[n - 1][i];
printf("%lld\n", ans);
}
return 0;
}``````

## Buggy algorithm CodeChef Solution in C++14

``````#include <iostream>
#define ll long long

using namespace std;

int main() {
int t;
cin >> t;
for(int a = 0; a < t; a++){
int n,m;
cin >> n >> m;
int arr [m];
for(int i = 0; i < m; i++){
cin >> arr[i];
arr[i]--;
}
int pos [m];
for(int i = 0; i < m; i++){
pos[i] = arr[i];
for(int j = 0; j < i; j++){
if(arr[j] < arr[i]){
pos[i]--;
}
}
// cout << pos[i] << " ";
}
//cout << endl;
ll f [n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
f[i][j] = 0;
}
}
f[0][0] = 1;
for(int i = 1; i < n; i++){
for(int j = 0; j < n; j++){
if(i < (n-m)){

if(j == 0){
for(int k = 0; k <= i; k++){
if(k == 0){
f[i][0] += f[i-1][j];
}else{
f[i][k] += f[i-1][j];
}
}
}else{
for(int k = 0; k <= j; k++){
if(k == 0){
f[i][j+1] += f[i-1][j];
}else{
f[i][k] += f[i-1][j];
}
}
}
}else{
int k = pos[n-1-i];
if(j == 0){
if(k == 0){
f[i][0] += f[i-1][j];
}else if(k <= i){
f[i][k] += f[i-1][j];
}
}else{
if(k == 0){
f[i][j+1] += f[i-1][j];
}else if(k <= j){
f[i][k] += f[i-1][j];
}
}

}
}
}
/*for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << f[i][j] << " ";
}
cout << endl;
}
cout << endl;*/
ll sum = 0;
for(int i = 0; i < n; i++){
sum += f[n-1][i];
}
cout << sum << endl;

}
}``````

## Buggy algorithm CodeChef Solution in C

``````#include <stdio.h>

#define MAX 40
#define DEBUG 0
#define max(a,b) ((a)>(b)?(a):(b))

int main()
{
long long a[MAX+1][MAX+1]={0};
long long ukupno;
int manji[MAX],brManji;
int veci[MAX],brVeci;
int i,j,k,l;
int n,t,m;
int tablica[MAX];
int moze;

scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);

for(i=1;i<=n;i++)
tablica[i]=0;

brVeci=brManji=1;
veci[0]=manji[0]=0;

for(i=1;i<=m;i++)
{
scanf("%d",&k);
if (k>=i)
{
veci[brVeci++]=k;
tablica[k]=2;
}
else
{
manji[brManji++]=k;
tablica[k]=1;
}
}

for(i=1;i<brManji;i++)
if (manji[i]<manji[i-1])
goto kraj;

for(i=1;i<brVeci;i++)
if (veci[i]<veci[i-1])
goto kraj;

for(i=1;i<brManji;i++)
for(j=manji[i-1]+1;j<manji[i];j++)
if (tablica[j]!=2)
{
if (j<veci[brVeci-1])
goto kraj;
tablica[j]=3;
}

for(i=1;i<brVeci;i++)
for(j=veci[i-1]+1;j<veci[i];j++)
if (tablica[j]!=1)
{
if (j<manji[brManji-1])
goto kraj;
tablica[j]=4;
}

#if DEBUG
for(i=1;i<=n;i++)
printf("%d ",tablica[i]);
printf("\n");
#endif

for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
a[i][j]=0;
moze=1;
for(i=1;i<=n;i++)
{
if (tablica[i]==1||tablica[i]==2||tablica[i]==3)
continue;
for(j=max(i+1,m+1);j<=n;j++)
{
ukupno=moze;
for(k=i-1;k>=1;k--)
{
if (tablica[k]==3||tablica[k]==2)
continue;

for(l=k+1;l<j;l++)
ukupno+=a[k][l];
if (tablica[k]==1||tablica[k]==4)
break;
}
a[i][j]=ukupno;
}
if (tablica[i]==4)
moze=0;
}
#if DEBUG
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%5lld",a[i][j]);
printf("\n");
}
#endif
ukupno=moze;
for(i=n;i>=0;i--)
{
for(j=i+1;j<=n;j++)
ukupno+=a[i][j];
if (tablica[i]==4||tablica[i]==1)
break;
}
printf("%lld\n",ukupno);
continue;
kraj:
printf("0\n");
}
}``````

## Buggy algorithm CodeChef Solution in JAVA

``````import java.io.*;
import java.util.*;

public class Main
{
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
public static void main(String[] args)
{
int t = sc.nextInt();
for(int i=0;i<t;i++)
solveCase();
}
static void solveCase()
{
n = sc.nextInt(); m = sc.nextInt();
init = new int[m];
for(int i=0;i<m;i++)
init[i] = sc.nextInt()-1;

earliest = new boolean[m];
for(int i=0;i<m;i++)
{
int lessCount = 0;
for(int j=0;j<i;j++)
if(init[j]<init[i])
lessCount++;
earliest[i] = lessCount==init[i];
}

memo = new long[n][n+1];
for(long[] arr:memo)
Arrays.fill(arr, -1L);
System.out.println(solve(0, 0));
}
static int m, n;
static long[][] memo;
static int[] init;
static boolean[] earliest;
static long solve(int index, int start)
{
if(index>=n)
return 1;
if(memo[index][start]==-1)
{
long res = 0;
for(int i=start;i<n;i++)
if(index>=m || init[index]==i)
res += solve(index+1, i+1);
if(start>index)
if(index>=m || earliest[index])
res += solve(index+1, start);
memo[index][start] = res;
}
return memo[index][start];
}
}``````
##### Buggy algorithm CodeChef Solution Review:

In our experience, we suggest you solve this Buggy algorithm CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Buggy algorithm CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Buggy algorithm CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!