简体   繁体   中英

how to use function correctly in java

Given an array arr[] of N positive integers, where elements are consecutive (sorted). Also, there is a single element which is repeating X (any variable) number of times. Now, the task is to find the element which is repeated and number of times it is repeated. here is my code:-

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
    public static void main (String[] args)
     {
         Scanner sc= new Scanner(System.in);
         int t,n,i,left,rightleftid,rightid;
         t=sc.nextInt();
         while(t!=0){
             n=sc.nextInt();
             int arr[]=new int [n];
             for(i=0;i<n;i++){
                 arr[i]=sc.nextInt();
             }
             left=0;right=n-1;
             mid=(left+right)/2;
             leftid=funcleft(arr[],n,left,right);
             rightid=funcright(arr[],n,left,right);
             System.out.println(arr[leftid]+" "+(rightid-leftid+1));
             t--;
         }
     }
     static int funcleft(int arr[],int n,int left,int right){
         mid=(left+right)/2;
         if(arr[mid]==arr[mid+1]&&arr[mid]!=arr[mid-1])
         return mid;
         if(arr[mid]==arr[mid-1])
         return funcleft(arr[],n,left,mid-1);
         if(arr[mid]-arr[0]<mid)
         return funcleft(arr[],n,left,mid-1);
         else
         return funcleft(arr[],n,mid+1,right);
     }
     static int funcright(int arr[],int n,int left,int right){
         mid=(left+right)/2;
         if(arr[mid]==arr[mid-1]&&arr[mid]!=arr[mid+1])
         return mid;
         if(arr[mid]==arr[mid+1])
         return funcright(arr[],n,mid+1,right);
         if(arr[mid]-arr[0]<mid)
         return funcleft(arr[],n,left,mid-1);
         else
         return funcleft(arr[],n,mid+1,right);
     }
}

the compiler is showing some errors

errors:-

plzz elaplain me what im doing wrong ima beginner in java

You should just call

funcleft(arr,n,left,mid-1);

without the brackets for your error.

But the code that you wrote does not solve the problem you are describing. You say "given" the array but are creating t number of arrays with length n, and filling up the elements of the array yourself which may or may not be sorted depending on your input.

First, you need to compile the code, there are a few problems:

  • scope of the variables
  • code line is java should end with ';'
  • also you do not need embeded class

As an example you can take this version of your code which passes compilation:

 package com.company;

import java.util.Scanner;


class GFG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t, n, i, left, right, leftid, rightid, mid;
        //int t,n,i,left,right,left,id,rightid;
        t = sc.nextInt();
        while (t != 0) {
            n = sc.nextInt();
            int[] arr = new int[n];
            for (i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            left = 0;
            right = n - 1;
            mid = (left + right) / 2;
            leftid = funcleft(arr, n, left, right);
            rightid = funcright(arr, n, left, right);
            System.out.println(arr[leftid] + " " + (rightid - leftid + 1));
            t--;
        }
    }

    static int funcleft(int[] arr, int n, int left, int right) {
        // mid = (left + right) / 2;
        int mid = (left + right) / 2;
        if (arr[mid] == arr[mid + 1] && arr[mid] != arr[mid - 1])
            return mid;
        if (arr[mid] == arr[mid - 1])
            return funcleft(arr, n, left, mid - 1);
        if (arr[mid] - arr[0] < mid)
            return funcleft(arr, n, left, mid - 1);
        else
            return funcleft(arr, n, mid + 1, right);
    }

    static int funcright(int[] arr, int n, int left, int right) {
        int mid = (left + right) / 2;
        if (arr[mid] == arr[mid - 1] && arr[mid] != arr[mid + 1])
            return mid;
        if (arr[mid] == arr[mid + 1])
            return funcright(arr,n, mid + 1, right);
        if (arr[mid] - arr[0] < mid)
            return funcleft(arr,n, left, mid - 1);
         else
        return funcleft(arr,n, mid + 1, right);
    }
}

Secondly, you might want to think about the algorithm itself, as a simple solution you can use Map eg TreeMap to iterate through the elements and bump the counter for every member.

