Programs On Arrays Lyst2178 Lyst3570
Programs On Arrays Lyst2178 Lyst3570
https://www.hackerrank.com/challenges/angry-professor/problem
import java.util.Scanner;
import java.util.Scanner;
Solution:
import java.util.Scanner;
public class Demo {
static int walkWidth(int[] a, int k) {
int width = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] <= k) {
width = width + 1;
} else {
width = width + 2;
}
}
return width;
}
Solution:
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
public class Test {
static int[] circularArrayRotation(int[] a, int k, int[] q) {
int[] res = new int[q.length];
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
b[(i + k) % a.length] = a[i];
}
for (int i = 0; i < q.length; i++) {
res[i] = b[q[i]];
}
return res;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int q = scan.nextInt();
int a[] = new int[n];
int queries[] = new int[q];
for (int i = 0; i < a.length; i++) {
a[i] = scan.nextInt();
}
for (int i = 0; i < queries.length; i++) {
queries[i] = scan.nextInt();
}
int[] res = circularArrayRotation(a, k, queries);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
}
Merge Sorted Array:
import java.util.Scanner;
Write a program to print the number of aes numbers in the given range.
NOTE: AES number is the number which contains exactly 4 divisors then it is called
as aes number
package mock;
import java.util.Scanner;
class Demo{
if(count==4) {
return true;
}
else {
return false;
}
}
Devu has n weird friends. Its his birthday today, so they thought that this is the best occasion
for testing their friendship with him. They put up conditions before Devu that they will break
the friendship unless he gives them a grand party on their chosen day. Formally, ith friend
will break his friendship if he does not receive a grand party on dith day.
Devu despite being as rich as Gatsby, is quite frugal and can give at most one grand party
daily. Also, he wants to invite only one person in a party. So he just wonders what is the
maximum number of friendships he can save. Please help Devu in this tough task !!
Input
● The first line of the input contains an integer T denoting the number of test cases.
● Second line will contain n space separated integers where ith integer corresponds to
Output
Sample Input:
2
2
32
2
11
Sample Output:
2
1
import java.util.Scanner;
class Sorting{
static void selectionSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
int min_i = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] > a[min_i]) {
min_i = j;
}
}
int temp = a[i];
a[i] = a[min_i];
a[min_i] = temp;
}
}
}
class Demo{
Sorting.selectionSort(d);
int count = 0;
for (int i = 0; i < d.length; i++) {
if(d[i] != d[i+1]) {
count++;
}
}
System.out.println(count+1);
}
}
Linear search
class Searching{
public static int linearSearch(int[] a, int key) {
for (int i = 0; i < a.length; i++) {
if(a[i] == key) {
return i;
}
}
return -1;
}
}
Binary search
import java.util.Arrays;
class tr {
public static int binarySearch(int[] a, int key) {
int l = 0,h = a.length-1, mid = 0;
while(l <= h) {
mid = (l+h)/2;
if(key == a[mid]) {
return mid;
}
else if(key < a[mid]) {
h = mid-1;
}
else {
l = mid-1;
}
}
return -1;
}
}
Bubble Sort
int[] a = { 3, 2, 7, 5, 9 };
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
insertionSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}