1) Given array of integers, find the elements which have an even
number of digits.
import [Link].*;
public class Main
public static void main(String[] args) {
ArrayList<Integer> arr=new ArrayList<>();
[Link](42);
[Link](564);
[Link](5775);
[Link](34);
for(int i:arr)
int n=[Link](i).length();
if (n%2==0)
[Link](i);
_________________________________________________________________________________
2) Given array of integers, find the max product of two distinct
elements in an array.
import [Link].*;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
[Link](1);
[Link](4);
[Link](3);
[Link](6);
[Link](7);
[Link](0);
int max = 0;
for (int i = 0; i < [Link](); i++) {
for (int j = i + 1; j < [Link](); j++) {
int product = [Link](i) * [Link](j);
if (product > max) {
max = product;
}
}
}
[Link](max); // ✅ 42
}
}
(or)
import [Link].*;
public class Main
{
public static void main(String ar[])
{
Scanner s=new Scanner([Link]);
[Link]("Enter number of elements:");
int n=[Link]();
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<n;i++)
{
[Link]([Link]());
}
int max=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int product=[Link](i)*[Link](j);
if(product>max)
{
max=product;
}
}
}
[Link]("maximum product of two numbers is:"+ max);
}
}
3) Given an array of integers nums and an integer target, return indices of the two
numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not
use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
public class Solution {
public static int[] twoSum(int[] nums, int target) {
// Outer loop: pick the first number
for (int i = 0; i < [Link]; i++) {
// Inner loop: pick the second number
for (int j = i + 1; j < [Link]; j++) {
// Check if both add up to target
if (nums[i] + nums[j] == target) {
return new int[] {i, j}; // return their indices
}
}
}
return new int[] {}; // just in case
}
public static void main(String[] args) {
int[] nums1 = {2, 7, 11, 15};
int target1 = 9;
int[] result1 = twoSum(nums1, target1);
[Link]("Output: [" + result1[0] + ", " + result1[1] +
"]");
int[] nums2 = {3, 2, 4};
int target2 = 6;
int[] result2 = twoSum(nums2, target2);
[Link]("Output: [" + result2[0] + ", " + result2[1] +
"]");
}
}
4) REVERSE INTEGER:
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes
the value to go outside the signed 32-bit integer range [-2 , 2 - 1], then return 0.
31 31
Assume the environment does not allow you to store 64-bit integers (signed
or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
class Solution {
public int reverse(int x) {
int rev=0;
while(x!=0)
{
int y=x%10;
rev=(rev*10)+y;
x/=10;
}
if ((rev<Integer.MIN_VALUE) || (rev>Integer.MAX_VALUE))
{
return 0;
}
return (int)rev;
}
}
class Solution {
public boolean isPalindrome(int x) {
int org=x;
int rev=0;
if (x<0)
{
return false;
}
while(x!=0)
{
int dig=x%10;
rev=(rev*10)+dig;
x/=10;
}
if(rev==org)
{
return true;
}
return false;
}
}
5) Remove Duplicates from sorted Array.
import [Link].*;
class Main {
public static int removeDuplicates(int[] nums)
{
Set<Integer> set = new HashSet<>();
// Add all elements to a set (automatically removes duplicates)
for (int num : nums) {
[Link](num);
}
// Convert back to a sorted list
List<Integer> list = new ArrayList<>(set);
[Link](list);
// Copy back to nums[]
int i = 0;
for (int num : list) {
nums[i++] = num;
}
// Return count of unique elements
return [Link]();
}
public static void main(String[] ar) {
Scanner s = new Scanner([Link]);
int n = [Link]();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
int len = removeDuplicates(arr);
[Link]("Unique count: " + len);
[Link]("Array after removing duplicates: ");
for (int i = 0; i < len; i++) {
[Link](arr[i] + " ");
}
}
}
6) Return the unique element in the array:
import [Link].*;
public class Main
public static void main(String ar[])
{
Scanner s=new Scanner([Link]);
int n=[Link]();
int arr[]=new int[n];
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link](arr[i],[Link](arr[i],0)+1);
for([Link]<Integer,Integer> entry:[Link]())
if([Link]()==1)
[Link]([Link]());
}}}
7) Return the first unique element in the array:
import [Link].*;
public class Main
{
public static void main(String ar[])
{
Scanner s = new Scanner([Link]);
int n = [Link]();
int arr[] = new int[n];
HashMap<Integer, Integer> map = new HashMap<>();
// read elements and count frequency
for (int i = 0; i < n; i++)
{
arr[i] = [Link]();
[Link](arr[i], [Link](arr[i], 0) + 1);
}
boolean found=false;
for(int i=0;i<n;i++)
{
if ([Link](arr[i])==1)
{
[Link](arr[i] + " ");
found=true;
break;
}
}
if(!found)
{
[Link]("No non repeating elemnts");
}
}
}
8) finding first and second max in array..
import [Link].*;
public class Main
{
public static void main(String ar[])
{
Scanner s=new Scanner([Link]);
int n=[Link]();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=[Link]();
}
int fimax=Integer.MIN_VALUE;
int secmax=Integer.MIN_VALUE;
for(int i=0;i<n;i++)
{
if(arr[i]>fimax)
{
secmax=fimax;
fimax=arr[i];
}
else if(arr[i]>secmax && arr[i]!=fimax)
{
secmax=arr[i];
}
}
[Link]("First Max is:" + fimax);
if(secmax==Integer.MIN_VALUE)
{
[Link]("No second large elemrnt present");
}
else{
[Link]("Second Max is:" + secmax);
}
}
}
9) Given a string s consisting of words and spaces, return the length of the last word in
the string.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Constraints:
1 <= [Link] <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.
CODE:
class Solution {
public int lengthOfLastWord(String s) {
s=[Link]();
int length=0;
for(int i=[Link]()-1;i>=0;i--)
{
if([Link](i)!=' ')
{
length++;
}
else
{
break;
}
}
return length;
}
}
10) You are given a large integer represented as an integer array digits, where
each digits[i] is the i digit of the integer. The digits are ordered from most significant to
th
least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
Example 2:
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].
Example 3:
Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].
CODE:
class Solution {
public int[] plusOne(int[] digits) {
for(int i=[Link]-1;i>=0;i--)
{
if(digits[i]<9)
{
digits[i]++;
return digits;
}
else
{
digits[i]=0;
}
}
// if all were 9s (like 999 -> 1000)
digits=new int[[Link]+1];
digits[0]=1;
return digits;
}
}
Explanation:
If the last digit < 9 → just add 1 and stop.
If it’s 9 → make it 0 and carry the 1 to the left.
If all are 9 → need a new array (like 999 → 1000).
11) Remove Element
Given an integer array nums and an integer val, remove all occurrences of val in nums in-
place. The order of the elements may be changed. Then return the number of elements
in nums which are not equal to val.
Consider the number of elements in nums which are not equal to val be k, to get
accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the
elements which are not equal to val. The remaining elements of nums are not
important as well as the size of nums.
Return k.
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums
containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).
CODE:
class Solution {
public int removeElement(int[] nums, int val) {
int j=0;
for(int i=0;i<[Link];i++)
{
if(nums[i]!=val)
{
nums[j]=nums[i];
j++;
}
}
return j;
}
}
12)Majority Element:
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may
assume that the majority element always exists in the array.
Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Constraints:
n == [Link]
1 <= n <= 5 * 104
-109 <= nums[i] <= 109
The input is generated such that a majority element will exist in the array.
Follow-up: Could you solve the problem in linear time and in O(1) space?
class Solution {
public int majorityElement(int[] nums) {
HashMap<Integer,Integer> map=new HashMap<>();
for(int i:nums)
{
[Link](i,[Link](i,0)+1);
}
for([Link]<Integer,Integer> entry:[Link]())
{
int n=[Link];
if([Link]()>n/2)
{
return [Link]();
}
}
return -1;
}
}
_____________________________________________________________________________
________