0% found this document useful (0 votes)
6 views26 pages

Java Array Manipulation Techniques

The document contains a series of Java programming tasks related to array manipulation, including swapping arrays, demonstrating call by value and reference, printing string lengths, finding the largest string, and counting strings with even lengths. It also covers various algorithms such as calculating running sums, finding differences between element sums and digit sums, rotating arrays, and checking for palindromic arrays. Additionally, it includes solutions for common coding problems like two-sum, missing numbers, and checking if an array is strictly increasing.

Uploaded by

aloneboy92519
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Java Array Manipulation Techniques

The document contains a series of Java programming tasks related to array manipulation, including swapping arrays, demonstrating call by value and reference, printing string lengths, finding the largest string, and counting strings with even lengths. It also covers various algorithms such as calculating running sums, finding differences between element sums and digit sums, rotating arrays, and checking for palindromic arrays. Additionally, it includes solutions for common coding problems like two-sum, missing numbers, and checking if an array is strictly increasing.

Uploaded by

aloneboy92519
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

..ARRAY..

Q.1 WAJP to swap two arrays.


static void main() {
int[] a = {1, 2, 3, 4, 5};
int[] b = {6, 7, 8, 9, 10};
[Link]("Array before swap");
printElements(a); int length =
printElements(b); [Link]([Link],[Link]);
int[] temp = a; for(int i=0;i<length-1;i++){
int temp = a[i];
a = b;
a[i] = b[i];
b = temp; b[i] = temp;
[Link]("Array } before after");
printElements(a);
printElements(b);
}

Q.2 Demonstrate call by value and call by reference.


 Call by Value: A copy of the variable is passed; changes don’t affect the original (Java uses this).
 Call by Reference: The actual variable is passed; changes affect the original (C++ supports this).
 Java always uses call by value, even for objects.

Q.3 For the given array of Strings, print the


length of all the Strings.
static void main() {
String[] arr = {"aka", "alen", "john wick"};
for (String str : arr) {
[Link]([Link]()+" ");
}
}
Q.4 For the given array of Strings, print the largest string.

static void main() {


String[] arr = {"aka", "alen", "john
wick"};
String largestStr = arr[0];
for (String str : arr) {
if ([Link]() >= [Link]())
largestStr = str;
}
[Link](largestStr);
}

Q.5 For the given array of Strings, print and count all
the Strings which have even number of characters.
static void main() {
String[] arr = {"aka", "alen", "john
wick","boneyard"};
int count = 0;
for (String str : arr) {
if ([Link]() % 2 == 0) {
[Link](str);
count++;
}
}
[Link]("All even str is:
"+count);
}
Q.6 WAJP for below requirements:
Original array: 2 5 4 3 6
Resultant array: 18 15 16 17 14

public static void requirements(int[] a) {


int sum = 0;
for(int i:a){
sum += i;
}
for(int i : a){
[Link](sum-i+" ");
}
}

Q.7 Running Sum of 1D Array(Leet1480).


public static int[] runningSum(int[] public
a) { static void runningSum(int[] a)
{
for (int i = 1; i < [Link]; i++) { int x = 0;
a[i] += a[i - 1]; int y = a[0];
for (int i = 0; i < [Link]; i++) {
} [Link](x + y + " ");
return a; x = x + y;
if (i == [Link] - 1) {
}
break;
}
y = a[i + 1];
}
}
Q.8 Difference B/w Element Sum and Digit
Sum of an Array(Leet2535).
public static int differenceOfSum(int[] a) {
int eleSum = 0, digSum = 0;
for (int i : a) {
eleSum += i;
digSum += digitSum(i);
}
return eleSum - digSum;
}

public static int digitSum(int n) {


int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

Q.9 Concatenation of Array(Leet1929).


public static int[] getConcatenation(int[] nums) {
int len = [Link];
int[] ans = new int[len * 2];
for (int i = 0; i < len; i++) {
ans[i] = nums[i];
ans[len + i] = nums[i];
}
return ans;
}
Q.10 WAJP for below requirements:
Original array: 2 5 4 3 6
Resultant array: 360 144 180 240 120
public static void requirements(int[] a) {
int mul = 1;
for(int i:a){
mul *= i;
}
for(int i : a){
[Link](mul/i+" ");
}
}

Q.11 Product of Array Except it self(leetq238)


public static int[] productExceptSelf(int[] a) {
int count = 0, product = 1;
int[] ans = new int[[Link]];
for (int i : a) {
if (i == 0) count++;
else product *= i;
}
if (count > 1) return ans;
for (int i = 0; i < [Link]; i++) {
if (count == 0)
ans[i] = product / a[i];
else
ans[i] = a[i] == 0 ? product : 0;
}
return ans;
}

Q.12 Max Consecutive Ones(Leet485).


public static int findMaxConsecutiveOnes(int[] a) {
int count = 0, maxCon = 0;
for (int i : a) {
if (i == 1) {
count++;
maxCon = [Link](count, maxCon);
} else
count = 0;
}
return maxCon;
}

Q.13 Max consecutive n in any array.


If input = 7 Output: 2
public static int findMaxConsecutiveOnes(int[] a, int n) {
int count = 0, maxCon = 0;
for (int i : a) {
if (i == n) {
count++;
maxCon = [Link](count, maxCon);
} else
count = 0;
}
return maxCon;
}

Q.14 WAJP to swap two index values of the array.


Original array: 10 20 30 40 50 60 70
Swapped array: 10 60 30 40 50 20 70
public static int[] swapTwoIndex(int[] a, int idx1, int
idx2) {
if ((idx1 < 0 || idx1 > [Link]) || (idx2 < 0 || idx2 >
[Link])) {
[Link]("Plz Enter Valid Indices");
[Link](0);
}
int temp = a[idx1];
a[idx1] = a[idx2];
a[idx2] = temp;
return a;
}
Q.15 WAJP to reverse each element of the array.
Original array: 10 20 30 40 50 60 70
Reversed array: 70 60 50 40 30 20 10
static void main() {
int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
[Link]("Before reverse:= ");
printElements(a);
reverseArr(a, 0, [Link] - 1);
[Link]("After reverse:= ");
printElements(a);
}

public static void reverseArr(int[] a, int idx1, int idx2) {


if (idx1 < 0 || idx2 < 0 || idx1 >= [Link] || idx2 >=
[Link]) {
[Link]("Please enter valid indices");
return;
}
while (idx1 < idx2) {
int temp = a[idx1];
a[idx1] = a[idx2];
a[idx2] = temp;
idx1++;
idx2--;
}
}

public static void printElements(int[] a) {


for (int i : a) {
[Link](i + " ");
}
[Link]();
}
Q.16 WAJP to reverse 1st half and 2nd half elements of Arr.
Original array: 10 20 30 40 50 60 70
Reversed array: 40 30 20 10 70 60 50
static void main() {
int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
[Link]("Before reverse:= ");
printElements(a);
int mid = ([Link] - 1) / 2;
reverseArr(a, 0, mid);
reverseArr(a, mid + 1, [Link] - 1);
[Link]("After reverse:= ");
printElements(a);
}
public static void reverseArr(int[] a, int idx1, int idx2) {
if (idx1 < 0 || idx2 < 0 || idx1 >= [Link] || idx2 >=
[Link]) {
[Link]("Please enter valid indices");
return;
}
while (idx1 < idx2) {
int temp = a[idx1];
a[idx1] = a[idx2];
a[idx2] = temp;
idx1++;
idx2--;
}
}
public static void printElements(int[] a) {
for (int i : a) {
[Link](i + " ");
}
[Link]();
}

Q.17 WAJP to check array is a palindromic array or not.


public static boolean isArrPalindrome(int[] a) {
int start = 0, end = [Link]-1;
while (start < end) {
if (a[start] != a[end])
return false;
start++; end--;
}
return true;
}

Q.18 Two Sum.(Leet1)


public static int[] twoSum(int[] nums, int target)
{
for (int i = 0; i < [Link]; i++)
for (int j = i + 1; j < [Link]; j++)
if (nums[i] + nums[j] == target)
return new int[]{i, j};
return new int[]{};
}
//////////////////////////////////////////////////////////////////
public static int[] twoSum(int[] arr, int target) {
Map<Integer, Integer> m1 = new HashMap<>();
for (int i = 0; i < [Link]; i++) {
int compliment = target - arr[i];
int val = [Link](compliment, -1);
if (val >= 0)
return new int[]{i, val};
[Link](arr[i], i);
}
return new int[]{};
}
Q.19 WAJP to remove an element from a certain position of
the array.
Original array: 10 20 30 40 50 60 70
Updated array: 10 20 40 50 60 70
public static int[] removeElement(int[] arr, int pos) {
if (pos < 0 || pos >= [Link]) {
[Link]("Enter valid position....");
[Link](0);
}
int[] ans = new int[[Link] - 1];
for (int i = 0; i < [Link] - 1; i++)
if (i < pos)
ans[i] = arr[i];
else
ans[i] = arr[i + 1];
return ans;
}
Q.20 WAJP to insert an element at certain position of the arr
Original array: 10 20 30 40 50 60 70
Updated array: 10 20 30 35 40 50 60 70
public static int[] insert(int[] arr, int pos, int ele) {
if (pos < 0 || pos >= [Link]) {
[Link]("Enter valid position...");
[Link](0);
}
int[] ans = new int[[Link] + 1];
for (int i = 0; i < [Link]; i++) {
if (i < pos)
ans[i] = arr[i];
else if (i == pos)
ans[i] = ele;
else
ans[i] = arr[i - 1];
}
return ans;
}
Q.21 WAJP to rotate each element of an array by one
position.
Original array: 10 20 30 40 50 60 70
Rotated array: 70 10 20 30 40 50 60
public static int[] rotate(int[] arr) {
int[] ans = new int[[Link]];
for (int i = 1; i < [Link]; i++)
ans[i] = arr[i - 1];
ans[0] = arr[[Link] - 1];
return ans;
}
public static int[] rotate(int[] arr) {
int[] ans = new int[[Link]];
if ([Link] > 0)
[Link](arr, 0, ans, 1, [Link] -
1);
ans[0] = arr[[Link] - 1];
return ans;
}

Q.22 WAJP to rotate all the elements of array k position to


its right.
array = {1,2,3,4,5,6,7}
k=2
Output: {6,7,1,2,3,4,5}
public static void rightRotate(int[] arr, int k) {
k = k % [Link];
reverse(arr, 0, [Link] - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, [Link] - 1);
}
public static void reverse(int[] a, int start, int end) {
while (start < end) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++; end--;
}
}

Q.23 Rotate Array (LeetCode189).


Q.24 WAJP to rotate each element of an array by one
position in left side.
public static int[] rotate(int[] arr) {
int temp = arr[0];
for (int i = 0; i < [Link]-1; i++) {
arr[i] = arr[i + 1];
}
arr[[Link]-1] = temp;
return arr;
}
Q.25 WAJP to rotate all the elements of array k position to
its left.
public static void leftRotate(int[] arr, int k) {
k = k % [Link];
reverse(arr, 0, [Link] - 1);
reverse(arr, 0, ([Link] - k) - 1);
reverse(arr, [Link] - k, [Link] - 1);
}
public static void reverse(int[] a, int start, int end) {
while (start < end) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
}

Q.26 WAJP find missing element from a given array.


public static int getMissingElement(int[] arr) {
int sum = 0;
for (int i : arr)
sum += i;
int n = [Link] + 1;
return n * (n + 1) / 2 - sum;
}

Q.27 Missing Number (LeetCode268).


public static int getMissingElement(int[] arr) {
[Link](arr);
for (int i = 0; i < [Link]; i++) {
if (arr[i] != i + 1)
return i + 1;
}
return [Link] + 1;
}

Q.28 Best Time to Buy and Sell Stock(LeetCode121).


public static int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;

for (int price : prices) {


if (price < minPrice) {
minPrice = price; // buy at lowest price
} else {
maxProfit = [Link](maxProfit, price - minPrice);
}
}
return maxProfit;
}

Q.29 WAJP to check if an array is strictly increasing.


public static boolean isStrictlyIncreasing(int[] arr) {
if (arr == null || [Link] < 2)
return true;
for (int i = 0; i < [Link] - 1; i++) {
if (arr[i] >= arr[i + 1])
return false;
}
return true;
}

Q.30 WAJP to check whether a given array is


in sorted order or not.
public static boolean isSorted(int[] arr) {
boolean ace = true; boolean dec = true;
if (arr == null || [Link] < 2)
return true;
for (int i = 0; i < [Link] - 1; i++) {
if (arr[i] > arr[i + 1]) {
ace = false;
break;
}
}
for (int i = 0; i < [Link] - 1; i++) {
if (arr[i] < arr[i + 1]) {
dec = false;
break;
}
}
return ace || dec;
}

Q.31 WAJP to move all zeroes of an array to the end.


public static int[] moveZero(int[] arr) {
for (int i = 0, j = 0; i < [Link]; i++) {
if (arr[i] != 0) {
if (i != j) {
arr[j] = arr[i];
arr[i] = 0;
}
j++;
}
}
return arr;
}

Q.32 WAJP the shift all 0’s to left and all 1’s to the
right(Without Sorting).
i/p: [0, 1, 1, 0, 0, 1, 0, 0]
o/p: [0, 0, 0, 0, 0, 1, 1, 1]

public static int[] moveZeroesLeftOnesRight(int[] nums) {


int[] ans = new int[[Link]];
int countZero = 0;
for (int num : nums) {
if (num == 0)
countZero++;
}
for (int i = countZero; i < [Link]; i++) {
ans[i] = 1;
}
return ans;
}
Q.33 For the given array of 0’s, 1’s and 2’s
Sort the elements(Without Sorting).
i/p: [0, 2, 0, 1, 2, 1, 0, 2]
o/p: [0, 0, 0, 1, 1, 2, 2, 2]
public static int[] sortWithoutSorting(int[] nums) {
int[] ans = new int[[Link]];
int idx = 0;
for(int i : nums) if(i==0) ans[idx++] = 0;
for(int i : nums) if(i==1) ans[idx++] = 1;
for(int i : nums) if(i==2) ans[idx++] = 2;
return ans;
}

Q.34 WAJP to print true if all elements in two


arrays are same.
public static boolean sameEle(int[] a, int[] b) {
for (int i = 0; i < [Link]; i++)
if (a[i] != b[i]) return false;
return true;
}

Q.35 WAJP to print and count all prime


numbers from array.
public static void allPrimeNumbers(int[] nums) {
for(int num:nums)
if(isPrime(num))
[Link](num+" ");
}

public static boolean isPrime(int n){


if(n<2) return false;
if(n==2) return true;
if(n%2==0) return false;
for(int i=3;i*i<=n;i+=2)
if(n%i==0) return false;
return true;
}
Q.36 WAJP to get sum of all prime numbers from array.
public static void allPrimeNumbers(int[] nums) {
int primeSum = 0;
for(int num:nums)
if(isPrime(num))
primeSum += num;
[Link]("Sum of all prime numbers is:
"+primeSum);
}

Q.37 WAJP to print and count palindrome


numbers from array.
public static void allPalindromeNumbers(int[] nums) {
int totalPalindrome = 0;
for (int num : nums)
if (isPalindrome(num)) {
[Link](num + " ");
totalPalindrome++;
}
[Link]();
[Link]("Total palindrome number is:
"+totalPalindrome);
}

public static boolean isPalindrome(int n) {


int rev = 0;
int temp = n;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
return rev == n;
}

Q.38 WAJP to store first n prime numbers into array.


public static int[] nThPrimeNumbers(int n) {
if (n <= 0) return new int[0];
int[] ans = new int[n];
int idx = 0;
for (int i = 0; ; i++) {
if (isPrime(i)) ans[idx++] = i;
if (idx == n) return ans;
}
}

Q.39 WAJP to store first n palindrome numbers into array.

public static int[] nThPalindromeNumbers(int n) {


if(n<=0) return new int[0];
int[] ans = new int[n];
int idx = 0;
for (int i = 0; ; i++) {
if (isPalindrome(i)) ans[idx++] = i;
if (idx == n) return ans;
}
}
Q.40 WAJP to store n terms of Fibonacci series into an array.
public static int[] nthTermOfFibonacci(int n) {
if (n <= 0) return new int[0];
int[] fib = new int[n];
if (n >= 1) fib[0] = 0;
if (n >= 2) fib[1] = 1;
int a = 0, b = 1, c = 0;

for (int i = 2; i < n; i++) {


c = a + b;
fib[i] = c;
a = b;
b = c;
}
return fib;
}

Q.41 WAJP to print nth term of Fibonacci using


memoization technique.

Q.42 WAJP to print all prime numbers up to n


using Sieve of Eratosthenes mechanism.

Q.43 WAJP to count all prime numbers up to n.

Q.44 WAJP to print second biggest element of the array.


public static int secondBiggest(int[] a) {
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int i : a) {
if (i > largest) {
secondLargest = largest;
largest = i;
} else if (i > secondLargest && i != largest) {
secondLargest = i;
}
}
return secondLargest;
}

Q.45 WAJP to print second smallest element of the array.


public static int secondSmallest(int[] a) {
int smallest = Integer.MAX_VALUE;
int secondSmall = Integer.MAX_VALUE;
for (int i : a) {
if (i < smallest) {
secondSmall = smallest;
smallest = i;
} else if (i < secondSmall && i != smallest) {
secondSmall = i;
}
}
return secondSmall;
}

Q.46 Third maximum number.

Q.47 WAJP to develop frequency array.

Q.48 Kth largest element in an array.

Q.49 WAJP to print frequency of each element (0–100).

Q.50 WAJP to print frequency of elements (any range).


public static void frequency(int[] a) {
int n = [Link] - 1;
for (int i = 0; i <= n; i++) {
int count = 1;
for (int j = i + 1; j <= n; j++) {
if (a[i] == a[j]) {
count++;
a[j] = a[n];
n--;
j--;
}
}
[Link](a[i] + " is " + count + "
times");
}
}

Q.51 WAJP to print all unique elements.

Q.52 WAJP to remove duplicate elements.

Q.53 WAJP to print elements that appear only once.

Q.54 WAJP to print element that appears only once.

Q.55 WAJP to print duplicate elements.

Q.56 WAJP to print elements with even frequency.

Q.57 WAJP to print elements with odd frequency.


Q.58 WAJP to print element with highest frequency.

Q.59 WAJP to return element with highest frequency.

Q.60 WAJP to print smaller element with highest frequency.

Q.61 WAJP to print bigger element with highest frequency.

Q.62 WAJP to find first non-repeating element index.

Q.63 Majority Element.

Q.64 Majority Element (LeetCode).

Q.65 WAJP to check if all elements are unique.

Q.66 Single Number.

Q.67 Single Number III.

Q.68 Single Number II.


Q.69 Find all lonely numbers.

Q.70 Sum of unique elements.

Q.71 Find pivot index.

Q.72 Counting bits.

Q.73 Number of even and odd bits.

Q.74 Kth missing positive number.

Q.75 First missing positive.

Q.76 Check fascinating number.

Q.77 Bubble sort.

Q.78 Selection sort.

Q.79 Insertion sort.


Q.80 Merge two arrays.

Q.81 Zigzag merge arrays.

Q.82 Merge two sorted arrays.

Q.83 Merge sorted array.

Q.84 Merge sort.

Q.85 Merge sort (LeetCode).

Q.86 Quick sort.

Q.87 Heap sort.

Q.88 Squares of sorted array.

Q.89 Sort array in descending order.

Q.90 Height checker problem.


Q.91 Half ascending and half descending array.

Q.92 Linear search.

Q.93 Binary search.

Q.94 Search insert position.

Q.95 First and last position of element.

Q.96 Square root.

Q.97 Valid perfect square.

Q.98 Single element in sorted array.

Q.99 First missing positive.

Q.100 Bitonic array search.

Q.101 Find peak element.


Q.102 Peak index in mountain array.

Q.103 Stone game.

Q.104 Container with most water.

Q.105 Trapping rain water.

Q.106 Maximum subarray.

Q.107 Product of array except self.

Q.108 3Sum.

Q.109 Additional practice problem.

You might also like