Sie sind auf Seite 1von 5

Hi Team,

#Wipro #System #Test #questions #answers


Problem
Q.#Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find
the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it
using only constant extra space complexity?

Program Structure:
public class Solution {
public int missingNumber(int[] nums) {

}
}
Solution
The basic idea is to use XOR operation. We all know that a^b^b =a, which means two
xor operations with the same number will eliminate the number and reveal the
original number.

In this solution, I apply XOR operation to both the index and value of the array.
In a complete array with no missing numbers, the index and value should be
perfectly corresponding( nums[index] = index), so in a missing array, what left
finally is the missing number.

public class Solution {

public int missingNumber(int[] nums) {

int xor = 0, i = 0;

for (i = 0; i < nums.length; i++) {


xor = xor ^ i ^ nums[i];
}

return xor ^ i;
}
}
Difficulty: Easy

Problem
#QGiven an array nums containing n + 1 integers where each integer is between 1 and
n (inclusive), prove that at least one duplicate number must exist. Assume that
there is only one duplicate number, find the duplicate one.

Note:
* You must not modify the array (assume the array is read only).
* You must use only constant, O(1) extra space.
* Your runtime complexity should be less than O(n2).
* There is only one duplicate number in the array, but it could be repeated more
than once.

Program Structure:
public class Solution {
public int findDuplicate(int[] nums) {
}
}
Solution
The main idea is the same with problem finding cycle in Linked List.

Use two pointers the fast and the slow. The fast one goes forward two steps each
time, while the slow one goes only step each time. They must meet the same item
when slow==fast. In fact, they meet in a circle, the duplicate number must be the
entry point of the circle when visiting the array from nums[0]. Next we just need
to find the entry point. We use a point(we can use the fast one before) to visit
form begining with one step each time, do the same job to slow. When fast==slow,
they meet at the entry point of the circle.

public class Solution {

public int findDuplicate(int[] nums) {


int n = nums.length;
int slow = n;
int fast = n;

do {
slow = nums[slow - 1];
fast = nums[nums[fast - 1] - 1];
} while (slow != fast);

slow = n;

while (slow != fast) {


slow = nums[slow - 1];
fast = nums[fast - 1];
}

return slow;
}
}
Difficulty: Medium

Problem
Given an array nums, write a function to move all 0's to the end of it while
maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums
should be [1, 3, 12, 0, 0].

Note:
* You must do this in-place without making a copy of the array.
* Minimize the total number of operations.

Program Structure:
public class Solution {
public void moveZeroes(int[] nums) {

}
}
Solution
public class Solution {
// Shift non-zero values as far forward as
// possible
// Fill remaining space with zeros

public void moveZeroes(int[] nums) {

if (nums == null || nums.length == 0)


return;

int insertPos = 0;

for (int num : nums) {


if (num != 0)
nums[insertPos++] = num;
}

while (insertPos < nums.length) {


nums[insertPos++] = 0;
}
}
}
Difficulty: Easy
Problem
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one
sorted array.

Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n)
to hold additional elements from nums2. The number of elements initialized in nums1
and nums2 are m and n respectively.

Program Structure:
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {

}
}
Solution
public class Solution {
public void merge(int A[], int m,
int B[], int n) {

int i = m - 1, j = n - 1, k = m + n - 1;

while (i > -1 && j > -1)


A[k--] = (A[i] > B[j]) ?
A[i--] : B[j--];

while (j > -1)


A[k--] = B[j--];
}
}
Difficulty: Easy
Problem
Given a matrix of m x n elements (m rows, n columns), return all elements of the
matrix in spiral order.

For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].

Program Structure:
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {

}
}
Solution
I traverse right and increment rowBegin, then traverse down and decrement colEnd,
then I traverse left and decrement rowEnd, and finally I traverse up and increment
colBegin.

The only tricky part is that when I traverse left or up I have to check whether the
row or col still exists to prevent duplicates.

public class Solution {


public List<Integer> spiralOrder(int[][] matrix) {

List<Integer> res = new ArrayList<Integer>();

if (matrix.length == 0) {
return res;
}

int rowBegin = 0;
int rowEnd = matrix.length - 1;
int colBegin = 0;
int colEnd = matrix[0].length - 1;

while (rowBegin <= rowEnd && colBegin <= colEnd) {


// Traverse Right
for (int j = colBegin; j <= colEnd; j++) {
res.add(matrix[rowBegin][j]);
}
rowBegin++;

// Traverse Down
for (int j = rowBegin; j <= rowEnd; j++) {
res.add(matrix[j][colEnd]);
}
colEnd--;

if (rowBegin <= rowEnd) {


// Traverse Left
for (int j = colEnd; j >= colBegin; j--) {
res.add(matrix[rowEnd][j]);
}
}
rowEnd--;

if (colBegin <= colEnd) {


// Traver Up
for (int j = rowEnd; j >= rowBegin; j--) {
res.add(matrix[j][colBegin]);
}
}
colBegin++;
}

return res;
}
}
Difficulty: Medium

Das könnte Ihnen auch gefallen