0% found this document useful (0 votes)
12 views

Final

Accenture questions

Uploaded by

Saisharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Final

Accenture questions

Uploaded by

Saisharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 17

For java is great then output is great is java

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().trim(); // Trim leading/trailing spaces
System.out.println(reverse(str));
sc.close(); // Close the scanner to avoid resource leaks
}

public static String reverse(String str) {


// Replace multiple spaces with a single space and trim again
String[] words = str.replaceAll("\\s+", " ").split(" ");
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]).append(" ");
}
return result.toString().trim(); // Trim trailing space
}
}

And for output: avaj si taerg

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(reverseWords(str));
sc.close(); // Close the scanner to avoid resource leaks
}
public static String reverseWords(String str) {
String[] words = str.split(" ");
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(new
StringBuilder(word).reverse().toString()).append(" ");
}
return result.toString().trim();
}
}

For getting taerg is avaj


import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(reverseWords(str));
sc.close();
}
public static String reverseWords(String str) {
String[] words = str.split("\\s+"); // Split the string by spaces
StringBuilder result = new StringBuilder();
// Reverse the order of the words and reverse each word
for (int i = words.length - 1; i >= 0; i--) {
result.append(new StringBuilder(words[i]).reverse()).append("
");
}
return result.toString().trim(); // Trim the trailing space
}
}

For making a particular word reverse

import java.util.Scanner;
public class Mnain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(reverseWords(str));
sc.close(); // Close the scanner to avoid resource leaks
}
public static String reverseWords(String str){
String words[]=str.split(" ");
String first=new StringBuilder(words[0]).reverse().toString();
words[0]=first;
String second=new StringBuilder(words[words.length-
1]).reverse().toString();
words[words.length-1]=second;
String stre=String.join(" ",words);
return stre.toString();
}
}
Palindrome
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(isPalindrome(str));
sc.close();
}

public static boolean isPalindrome(String str) {


int left = 0;
int right = str.length() - 1;

while (left < right) {


// Compare characters from the start and end
if (str.charAt(left) != str.charAt(right)) {
return false; // If any character doesn't match, it's not a
palindrome
}
left++;
right--;
}
return true; // If all characters match, it's a palindrome
}
}

Roman number

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String romanNumeral = sc.nextLine().trim();
int result = romanToInteger(romanNumeral);
if (result == -1) {
System.out.println("INVALID");
} else {
System.out.println(result);
}
}

public static int romanToInteger(String s) {


// Map to store the value of each Roman numeral
Map<Character, Integer> romanMap = new HashMap<>();
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);

int total = 0; // Variable to store the total value


int prevValue = 0; // Variable to track the previous numeral value

for (int i = s.length() - 1; i >= 0; i--) { // Traverse from right to


left
char ch = s.charAt(i);

if (!romanMap.containsKey(ch)) { // Invalid character check


return -1; // Return -1 to indicate INVALID
}

int currentValue = romanMap.get(ch); // Get the current


numeral's value

// If the current value is less than the previous value, subtract


it
if (currentValue < prevValue) {
total -= currentValue;
} else {
// Otherwise, add it
total += currentValue;
}

prevValue = currentValue; // Update the previous value


}

// Additional validation for correct Roman numeral formatting


if (!isValidRoman(s, romanMap)) {
return -1;
}

return total; // Return the computed total


}
// Method to validate the correctness of the Roman numeral string
public static boolean isValidRoman(String s, Map<Character, Integer>
romanMap) {
// Validate patterns like IIII, VV, LL, etc. are not allowed
String invalidPatterns = "IIII|VV|XXXX|LL|CCCC|DD|MMMM";
if (s.matches(".*(" + invalidPatterns + ").*")) {
return false;
}

// Additional rules can be added here for comprehensive validation


// Examples: Checking for invalid consecutive placements

return true;
}
}

To store duplicate elements


