3Sum - Closest

Problem Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Examples Example 1: Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). ...

Reverse Vowels of a String

Problem Write a function that takes a string as input and reverse only the vowels of a string. Examples Example 1: Input : s = hello Output : holle Example 2: Input : s = hello world Output : hollo werld Solution Method 1 - Store the vowels and place them in reverse order One simple solution is to store all the vowels while scanning the string and placing the vowels in the reverse order in another iteration of string. ...

Reverse String Problem

Problem Write a function that takes a string as input and returns the string reversed. Examples Example 1: Given s = "hello", return "olleh". Given s = “kodeknight” , return “thginkedok“. Solution Method 0 - Iterative, Not in Place In Java, string is immutable. So, cannot do in place!. public String reverseString(String s) { int n = s.length; char[] reversed = s.toCharArray(); return reverse(reversed); } ...

2Sum 2 – Input array is sorted

Problem This problem is similar to 2Sum Problem, only difference is input array is now sorted. Detailed problem Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. ...

Best Time To Buy And Sell Stock 1 - only one transaction

Problem You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. OR ...

Check if given string is palindrome or not

Problem Write an algorithm to find Whether Given String is palindrome or Not. Definition Palindrome Definition Examples Example 1: Input: str = "malayalam" Output: true Example 2: Input: str = "abc" Output: false Solution Method 1 - Recursion Algorithm If the first and last characters don’t match, return false (not a palindrome). If they match, remove the first and last characters and call the function recursively with the remaining substring. This continues until the entire string is checked. Code Java public Boolean isPalindrome(String s) { if (s.length()<2) { return true; } if (s.charAt(0) == s.charAt(s.length() - 1)) { return isPalindrome(s.substring(1, s.length() - 1)); } else { return false; } } ...

Merge Two Sorted Arrays

Problem Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. Similar Problem You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. ...

Move Zeroes at the end of array

Problem Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Examples Example 1: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] Example 2: Input: nums = [0] Output: [0] ...

Split Linked List into two halves fractionally given n

Problem Given a linked list with head pointer and a number n. Split the linked list into two halves such that first half contains 1/n elements and remaining linkedlist contains remaining elements. Return the head of the second list or the right partition pointer. Examples Example 1: Input: head = [1, 2, 3, 4, 5, 6, 7, 8], n = 2 Output: [5, 6, 7, 8] Explanation: As n = 2, we have 2 lists like this [ [1, 2, 3, 4], [5, 6, 7, 8] ] and we return the list with head at 5. ...

Squares of a Sorted array Problem

Problem Given an array of numbers sorted in increasing order, return a new array containing squares of all the numbers of the input array sorted in increasing order. Examples Example 1: Input: a[] = [-5, -2, -1, 0, 4, 6] Output: [0, 1, 4, 16, 25, 36] Explanation: After squaring, the array becomes [25, 4, 1, 0, 16, 36]. After sorting, it becomes [0, 1, 4, 16, 25, 36]. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy