Yesterday I participated in May cookoff 2021 and then I came across this question( CSUBS Problem - CodeChef )
I tried to solve it and I wrote the answer in JAVA and it got me wrong answer. I was really frustrated as I was pretty sure my answer was correct. Then today after several tries I thought to write the same code in C++ and I wrote the same code (even with the same variable name) and it got accepted. I am really frustrated now eager to know what was my mistake. Can you pls help me.
JAVA code: ( CodeChef: Practical coding for everyone )
C++ code: ( CodeChef: Practical coding for everyone )
please please please help me out
Couldn’t come up with a smaller test case. 
1
58 1
2 7 11 17 23 25 26 26 27 31 33 36 36 37 40 48 49 52 53 54 56 58 62 65 67 68 68 69 70 72 73 74 76 78 80 83 92 92 93 100 100 109 113 118 120 122 127 128 128 129 129 129 135 141 142 148 149 150
Output of CPP Solution: 55
Output of Java Solution: 56
1 Like
by the way… how you got such test cases
can you also help me find what is the mistake or difference in the code as both of the codes are exactly same.

Randomly generated using Python Script
Script.py
"""
Author: Chitturi Sai Suman
Created: 2021-05-23 21:24:10
Contest: May Cook-Off 2021 Division 2
"""
from random import randint
def generate_input_as_string():
size = 150
n = randint(1,size)
k = randint(1, 1)
inputs = [randint(1, size) for i in range(n)]
string = str(n) + ' ' + str(k) + '\n'
string += ' '.join([str(i) for i in inputs])
return string
def main():
test = 10000
with open("in.in","w") as file:
file.write(str(test)+'\n')
for t in range(test):
string = generate_input_as_string()
file.write(string+"\n")
main()
1 Like
This thing messed up:
if(al.get(p) == al.get(p-1))
It should have been
if(((int)(al.get(p))) == ((int)(al.get(p-1))))
1 Like
Hi, Can you please explain the reason behind it because if in ide I run this code it prints true
ArrayList<Integer> al=new ArrayList();
al.add(1);
al.add(1);
if(al.get(1)==al.get(0))
System.out.println("True");
else
System.out.println("False");
I don’t know the reason, but can surely say that it is the thing that messed up. Proof:
Java Code
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int ni()
{
return Integer.parseInt(next());
}
long nl()
{
return Long.parseLong(next());
}
double nd()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// MAIN FUNCTION
public static void main (String[] args) throws java.lang.Exception
{
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = fr.ni();
while(t-->0){
int n = fr.ni();
int k = fr.ni();
int arr [] = new int [n];
for(int i = 0 ; i < n ; i++){
arr[i] = fr.ni();
}
int ans = 0;
for(int i = 0 ; i < k ; i++){
int j = i;
ArrayList<Integer> al = new ArrayList<>();
while(j < n){
al.add(arr[j]);
j += k;
}
int size = al.size();
// out.println(size);
Collections.sort(al);
int common = 0;
int temp = 1;
// for(int it = 0; it < al.size(); it++) {
// out.print(al.get(it) + " ");
// }
// out.println();
for(int p = 1 ; p < size ; p++){
if((al.get(p)) == (al.get(p-1))){
temp++;
out.print("Here ");
}else{
common = Math.max(common , temp);
temp = 1;
}
}
common = Math.max(common , temp);
ans += size - common;
}
out.println(ans);
}
out.close();
}
}
Output
Here Here Here Here Here Here Here Here 55
CPP Code
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n , k;
cin >> n;
cin >> k;
int arr[n];
for(int i = 0 ; i < n ; i++){
cin >> arr[i];
}
int ans = 0;
for(int i = 0 ; i < k ; i++){
int j = i;
vector<int> al;
while(j < n){
al.push_back(arr[j]);
j += k;
}
int size = al.size();
// cout << size << '\n';
sort(al.begin() , al.end());
int common = 0;
int temp = 1;
// for(int it = 0; it < al.size(); it++) {
// cout << al[it] << " ";
// }
// cout << '\n';
for(int p = 1 ; p < size ; p++){
if(al[p] == al[p-1]){
temp++;
cout << "Here ";
}else{
common = max(common , temp);
temp = 1;
}
}
common = max(common , temp);
ans += size - common;
}
cout << ans << endl;
}
return 0;
}
Output
Here Here Here Here Here 56
Now I got it… you can use .equals method also
if(al.get§.equals(al.get(p-1)))
this will also work
I submitted it and now I got AC
thank you so much
1 Like