Algorithms using JAVA

Can someone please help me solve few algorithm assignments using java ? I am facing difficulty and could not find solution to it online.

What algorithm assignments are you seeking help with? What code have you written so far?

1 Like

I have tried solving many problems but failed to solve few of them so I need help with them.

You can just tell us the description of one of these problems and show us the code you tried. Also share your idea of how you wanted to solve it and what errors or issues you are having.

The more details you give, the more likely you are to get a response.

1 Like

I am getting the expected output but when i try to submit it says non zero error.
Is it possible to connect with you on any other app like discord?
Given an array A of size N, you need to find its maximum, 2nd maximum and 3rd maximum element.

Try solving it in O(N) per test case

class Main {
   
    public static void main (String[] args) {
                      // Your code here
                      Scanner sc= new Scanner(System.in);
                      long t = sc.nextLong();
                      while (t-->0){
                          long n = sc.nextLong();
                      
                      long max1=0 , max2=0, max3=0;
                      long x;
for(int i=0; i<n; i ++){
    x = sc.nextInt();
    if(x > max1){
        max3 = max2;
        max2 = max1;
        max1=x;

    }
     else if (max2 < x){
        max3 = max2;
        max2= x;
}
else if (x > max3){
        max3 = x;
}
}

System.out.println(max1 + " " + max2 + " " + max3);

}
                      }
    }
1 Like

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

hi there,

fCC has a discord server. You can join it here: https://www.freecodecamp.org/news/freecodecamp-discord-chat-room-server/

I also want to mention that when asking for help with code, you should take the time to indent the code properly to help people read it and perhaps add comments to it as well so as to help people understand it.

2 Likes

I’ve indented the code:

Please explain what are the arguments being passed to this program?
(what is the first arg, second etc)

class Main {
    
    public static void main (String[] args) {
        // Your code here
        Scanner sc= new Scanner(System.in);
        long t = sc.nextLong();
        while (t-->0){
            long n = sc.nextLong();
            
            long max1=0 , max2=0, max3=0;
            long x;
            for(int i=0; i<n; i ++){
                x = sc.nextInt();
                if(x > max1){
                    max3 = max2;
                    max2 = max1;
                    max1=x;
                    
                }
                else if (max2 < x){
                    max3 = max2;
                    max2= x;
                }
                else if (x > max3){
                    max3 = x;
                }
            }
            
            System.out.println(max1 + " " + max2 + " " + max3);
            
        }
    }
}

1 Like

Yes I will take care henceforth. Thank you

Some arguments are already predefined in the portal from where I am solving the problems.

I’m not sure how to interpret your response. Are you unsure what the arguments are?
If yes, please share the link to the problem and perhaps I can find the answer there.

I would like to know what the arguments are because I want to try your code.

1 Like

https://my.newtonschool.co/playground/code/2691htzyrwca/

Need help with at least 12-15 problems .

Here’s the information I needed (you just needed to copy it out from the problem)

Problem Statement
Given an array A of size N, you need to find its maximum, 2nd maximum and 3rd maximum element.

Try solving it in O(N) per test case
Input
The first line of the input contains the number of test cases T.

For each test case, the first line of the input contains an integer N denoting the number of elements in the array A. The next line contains N (space separated) elements of A.


Constraints:
1 <= T <= 100
3 <= N <= 10^6
1 <= A[i] <= 10^9

Note:-It is guaranteed that the sum of N over all text cases does not exceed 10^6
Output
For each test case, output the first, second and third maximum elements in the array.
Example
Sample Input:
3
5
1 4 2 4 5
6
1 3 5 7 9 8
7
11 22 33 44 55 66 77

Sample Output:
5 4 4
9 8 7
77 66 55

Explanation(might now be the optimal solution):
Testcase 1:
[1 4 2 4 5]
First max = 5
Second max = 4
Third max = 4
1 Like

based on my testing, it seems your code works but the issue is that the algorithm is exceeding the time limit needed? (is that your understanding too?)

1 Like

Yes that is the issue I am facing

Unfortunately I cannot provide any insight to why the memory limit is being exceeded. (I said time limit before, but I think the error is about memory use not time because the time limit is 2 sec and the failing testcase ran in less than that time) To me it looks like you are not creating more than a few variables each time (you probably should change all the long variables to ints though, however that still doesn’t solve the issue).

Is there any guidance on the site for this type of issue that you can refer to?
The cheatsheet given for the exercise says you need to use arrays, so that might also be a clue. Maybe instead of keeping track of the 3 max values, you should keep track of their location in the array only?

And weirdly the cheatsheet mentions that you should know how to do type conversion? That really doesn’t seem related to the problem but perhaps there is some hidden requirement.

Unfortunately that’s all the guidance I can offer. I hope you can find someone from the website to give you some tips on how to deal with these memory limit issues.

1 Like

Thanks a lot for your help . Can you help me with other problems?

i can try. Please open a new topic for the new issue.
(Leave this one in case someone else has other thoughts on it that may help)

Be sure to include all the details in the next one…

It appears the solution is to use sc.nextLine() which comes in as an integer but then convert to integer using Integer.parseInt(). Also, you when bringing in the line with the actual integers, you can bring them into an array of strings and then inside the for loop convert the string version of the number into an integer using the Integer.parseInt() again.

I am not a JAVA expert at all, so I am not sure why bringing it each number one at a time using sc.nextLong (even tried sc.nextInt) would consume more memory than bringing in the entire line in and putting numbers into an array of strings. I would have thought the array of strings would consume more memory than a single integer being reassigned each time. I will keep researching though.

2 Likes