Write a Program in a high level language like JAVA
Next Greater Element I (LeetCode 496) Problem Description:
1.You are given two integer arrays nums1 and nums2 where nums1 is a subset of numbers
2. For each element of nums1, find its next greater element in nums2. If it does not exist, return -1 for that element.
Program in Java:
package compare2arays;
import java.io.DataInputStream;
import java.io.IOException;1
/**
*
* @author MAXHUB
*/
public class Compare2Arays {
public static void main(String[] args) throws IOException
{
DataInputStream din;
int arrMain[] = new int[10];
int arrSub[] = new int[10];
int i,j, nextHigh=0, iHigh=0;
din = new DataInputStream(System.in);
System.out.println("Enter upto 10 numbers for Main Array:");
for(i=0; i<10; i++)
{
j = Integer.parseInt(din.readLine());
if (j==0)
break;
else
arrMain[i] = j;
}
System.out.println("Enter upto 10 numbers for Sub Array:");
for(i=0; i<10; i++)
{
j = Integer.parseInt(din.readLine());
if (j==0)
break;
else
arrSub[i] = j;
}
System.out.println("Next Highest Element of Integers of Main Array:");
for(i=0; i<10; i++)
{
if(arrMain[i] > 0)
{
nextHigh = -1;
for(j=0; j<10; j++)
{
if(arrMain[i] < arrSub[j])
if(nextHigh == -1)
nextHigh = arrSub[j];
else if(nextHigh > arrSub[j])
nextHigh = arrSub[j];
}
System.out.println(" " + nextHigh);
}
}
}
}
Sample Run: