Basic Array Problems
1. Write a java program to Print elements from1D array using for-
loop.
Input : arr[]={1,2,3,4,5,6};
Output: 1 2 3 4 5 6
2. Write a java program to Print elements from1D array using while-
loop.
Input : arr[]={1,2,3,4,5,6};
Output: 1 2 3 4 5 6
3. Write a java program to Print elements from1D array using
do- while loop.
Input : arr[]={1,2,3,4,5,6};
Output: 1 2 3 4 5 6
4. Write a java program to Print elements from 1D array using
for-each loop.
Input : arr[]={1,2,3,4,5,6};
Output: 1 2 3 4 5 6
5. Write a java program to Print elements from2D array using for-
loop.
Ip : a={{1,2,3},{4,5,6}};
Op: 1 2 3
456
By – Shrikant sir
[Link] a java program to Print elements from 2D array using while -
loop.
Ip : a={{1,2,3},{4,5,6}};
Op: 1 2 3
456
[Link] a java program to Print elements from 2D array using do-
while loop.
Ip : a={{1,2,3},{4,5,6}};
Op: 1 2 3
456
[Link] a java program to Print elements from 2D array using for-
each loop.
Ip : a={{1,2,3},{4,5,6}};
Op: 1 2 3
456
[Link] a java program to Print elements from3D array using for-
loop.
Ip : a={{{1,2},{2,3}},{{4,5},{5,6}}};
Op: 1 2
2 3
4 5
5 6
By – Shrikant sir
[Link] a java program to Print elements from 3D array using
while-loop.
Ip : a={{{1,2},{2,3}},{{4,5},{5,6}}};
Op: 1 2
2 3
4 5
5 6
[Link] a java program to Print elements from 3D array using do-
while loop.
Ip : a={{{1,2},{2,3}},{{4,5},{5,6}}};
Op: 1 2
2 3
4 5
5 6
[Link] a java program to Print elements from 3D array using for-
each loop.
Ip : a={{{1,2},{2,3}},{{4,5},{5,6}}};
Op: 1 2
2 3
4 5
5 6
By – Shrikant sir
[Link] number of elements without using length variable.
Ip : a={1, 2, 3, 4, 5, 6}
Op : 6
By – Shrikant sir
[Link] sum of all elements present in an array.
Ip : a={2, 2, 4, 4, 6, 6}
Op : 24
[Link] elements in an array.
Ip : a={1, 2, 3, 4, 5, 6}
Op : 6 5 4 3 2 1
[Link] product of all elements present in an array.
Ip : a={2, 2, 4, 4, 6, 6}
Op : 2304
[Link] all even elements present in an array.
Ip : a={2, 8, 5, 4, 7, 9}
Op : 2 8 4
[Link] all odd elements present in an array.
Ip : a={2, 8, 5, 4, 7, 9}
Op : 5 7 9
[Link] all prime elements present in an array.
Ip : a={2, 8, 5, 4, 7, 9}
Op : 2 5 7
[Link] count of positive and negative elements present in an array.
Ip : a={7, -6, 4, -1, 5}
Op : Positive : 3
Negative : 2
By – Shrikant sir
[Link] odd elements and store it in a new array.
Ip : a={1, 2, 3, 4, 5, 6}
Op :b={1,3,5}
[Link] even elements and store it in a new array.
Ip : a={1, 2, 3, 4, 5, 6}
Op :b={2,4,6}
[Link] first 10 prime numbers and store it in an array.
Op :a={2,3,5,7,11,13,17,19,23,29}
[Link] first 10 prime numbers and store it in an array.
Op :a={2,3,5,7,11,13,17,19,23,29}
[Link] first 10 Fibonacci numbers and store it in an array.
Op :a={0,1,1,2,3,5,8,13,21,34}
[Link] sum of all elements at even indices in an array.
Ip : a={6,5,4,3,7,8,0}
Op : 17
[Link] sum of all elements at odd indices in an array.
Ip : a={6,5,4,3,7,8,0}
Op : 17
[Link] two 1D arrays.
Ip : a={1,2,3}
b={4,5,6}
Op : c={1,2,3,4,5,6}
By – Shrikant sir
[Link] three 1D arrays.
Ip : a={1,2,3}
b={4,5,6}
c={7,8,9}
Op : c={1,2,3,4,5,6,7,8,9}
30. Merge elements of a 2D array.
Ip : a={{4,5,6},{3,2,1}};
Op: 4 5 6 3 2 1
[Link] factors of a number and store them in an array.
Ip : num : 12
Op : a={1,2,3,4,6,12}
[Link] a clone of 1D array without using clone().
Ip : a={4,5,6}
Op : copy={4,5,6}
[Link] a clone of 2D array without using clone().
Ip : a={{5,6,4},{7,3,8}}
Op : copy={{5,6,4},{7,3,8}}
[Link] a clone of 3D array without using clone().
Ip : a={{{1,2},{2,3}},{{4,5},{5,6}}}
Op : copy={{{1,2},{2,3}},{{4,5},{5,6}}}
By – Shrikant sir
[Link] Zig Zag merging of arrays.
Ip : a={1,2,3}
b={4,5,6,7,8}
Op : c={1,4,2,5,3,6,7,8}
By – Shrikant sir
[Link] duplicate elements in an array.
Ip : a={6,5,4,6,5,8,6}
Op : 6 , 5
[Link] all distinct elements in an array and store them in a new
array.
Ip : a={4,3,2,3,4,5,6,7}
Op : b={4,3,2,5,6,7}
38. Write a java program to find Palindrome numbers from an array .
Input: arr[]={121,432,535,986,222};
Output: 121,535,222
39. Write a java program to find Palindrome strings from an array.
Array: ["madam", "racecar", "apple", "civic", "level", "hello"]
Output: madam,racecar, civic, level
40. Write a java program to convert a string into char array.
Input: String str=”java”
Output: ['j', 'a', 'v', 'a']
41. Write a java program to create a subarray from an array.
Input: arr: [1, 2, 3, 4, 5, 6, 7, 8, 9]Start Index: 2,End Index: 5
Output: [3, 4, 5]
By – Shrikant sir
42. Write a java program to store user elements in an array and tak e
length from user.
43. Write a java program to Rotate an array ‘k’ times forward.(left-
rotate)
arr[] = {1,2,3,4,5}; k=2
output: 3,4,5,1,2
44. Write a java program to Rotate an array ‘k’ times
backward.(right-rotate)
arr[] = {1,2,3,4,5};
k=2;
output: 4,5,1,2,3
45. Write a java program to check if 2 arrays are equal or not.
Array 1: [1, 2, 3, 4, 5], Array 2: [1, 2, 3, 4, 5]
Output: true
Array 1: [7, 14, 21,45,35], Array 2: [7, 14, 21, 36]
Output: false
46. Write a java program to find largest element from an array
without using sorting method.
Array :[7, 84, 21,45,35]
Output: 84
Array :[2,2,2]
Output: 2
By – Shrikant sir
47. Write a java program to find smallest element from an array
without using sorting method.
Array :[7, 84, 21,5,35]
Output: 5
Array Problems based on Sorting and Searching Techniques
48. Write a java program to sort an array in ascending order using
Bubble sort.
arr[]={4,2,1,3,5};
Output: 1,2,3,4,5
49. Write a java program to sort an array in ascending order using
Selection sort.
50. Write a java program to sort an array in ascending order using
Insertion sort.
51. Write a java program to sort an array in descending order using
Bubble sort.
52. Write a java program to sort an array in descending order using
Selection sort.
By – Shrikant sir
53. Write a java program to sort an array in descending order using
Insertion sort.
54. Write a java program to find the largest element from an array
using sorting.
Arr[]={32,54,76,23,2};
Output: 76
55. Write a java program to find the smallest element from an array
using sorting.
Arr[]={32,54,76,23,2};
Output: 2
56. Write a java program to find the Second Largest element from
an array using sorting.
Arr[]={32,12,76,23,28};
Output: 32
57. Write a java program to find the Third Largest element from an
array using sorting.
Arr[]={32,54,76,23,2};
Output:32
58. Write a java program to find the Third Smallest element from an
array using sorting.
Arr[]={7, 44, 21,45,35};
Output: 35
By – Shrikant sir
59. Write a java program to find the Second Smallest element from
an array using sorting.
Arr[]={7, 44, 21,45,35};
Output: 44
60. Find Missing and Repeating Numbers from an array.
Given an unsorted array arr of size n of positive integers. One
number 'A' from set {1, 2, .... ,N} is missing and one number 'B'
occurs twice in array. Find these two numbers.
Input: n=3, arr[]={1,3,3}
Output: 3 2
Explanation: Repeating number is 3 and smallest positive missing
number is 2.
61. In the assembly line, the factor assembles three parts ‘a’, ‘b’, ‘c’
of a triangle toy. A valid toy is one where the two shorter sides
added together are greater in length than the longest side.
- There are two forms of valid triangles to identify.
1. if 2 parts are of equal length ,the form is ‘isosceles’.
2. if all 3 parts are of equal length, the form is ‘Equilateral’.
Input: 36 36 30, output: Isosceles
Input: 47 8 60, output: None of these
Input: 86 86 86, output: Equilateral
By – Shrikant sir
62. Maximum Candies : There is a candy shop near your house.
There are two types of candies:
1. Candy with a plastic wrapper, which costs A rupees.
2. Candy with a paper wrapper, which costs B rupees.
You can also sell the plastic wrapper for C rupees (C<A).
You have N rupees. you want to buy maximum candies.
Input: N=9, A=8, B=10, C=7
Output: 2
Explanation: You can buy a plastic candy, return its wrapper, and buy
one more plastic candy. Thus, you will have 2 candies.
63. In a household, a family keeps tracks of their monthly expenses
using matrix of size NXN, with each row representing different
month, and each column representing an expense category.
They want your help in determining the month with the highest
expenses using a program.
The program must print all expenses of the month that has the
highest total expenditure. If more than one month has the same
highest sum then print the expenditure of the month that occurs
first.
Input: N=3
123
456
789
Output: 7 8 9
By – Shrikant sir
Explanation: The sum of expenditure amount for 1st month is:
1+2+3=6
The sum of expenditure amount for 2nd month is: 4+5+6=15
The sum of expenditure amount for 3rdmonth is: 7+8+9=24
Among all three months, the 3rd month has the highest expenses.
So, expenditure amounts are 7, 8, 9 will be printed as output.
64. A stock trader trades in N selected stocks. The trader has
calculated the relative stock price changes in the N stocks from the
previous day stock prices. The lucky number of the trader is K, so
the trader wishes to invest in the particular stock that has the kth
smallest relative stock value.
Input: arr[]={10,5,7,88,19}, k=3
Output: 1
[Link] Number of an Integer :
Given a positive integer n, return the magic number of n where n is
defined as the sum of the squares of all integers i such that :
1<= i<= n , and the decimal representation of i*i can be partitioned
into contiguous substrings such that the sum of the integer values
of these substrings equals i.
Input: n=10, Output: 182
Explanation: There are exactly 3 integers i that satisfy the
conditions in the statement: 1 since 1*1=1
: 9 since 9*9=81 and 81 can be partitioned into 8+1
: 10 since 10*10=100 and 100 partitioned into 10+0.
Hence, the partition number of 10 is 1+81+100=182.
By – Shrikant sir
66. Word Dominance: Who Rules the Textual Kingdom ?
In short, write a java program to return a maximum times occurring
string in string array.
Input: “apple”, “banana” ,”apple”, “banana”, “apple”, “cherry”
Output: apple
67. Given an integer array nums, return an array answer such that
answer[i] is equal to the product of all the elements of nums except
nums[i].
Ip : nums={1, 2, 3, 4}
Op :answer={24,12,8,6}
68. Given an integer array nums, move all O'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.
Ip : nums={0,1,0,3,12}
Op :answer={1,3,12,0,0}
69. You are given an array of prices where prices[i] is the price of a
given stock on an ith day. You want to maximise 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.
By – Shrikant sir
Ip : prices={7,1,5,3,6,4}
Op : 5
Explanation : Buy on day 2 (price = 1) and sell on day 5 (price = 6),
profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because
you must buy before you sell.
70. The next permutation of an array of integers is the next
lexicographically greater permutation of its integer. Given an array
of integers nums, find the next permutation of nums.
The replacement must be in place and use only constant extra
memory.
Ip : nums={1,2,3}
Op :answer={1,3,2}
71. Given an array of integers and an integer target, return indices
of the two numbers such that they add up to target.
Ip : nums={2,7,11,15} target=9
Op :answer={0,1}
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
By – Shrikant sir
Assessment Questions :
72.
By – Shrikant sir
73.
By – Shrikant sir
74.
By – Shrikant sir
75.
By – Shrikant sir
76.
By – Shrikant sir
77.
78.
By – Shrikant sir
79.
80.
By – Shrikant sir
81.
82.
By – Shrikant sir
83.
By – Shrikant sir
By – Shrikant sir
84.
By – Shrikant sir
85.
By – Shrikant sir
86.
By – Shrikant sir
87.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
88.
By – Shrikant sir
By – Shrikant sir
89.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
90.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
91.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
92.
By – Shrikant sir
By – Shrikant sir
93.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
94.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
95.
By – Shrikant sir
By – Shrikant sir
96.
By – Shrikant sir
97.
By – Shrikant sir
By – Shrikant sir
98.
By – Shrikant sir
By – Shrikant sir
99.
By – Shrikant sir
100.
By – Shrikant sir
By – Shrikant sir
101.
By – Shrikant sir
By – Shrikant sir
102.
By – Shrikant sir
By – Shrikant sir
103.
By – Shrikant sir
By – Shrikant sir
104.
By – Shrikant sir
By – Shrikant sir
By – Shrikant sir
105.
By – Shrikant sir
By – Shrikant sir