0% found this document useful (0 votes)
7 views24 pages

1D Array

The document contains Java code examples for various operations on one-dimensional arrays, including finding sums, largest and smallest elements, counting even and odd numbers, and more. It also includes advanced array problems such as finding leaders, majority elements, maximum differences, and digit frequencies. Each section provides a clear problem statement, expected outcomes, and the corresponding Java implementation.

Uploaded by

kamaltej60
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)
7 views24 pages

1D Array

The document contains Java code examples for various operations on one-dimensional arrays, including finding sums, largest and smallest elements, counting even and odd numbers, and more. It also includes advanced array problems such as finding leaders, majority elements, maximum differences, and digit frequencies. Each section provides a clear problem statement, expected outcomes, and the corresponding Java implementation.

Uploaded by

kamaltej60
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

1D ARRAY

package [Link];

public class OneDArrayRevision {

public static void main(String[] args) {

// ---------------------------------------------------------------
// Q1) Basic: Find the sum of all elements in a 1D array
// ---------------------------------------------------------------
/*
Question:
Given an array: [5, 10, 15, 20, 25]
Find the TOTAL SUM.

Expected Answer: 75
*/
int[] a1 = {5,10,15,20,25};
int sum1 = 0;

for(int i=0; i<[Link]; i++){


sum1 += a1[i];
}

[Link]("Q1 Sum = " + sum1);

// ---------------------------------------------------------------
// Q2) Basic: Find the largest element in a 1D array
// ---------------------------------------------------------------
/*
Array: [22, 9, 51, 17, 6]
Largest element = 51
*/
int[] a2 = {22, 9, 51, 17, 6};
int max = a2[0];

for(int i=1; i<[Link]; i++){


if(a2[i] > max){
max = a2[i];
}
}

[Link]("Q2 Largest = " + max);


// ---------------------------------------------------------------
// Q3) Basic: Find the smallest element
// ---------------------------------------------------------------
/*
Array: [34, 7, 19, 2, 11]
Smallest = 2
*/
int[] a3 = {34,7,19,2,11};
int min = a3[0];

for(int i=1; i<[Link]; i++){


if(a3[i] < min){
min = a3[i];
}
}

[Link]("Q3 Smallest = " + min);

// ---------------------------------------------------------------
// Q4) Count even and odd numbers
// ---------------------------------------------------------------
/*
Array: [3, 4, 5, 6, 7, 8]
Evens = 3
Odds = 3
*/
int[] a4 = {3,4,5,6,7,8};
int ev=0, od=0;

for(int x : a4){
if(x % 2 == 0) ev++;
else od++;
}

[Link]("Q4 Evens = " + ev + ", Odds = " + od);

// ---------------------------------------------------------------
// Q5) Find second largest element
// ---------------------------------------------------------------
/*
Array: [10, 50, 30, 40, 20]
2nd Largest = 40
*/
int[] a5 = {10,50,30,40,20};
int first = -1, second = -1;

for(int x : a5){
if(x > first){
second = first;
first = x;
}
else if(x > second && x != first){
second = x;
}
}

[Link]("Q5 Second Largest = " + second);

// ---------------------------------------------------------------
// Q6) Count duplicates in the array
// ---------------------------------------------------------------
/*
Array: [5, 1, 5, 3, 1, 4]
Duplicates:
5 → appears 2 times
1 → appears 2 times
*/
int[] a6 = {5,1,5,3,1,4};

[Link]("Q6 Duplicate Elements:");


boolean[] visited = new boolean[[Link]];

for(int i=0; i<[Link]; i++){


if(visited[i]) continue;
int count = 1;

for(int j=i+1; j<[Link]; j++){


if(a6[i] == a6[j]){
count++;
visited[j] = true;
}
}

if(count > 1){


[Link](a6[i] + " occurs " + count + " times");
}
}
// ---------------------------------------------------------------
// Q7) Reverse the array
// ---------------------------------------------------------------
/*
Array: [1, 2, 3, 4, 5]
Reversed = [5,4,3,2,1]
*/
int[] a7 = {1,2,3,4,5};

[Link]("Q7 Reversed = ");


for(int i=[Link]-1; i>=0; i--){
[Link](a7[i] + " ");
}
[Link]();

// ---------------------------------------------------------------
// Q8) Move all zeros to the end (without changing order)
// ---------------------------------------------------------------
/*
Array: [0,1,0,3,12]
Output: [1,3,12,0,0]
*/
int[] a8 = {0,1,0,3,12};
int index = 0;

for(int x : a8){
if(x != 0){
a8[index++] = x;
}
}

while(index < [Link]){


a8[index++] = 0;
}

[Link]("Q8 Move Zeros = ");


for(int x : a8){
[Link](x + " ");
}
[Link]();

// ---------------------------------------------------------------
// Q9) Find element frequency (how many times it appears)
// ---------------------------------------------------------------
/*
Array: [7, 8, 7, 2, 4, 7]
Element: 7
Output: 3 times
*/
int[] a9 = {7,8,7,2,4,7};
int search = 7;
int ct = 0;

for(int x : a9){
if(x == search) ct++;
}

[Link]("Q9 Frequency of " + search + " = " + ct);

// ---------------------------------------------------------------
// Q10) Check array is sorted or not
// ---------------------------------------------------------------
/*
Array: [2,4,6,8,10]
Sorted? YES

Array: [1,3,2]
Sorted? NO
*/
int[] a10 = {2,4,6,8,10};
boolean sorted = true;

for(int i=1; i<[Link]; i++){


if(a10[i] < a10[i-1]){
sorted = false;
break;
}
}

[Link]("Q10 Sorted? " + sorted);

// ---------------------------------------------------------------
// Q11) Find pairs whose sum = given number K
// ---------------------------------------------------------------
/*
Array: [1, 4, 5, 6, 2]
K=7
Pairs:
(1,6), (4,3), (5,2)
*/
int[] a11 = {1,4,5,6,2};
int K = 7;

[Link]("Q11 Pairs with sum 7:");


for(int i=0; i<[Link]; i++){
for(int j=i+1; j<[Link]; j++){
if(a11[i] + a11[j] == K){
[Link]("(" + a11[i] + "," + a11[j] + ")");
}
}
}

// ---------------------------------------------------------------
// Q12) Rotate array to the right by 1
// ---------------------------------------------------------------
/*
Array: [10,20,30,40]
After right rotate: [40,10,20,30]
*/
int[] a12 = {10,20,30,40};
int last = a12[[Link] - 1];

for(int i=[Link]-1; i>0; i--){


a12[i] = a12[i-1];
}
a12[0] = last;

[Link]("Q12 Right-Rotate = ");


for(int x : a12){
[Link](x + " ");
}
[Link]();
}

/*
===========================================
ADVANCED 1D ARRAY QUESTIONS
===========================================

11) LEADERS IN AN ARRAY


A leader is an element which is greater than all elements to its right.
Example:
Input : 16 17 4 3 5 2
Output: 17, 5, 2

12) MAJORITY ELEMENT (> n/2 times)


Example:
3 3 4 3 5 3 → Majority = 3

13) MAXIMUM DIFFERENCE (arr[j] - arr[i], j > i)


Example:
2 3 10 6 4 8 1 → Answer = 10-2 = 8

14) COUNT PEAK ELEMENTS


Peak = element greater than left & right.
Example:
1 3 2 5 4 7 → Peaks: 3, 5, 7

15) STOCK BUY-SELL MAX PROFIT


Example:
7 1 5 3 6 4 → Max Profit = 5

16) TRIPLETS WHOSE SUM = TARGET


Example:
Array: 1 4 6 2 3 7, Target = 10
Triplets: (1,2,7), (4,3,3 if exists)

17) LONGEST CONSECUTIVE INCREASING SEQUENCE


Example:
5 2 3 4 10 11 12 13 1
Longest = 10 11 12 13 (length 4)

18) CHECK IF ARRAY CAN BE SORTED BY ONE SWAP


Example:
1 5 3 4 2 6 → YES (swap 5 & 2)

19) ARRANGE POSITIVE NUMBERS FIRST, THEN NEGATIVE


Example:
-1 4 -3 5 -2 7 → 4 5 7 -1 -3 -2

20) FREQUENCY OF EACH DIGIT IN A NUMBER


Example:
Number = 5553121
Digit freq: 5→3, 1→1, 2→1, 3→1

===========================================
*/
import [Link].*;

public class AdvancedArrayProblems {


public static void main(String[] args) {

int[] arr = {16, 17, 4, 3, 5, 2};


int n = [Link];

// --------------------------------------------------
// 11) LEADERS IN ARRAY
// --------------------------------------------------
[Link]("11) Leaders in Array:");
int maxRight = Integer.MIN_VALUE;
for (int i = n - 1; i >= 0; i--) {
if (arr[i] > maxRight) {
[Link](arr[i] + " ");
maxRight = arr[i];
}
}
[Link]("\n");

// --------------------------------------------------
// 12) MAJORITY ELEMENT (>n/2 times)
// --------------------------------------------------
[Link]("12) Majority Element:");
int[] arr2 = {3, 3, 4, 3, 5, 3};
int cand = -1, count = 0;

for (int x : arr2) {


if (count == 0) {
cand = x;
count = 1;
} else if (x == cand) {
count++;
} else {
count--;
}
}
[Link]("Majority = " + cand + "\n");

// --------------------------------------------------
// 13) MAXIMUM DIFFERENCE arr[j] - arr[i]
// --------------------------------------------------
[Link]("13) Max Difference (j>i):");
int[] arr3 = {2, 3, 10, 6, 4, 8, 1};

int min = arr3[0];


int maxDiff = arr3[1] - arr3[0];
for (int i = 1; i < [Link]; i++) {
maxDiff = [Link](maxDiff, arr3[i] - min);
min = [Link](min, arr3[i]);
}
[Link]("Max diff = " + maxDiff + "\n");

// --------------------------------------------------
// 14) PEAK ELEMENTS
// --------------------------------------------------
[Link]("14) Peak Elements:");
int[] arr4 = {1, 3, 2, 5, 4, 7};
for (int i = 1; i < [Link] - 1; i++) {
if (arr4[i] > arr4[i - 1] && arr4[i] > arr4[i + 1]) {
[Link](arr4[i]);
}
}
[Link]();

// --------------------------------------------------
// 15) STOCK BUY-SELL MAX PROFIT
// --------------------------------------------------
[Link]("15) Max Stock Profit:");
int[] price = {7, 1, 5, 3, 6, 4};
int minPrice = price[0], profit = 0;

for (int p : price) {


minPrice = [Link](minPrice, p);
profit = [Link](profit, p - minPrice);
}
[Link]("Profit = " + profit + "\n");

// --------------------------------------------------
// 16) TRIPLETS SUM = TARGET
// --------------------------------------------------
[Link]("16) Triplets with sum = 10:");
int[] arr5 = {1, 4, 6, 2, 3, 7};
int target = 10;

for (int i = 0; i < [Link]; i++) {


for (int j = i + 1; j < [Link]; j++) {
for (int k = j + 1; k < [Link]; k++) {
if (arr5[i] + arr5[j] + arr5[k] == target) {
[Link](arr5[i] + " " + arr5[j] + " " + arr5[k]);
}
}
}
}
[Link]();

// --------------------------------------------------
// 17) LONGEST CONSECUTIVE INCREASING SEQUENCE
// --------------------------------------------------
[Link]("17) Longest Consecutive Increasing Sequence Length:");
int[] arr6 = {5, 2, 3, 4, 10, 11, 12, 13, 1};

int maxLen = 1, currLen = 1;


for (int i = 1; i < [Link]; i++) {
if (arr6[i] == arr6[i - 1] + 1) {
currLen++;
} else {
currLen = 1;
}
maxLen = [Link](maxLen, currLen);
}
[Link]("Length = " + maxLen + "\n");

// --------------------------------------------------
// 18) CAN ARRAY BE SORTED BY ONE SWAP?
// --------------------------------------------------
[Link]("18) Can be sorted by one swap?");
int[] arr7 = {1, 5, 3, 4, 2, 6};
int[] sorted = [Link]();
[Link](sorted);

int diff = 0;
for (int i = 0; i < [Link]; i++) {
if (arr7[i] != sorted[i]) diff++;
}
[Link](diff == 2 ? "YES" : "NO");
[Link]();

// --------------------------------------------------
// 19) ARRANGE POSITIVE FIRST THEN NEGATIVE
// --------------------------------------------------
[Link]("19) Positive First, Negative Last:");
int[] arr8 = {-1, 4, -3, 5, -2, 7};
int[] result = new int[[Link]];
int index = 0;

for (int x : arr8) if (x >= 0) result[index++] = x;


for (int x : arr8) if (x < 0) result[index++] = x;

[Link]([Link](result) + "\n");

// --------------------------------------------------
// 20) DIGIT FREQUENCY
// --------------------------------------------------
[Link]("20) Digit Frequency:");
int num = 5553121;
int[] freq = new int[10];

while (num > 0) {


freq[num % 10]++;
num /= 10;
}

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


[Link](i + " => " + freq[i]);
}
}
}