import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string or number:");
String input = sc.nextLine();
Set<Character> duplicates = getDuplicateCharacters(input); //
Get duplicate characters
System.out.println("Duplicate characters: " + duplicates); // Print
duplicate characters
}
public static Set<Character> getDuplicateCharacters(String input) {
Set<Character> seen = new HashSet<>(); // Set to store seen
characters
Set<Character> duplicates = new HashSet<>(); // Set to store
duplicate characters

for (char ch : input.toCharArray()) { // Iterate through each


character in the string
if (seen.contains(ch)) { // If character has already been seen,
it's a duplicate
duplicates.add(ch); // Add to duplicates set
} else {
seen.add(ch); // Otherwise, add to seen set
}
}

return duplicates; // Return the set containing only duplicate


characters
}
}

Only one time sell stock


public class StockProfit {

// Function to find the maximum profit


public static int maxProfit(int[] prices) {
if (prices.length == 0) return 0;

int minPrice = Integer.MAX_VALUE; // Initialize the minimum price


to a large value
int maxProfit = 0; // Initialize maximum profit to 0

for (int price : prices) {


if (price < minPrice) {
minPrice = price; // Update minimum price
} else {
int profit = price - minPrice; // Calculate profit
if (profit > maxProfit) {
maxProfit = profit; // Update maximum profit
}
}
}

return maxPorfit;
}

1A)
import java.util.*;
public class main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int n=str.length();//you can you int n=sc.nextInt();
sc.nextLine();
char ch=sc.nextLine().charAt(0);
if(n>0){
int count=0;
for(int i=0;i<n;i++){
if(ch==str.charAt(i))
count++;
}
System.out.println(count);
}
}
}

2Answer):
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int H=sc.nextInt();
int V=sc.nextInt();
int Vn=sc.nextInt();
int N=sc.nextInt();// not given if exam not given take one
double e=(double)V/Vn;
double HF=H*Math.pow(e, 2*N);

System.out.println((int)HF);//s.o.p((int)H*Math.pow((double)V/Vn,2*N))
}
}
q

3Aand 9ans)import java.util.*;


public class main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
EvenOdd(arr);
}
public static void EvenOdd(int arr[]){
StringBuilder result=new StringBuilder();
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0){
result.append("even"+" ");
}
else{
result.append("odd"+" ");
}
}
System.out.println("["+result.toString().trim()+"]");
}
}
Or
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String app[]=sc.nextLine().split(" ");
int n=sc.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(app[i]);
}
String result=EvenOdd(arr);
System.out.println("["+result+"]");
sc.close();
}
public static String EvenOdd(int arr[]){
StringBuilder res=new StringBuilder();
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0)
res.append("even"+" ");
else
res.append("odd"+" ");
}
return res.toString().trim();
}
}

4)import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int res=evenbit(arr);
System.out.println(res);
sc.close();
}
public static int evenbit(int arr[]){
int result=0;
int sum=0;
int sum2=0;
for(int i=0;i<arr.length;i++){
if(i%2!=0)
sum+=arr[i];
else
sum2^=arr[i];
}
result=sum-sum2;
return result;
}
}

5A)
import java.util.*;
public class pairs{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int A[]=new int[N];
for(int i=0;i<N;i++){
A[i]=sc.nextInt();
}
int result[]=maxPair(A,N);
if(result.length>0){
System.out.println(Arrays.toString(result));
}
else{
System.out.println("not found");
}
}
public static int[] maxPair(int A[],int N){
Arrays.sort(A);
int left=0;
int right=N-1;
int maxi=Integer.MIN_VALUE;
int resultPair[]=new int[2];
while(left<right){
int sum=A[left]+A[right];
if(sum==18 && A[right]>A[left]){
int product=A[right]*A[left];
if(product>maxi){
maxi=product;
resultPair[0]=A[right];
resultPair[1]=A[left];
}
left++;
right--;
}
else if(sum<18){
left++;
}
else{
right--;
}
}
return maxi==Integer.MIN_VALUE? new int[]{}:resultPair;
}
}

6A)
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(fibno(n));
}
public static int fibno(int n){
if(n<0){
return -1;
}
if(n==0||n==1){
return 1;
}
int a=1;
int b=1;
for(int i=2;i<=n;i++){
int temp=(a*a+b*b)%47;
a=b;
b=temp;
}
return b;
}
}

