PAIRS (Interview Street Problem)

Hello,
         This time I came across a very easy problem on the Interview Street. I am posting the solution to the problem in JAVA language .

Problem Statement:: https://www.interviewstreet.com/challenges/dashboard/#problem/4e14b83d5fd12

My solution satisfied 14/15 test cases with last exceeding the TIME LIMIT !



Here is the code~~~~~~~~~~

//Author Akshay Ratan (ARC)
//Interview street PAIRS problem !
import java.util.Scanner;
public class Pairs
{
    public static void main(String args[])
    {
        int n;
       
        int c=0; //counter
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt(); int k=sc.nextInt();
        if(n>100000||k<0)
            System.exit(0);
        int arr[]=new int[n];
        for(int i=0;i<n;i++)
            arr[i]=sc.nextInt();
        //Values stored in the array till now
        int l=arr.length;
        for(int j=0;j<l;j++)
        {
            for(int m=j;m<l;m++)
            {
                int d=arr[j]-arr[m];
                if(d==k||d==-k)
                  c++;
            }
        }
        System.out.println(c);
    }
}



Any suggestions for removing the TIME LIMIT EXCEEDED Error are most welcome !

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Post a Comment