0% found this document useful (0 votes)
18 views

Array Assignment

Uploaded by

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

Array Assignment

Uploaded by

Piyush Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1.

Rotate Array to the Right After kth Position


import java.util.Arrays;

public class RotateArray {

public static void rotateRight(int[] arr, int k) {

int n = arr.length;

if (k > n) {

k = k % n; // If k is larger than the array size

int[] rotated = new int[n];

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

rotated[i] = arr[(i + k) % n];

System.out.println("Rotated Array: " + Arrays.toString(rotated));

public static void main(String[] args) {

int[] arr = {10, 20, 30, 40, 50, 60, 70};

int k = 3;

rotateRight(arr, k);

}
Q2. Minimum Scalar Product of Two Vectors
import java.util.Arrays;

import java.util.Scanner;

public class ScalarProduct {

public static int minScalarProduct(int[] vec1, int[] vec2) {

Arrays.sort(vec1);

Arrays.sort(vec2);

int product = 0;

int n = vec1.length;

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

product += vec1[i] * vec2[n - 1 - i]; // Multiply smallest of vec1 with largest of vec2

return product;

public static void main(String[] args) {

int[] vec1 = {1, 3, 5, 7};

int[] vec2 = {2, 4, 6, 8};

int result = minScalarProduct(vec1, vec2);

System.out.println("Minimum Scalar Product: " + result);

}
Q3. Display Prime Numbers in an Array
import java.util.Scanner;

public class PrimeInArray {

public static boolean isPrime(int n) {

if (n <= 1) return false;

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

if (n % i == 0) return false;

return true;

public static void displayPrimes(int[] arr) {

boolean foundPrime = false;

for (int num : arr) {

if (isPrime(num)) {

System.out.print(num + " ");

foundPrime = true;

if (!foundPrime) {

System.out.println("No Prime Number present");

public static void main(String[] args) {

int[] arr = {10, 11, 12, 13, 14, 15};

System.out.print("Prime numbers in array: ");

displayPrimes(arr);

}
Q4. Sort First Half in Ascending and Second Half in Descending Order
import java.util.Arrays;

import java.util.Collections;

public class SortArray {

public static void sortHalfArray(int[] arr) {

int n = arr.length;

int mid = n / 2;

// Sort first half in ascending order

Arrays.sort(arr, 0, mid);

// Sort second half in descending order

Integer[] secondHalf = new Integer[n - mid];

for (int i = 0; i < secondHalf.length; i++) {

secondHalf[i] = arr[mid + i];

Arrays.sort(secondHalf, Collections.reverseOrder());

// Merge sorted parts back

for (int i = 0; i < secondHalf.length; i++) {

arr[mid + i] = secondHalf[i];

System.out.println("Sorted Array: " + Arrays.toString(arr));

public static void main(String[] args) {

int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};

sortHalfArray(arr);

You might also like