Those specific compile Errors are due to the way, how arrays are defined and accessed in Java:

While defining the varible arr which should point to your stored array use the brackets after the type, not after the name, because the it's a varible of the type array of intergers, therefore the declaration should be

int[] arr=new int [n];

Then when using the whole array as such, and not a specific element, use only the name of the variable which is just arr so when accessing funcleft or funcright it should be

funcleft(arr,n,left,right);

or the same for funcright .

After fixing those errors, you'll run in further compile errors which are not yet given by the compiler as he doesn't come that far. A working example of your code could look like this:

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
    public static void main (String[] args)
     {
         Scanner sc= new Scanner(System.in);
         int t,n,i,left,right,rightleftid,rightid,leftid,mid;
         t=sc.nextInt();
         while(t!=0){
             n=sc.nextInt();
             int[] arr=new int [n];
             for(i=0;i<n;i++){
                 arr[i]=sc.nextInt();
             }
             left=0;
             right=n-1;
             mid=(left+right)/2;
             leftid=funcleft(arr,n,left,right);  
             rightid=funcright(arr,n,left,right);
             System.out.println(arr[leftid]+" "+(rightid-leftid+1));
             t--;
         }
     }
     static int funcleft(int arr[],int n,int left,int right){
         int mid=(left+right)/2;
         if(arr[mid]==arr[mid+1]&&arr[mid]!=arr[mid-1])
         return mid;
         if(arr[mid]==arr[mid-1])
         return funcleft(arr,n,left,mid-1);
         if(arr[mid]-arr[0]<mid)
         return funcleft(arr,n,left,mid-1);
         else
         return funcleft(arr,n,mid+1,right);
     }
     static int funcright(int arr[],int n,int left,int right){
         int mid=(left+right)/2;
         if(arr[mid]==arr[mid-1]&&arr[mid]!=arr[mid+1])
         return mid;
         if(arr[mid]==arr[mid+1])
         return funcright(arr,n,mid+1,right);
         if(arr[mid]-arr[0]<mid)
         return funcleft(arr,n,left,mid-1);
         else
         return funcleft(arr,n,mid+1,right);
     }
}

Though that won't exactly solve your given task as @Gokberk Gul pointed out

thanks to all.....the thing which i dont know was - during a function call if i want to pass an array(arr[]) as argument then i just need to pass its name(arr) as argument not the square brackets([]) along with it. Here is my new optimized error free code which has been submitted successfully.

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
    public static void main (String[] args)
     {
         Scanner sc= new Scanner(System.in);
         int t,n,i,left,right,leftid,rightid;
         t=sc.nextInt();
         while(t!=0){
             n=sc.nextInt();
             int arr[]=new int [n];
             for(i=0;i<n;i++){
                 arr[i]=sc.nextInt();
             }
             left=0;right=n-1;
             leftid=funcleft(arr,n,left,right);
             rightid=funcright(arr,n,left,right);
             System.out.println(arr[leftid]+" "+(rightid-leftid+1));
             t--;
         }
     }
     static int funcleft(int arr[],int n,int left,int right){
         int mid=(left+right)/2;
         if(mid==0)
         return mid;
         if(arr[mid]==arr[mid+1]&&arr[mid]!=arr[mid-1])
         return mid;
         if(arr[mid]==arr[mid-1])
         return funcleft(arr,n,left,mid-1);
         if(arr[mid]-arr[0]<mid)
         return funcleft(arr,n,left,mid-1);
         else
         return funcleft(arr,n,mid+1,right);
     }
     static int funcright(int arr[],int n,int left,int right){
         int mid=(left+right)/2;
         if(mid==n-1)
         return mid;
         if(arr[mid]==arr[mid-1]&&arr[mid]!=arr[mid+1])
         return mid;
         if(arr[mid]==arr[mid+1])
         return funcright(arr,n,mid+1,right);
         if(arr[mid]-arr[0]<mid)
         return funcright(arr,n,left,mid-1);
         else
         return funcright(arr,n,mid+1,right);
     }
}```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM