304 North Cardinal St.
Dorchester Center, MA 02124

# Chef and Happiness CodeChef Solution

## Chef and Happiness CodeChef Solution in C++14

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

int main() {
int T;
cin >> T;
while(T--){
set<int> s;
map<int,int> m;
int n;
cin >> n;
int arr[n];
int z=0;
for(int i=0;i<n;i++){
cin >> arr[i];

s.insert(arr[i]);

}
for(auto i:s){
if(i<=n){
m[arr[(i-1)]]++;
}
}
for(auto i:m){
if(i.second>=2){
z=1;
break;
}
}
if(z==0) cout << "Poor Chef" << endl;
else cout << "Truly Happy" << endl;

}
}``````

## Chef and Happiness CodeChef Solution in PYTH 3

``````
for t in range(int(input())):
n = int(input())
l = [int(k) for k in input().split()]
arr =[]
for i in range(n):
arr.append([])
ans="Poor Chef"
for i in range(n):
arr[l[i]-1].append(i+1);
for i in range(n):
ctr =0
for j in arr[i]:
if(len(arr[j-1])>0):
ctr=ctr+1
if(ctr>=2):
ans="Truly Happy"
break;
print(ans)``````

## Chef and Happiness CodeChef Solution in C

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

int main()
{
int tag,n,*A,*Aa,i,j,k;

int lenth[100000];

scanf("%d",&tag);

while(tag >0)
{
int max=0;
int flg=1;
int count=0;
int vg=0;

scanf("%d",&n);
A = (int*)malloc(sizeof(int)*n);
Aa = (int*)malloc(sizeof(int)*n);
j=max;

memset(lenth,0,sizeof(int)*100000);

for(i=0; i<n; ++i)
{
scanf("%d",A+i);
if(A[i]>max) max=A[i];
++lenth[A[i]];
}
for(i=1; i<=max; ++i)
lenth[i]+=lenth[i-1];

for(i=n-1; i>=0; --i)
{
Aa[lenth[A[i]]-1]=i;
--lenth[A[i]];
}

memset(lenth,0,sizeof(int)*100000);
//using lenth again...//

for(i=0;i<n; ++i)
++lenth[A[i]];

for(i=0;i<n-1; ++i)
{
if(i==0 || A[Aa[i]]!=A[Aa[i-1]])
{
if(lenth[Aa[i]+1]>0)
count = 1;
else
count = 0;
}
if(A[Aa[i+1]]==A[Aa[i]] && lenth[Aa[i+1]+1]>0)
++count;

if(count>=2)
{
printf("Truly Happy\n");
flg=0;
break;
}
}
// if chef is really poor  ...//
if(flg)
printf("Poor Chef\n");

tag -= 1;
}
return 0;
}``````

## Chef and Happiness CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
HashSet<Integer> set = new HashSet<>();
HashMap<Integer, Integer> map = new HashMap<>();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
for (int i = 0; i < n; i++)
int c=0;
for (int i = 0; i < n; i++) {
if(!map.containsKey(arr[i]))
map.put(arr[i],i+1);
else{
if(set.contains(map.get(arr[i])) && set.contains(i+1)){
c=1; break;
}
else if(set.contains(i+1))
map.replace(arr[i],i+1);
}
}
if(c==1) System.out.println("Truly Happy");
else
System.out.println("Poor Chef");
}
}
}``````

## Chef and Happiness CodeChef Solution in PYPY 3

``````# cook your dish here
import math
import sys

def solve(A,n):
d = {}
d1 = {}

for i in range(n):
if A[i] not in d and A[A[i]-1] in d1:

return True
else:
d[A[i]] = d.get(A[i],0)+1
d1[A[A[i]-1]] = d1.get(A[A[i]-1],0)+1

return False

try:
T = int(input())
for l in range(T):

n = int(input())
path = [int(i) for i in input().strip().split(" ")]
if solve(path,n):
print("Truly Happy")
else:
print("Poor Chef")

except EOFError:
pass

``````

## Chef and Happiness CodeChef Solution in PYTH

``````t = int(raw_input())
for i in range(t):
N = int(raw_input())
st = raw_input().split()
A = []
S = set()
for k in range(1,N+1):
n = int(st[k-1])
A.append([n,k])
# endfor k
A.sort()
A.append([10**6,0])
valid = False
p = 0
while (not valid) and (p < N):
tot = 0
n = A[p][0]
if A[p][1] in S:
tot += 1
# endif
while A[p+1][0] == n:
p += 1
if A[p][1] in S:
tot += 1
# endif
# endwhile
if tot > 1:
valid = True
else:
p += 1
tot = 0
n = A[p][0]
if A[p][1] in S:
tot += 1
# endif
# endif
# endwhile
if valid:
print 'Truly Happy'
else:
print 'Poor Chef'
# endif
# endfor i
``````

## Chef and Happiness CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{

for (int i = 0; i < testCases; i++)
{
bool wins = IsHappy();
Console.WriteLine(wins ? "Truly Happy" : "Poor Chef");
}
}

public static bool IsHappy()
{
var hSet = new HashSet<int>(sequence);
var hSet1 = new HashSet<int>();

for (int i = 1; i <= sequenceLen; i++)
{
if (hSet.Contains(i))
{
if (hSet1.Contains(sequence[i - 1]))
{
return true;
}
else {
}

}

}

return false;
}
}``````

## Chef and Happiness CodeChef Solution in NODEJS

``````process.stdin.resume();
process.stdin.setEncoding('utf8');

input: process.stdin,
output: process.stdout,
terminal: false
});
let lines=[];
rl.on('line', function(line){
//console.log(line.split(" "));
lines.push(line.split(" ").map(Number));
})
rl.on('close', function(){
//console.log(lines);
let q=lines[0]
for(let i=0;i<q;i++){
//console.log(doit(lines[2*i+1]));
console.log(doit(lines[2*i+2]));
//console.log()
}
})
function doit(line){
//console.log(line);
let res={};
let result="Poor Chef";
line.forEach(ind=>{
let val=line[ind-1];

if (res[val] && !res[val][ind])
{
result="Truly Happy"

}
if (!res[val]) res[val]={};
res[val][ind]=1;
//console.log("  ",ind, val,res);
})
return result;
}``````

## Chef and Happiness CodeChef Solution in GO

``````package main

import (
"bufio"
"fmt"
"os"
"strconv"
)

var scanner *bufio.Scanner

func init() {
scanner.Split(bufio.ScanWords)
}

func next() string {
scanner.Scan()
return scanner.Text()
}

func nextInt() int {
num, err := strconv.Atoi(next())
if err != nil {
panic(err)
}
return num
}

func main() {
T := nextInt()
for caseNum := 1; caseNum <= T; caseNum++ {
N := nextInt()
data := make([]int, N+1)
for i := 1; i <= N; i++ {
data[i] = nextInt()
}

referrers := make([]map[int]bool, N+1)
for i := range referrers {
referrers[i] = make(map[int]bool)
}
for i := 1; i <= N; i++ {
referrers[data[data[i]]][data[i]] = true
}
happy := false
for _, ed := range referrers {
if len(ed) > 1 {
happy = true
break
}
}
if happy {
fmt.Println("Truly Happy")
} else {
fmt.Println("Poor Chef")
}
}
}``````
##### Chef and Happiness CodeChef Solution Review:

In our experience, we suggest you solve this Chef and Happiness 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 Chef and Happiness CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Chef and Happiness 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!