# Code worked in cpp but not in java

**URL:** <https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138>\
**Category:** help\
**Tags:** help, c-plus-plus, error, help\_please, java, java8, wrong-answer\
**Created:** [May 24, 2021, 12:17pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138 "2021-05-24T12:17:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![thisisujjawal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/thisisujjawal/32/24644_2.png) [@thisisujjawal](https://discuss.codechef.com/u/thisisujjawal)\
**Post date:** [May 24, 2021, 12:17pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/1 "2021-05-24T12:17:37Z")

</div>

Yesterday I participated in May cookoff 2021 and then I came across this question( [CSUBS Problem - CodeChef](https://www.codechef.com/problems/CSUBS) )

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](https://www.codechef.com/viewsolution/46822510) )

C++ code: ( [CodeChef: Practical coding for everyone](https://www.codechef.com/viewsolution/46851747) )

please please please help me out

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 24, 2021, 1:11pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/2 "2021-05-24T13:11:00Z")

</div>

Couldn’t come up with a smaller test case. 🙁

```auto
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`

---

<div class="post-metadata">

**Author:** ![thisisujjawal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/thisisujjawal/32/24644_2.png) [@thisisujjawal](https://discuss.codechef.com/u/thisisujjawal)\
**Post date:** [May 24, 2021, 1:17pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/3 "2021-05-24T13:17:42Z")

</div>

thanks you so much

---

<div class="post-metadata">

**Author:** ![thisisujjawal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/thisisujjawal/32/24644_2.png) [@thisisujjawal](https://discuss.codechef.com/u/thisisujjawal)\
**Post date:** [May 24, 2021, 1:19pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/4 "2021-05-24T13:19:01Z")

</div>

by the way… how you got such test cases

---

<div class="post-metadata">

**Author:** ![thisisujjawal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/thisisujjawal/32/24644_2.png) [@thisisujjawal](https://discuss.codechef.com/u/thisisujjawal)\
**Post date:** [May 24, 2021, 1:22pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/5 "2021-05-24T13:22:07Z")

</div>

can you also help me find what is the mistake or difference in the code as both of the codes are exactly same.

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 24, 2021, 1:23pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/6 "2021-05-24T13:23:46Z")

</div>

🤷‍♂️  
Randomly generated using Python Script

> **Script.py**
>
> ```auto
> """
> 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()
> 
> ```

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 24, 2021, 1:33pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/7 "2021-05-24T13:33:08Z")

</div>

This thing messed up:

```auto
if(al.get(p) == al.get(p-1))

```

It should have been

```auto
if(((int)(al.get(p))) == ((int)(al.get(p-1))))

```

---

<div class="post-metadata">

**Author:** ![peeyushp245](https://discuss.codechef.com/user_avatar/discuss.codechef.com/peeyushp245/32/24644_2.png) [@peeyushp245](https://discuss.codechef.com/u/peeyushp245)\
**Post date:** [May 24, 2021, 1:38pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/8 "2021-05-24T13:38:42Z")

</div>

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");
```

---

<div class="post-metadata">

**Author:** ![suman\_18733097](https://discuss.codechef.com/user_avatar/discuss.codechef.com/suman_18733097/32/218666_2.png) [@suman\_18733097](https://discuss.codechef.com/u/suman_18733097)\
**Post date:** [May 24, 2021, 1:42pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/9 "2021-05-24T13:42:58Z")

</div>

I don’t know the reason, but can surely say that it is the thing that messed up. Proof:

> **Java Code**
>
> ```auto
> 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**

```auto
Here Here Here Here Here Here Here Here 55

```

> **CPP Code**
>
> ```auto
> #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**

```auto
Here Here Here Here Here 56

```

---

<div class="post-metadata">

**Author:** ![thisisujjawal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/thisisujjawal/32/24644_2.png) [@thisisujjawal](https://discuss.codechef.com/u/thisisujjawal)\
**Post date:** [May 24, 2021, 2:08pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/10 "2021-05-24T14:08:09Z")

</div>

Now I got it… you can use .equals method also

---

<div class="post-metadata">

**Author:** ![thisisujjawal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/thisisujjawal/32/24644_2.png) [@thisisujjawal](https://discuss.codechef.com/u/thisisujjawal)\
**Post date:** [May 24, 2021, 2:15pm UTC](https://discuss.codechef.com/t/code-worked-in-cpp-but-not-in-java/90138/12 "2021-05-24T14:15:37Z")

</div>

if(al.get§.equals(al.get(p-1)))  
this will also work  
I submitted it and now I got AC  
thank you so much
