Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays and Strings #69

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Nelliuslinda/Arrays and Strings/ReverseArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.company;

public class Main {
//Function to reverse Array
static void reverseArray(int[] arr){
int x = arr.length;
for (int i=0; i< arr.length/2; i++){
int temp = arr[i];
arr[i]=arr[x-1-i];
arr[x-1-i]=temp;
}
for (int j : arr) {
System.out.println(j);
}

}

//Driver function to reverse Array
public static void main(String[] args) {
int[] A = new int[]{10,5,6,9};
System.out.println("The new reversed array is:");
reverseArray(A);
}
}
58 changes: 58 additions & 0 deletions Nelliuslinda/Big O Notation/BetweenTwoSets-HackerRank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.company;

import java.util.Arrays;
import java.util.List;

public class Main {
//Function to find GCD
static int findGCD(int n1, int n2) {
if (n2 == 0) {
return n1;
}
return findGCD(n2, n1 % n2);
}

//Function to find LCM
static int findLCM(int n1, int n2) {
if (n1 == 0 || n2 == 0)
return 0;
else {
int gcd = findGCD(n1, n2);
return Math.abs(n1 * n2) / gcd;
}
}

public static int getTotalX(List<Integer> a, List<Integer> b) {
int result = 0;

// Call LCM function to get lcm of array a
int lcm = a.get(0);
for (Integer integer : a) {
lcm = findLCM(lcm, integer);
}

// Call GCD function to find gcd of array b
int gcd = b.get(0);
for (Integer integer : b) {
gcd = findGCD(gcd, integer);
}

// Find common multiple of LCM that can divide GCD
int cm = 0;
while (cm <= gcd) {
cm += lcm;

if (gcd % cm == 0)
result++;
}

return result;
}
public static void main(String[] args) {

List<Integer>a = Arrays.asList(2,4);
List<Integer>b = Arrays.asList(16,32,96);

System.out.println(getTotalX(a,b));
}
}