Skip to main content

Posts

Showing posts with the label Arrays

Valid Mountain Array ( Google )

  Problem :- Given  an  array   A   of  integers ,  return  true  if  and  only  if  it  is  a  valid  mountain  Array ,  otherwise  return  false . ☝ Sample Input :-  [ 0 , 2 , 3 , 4 , 5 , 2 , 1 ] Sample Output :-  true ✌ Sample Input :-  [ 0 , 2 , 3 , 4 , 5  ] Sample Output :-  false   Explanation :- Solution : - Java Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Mountain { public static void main (String[] args) { int [] arr = { 0 , 2 , 3 , 4 , 5 , 2 , 1 } ; System. out . println (validMountain(arr)) ; } public static boolean validMountain ( int [] arr) { int i = 0 , n = arr. length - 1 ; while (i<n && arr[i]<arr[i+ 1 ]) i++; if (i== 0 || i==n) return false ; while (i<n &...

Placement Problem 1

 Don't jump directly to the solution.  Read the problem carefully and try to solve it on your own. Solution : Java Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.Scanner; public class PlacementProblem1 { public static void main (String[] args) { Scanner sc = new Scanner(System. in ); int t = sc. nextInt (); while (t--> 0 ) { int size = sc. nextInt (); int target = sc. nextInt (); int arr[] = new int [size]; for ( int i= 0 ; i<arr. length ; i++) arr[i] = sc. nextInt (); for ( int i= 0 ; i<arr. length ; i++) { boolean flag = false ; for ( int j=i+ 1 ; j<arr. length ; j++) { if (arr[i]+arr[j]==target) { System. out . println (i+ " " +j); ...

How to sort a string array by 2nd char of each string in the array ?

 Given a string array. Write a code to sort the strings in the array based on their 2nd character. Assumption :-  Each array element contains more than one character. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class SortString { public static void main (String[] args) { String[] arr = { "heist" , "code" , "blog" , "python" , "java" }; for ( int i= 0 ; i<arr. length - 1 ; i++){ for ( int j=i+ 1 ; j<arr. length ; j++){ if (arr[i]. charAt ( 1 )>arr[j]. charAt ( 1 )){ String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (String s:arr) System. out . println (s); } } Python  (using sort and lambda) arr = [ "heist" , "code" , "blog" , "python" , ...

Contains duplicates

 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1 Input: [1,2,3,1] Output: true Example 2 Input: [1,2,3,4] Output: false Java 1. Using Linear search (takes more time) 1 2 3 4 5 6 7 8 public boolean containsDuplicate ( int [] nums) { for ( int i = 0 ; i < nums. length ; ++i) { for ( int j = 0 ; j < i; ++j) { if (nums[j] == nums[i]) return true ; } } return false ; } 2. Using sorting 1 2 3 4 5 6 7 8 public boolean containsDuplicate ( int [] nums) { Arrays. sort (nums); for ( int i = 0 ; i < nums. length - 1 ; ++i) { if (nums[i] == nums[i + 1 ]) return true ; } return false ; } 3. Using HashSet 1 2 3 4 5 6 7 8 9 public boolean containsDuplicate ( int [] nu...

Find the running sum of an Array

nums = [ 3, 1, 2, 10, 1 ] runningSum = [ 3, 3+1, 3+1+2, 3+1+2+10, 3+1+2+10+1 ] runningSum = [ 3, 4, 6, 16, 17 ]  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class RunningSum { public static void main (String args[]) { int [] nums = { 1 , 2 , 3 , 4 }; int [] runningSum = findRunningSum(nums); for ( int x: runningSum) System. out . print (x+ " " ); } public static int [] findRunningSum ( int [] nums) { int [] runningSum = new int [nums. length ]; int sum = 0 ; for ( int i= 0 ; i<nums. length ; i++){ sum += nums[i]; runningSum[i]=sum; } return runningSum; } } Python def runningSum(arr): result = [ None ] * len (arr) sum = 0 for i in range ( 0 , len (arr)): sum += arr[i] result[i] = sum return result arr = [ 1 , 2 , 3 , 4 ] print (ru...

Check Array elements can make Arithmetic Progression

Example 1: Input: arr = [3,5,1] Output: true Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements. Example 2: Input: arr = [1,2,4] Output: false Explanation: There is no way to reorder the elements to obtain an arithmetic progression.  Constraints: 2 < arr.length <= 1000 -10^6 <= arr[i] <= 10^6 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.util.*; public class AP { public static void main (String args[]) { int [] arr = { 3 , 5 , 1 }; System. out . println (checkAP(arr)); } public static boolean checkAP ( int [] arr){ Arrays. sort (arr); // sort the array int diff = arr[ 1 ] - arr[ 0 ]; // difference for ( int i= 2 ; i<arr. length ; i++) if (arr[i]-arr[i- 1 ]!=diff) return false ; return true ; } } ...

Number of Students Doing Homework at a Given Time

Given two integer arrays startTime and endTime and given an integer queryTime . The ith student started doing their homework at the time startTime[ i ] and finished it at the time endTime[ i ] . Return the number of students doing their homework at time queryTime .   Example 1: Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4 Output: 1 Explanation: We have 3 students where: The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4. The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4. The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4. Example 2: Input: startTime = [4], endTime = [4], queryTime = 4 Output: 1 Explanation: The only student was doing their homework at the queryTime. Example 3: Input: startTime = [4], endTime = [4...

Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: Input: [1,3,5,6], 5 Output: 2 Example 2: Input: [1,3,5,6], 2 Output: 1 Example 3: Input: [1,3,5,6], 7 Output: 4 Example 4: Input: [1,3,5,6], 0 Output: 0 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class SearchInsertPos { public static void main (String args[]) { int [] arr = { 1 , 3 , 5 , 6 } ; int target = 0 ; System. out . println (searchInsPos(arr, target)); } public static int searchInsPos ( int [] arr, int target){ if (arr. length == 0 ) return - 1 ; int i = 0 ; while (i < arr. length && target > arr[i]){ i++...

Merge two sorted array

 Write a function to merge two sorted arrays. Ex:- [ 1, 2, 4 ]  and [ 1, 3, 4 ] output:-  [ 1, 1, 2, 3, 4, 4] Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class MergeList { public static void main (String args[]) { int [] arr1 = { 1 , 2 , 4 }; int [] arr2 = { 1 , 3 , 4 }; int [] arr = mergeArray(arr1, arr2); for ( int x: arr) System. out . print (x+ " " ); // 1 1 2 3 4 4 } public static int [] mergeArray ( int [] arr1, int [] arr2){ int [] arr = new int [arr1. length + arr2. length ]; int i = 0 , j = 0 , k = 0 ; while (i<arr1. length && j<arr2. length ){ if (arr1[i] < arr2[j]) arr[k++] = arr1[i++]; else arr[k++] = arr2[j++]; } while (i < arr1....