## nsenthilrajaed

BAN USERpublic class LargestNumberDevideBy3 {

/*

* Sort Array

* Add all elments of array

* check sum of array is devided by 3, if not get the mod value and search the value in array

* if it is there, the remove the element from and form largest number.

*

*/

private static String findLargestNumber(int a[]) {

quickSort(a, 0, a.length-1);

int sum = sumOfArray(a);

if((sum % 3) != 0) {

for(int i = 0; i < a.length; i++) {

for(int j = i; j < a.length; j++) {

if(a[j] != -1 && (sum - a[j]) % 3 == 0 ) {

sum = sum - a[j];

a[j] = -1;

break;

}

}

if(sum % 3 == 0) {

break;

}

sum = sum - a[i];

a[i] = -1;

}

}

String s = "";

for(int i = a.length-1; i >=0; i--) {

if(a[i] != -1) {

s += a[i];

}

}

return s;

}

private static int sumOfArray(int a[]) {

int sum = 0;

for(int i = 0 ; i < a.length; i++) {

sum += a[i];

}

return sum;

}

private static void quickSort(int a[], int low, int high) {

if(low < high) {

int value = partition(a, low, high);

quickSort(a, low, value - 1);

quickSort(a, value + 1, high);

}

}

private static int partition(int a[], int low, int high) {

//Find pivot -- pivot is the last element

int pivot = a[high];

//Make i value low - 1. if low is 0 then i value will be -1

int i = low -1;

for(int j = low; j <= high -1; j++) {

if(pivot >= a[j]) {

int temp = a[i+1];

a[i+1] = a[j];

a[j] = temp;

i++;

}

}

int temp = a[i+1];

a[i+1] = a[high];

a[high] = temp;

return i+1;

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int m = scanner.nextInt();

int a[] = new int[m];

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

a[i] = scanner.nextInt();

}

System.out.println(findLargestNumber(a));

}

}

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

import java.util.Scanner;

- nsenthilrajaed June 12, 2017public class JumbledNumber {

private static boolean isJumbledNumber(long n) {

int previous = (int)(n % 10);

n = n / 10;

while(n > 0) {

int current = (int)(n % 10);

n = n / 10;

if((current+1) != previous && (current-1) != previous){

return false;

}

previous = current;

}

return true;

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

long m = scanner.nextLong();

System.out.println(isJumbledNumber(m));

}

}