Interview Question Next Greater Element I

Last update: 2/22/2017 6:13:00 PM

By Fatih KABAKCI

The problem is to figure out if there is something greater in the second array than right of the element in the first array. Once found something, it is added immediately into the result array. Otherwise -1 is added.

Problem #496 - Next Greater Element I

Description: You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
a. For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
b. For number 1 in the first array, the next greater number for it in the second array is 3.
c. For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
a. For number 2 in the first array, the next greater number for it in the second array is 3.
b. For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

Note:
1. All elements in nums1 and nums2 are unique.
2. The length of both nums1 and nums2 would not exceed 1000.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.NextGreaterElementI;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author fkabakci
* Problem Description: You are given two arrays (without duplicates) nums1 and nums2 where nums1 s elements are subset of nums2.
* Find all the next greater numbers for nums1"s elements in the corresponding places of nums2.
*
* The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2.
* If it does not exist, output -1 for this number.
*
* Example 1:
* Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
* Output: [-1,3,-1]
*
* Explanation:
* For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
* For number 1 in the first array, the next greater number for it in the second array is 3.
* For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
*
* Example 2:
* Input: nums1 = [2,4], nums2 = [1,2,3,4].
* Output: [3,-1]
* Explanation:
* For number 2 in the first array, the next greater number for it in the second array is 3.
* For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
*
* Note:
* 1. All elements in nums1 and nums2 are unique.
* 2. The length of both nums1 and nums2 would not exceed 1000.
*
* Solution:
* 1. Find the element with its index in the second array, then stop.
* 2. Iterate the array again by starting from the index until find something lower than the element.
* 3. Add it into the list.
* 4. Convert list to array.
* 5. return it.
*/
public class Solution {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
List<Integer> list = new ArrayList<Integer>();
for (int num : findNums) {
int j;
for (j = 0; j < nums.length && num != nums[j]; j++)
;
// stop
int greater = -1;
for (int k = j + 1; k < nums.length; k++)
if (num < nums[k]) {
greater = nums[k];
break;
}
}

// copy list to array
int[] resultSet = new int[list.size()];
for (int i = 0; i < list.size(); i++)
resultSet[i] = list.get(i);
return resultSet;
}

public static void main(String[] args) {
Solution s = new Solution();
System.out.println(Arrays.toString(s.nextGreaterElement(new int[] {4, 1, 2}, new int[] {1, 3, 4, 2})));
}
}

Similar Problems

 There has been no comment yet

Name:

Question/Comment
Please verify the image The Topics in Computer Science

Search this site for

Software & Algorithms In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.