7A):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String app[]=sc.nextLine().split(" ");
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(app[n-i-1]);
}
int result=reverseEven(arr);
System.out.println(result);
sc.close();
}
public static int reverseEven(int arr[]){
if(arr.length<=0){
return -1;
}
int sum=0;
for(int i=0;i<arr.length;i++){
if(i%2==0){
sum=sum+arr[i];
}
}
return sum;
}
}

8Answe):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String file[]=sc.nextLine().split(" ");
int n=sc.nextInt();
int result=versions(file);
System.out.println(result);
sc.close();
}
public static int versions(String file[]){
int version=-1;
for(int i=0;i<file.length;i++){
String filename =file[i];
if(filename.matches("File_\\d+")){
int newver=Integer.parseInt(filename.split("_")[1]);
version=Math.max(version,newver);
}
}
return version;
}
}

10A)
import java.util.Scanner;
public class CandyShopping {
public static int maxCandies(int n, int[] A, int M) {
int count = 0;
for (int i = 0; i < n; i++) {
if(M>A[i]){
if (A[i] % 5 == 0) {
count++;
}
else if (M >= A[i]) {
M -= A[i];
count++;
}

else {
break;
}
}
}

return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = sc.nextInt();
}
int M = sc.nextInt();
System.out.println(maxCandies(n, A, M));
}
}

Or
import java.util.Scanner;

public class CandyShopping {

public static int maxCandies(int n, int[] A, int M) {


int count = 0;

for (int i = 0; i < n; i++) {


if (A[i] % 5 == 0) {
count++; // Buy without spending money
} else if (M >= A[i]) {
M -= A[i]; // Buy if enough money
count++;
}
else if (M > 0) {
break;
}
}
return count;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];

for (int i = 0; i < n; i++) {


A[i] = sc.nextInt();
}

int M = sc.nextInt();

System.out.println(maxCandies(n, A, M));
}
}

Or
import java.util.Scanner;
public class CandyShopping {
public static int maxCandies(int n, int[] A, int M) {
int count = 0;
for (int i = 0; i < n; i++) {
if (A[i] % 5 == 0) {
count++; // Buy without spending money
} else if (M >= A[i]) {
M -= A[i]; // Buy if enough money
count++;
}
}

return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = sc.nextInt();
}
int M = sc.nextInt();
System.out.println(maxCandies(n, A, M));
}
}
12Answe):

import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int D=sc.nextInt();
int result = (int)(Math.PI * D * D);
//float result=3.14f*D*D;
//int resultt=(int)result;
//because asking about the tree shadow of upper which like circle so so
we keep logic and read all formulas
System.out.println(result);
}
}

14 ans):

import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String firstname=sc.nextLine();
String Last=sc.nextLine();
System.out.println(firstname.toLowerCase()+"
"+Last.toUpperCase());
}
}
Orrr if full name is given
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String str[]=sc.nextLine().split(" ");
str[0]=str[0].toLowerCase();
str[str.length-1]=str[str.length-1].toUpperCase();
String result=String.join(" ",str);
System.out.println(result);
}
}

15A):
import java.util.Scanner;
public class PicnicPrimeSum {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static int sumOfPrimes(int N) {
if (N <= 1) {
return 0; // No primes for N = 0 or N = 1
}

int sum = 0;
for (int i = 2; i <= N; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input: The number N
int N = sc.nextInt();
// Output: Sum of all primes till N
System.out.println(sumOfPrimes(N));
}
}

16A):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<0){
System.out.println("error because negtavie number");
return;
}
String a=Integer.toString(n);
a=a.replace("0","5");
System.out.println(Integer.parseInt(a));
}
}

17answe):

import java.util.HashSet;
import java.util.Scanner;
public class UniquePairsMultipleOfThree {
public static int countAndPrintUniquePairs(int[] A, int N) {
HashSet<String> pairs = new HashSet<>();
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if ((A[i] * A[j]) % 3 == 0) {
String pair = Math.min(A[i], A[j]) + "," +
Math.max(A[i], A[j]);
if (!pairs.contains(pair)) {
pairs.add(pair);
System.out.println("(" + A[i] + ", " + A[j] + ")");
count++;
}
}
}
}

return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextInt();
}
int result = countAndPrintUniquePairs(A, N);
System.out.println("Total number of unique pairs: " + result);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy