Program 1. Numbers Puzzle
Given a set of numbers, one can arrange them in any order but must pay a penalty equal to the sum of the absolute differences between adjacent numbers. Return the minimum penalty that must be paid.
Input Specification:
input1:
Length of an integer array of numbers()2<=input1<=1000)
input2:
Integer array(1<=input2[i]<=10000)
Ouput specification:
Return the minimum penalty.
Example 1
Input:
Input1: 3
Input2: 1 3 2
Output:
2
Explanation:
The order that incurs the minimum penalty is 1,2,3. The penalty is abs(2-1)+ abs(3-2)=2.
Example 2
Input:
Input1: 4
Input2: 1 6 -2 4
Output:
8
Explanation:
The order that incurs the minimum penalty is -2,1,4,6. The penalty is abs(6-4) + abs(1-(-2)) = 2+3+3=8
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str = br.readLine().split(" ");
int num[] = new int[n];
for(int i=0; i<str.length; i++)
num[i] = Integer.parseInt(str[i]);
Arrays.sort(num);
int minPenalty = 0;
for(int i=0; i<n-1; i++)
minPenalty += Math.abs(num[i] - num[i+1]);
System.out.println(minPenalty);
}
}
Program 2. Anagrams
An Anagram is a word, phrase, or name formed by rearranging the letters of another word, phrase, or name.
Write a function to check if two given strings are anagrams or not. Return “yes” if they are anagrams, otherwise return “no”.
Input Specifications:
input1: the first string
input2: the second string
Output Specification:
Return “yes” if they are anagrams, otherwise, return “no”
Example 1
Input:
input1: build
input2: dubli
Output:
yes
Explanation:
The first string can be rearranged from the second string. Hence, they are anagrams of each other.
Example 2
Input:
input1: beast
input2: yeast
Output:
no
Explanation:
The first string contains the letter ‘b’ which is not present in the second string. Similarly, the second string contains the letter ‘y’ which is not present in the first string. Hence, the two strings are not anagrams of each other.
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
static String sortString(String str) {
char tempStrArr[] = str.toCharArray();
Arrays.sort(tempStrArr);
return new String(tempStrArr);
}
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = sortString(br.readLine());
String str2 = sortString(br.readLine());
if(str1.equals(str2))
System.out.println("yes");
else
System.out.println("no");
}
}
Program 3. There is an array in which many elements repeated 4 times or more. Write a function to find the elements repeated less than 4 times
Example 1
Input:
5 4 5 2 5 4 4 5 4 5
Output:
2
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
public static void main (String[] args) throws Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int arr[] = new int[str.length];
for(int i=0; i<str.length; i++)
arr[i] = Integer.parseInt(str[i]);
Arrays.sort(arr);
for(int i=0; i<arr.length; i++) {
int total = 0, j = i;
while(j < arr.length && arr[j] == arr[i]) {
total++;
j++;
}
if(total < 4)
System.out.print(arr[i] + " ");
i = j-1;
}
}
}
Also Checkout