Thursday, September 13, 2018

GAME OF ARRAY

  • Problem Description

    You have an array A of size N containing only positive numbers. You have to output the maximum possible value of A[i]%A[j] where 1<=i,j<=N.

    Input
    The first line of each test case contains a single integer N denoting the size of the array. The next N lines contains integers A1, A2, ..., AN denoting the numbers

    Output
    Output a single integer answering what is asked in the problem.


    Subtask 1 
    1<= N <= 5000
    1 <= A[i] <= 2*(10^9)
    Subtask 2 
    1 <= N <= 1000000
    1 <= A[i] <= 2*(10^9)


    Example
    Input:
    2
    1
    2

    Output:
    1


    Explanation
    There will be four values, A[0]%A[0] = 0, A[0]%A[1]=1, A[1]%A[0]=0, A[1]%A[1]=0, and hence the output will be the maximum among them all, that is 1.
  • CODING ARENA
  • #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
      long int n;
      scanf("%ld",&n);
      int i,mod,max1=0,max2=0;
      long int a[n];
      for(i=0;i<n;i++)
      {
        scanf("%ld",&a[i]);
        if(a[i]>max1)
          max1=a[i];
      }
      for(i=0;i<n;i++)
      {
        if(a[i]>max2 && a[i]<max1)
          max2=a[i];
      }
      mod=max2%max1;
      printf("%d",mod);
      return 0;
    }
  • Test Case 1

    Input (stdin)
    2
    
    1
    
    2
    
    
    Expected Output
    1
  • Test Case 2

    Input (stdin)
    5
    
    1
    
    2
    
    3
    
    9
    
    8
    
    
    Expected Output
    8

No comments:

Post a Comment