Compare commits

...

20 Commits

Author SHA1 Message Date
Sami 8292c346aa fix: 2 more challenges 2024-11-24 19:24:23 -05:00
Sami fb5f689af3 fix: ReversedString+Palindrom 2024-11-16 14:08:46 -05:00
Sami b79fe73ffb fix: ReversedString 2024-11-16 13:51:40 -05:00
Sami 47bab72ff3 fix: OneOne 2024-11-13 10:14:43 -05:00
Sami c93a26c059 fix: OneOne 2024-11-13 09:59:50 -05:00
Sami f8748fcbdf fix: numbersofAs 2024-11-12 12:36:29 -05:00
Sami ba2db6a59e fix: JumpingClounds 2024-11-12 10:25:03 -05:00
Sami 29251ff399 fix: counting Valleys 2024-11-09 06:08:14 -05:00
Sami 9c622c1aa4 fix: added a new challenge 2024-11-08 06:52:31 -05:00
Sami 525fa089b9 fix: added a new challenge 2024-11-07 10:14:35 -05:00
Sami 8906e857ea fix: added a new challenge 2024-11-07 10:14:19 -05:00
Sami 880f1d5854 reversingArrays 2024-11-05 06:25:41 -05:00
Sami 0e13691185 DecreasingScoresRanks 2024-11-04 09:55:00 -05:00
Zakaria a3dd8f3ddc nothingChanged 2024-11-02 19:29:05 +00:00
Sami 58f386f775 printTokensandCurlybraces 2024-11-02 15:03:46 -04:00
Sami e5e4195765 printTokensandCurlybraces 2024-11-02 15:03:37 -04:00
Sami c02fa16282 MakeAnagramMethod2 2024-10-25 23:12:19 -04:00
Sami 322aa305a3 MakeAnagram 2024-10-25 19:05:45 -04:00
Sami 019f62504b MODULO 10power9+7 2024-10-23 18:42:15 -04:00
Sami b9b9b18218 MODULO 10power9+7 2024-10-23 18:42:15 -04:00
32 changed files with 978 additions and 4 deletions
Binary file not shown.
+34 -4
View File
@@ -1,12 +1,42 @@
import java.util.Scanner;
public class AnagramPeriod {
public static void main(String[] args) {
getAnagramPeriod("ababbaab");
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
sc.close();
int totalDel = makeAnagram(a, b);
System.out.println(totalDel);
}
private static void getAnagramPeriod(String input_str) {
int n = input_str.length();
System.out.println(n);
private static int makeAnagram(String strOne, String strTwo) {
// bacdc
// dcbac
// bacdc
// dcbad
int[] a_frequency = new int[26]; // 26 numbers of letters in the alphabet.
int[] b_frequency = new int[26]; // 26 numbers of letters in the alphabet.
// populate the frequence array for String strOne
for (char c : strOne.toCharArray()) {
a_frequency[c - 'a']++;
}
// populate the frequence array for String strTwo
for (char c : strTwo.toCharArray()) {
b_frequency[c - 'a']++;
}
// calculate the total number of deletions needed
int deletion = 0;
for (int i = 0; i < 26; i++) {
deletion += Math.abs(a_frequency[i] - b_frequency[i]);
}
return deletion;
}
}
+67
View File
@@ -0,0 +1,67 @@
import java.util.ArrayList;
import java.util.List;
/**
* BracketExpension
*/
public class BracketExpension {
// You are given a string expression which consists of several comma separated
// tokens
// enclosed within opening ('{') and closing ('}') curly braces.
// The string expression might or might not have a prefix before opening curly
// brace('{') and
// a suffix after closing curly brace ('}').
// You have to return a list of strings as output for each comma separated item
// as shown below in the examples.
//
// Example 1:
// Input = "/2022/{jan,feb,march}/report"
// Output = "/2022/jan/report"
// "/2022/feb/report"
// "/2022/march/report"
//
// Example 2:
// Input = "over{crowd,eager,bold,fond}ness"
// Output = "overcrowdness"
// "overeagerness"
// "overboldness"
// "overfondness"
//
// Example 3:
// Input = "read.txt{,.bak}"
// Output = "read.txt"
// "read.txt.bak"
public static void main(String[] args) {
String tokens = "over{crowd,eager,bold,fond}ness";
String tokeneez = "/2022/{jan,feb,march}/report";
List<String> options = TokensListed(tokeneez);
for (int i = 0; i < options.size(); i++) {
System.out.println(options.get(i));
}
}
public static List<String> TokensListed(String str) {
List<String> ans = new ArrayList<>();
// let's find the starting and the ending of the indecies of the curly braces;
int startIndex = str.indexOf("{");
int endIndex = str.indexOf("}");
if (startIndex == -1 || endIndex == -1 || startIndex > endIndex) {
ans.add(str);
return ans;
}
// Extract the prefix, suffix and the options;
String prefix = str.substring(0, startIndex);
String suffix = str.substring(endIndex + 1);
String options = str.substring(startIndex + 1, endIndex);
// Split the options by comma
String[] tokens = options.split(",");
for (String token : tokens) {
ans.add(prefix + token + suffix);
}
return ans;
}
}
+112
View File
@@ -0,0 +1,112 @@
import java.util.Scanner;
/**
* CountingValleys
* An avid hiker keeps meticulous records of their hikes. During the last hike
* that took exactly steps, for every step it was noted if it was an uphill, ,
* or a downhill, step. Hikes always start and end at sea level, and each step
* up or down represents a
*
* unit change in altitude. We define the following terms:
*
* A mountain is a sequence of consecutive steps above sea level, starting with
* a step up from sea level and ending with a step down to sea level.
* A valley is a sequence of consecutive steps below sea level, starting with a
* step down from sea level and ending with a step up to sea level.
*
* Given the sequence of up and down steps during a hike, find and print the
* number of valleys walked through.
*
* Example
*
* The hiker first enters a valley units deep. Then they climb out and up onto a
* mountain
*
* units high. Finally, the hiker returns to sea level and ends the hike.
*
* Function Description
*
* Complete the countingValleys function in the editor below.
*
* countingValleys has the following parameter(s):
*
* int steps: the number of steps on the hike
* string path: a string describing the path
*
* Returns
*
* int: the number of valleys traversed
*
* Input Format
*
* The first line contains an integer
* , the number of steps in the hike.
* The second line contains a single string , of
*
* characters that describe the path.
*
* Constraints
*
* Sample Input
*
* 8
* UDDDUDUU
*
* Sample Output
*
* 1
* Explanation
*
* If we represent _ as sea level, a step up as /, and a step down as \, the
* hike can be drawn as:
*
* The hiker enters and leaves one valley.
* BlogScoringEnvironmentFAQAbout UsSupportCareersTerms Of ServicePrivacy Policy
*/
public class CountingValleys {
// /\
// _ / \_
// \ /
// \/
public static void main(String[] args) {
System.out.println("Please enter the number of steps: ");
Scanner sc = new Scanner(System.in);
int steps = sc.nextInt();
sc.nextLine(); // consume the newline
System.out.println("The hiker is taking " + steps + " steps starting from sea level.");
System.out.println("Please enter the path (U for up, D for down):");
String path = sc.nextLine();
sc.close();
System.out.println("Number of valleys: " + countingHikeValleys(steps, path));
}
private static int countingHikeValleys(int steps, String path) {
int valleysCount = 0;
int seaLevel = 0;
boolean inValley = false;
for (char step : path.toCharArray()) {
if (Character.toUpperCase(step) == 'U') {
seaLevel++;
} else if (Character.toUpperCase(step) == 'D') {
seaLevel--;
}
// If we're below sea level and weren't before, we're entering a valley
if (seaLevel < 0 && !inValley) {
valleysCount++;
inValley = true;
}
// If we're at or above sea level, we're no longer in a valley
if (seaLevel >= 0) {
inValley = false;
}
}
return valleysCount;
}
}
+3
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/DecreasingScores.iml" filepath="$PROJECT_DIR$/DecreasingScores.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
Binary file not shown.
Binary file not shown.
+101
View File
@@ -0,0 +1,101 @@
/* DecreasingScore
* Comparators are used to compare two objects. In this challenge, you'll create
* a comparator and use it to sort an array.
*
* The Player class is provided for you in your editor. It has
* fields: a String and a
*
* integer.
*
* Given an array of
* Player objects, write a comparator that sorts them in order of decreasing
* score; if
*
* or more players have the same score, sort those players alphabetically by
* name. To do this, you must create a Checker class that implements the
* Comparator interface, then write an int compare(Player a, Player b) method
* implementing the Comparator.compare(T o1, T o2) method.
*
* Input Format
*
* Input from stdin is handled by the locked stub code in the Solution class.
*
* The first line contains an integer,
* , denoting the number of players.
* Each of the subsequent lines contains a player's and
*
* , respectively.
*
* Constraints
*
* players can have the same name.
* Player names consist of lowercase English letters.
*
* Output Format
*
* You are not responsible for printing any output to stdout. The locked stub
* code in Solution will create a Checker object, use it to sort the Player
* array, and print each sorted element.
*
* Sample Input
*
* 5
* amy 100
* david 100
* heraldo 50
* aakansha 75
* aleksa 150
*
* Sample Output
*
* aleksa 150
* amy 100
* david 100
* aakansha 75
* heraldo 50
*/
import java.util.*;
// Write your Checker class here
class Checker implements Comparator<Player> {
@Override
public int compare(Player p1, Player p2) {
if (p1.score == p2.score) {
return p1.name.compareTo(p2.name);
}
return p2.score - p1.score;
}
}
class Player {
String name;
int score;
Player(String name, int score) {
this.name = name;
this.score = score;
}
}
public class DecreasingScore {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Player[] player = new Player[n];
Checker checker = new Checker();
for (int i = 0; i < n; i++) {
player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
Arrays.sort(player, checker);
for (int i = 0; i < player.length; i++) {
System.out.printf("%s %s\n", player[i].name, player[i].score);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
+55
View File
@@ -0,0 +1,55 @@
import java.util.Scanner;
/**
* EggDropping
* You are given k identical eggs and you have access to a building with n
* floors labeled from 1 to n.
*
* You know that there exists a floor f where 0 <= f <= n such that any egg
* dropped at a floor higher than f will break,
* and any egg dropped at or below floor f will not break.
*
* Each move, you may take an unbroken egg and drop it from any floor x (where 1
* <= x <= n).
* If the egg breaks, you can no longer use it. However, if the egg does not
* break, you may reuse it in future moves.
*
* Return the minimum number of moves that you need to determine with certainty
* what the value of f is.
*
* Example 1:
*
* Input: k = 1, n = 2
* Output: 2
* Explanation:
* Drop the egg from floor 1. If it breaks, we know that f = 0.
* Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1.
* If it does not break, then we know f = 2.
* Hence, we need at minimum 2 moves to determine with certainty what the value
* of f is.
*
*/
public class EggDropping {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
scanner.close();
System.out.println(minMoves(a, b));
}
private static int minMoves(int k, int n) {
// if n > f ==> eggs break
// if n < f ==> eggs are good
int min = 0;
int[][] dp = new int[n + 1][k + 1];
while (dp[min][k] < n) {
min++;
for (int i = 1; i <= k; i++) {
dp[min][i] = dp[min - 1][i - 1] + dp[min - 1][i] + 1;
}
}
return min;
}
}
+38
View File
@@ -0,0 +1,38 @@
import java.util.ArrayList;
import java.util.List;
/**
* FindTheOne
* given an array of ints, find the one that start with 1
*/
public class FindTheOne {
public static void main(String[] args) {
int[] arr = { 1, 23, 123, 45, 67, 176 };
System.out.println(OneOne(arr));
}
// private static List<String> OneOne(int[] arr) {
// List<String> Ones = new ArrayList<>();
// String[] Convereted = new String[arr.length];
// for (int i = 0; i < arr.length; i++) {
// Convereted[i] = String.valueOf(arr[i]);
// }
// for (String str : Convereted) {
// if (str.startsWith("1")) {
// Ones.add(str);
// }
// }
// return Ones;
// }
private static List<String> OneOne(int[] arr) {
List<String> Ones = new ArrayList<>();
for (int x : arr) {
String str = Integer.toString(x);
if (str.startsWith("1")) {
Ones.add(str);
}
}
return Ones;
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Vowels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your String here: ");
String str = sc.nextLine();
System.out.println(vowelsFound(str));
sc.close();
}
private static boolean vowelsFound(String str) {
String vowels = "aeoui";
for (char c : str.toLowerCase().toCharArray()) {
if (vowels.indexOf(c) != -1) {
return true;
}
}
return false;
}
}
+3
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/GamePlatform.iml" filepath="$PROJECT_DIR$/GamePlatform.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
+25
View File
@@ -0,0 +1,25 @@
/**
* FinalSpeed
*/
public class FinalSpeed {
public static double calculateFinalSpeed(double initialSpeed, int[] inclinations) {
/*
* double finalSpeed = 0;
* for (int speed : inclinations) {
* double temp = initialSpeed;
* initialSpeed = (-1 * speed) + temp;
* finalSpeed = initialSpeed;
* }
*/
double finalSpeed = initialSpeed;
for (int speed : inclinations) {
finalSpeed -= speed;
}
return finalSpeed;
}
public static void main(String[] args) {
System.out.println(calculateFinalSpeed(60.0, new int[] { 0, 30, 0, -45, 0 }));
}
}
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
+117
View File
@@ -0,0 +1,117 @@
import java.util.Scanner;
/**
* InfinityString
* There is a string, , of lowercase English letters that is repeated infinitely
* many times. Given an integer, , find and print the number of letter a's in
* the first
*
* letters of the infinite string.
*
* Example
*
* The substring we consider is , the first characters of the infinite string.
* There are
*
* occurrences of a in the substring.
*
* Function Description
*
* Complete the repeatedString function in the editor below.
*
* repeatedString has the following parameter(s):
*
* s: a string to repeat
* n: the number of characters to consider
*
* Returns
*
* int: the frequency of a in the substring
*
* Input Format
*
* The first line contains a single string,
* .
* The second line contains an integer,
*
* .
*
* Constraints
*
* For of the test cases,
*
* .
*
* Sample Input
*
* Sample Input 0
*
* aba
* 10
*
* Sample Output 0
*
* 7
*
*/
public class InfinityString {
public static void main(String[] args) {
// MY SOLUTION IS SLOW BUT IT WORKS
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your 'String' here and it must contains the letter A: ");
String str = sc.nextLine();
System.out.println("Please enter how many times you want to repeat that: ");
int x = sc.nextInt();
System.out.println(numsOfAs(str, x));
sc.close();
}
//
// private static int numsOfAs(String str, int n) {
// int numberOfAs = 0;
// int x = n;
// String newSTR = "";
// int remeinder = n / str.length();
// while (n > 0 && newSTR.toCharArray().length < n + remeinder) {
// newSTR += str;
// n--;
// }
//
// char[] charNewstr = newSTR.toCharArray();
// for (int i = 0; i < x; i++) {
// if (charNewstr[i] == 'a') {
// numberOfAs++;
// }
// }
// return numberOfAs;
//
//
private static int numsOfAs(String str, int n) {
int numberOfAs = 0;
int numRepeats = n / str.length();
int remainder = n % str.length();
// Count the number of 'a' characters in the repeated string
numberOfAs = countAs(str) * numRepeats;
// Count the number of 'a' characters in the remaining portion
if (remainder > 0) {
numberOfAs += countAs(str.substring(0, remainder));
}
return numberOfAs;
}
private static int countAs(String str) {
int count = 0;
for (char c : str.toCharArray()) {
if (c == 'a') {
count++;
}
}
return count;
}
}
+87
View File
@@ -0,0 +1,87 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* JumpingClouds
* There is a new mobile game that starts with consecutively numbered clouds.
* Some of the clouds are thunderheads and others are cumulus. The player can
* jump on any cumulus cloud having a number that is equal to the number of the
* current cloud plus or
*
* . The player must avoid the thunderheads. Determine the minimum number of
* jumps it will take to jump from the starting postion to the last cloud. It is
* always possible to win the game.
*
* For each game, you will get an array of clouds numbered
* if they are safe or
*
* if they must be avoided.
*
* Example
* Index the array from . The number on each cloud is its index in the list so
* the player must avoid the clouds at indices and . They could follow these two
* paths: or . The first path takes jumps while the second takes . Return
*
* .
*
* Function Description
*
* Complete the jumpingOnClouds function in the editor below.
*
* jumpingOnClouds has the following parameter(s):
*
* int c[n]: an array of binary integers
*
* Returns
*
* int: the minimum number of jumps required
*
* Input Format
*
* The first line contains an integer
* , the total number of clouds. The second line contains space-separated binary
* integers describing clouds where
*
* .
*
* Constraints
*
* Output Format
*
* Print the minimum number of jumps needed to win the game.
*
* Sample Input 0
*
* 7
* 0 0 1 0 0 1 0
*
* Sample Output 0
*
* 4
*
*/
public class JumpingClouds {
public static void main(String[] args) {
int[] steps = { 0, 0, 0, 0, 0, 1, 0 };
System.out.println(JumpsCount(steps));
}
private static int JumpsCount(int[] clounds) {
int jumps = 0;
int i = 0;
while (i < clounds.length - 1) {
if (i + 2 == clounds.length || clounds[i + 2] == 1) {
i++;
jumps++;
} else {
i += 2;
jumps++;
}
}
return jumps;
}
}
+49
View File
@@ -0,0 +1,49 @@
import java.util.Scanner;
/**
* MakeAnagram
*/
public class MakeAnagram {
// Anagran is 2 words compose of the sames letters such LISTEN and SILENT, MUG
// and GUM, RATE and TEAR etc...
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the first String here: " + "\t");
String str1 = sc.nextLine();
System.out.print("Please enter the second String here: " + "\t");
String str2 = sc.nextLine();
sc.close();
System.out.println(DeletionCount(str1, str2));
}
private static int DeletionCount(String strOne, String strTwo) {
int deletionCount = 0;
int[] Freqency = new int[26];
strOne = strOne.toLowerCase();
strTwo = strTwo.toLowerCase();
// fill the frequency for strOne chars
for (int i = 0; i < strOne.length(); i++) {
Freqency[strOne.charAt(i) - 'a']++;
}
// fill the frequency for strTwo chars
for (int i = 0; i < strTwo.length(); i++) {
Freqency[strTwo.charAt(i) - 'a']--;
}
// to calculate the deletion count we are going to do the absolute addition of
// the strOne and strTwo
for (int x : Freqency) {
deletionCount += Math.abs(x);
}
return deletionCount;
}
}
+39
View File
@@ -0,0 +1,39 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* PairedSocks
*/
public class PairedSocks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] randSocks = new int[n];
for (int i = 0; i < randSocks.length; i++) {
randSocks[i] = sc.nextInt();
}
sc.close();
int pairCounted = pairingSocks(n, randSocks);
System.out.println(pairCounted);
}
public static int pairingSocks(int n, int[] randSocks) {
int socks = 0;
Map<Integer, Integer> pairCount = new HashMap<>();
// we are gonna go through the array of integer and check if there is 2 paires
for (int x : randSocks) {
System.out.print(x + "\t");
pairCount.put(x, pairCount.getOrDefault(x, 0) + 1);
}
// count how many paires there is and return the sum.
System.out.println("\n");
for (int c : pairCount.values()) {
socks = socks + c / 2;
}
return socks;
}
}
+61
View File
@@ -0,0 +1,61 @@
// public class ArrayReversing {
// public static void main(String[] args) {
// int[] intArr = { 1, 2, 3, 4, 5 };
// Reverse(intArr);
// Printing(intArr);
// }
//
// public static void Reverse(int[] arr) {
// // initiate the start and the end of the ArrayReversing
// int start = 0;
// int end = arr.length - 1;
//
// while (start < end) {
// int temp = arr[start];
// arr[start] = arr[end];
// arr[end] = temp;
// // Moving pointers
// start++;
// end--;
// }
// }
//
// public static void Printing(int[] arr) {
// for (int x : arr) {
// System.out.println(x + " ");
// }
// System.out.println();
// }
//
// }
/**
* ArrayReversing
*/
public class ArrayReversing {
public static void main(String[] args) {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// revNumbers = {10,9,8,7,6,5,4,3,2,1}
for (int i : numbers) {
System.out.print(i + "\t");
}
System.out.println();
int start = 0;
int end = numbers.length - 1;
while (start < end) {
int temp = numbers[start];
numbers[start] = numbers[end];
numbers[end] = temp;
start++;
end--;
}
for (int x : numbers) {
System.out.print(x + "\t");
}
System.out.println();
}
}
+43
View File
@@ -0,0 +1,43 @@
import java.util.Arrays;
import java.util.Scanner;
/**
* SequenceFunc
*/
public class SequenceFunc {
private static final int MOD = 1000000007;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of elements: ");
int n = scan.nextInt();
System.out.println("Please enter the elements of the Array");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
System.out.println("The array has the following elements: ");
for (int x : arr) {
System.out.print(x + "\t");
}
// to store the last result
// let's create a temporary array
long result = 0;
int[] temp = new int[n];
for (int i = 1; i <= n; ++i) {
System.arraycopy(arr, 0, temp, 0, i);
Arrays.sort(temp, 0, i);
long fi = 0;
for (int j = 0; j < i; j++) {
fi = (fi + (long) (j + 1) * temp[j]) % MOD;
}
result = (result + fi) % MOD;
}
System.out.println(result);
scan.close();
}
}
+32
View File
@@ -0,0 +1,32 @@
import java.util.Scanner;
public class Palindrom {
public static String reversedStr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
System.out.println(revString(string));
System.out.println(PalindromFlag(string));
sc.close();
}
private static String revString(String str) {
reversedStr = "";
for (int i = str.length() - 1; i >= 0; i--) {
char temp = str.charAt(i);
reversedStr += temp;
}
return reversedStr;
}
private static boolean PalindromFlag(String str) {
// let's reverse the String first
if (str.equals(reversedStr)) {
return true;
}
return false;
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.Scanner;
/**
* ReversedString
*/
public class ReversedString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your String:");
String str = sc.nextLine();
System.out.println(revSTR(str));
}
private static String revSTR(String str) {
String revString = "";
for (int i = str.length() - 1; i >= 0; i--) {
char temp = str.charAt(i);
revString += temp;
}
return revString;
}
}
+4
View File
@@ -0,0 +1,4 @@
{
"status": "failed",
"failedTests": []
}