Skip to content

Commit 0d1f43b

Browse files
committed
Merge branch 'master' of github.com:Kvaibhav01/HackerEarth-Solutions into dev
2 parents e07698c + be0e36f commit 0d1f43b

File tree

4 files changed

+171
-10
lines changed

4 files changed

+171
-10
lines changed

Online Programming/Add two arrays/AddTwoArrays.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,37 @@ public class AddTwoArrays {
1010
public static void main(String[] args) {
1111

1212
Scanner s = new Scanner(System.in);
13-
13+
int N = 0;
1414
// Reading integer from input
15-
int N = s.nextInt();
15+
do {
16+
System.out.println("Enter the size of array (greater than Zero) \n");
17+
N = s.nextInt();
18+
} while (N <= 0);
1619

1720
int[] numArrayA = new int[N];
1821
int[] numArrayB = new int[N];
1922
int[] sumArray = new int[N];
2023

2124
// Read numArray1
22-
for(int i=0; i<N; i++) {
25+
System.out.println("Enter values for array 1");
26+
for (int i = 0; i < N; i++) {
2327
numArrayA[i] = s.nextInt();
2428
}
2529
// Read numArray2
26-
for(int i=0; i<N; i++) {
30+
System.out.println("Enter values for array 2");
31+
for (int i = 0; i < N; i++) {
2732
numArrayB[i] = s.nextInt();
2833
}
2934

3035
// Write your logic here:
31-
for (i = 0;i < N;i++) {
32-
sumArray[i] = numArrayA[i] + numArrayB[i];
33-
}
36+
System.out.println("Resultant Array is: ");
37+
for (int i = 0; i < N; i++) {
38+
sumArray[i] = numArrayA[i] + numArrayB[i];
39+
}
3440

35-
// Print the sumArray
36-
for(int i=0; i<N; i++) {
41+
// Print the sumArray
42+
for (int i = 0; i < N; i++) {
3743
System.out.print(sumArray[i] + " ");
3844
}
39-
}
45+
}
4046
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Problem: Print out the minimum number of operations so that every element in
2+
// array a is greater than or equal to every element in array b
3+
4+
// Solution:
5+
6+
import java.util.Scanner;
7+
8+
public class ArrayGame {
9+
10+
public static void main(String[] args) {
11+
12+
// Create Scanner object to handle user input
13+
Scanner input = new Scanner(System.in);
14+
15+
// Variable to keep track of min number of operations
16+
// Needed to satisfy game's constraints
17+
int minOperations = 0;
18+
19+
// Input sizes of both arrays
20+
int aSize = input.nextInt();
21+
int bSize = input.nextInt();
22+
23+
int[] a = new int[aSize];
24+
int[] b = new int[bSize];
25+
26+
// Fill elements in array a
27+
for (int i = 0; i < aSize; i++) {
28+
a[i] = input.nextInt();
29+
}
30+
31+
// Fill elements in array b
32+
for (int i = 0; i < bSize; i++) {
33+
b[i] = input.nextInt();
34+
}
35+
36+
for (int j = 0; j < b.length; j++) {
37+
38+
for (int i = 0; i < a.length; i++) {
39+
40+
// While the element in a is smaller
41+
// than the element in b
42+
while (a[i] < b[j]) {
43+
44+
// Decrement the number by 1 (one operation)
45+
a[i] += 1;
46+
minOperations++;
47+
48+
// If element in a is still smaller,
49+
// increment the element in a (one operation)
50+
if (a[i] < b[j]) {
51+
b[j] -= 1;
52+
minOperations++;
53+
}
54+
55+
}
56+
}
57+
}
58+
59+
// Print out the minimum operations to
60+
// make every element in a >= every element in b
61+
System.out.println(minOperations);
62+
}
63+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Problem: Write a program to print the sum of all the elements of an array of size N where N can be any integer between 1 and 100.
2+
//1<=N<=100
3+
4+
//Solution:
5+
6+
import java.util.Scanner;
7+
8+
public class SumOfN {
9+
10+
public static void main(String[] args) {
11+
int N ;
12+
int[] numArray;
13+
int sum = 0;
14+
int i= 0;
15+
16+
Scanner s = new Scanner(System.in);
17+
18+
System.out.print("How many number the array have? ");
19+
N = s.nextInt();
20+
21+
if (1>=N) {
22+
System.out.println("The array must have at leats one element.");
23+
System.exit(0);
24+
}
25+
26+
if (N>=100) {
27+
System.out.println("The array is too large.");
28+
System.exit(0);
29+
}
30+
31+
32+
// Define an array of integers of size N.
33+
numArray = new int[N];
34+
35+
// Get the input
36+
while (i < N) {
37+
System.out.print("Input a number: ");
38+
numArray[i] = s.nextInt();
39+
i++;
40+
}
41+
42+
i = 0;
43+
44+
// Write the logic to add these numbers here:
45+
while (i < numArray.length) {
46+
sum += numArray[i];
47+
i++;
48+
}
49+
50+
51+
// Print the sum
52+
System.out.print(sum);
53+
s.close();
54+
}
55+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Manish has got the task to frame a speech for his professor at the university at the Annual sports meet.But the problem is that the professor has speech dyslexia and he can't speak the words clearly which have vowels in them. So Manish has to avoid such words and has to minimise their usage in the speech letter. Your task is to help Manish mark the vowels in the words so that he can minimise their use. You are given a string S consisting of lower case letters only. You need to count the number of vowels in the string S.
3+
4+
INPUT The first line will contain an integer T denoting the number of test cases. The following T lines will contain a string S in lower case letters only.
5+
6+
OUTPUT Print the number the vowels in the string S.
7+
8+
CONSTRAINTS 1<=T<=100
9+
*/
10+
11+
import java.util.Scanner;
12+
13+
class VowelPhobia {
14+
public static void main(String args[] ) throws Exception {
15+
Scanner in = new Scanner(System.in);
16+
String word = in.nextLine();
17+
int count = 0;
18+
if (word != null) {
19+
word = word.trim().toLowerCase();
20+
}
21+
if (word == null || word.length() < 1) {
22+
System.out.println(count);
23+
}
24+
for (int index = 0; index < word.length(); index++) {
25+
// That Fred he's a
26+
char fred = word.charAt(index);
27+
if ((fred == 'a') || (fred == 'e')
28+
|| (fred == 'i') || (fred == 'o')
29+
|| (fred == 'u')) {
30+
++count;
31+
}
32+
}
33+
34+
System.out.println(count);
35+
36+
}
37+
}

0 commit comments

Comments
 (0)