package [Link];

public class MediumLevel1DArray {

public static void main(String[] args) {

// ================================================================
// Q1) Count how many elements appear more than once
// ================================================================
/*
Array: [4, 2, 7, 4, 9, 2, 4]

Frequency:
4 → 3 times
2 → 2 times
7 → 1 time
9 → 1 time

Answer: 2 elements appear more than once (4 and 2)


*/

int[] q1 = {4,2,7,4,9,2,4};
boolean[] visited = new boolean[[Link]];
int duplicateElementsCount = 0;

for(int i=0; i<[Link]; i++) {


if(visited[i]) continue;

int count = 1;
for(int j=i+1; j<[Link]; j++) {
if(q1[i] == q1[j]) {
count++;
visited[j] = true;
}
}

if(count > 1) duplicateElementsCount++;


}
[Link]("Q1 Duplicate elements count = " + duplicateElementsCount);

// ================================================================
// Q2) Count how many elements are strictly greater than both neighbors
// ================================================================
/*
Local peak means:
arr[i] > arr[i-1] AND arr[i] > arr[i+1]

Example:
[3, 8, 6, 7, 2, 9, 4]

Local peaks:
8 (because 8>3 & 8>6)
7 (because 7>6 & 7>2)
9 (because 9>2 & 9>4)

Answer: 3 peaks
*/

int[] q2 = {3,8,6,7,2,9,4};
int peaks = 0;

for(int i=1; i<[Link]-1; i++) {


if(q2[i] > q2[i-1] && q2[i] > q2[i+1]) {
peaks++;
}
}
[Link]("Q2 Number of peaks = " + peaks);

// ================================================================
// Q3) Find the longest increasing continuous subsequence
// ================================================================
/*
Array: [5, 6, 7, 2, 3, 4, 8]

Increasing sequences:
[5, 6, 7] → length 3
[2, 3, 4, 8] → length 4 ⬅ longest

Answer: 4
*/

int[] q3 = {5,6,7,2,3,4,8};
int longest = 1, current = 1;

for(int i=1; i<[Link]; i++) {


if(q3[i] > q3[i-1]) {
current++;
} else {
current = 1;
}
if(current > longest) longest = current;
}

[Link]("Q3 Longest Increasing Sequence Length = " + longest);

// ================================================================
// Q4) Count pairs with difference K
// ================================================================
/*
Array: [1, 5, 3, 4, 2]
K=2

Pairs with diff 2:


(1,3)
(5,3)
(3,1)
(4,2)
(2,4)

Answer: 5 pairs
*/

int[] q4 = {1,5,3,4,2};
int K = 2;
int pairCount = 0;

for(int i=0; i<[Link]; i++) {


for(int j=0; j<[Link]; j++) {
if(i != j && [Link](q4[i] - q4[j]) == K) {
pairCount++;
}
}
}
[Link]("Q4 Pairs with difference " + K + " = " + pairCount);

// ================================================================
// Q5) Check if array is a palindrome
// ================================================================
/*
Array: [4, 7, 9, 7, 4]
Same forward and backward → Palindrome → true

Array: [1, 2, 3] → false


*/

int[] q5 = {4,7,9,7,4};
boolean palindrome = true;

for(int i=0; i<[Link]/2; i++) {


if(q5[i] != q5[[Link]-1-i]) {
palindrome = false;
break;
}
}
[Link]("Q5 Palindrome? " + palindrome);

// ================================================================
// Q6) Find the first repeating number
// ================================================================
/*
Array: [6, 3, 8, 3, 5, 6]
First repeat = 3 (because index 1 -> index 3)
*/

int[] q6 = {6,3,8,3,5,6};
int firstRepeat = -1;

for(int i=0; i<[Link]; i++) {


for(int j=i+1; j<[Link]; j++) {
if(q6[i] == q6[j]) {
firstRepeat = q6[i];
break;
}
}
if(firstRepeat != -1) break;
}

[Link]("Q6 First repeating number = " + firstRepeat);

// ================================================================
// Q7) Find missing number in 1 to N
// ================================================================
/*
Array: [1, 2, 4, 5]

N=5
Missing = 3
*/

int[] q7 = {1,2,4,5};
int N = 5;

int expectedSum = N * (N+1) / 2;


int actualSum = 0;

for(int x : q7) actualSum += x;

[Link]("Q7 Missing number = " + (expectedSum - actualSum));

// ================================================================
// Q8) Find how many elements are divisible by both 3 and 5
// ================================================================
/*
Array: [15, 30, 10, 20, 45, 9]

Divisible by both 3 & 5 = 15, 30, 45 → 3 elements


*/

int[] q8 = {15,30,10,20,45,9};
int countDiv = 0;

for(int x : q8) {
if(x % 3 == 0 && x % 5 == 0) {
countDiv++;
}
}

[Link]("Q8 Divisible by 3 & 5 = " + countDiv);


// ================================================================
// Q9) Count how many elements are greater than average
// ================================================================
/*
Array: [10, 20, 30, 40, 50]

avg = 30
greater = 40,50 → 2 elements
*/

int[] q9 = {10,20,30,40,50};
int sum = 0;

for(int x : q9) sum += x;


double avg = sum / (double) [Link];

int aboveAvg = 0;
for(int x : q9) {
if(x > avg) aboveAvg++;
}

[Link]("Q9 Elements > average = " + aboveAvg);

// ================================================================
// Q10) Find the longest consecutive NUMBER sequence
// ================================================================
/*
Array: [2, 3, 4, 10, 11, 12, 13, 5]

Consecutive sequences:
[2,3,4] → length 3
[10,11,12,13] → length 4 (longest)

Answer: 4
*/

int[] q10 = {2,3,4,10,11,12,13,5};


int longSeq = 1, currSeq = 1;

for(int i=1; i<[Link]; i++) {


if(q10[i] == q10[i-1] + 1) { // consecutive
currSeq++;
} else {
currSeq = 1;
}

if(currSeq > longSeq) longSeq = currSeq;


}

[Link]("Q10 Longest consecutive number sequence = " + longSeq);

}
}

