Posts

Showing posts with the label Arrays

Get Maximum in Generated Array

  You are given an integer   n . An array   nums   of length   n + 1   is generated in the following way: nums[0] = 0 nums[1] = 1 nums[2 * i] = nums[i]  when  2 <= 2 * i <= n nums[2 * i + 1] = nums[i] + nums[i + 1]  when  2 <= 2 * i + 1 <= n Return   the  maximum  integer in the array  nums ​​​. Example 1: Input: n = 7 Output: 3 Explanation: According to the given rules: nums[0] = 0 nums[1] = 1 nums[(1 * 2) = 2] = nums[1] = 1 nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2 nums[(2 * 2) = 4] = nums[2] = 1 nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3 nums[(3 * 2) = 6] = nums[3] = 2 nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3 Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3. Example 2: Input: n = 2 Output: 1 Explanation: According to the given rules, the maximum between nums[0], nums[1], and n...

Merge Sorted Array

Given two sorted integer arrays  nums1  and  nums2 , merge  nums2  into  nums1  as one sorted array. The number of elements initialized in  nums1  and  nums2  are  m  and  n  respectively. You may assume that  nums1  has enough space (size that is equal to  m + n ) to hold additional elements from  nums2 . Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Example 2: Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1] Constraints: 0 <= n, m <= 200 1 <= n + m <= 200 nums1.length == m + n nums2.length == n -10 9  <= nums1[i], nums2[i] <= 10 9 Solution : class Solution {     public void merge(int[] nums1, int m, int[] nums2, int n) {         int i;     ...

Check If Two String Arrays are Equivalent

Given two string arrays  word1  and  word2 , return   true  if the two arrays  represent  the same string, and  false  otherwise. A string is  represented  by an array if the array elements concatenated  in order  forms the string. Example 1: Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true. Example 2: Input: word1 = ["a", "cb"], word2 = ["ab", "c"] Output: false Example 3: Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] Output: true   Constraints: 1 <= word1.length, word2.length <= 10 3 1 <= word1[i].length, word2[i].length <= 10 3 1 <= sum(word1[i].leng...

The kth Factor of n

Given two positive integers   n   and   k . A factor of an integer  n  is defined as an integer  i  where  n % i == 0 . Consider a list of all factors of  n  sorted in  ascending order , return  the  kth  factor  in this list or return  -1  if  n  has less than  k  factors. Example 1: Input: n = 12, k = 3 Output: 3 Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3. Example 2: Input: n = 7, k = 2 Output: 7 Explanation: Factors list is [1, 7], the 2nd factor is 7. Example 3: Input: n = 4, k = 4 Output: -1 Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1. Example 4: Input: n = 1, k = 1 Output: 1 Explanation: Factors list is [1], the 1st factor is 1. Example 5: Input: n = 1000, k = 3 Output: 4 Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 25...

Longest Mountain in Array

  Let's call any (contiguous) subarray B (of A) a   mountain   if the following properties hold: B.length >= 3 There exists some  0 < i < B.length - 1  such that  B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array  A  of integers, return the length of the longest  mountain .  Return  0  if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in  O(1)  space? Try it on Leetcode  ...

Maximum XOR of Two Numbers in an Array

  Given a   non-empty   array of numbers, a 0 , a 1 , a 2 , … , a n-1 , where 0 ≤ a i   < 2 31 . Find the maximum result of a i  XOR a j , where 0 ≤  i ,  j  <  n . Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum result is 5 ^ 25 = 28.   class Solution {     public int findMaximumXOR(int[] nums) {         int i, j, length = nums.length, max=0;         for(i=0;i<length;i++){             int tmp = 0;             for(j=0;j<length;j++){                 if(i!=j){                     tmp = nums[i]^num...

Prison Cells After N Days

There are 8 prison cells in a row, and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules: If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied. Otherwise, it becomes vacant. (Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.) We describe the current state of the prison in the following way:  cells[i] == 1 if the i -th cell is occupied, else cells[i] == 0 . Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.) Example 1: Input: cells = [0,1,0,1,1,0,0,1] , N = 7 Output: [0,0,1,1,0,0,0,0] Explanation: The following table summarizes the state of the prison on each day: Day 0: [0, 1, 0, 1, 1, 0, 0, 1] Day 1: [0, 1, 1, 0, 0, 0, 0, 0] Day 2: [0, 0, 0, 0, 1, 1, 1, 0] Day 3: [0, 1, 1, 0, 0, 1, ...

Permutation in String

Given two strings s1 and s2 , write a function to return true if s2 contains the permutation of s1 . In other words, one of the first string's permutations is the substring of the second string. Example 1: Input: s1 = "ab" s2 = "eidbaooo" Output: True Explanation: s2 contains one permutation of s1 ("ba"). Example 2: Input: s1= "ab" s2 = "eidboaoo" Output: False Constraints: The input strings only contain lower case letters. The length of both given strings is in range [1, 10,000].   public class Solution {     public boolean checkInclusion(String s1, String s2) {         if (s1.length() > s2.length())             return false;         int[] s1Arr = new int[26];         int s1Length = s1.length(), s2Length = s2.length();         for (int ...