0% found this document useful (0 votes)
13 views3 pages

TCS NQT Array Programs Java

The document contains Java programs for various array operations including finding the largest and second largest elements, checking if an array is sorted, removing duplicates, rotating elements, moving zeros to the end, performing linear search, finding a missing number, counting maximum consecutive ones, and identifying a single element using XOR. Additionally, it includes methods for finding the longest subarray with a sum equal to a given value for both positive integers and general cases. Each program is presented with its corresponding logic and implementation in Java.
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)
13 views3 pages

TCS NQT Array Programs Java

The document contains Java programs for various array operations including finding the largest and second largest elements, checking if an array is sorted, removing duplicates, rotating elements, moving zeros to the end, performing linear search, finding a missing number, counting maximum consecutive ones, and identifying a single element using XOR. Additionally, it includes methods for finding the longest subarray with a sum equal to a given value for both positive integers and general cases. Each program is presented with its corresponding logic and implementation in Java.
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

TCS NQT Array Programs (Java with Input)

1. Largest Element
int max = arr[0];
for(int i = 1; i < n; i++){
if(arr[i] > max) max = arr[i];
}
[Link](max);

2. Second Largest Element


int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for(int num : arr){
if(num > first){
second = first;
first = num;
} else if(num > second && num != first){
second = num;
}
}
[Link](second);

3. Check if Array is Sorted


boolean sorted = true;
for(int i = 1; i < n; i++){
if(arr[i] < arr[i-1]){
sorted = false;
break;
}
}
[Link](sorted);

4. Remove Duplicates
int i = 0;
for(int j = 1; j < n; j++){
if(arr[j] != arr[i]){
i++;
arr[i] = arr[j];
}
}

5. Left Rotate by One


int first = arr[0];
for(int i = 1; i < n; i++){
arr[i-1] = arr[i];
}
arr[n-1] = first;

6. Move Zeros to End


int j = 0;
for(int i = 0; i < n; i++){
if(arr[i] != 0){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}

7. Linear Search
int index = -1;
for(int i = 0; i < n; i++){
if(arr[i] == key){
index = i;
break;
}
}
[Link](index);

8. Missing Number
int sum = n*(n+1)/2;
int arrSum = 0;
for(int num : arr) arrSum += num;
[Link](sum - arrSum);

9. Maximum Consecutive Ones


int count = 0, max = 0;
for(int num : arr){
if(num == 1){
count++;
max = [Link](max, count);
} else {
count = 0;
}
}
[Link](max);
10. Single Element (XOR)
int res = 0;
for(int num : arr) res ^= num;
[Link](res);

11. Longest Subarray Sum = K (Positive)


int left=0,sum=0,maxLen=0;
for(int right=0; right<n; right++){
sum+=arr[right];
while(sum>k){
sum-=arr[left++];
}
if(sum==k){
maxLen=[Link](maxLen,right-left+1);
}
}
[Link](maxLen);

12. Longest Subarray Sum = K (General)


HashMap<Integer,Integer> map=new HashMap<>();
int sum=0,maxLen=0;
for(int i=0;i<n;i++){
sum+=arr[i];
if(sum==k) maxLen=i+1;
if([Link](sum-k)){
maxLen=[Link](maxLen,[Link](sum-k));
}
if(![Link](sum)){
[Link](sum,i);
}
}
[Link](maxLen);

You might also like