package [Link];

public class HardLevel1DArray {

public static void main(String[] args) {

// ========================================================================
// Q1) Find the *first non-repeating* element in the array
// ========================================================================
/*
Array: [4, 5, 9, 4, 9, 7, 5, 8]

Frequencies:
4 → 2 times
5 → 2 times
9 → 2 times
7 → 1 time
8 → 1 time

The FIRST element whose frequency is 1 → 7

Answer: 7
*/

int[] q1 = {4,5,9,4,9,7,5,8};
int firstNonRepeat = -1;

for (int i = 0; i < [Link]; i++) {


int count = 0;
for (int j = 0; j < [Link]; j++) {
if (q1[i] == q1[j]) count++;
}
if (count == 1) {
firstNonRepeat = q1[i];
break;
}
}
[Link]("Q1 First non-repeating element = " + firstNonRepeat);

// ========================================================================
// Q2) Find the LONGEST subarray whose sum = K
// ========================================================================
/*
Array: [2, 1, 3, -1, 4, 2, -3, 1]
K=5

Subarrays with sum 5:


[2,1,3,-1] = 5 (length 4)
[3,-1,4,-1] = 5
[4,1] = 5
[2, -3, 1, 5] = 5

Longest length = 4
*/

int[] q2 = {2,1,3,-1,4,2,-3,1};
int K = 5;
int maxLen = 0;

for (int i = 0; i < [Link]; i++) {


int sum = 0;
for (int j = i; j < [Link]; j++) {
sum += q2[j];
if (sum == K) {
maxLen = [Link](maxLen, j - i + 1);
}
}
}
[Link]("Q2 Longest subarray sum = K has length = " + maxLen);

// ========================================================================
// Q3) Find the maximum PRODUCT of any 3 numbers
// ========================================================================
/*
Array: [-10, -3, 5, 6, -2]

Possibilities:
(−10 * −3 * 6) = 180
(5 * 6 * -2) = -60
(−10 * 5 * 6) = -300
Maximum product = 180
*/

int[] q3 = {-10, -3, 5, 6, -2};

// sort approach not used – implemented manually (hard logic)


int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;

for (int x : q3) {

// Top 3 max numbers


if (x > max1) { max3 = max2; max2 = max1; max1 = x; }
else if (x > max2) { max3 = max2; max2 = x; }
else if (x > max3) { max3 = x; }

// Two smallest numbers


if (x < min1) { min2 = min1; min1 = x; }
else if (x < min2) { min2 = x; }
}

int product1 = max1 * max2 * max3;


int product2 = max1 * min1 * min2;

int maxProduct = [Link](product1, product2);

[Link]("Q3 Max product of any 3 numbers = " + maxProduct);

// ========================================================================
// Q4) Find the majority element (appears more than n/2 times)
// ========================================================================
/*
Array: [2, 2, 1, 2, 3, 2, 2]

n = 7 → n/2 = 3
2 appears 5 times → Majority element = 2
*/

int[] q4 = {2,2,1,2,3,2,2};
int majority = -1;

for (int i = 0; i < [Link]; i++) {


int count = 0;
for (int j = 0; j < [Link]; j++) {
if (q4[i] == q4[j]) count++;
}
if (count > [Link] / 2) {
majority = q4[i];
break;
}
}

[Link]("Q4 Majority element = " + majority);

// ========================================================================
// Q5) Find the *longest* subarray with ALTERNATING even–odd pattern
// ========================================================================
/*
Array: [5, 2, 7, 8, 9, 4, 3, 6]

Alternating:
5 (odd) → 2 (even) → 7 (odd) → 8 (even) → 9 (odd) → 4 (even) → 3 (odd) → 6 (even)
Entire array alternates!

Length = 8
*/

int[] q5 = {5,2,7,8,9,4,3,6};
int best = 1, curr = 1;

for (int i = 1; i < [Link]; i++) {


boolean cond = (q5[i] % 2 == 0 && q5[i - 1] % 2 != 0) ||
(q5[i] % 2 != 0 && q5[i - 1] % 2 == 0);

if (cond) curr++;
else curr = 1;

best = [Link](best, curr);


}

[Link]("Q5 Longest alternating even-odd length = " + best);

// ========================================================================
// Q6) Find the second smallest and second largest elements
// ========================================================================
/*
Array: [12, 5, 18, 7, 1, 20]

Smallest = 1
Second smallest = 5
Largest = 20
Second largest = 18
*/

int[] q6 = {12,5,18,7,1,20};

int smallest = Integer.MAX_VALUE;


int secondSmallest = Integer.MAX_VALUE;

int largest = Integer.MIN_VALUE;


int secondLargest = Integer.MIN_VALUE;

for (int x : q6) {

if (x < smallest) {
secondSmallest = smallest;
smallest = x;
} else if (x < secondSmallest) {
secondSmallest = x;
}

if (x > largest) {
secondLargest = largest;
largest = x;
} else if (x > secondLargest) {
secondLargest = x;
}
}

[Link]("Q6 Second smallest = " + secondSmallest);


[Link]("Q6 Second largest = " + secondLargest);

// ========================================================================
// Q7) Find number of subarrays with sum divisible by K
// ========================================================================
/*
Array: [2, 3, 4, 6]
K=3

Subarrays divisible by 3:
[3]
[2,3,4] = 9
[3,4,6] = 13 → NOT divisible
[6] = 6
[2,3,4,6] = 15
Total = 3
*/

int[] q7 = {2,3,4,6};
int kk = 3;
int subCount = 0;

for (int i = 0; i < [Link]; i++) {


int sum = 0;
for (int j = i; j < [Link]; j++) {
sum += q7[j];
if (sum % kk == 0) subCount++;
}
}

[Link]("Q7 Number of subarrays sum divisible by " + kk + " = " + subCount);

// ========================================================================
// Q8) Rearrange array in wave form: arr[i] >= arr[i+1] <= arr[i+2]
// ========================================================================
/*
Array: [10, 5, 6, 3, 2, 20, 100, 80]

Result possible:
[5,10,3,6,2,20,80,100]
*/

int[] q8 = {10,5,6,3,2,20,100,80};

for (int i = 0; i < [Link]-1; i += 2) {


if (q8[i] < q8[i+1]) {
int temp = q8[i];
q8[i] = q8[i+1];
q8[i+1] = temp;
}
}

[Link]("Q8 Wave array = ");


for (int x : q8) [Link](x + " ");
[Link]();

// ========================================================================
// Q9) Find element occurring odd number of times
// ========================================================================
/*
Array: [4, 4, 5, 5, 5, 3, 3]

Odd count:
5 → occurs 3 times → answer = 5
*/

int[] q9 = {4,4,5,5,5,3,3};
int oddElement = -1;

for (int i = 0; i < [Link]; i++) {


int count = 0;
for (int j = 0; j < [Link]; j++) {
if (q9[i] == q9[j]) count++;
}
if (count % 2 == 1) {
oddElement = q9[i];
break;
}
}

[Link]("Q9 Element occurring odd times = " + oddElement);

// ========================================================================
// Q10) Rotate array “K” times to the RIGHT
// ========================================================================
/*
Array: [1, 2, 3, 4, 5]
K=2

After 1 rotation → [5, 1, 2, 3, 4]


After 2 rotations → [4, 5, 1, 2, 3]

Result = [4, 5, 1, 2, 3]
*/

int[] q10 = {1,2,3,4,5};


int rotate = 2;

rotate %= [Link];

for (int r = 0; r < rotate; r++) {


int last = q10[[Link] - 1];

for (int i = [Link] - 1; i > 0; i--) {


q10[i] = q10[i-1];
}

q10[0] = last;
}

[Link]("Q10 Rotated array = ");


for (int x : q10) [Link](x + " ");
[Link]();
}
}

You might also like