Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why this piece of code is not working? and please suggest the correct way to solve this problem

  1. #1
    Junior Member
    Join Date
    Aug 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Why this piece of code is not working? and please suggest the correct way to solve this problem

    Question -- Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

    The overall run time complexity should be O(log (m+n)).

    My Method


    class Solution {
    public double sum(int[] nums){
    int summ = 0;
    for(int i = 0;i<nums.length;i++){
    summ = summ + nums[i];
    }
    return sum/nums.length;
    }
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {

    int j = 0;
    if(nums1.length>=nums2.length){

    int nums1[] = new nums1[nums1.length + nums2.length];
    for(int i =0;i<nums1.length;i++) if(nums1[i]==0) {

    nums1[i] = nums2[j];
    j++;

    }

    }else
    {
    nums2[] = new nums2[nums1.length + nums2.length];
    for(int i =0;i<nums2.length;i++) if(nums2[i]==0) {

    nums2[i] = nums1[j];
    j++;

    }
    if(nums2.length%2==0){
    return nums2[nums2.length/2] + nums2[nums2.length/2 - 1];
    } else
    {
    return sum();
    }
    }


    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why this piece of code is not working? and please suggest the correct way to solve this problem

    Please explain what the problem is.
    Post the program's output with some comments describing what is wrong or what you think should be different.
    Post any error messages.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What Is The Purpose Of This Piece Of Code
    By amb101293 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: October 5th, 2014, 01:53 PM
  2. How do I represent any character in a piece of code?
    By 93tomh in forum Java Theory & Questions
    Replies: 3
    Last Post: July 15th, 2012, 05:28 AM
  3. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM
  4. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM

Tags for this Thread