1

THE_INPUT I can only edit the class Solution and the method inside it. The input will be given by the problem only.

further details: I basically have to create a method in a class which accepts two arrays or whatever that input is trying to say and pick out one element which is extra in one of them.

my Code:

import java.util.Arrays;

public class Solution {
    
    public static int solution(int[] x, int[] y) {
        // Your code here
       
        int[] shorter = (x.length>y.length)?  y : x;
        int[] longer = (x.length>y.length)?  x : y;
        
        Arrays.sort(shorter);
        Arrays.sort(longer);
        
        for(int i=0; i<shorter.length; i++){
            if(shorter[i]!=longer[i]){
                //System.out.println(longer[i]);
                return longer[i];
            }
            else {
               // System.out.println(longer[longer.length-1]);
                return longer[longer.length-1];
            }
                
        }
        return 0;
    }
    
}

The input that they are giving refers to the arrays which are missing new int[] ...

Aditya W
  • 43
  • 5
  • I don't quite get what you mean with "accept inputs like these". You have a method that accepts two int arrays as a parameter. What exactly are you confused about with those parameters `int[] x` and `int[] y`? – OH GOD SPIDERS Aug 18 '21 at 15:13
  • Just see the image, in that the input is like only {1,2,3,4} is this any of the pre defined inputs? As they surely are not arrays. – Aditya W Aug 18 '21 at 15:16
  • 3
    The code in the image `Solution.solution({13,5,6,2,5}, {5,2,5,13})` is not valid Java code. It is not possible to write a method for which it will work. – khelwood Aug 18 '21 at 15:17
  • Yeah Thats the problem! But I recieved it from google! – Aditya W Aug 18 '21 at 15:18
  • 1
    I'm pretty sure that screenshot does not show code but just some formatted output. Are you sure that this is really how the solution method is tried to be called? – OH GOD SPIDERS Aug 18 '21 at 15:19
  • Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Aug 18 '21 at 15:20
  • The example is intended to show the input in a terse and easy to read way. Your function *already* takes arrays of the right type - if you want to test your code yourself with your own `main`, pass the arrays in whatever syntax you know and are comfortable with. – nanofarad Aug 18 '21 at 15:26
  • I tried and it is working in my editor by adding new int[]. But in the problem's editor the test cases are failing despite of correct code! – Aditya W Aug 18 '21 at 15:28
  • @AdityaW Are they failing with a *syntax error*, or a *wrong answer*? – nanofarad Aug 18 '21 at 15:31
  • In my editor the error is shown saying array initializer is not expected here. I have to add new int[] before the braces. And the problem does not tell the errors, just 'test case failed'! – Aditya W Aug 18 '21 at 15:33
  • 1
    In short your problem has nothing to do with the arguments. Your logic is simply wrong. If your current solution is printing the right answers in your System.out statements then change those to return the value instead. – OH GOD SPIDERS Aug 18 '21 at 15:34
  • @OHGODSPIDERS the return 0 statement will never be reached according to the method as there are only two conditions there 1: either the elements do not match and gets returned. 2: All the elements get matched so the last element is returned as the arrays are sorted first! – Aditya W Aug 18 '21 at 15:36
  • @OHGODSPIDERS I am sorry I just added those system.out statements for checking in my editor. I will edit the question but still the problem is same and the solution working. I had to add system.out so i can see output in my editor. – Aditya W Aug 18 '21 at 15:38
  • 2
    @AdityaW You are right, i misread the code. Still, I just tested the code. Still my point stands: This has nothing to do with the arguments not being passed correct. I just tested your code with `Solution.solution(new int[]{13,5,6,2,5}, new int[]{5,2,5,13})` and it returned `13` which is wrong because `13` is contained in both arrays and you yourself said the expected output is `6`. Honestly i would stop obsessing about how the arguments are passed and instead go over your logic again – OH GOD SPIDERS Aug 18 '21 at 15:40
  • Hint: if the arrays are guaranteed to differ by *exactly a single number* you can just, for both arrays, sum the numbers, and then calculate the difference between both sums. – MC Emperor Aug 18 '21 at 15:46
  • @OHGODSPIDERS Yes I checked the logic, there were some errors, thank you so much for pointing that out! – Aditya W Aug 18 '21 at 15:46

1 Answers1

0

It is possible but you need to accept input as a string and need to parse it afterward to get desired integers

Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31
  • I tried that but the problem is that the input also does not contain the characters under the inverted commas. – Aditya W Aug 18 '21 at 15:25