I have an array with integers, I just have to count the pairs and return that counted value.
static int StockMerchant(int n , int arr[]){
int[] temp = new int[n];
int count = 0;
for(int i =0 ; i<n ; i++){
for(int j= i+1 ; j<n ; j++){
if(arr[i]==arr[j]){
arr[j] = '0';
arr[i] ='0';
count++;
}
}
}
return count;
}
when I try: 1 1 2 2 2: gives output 2
But when I change the input to 1 2 1 2 2: gives output 3
I want the output to be 2 in the second case also. please help.
*Sorry for my bad paint skills.
Hope I was able to point out your mistake:)
Here is my solution :
Its basically sorting the array first and checking if it matches the value increment i position to j+1 and j as i+1
static int StockMerchant(int n, int arr[]) {
Arrays.sort(arr);
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
i = j + 1;
j = i;
count++;
}
}
}
return count;
}
Here i'm assuming all your test inputs contains positive integers, this logic works for the positive values only.
static int StockMerchant(int n , int arr[]){
int starter = n>0 && arr[0] != 0 ? 0 : -1;
int temp = starter;
int count = 0;
Arrays.sort(arr);
for(int i =0 ; i<n ; i++){
if(temp == arr[i]){
temp = starter;
count++;
}else{
temp = arr[i];
}
}
return count;
}
Related
public class first {
public static void main(String args[]){
int arr[]={5,4,1,3,2};
for(int i=0; i<arr.length-1;i++){
int smallest=arr[i];
for(int j=i+1; j<arr.length;j++){
if(smallest>arr[j]){
smallest=arr[j];
}
}
//swap
int temp=smallest;
smallest=arr[i];
arr[i]=temp;
}
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
i have done this problem by getting the smallest in terms of index number
and the program works properly .
but when i am taking smallest in terms of number present at index number ,
this program did not work.your text
you need to save the smallest number index not the number it self so you know the positions to make the swap
for (int i = 0; i < arr.length - 1; i++) {
int indexOfSmallestNum = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[indexOfSmallestNum] > arr[j]) indexOfSmallestNum = j;
}
int temp = arr[i];
arr[i] = arr[indexOfSmallestNum];
arr[indexOfSmallestNum] = temp;
}
This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
Closed 3 years ago.
I am working with 2-D arrays and I require help on this topic. My task is to create a 2-D array such that it is n by n (i.e. the number of rows and columns are equal). Fill the array with alternating 0's and 1's
void setup()
{
int n=3;
// code to populate the array
// code to display the output of array in a table format
/* output should be as follows:
The expected result when n=3 should be as the following:
1 0 1
0 1 0
1 0 1
*/
}
Solution:
class test{
public static void main(String[] args) {
int n = 3;
int[][] arr = setup(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
static int[][] setup(int n){
int[][] arr = new int [n][n];
boolean even = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = even ? 1 : 0;
even = !even;
}
}
return arr;
}
}
Output:
0 1 0
1 0 1
0 1 0
This should work.
You can replace the 3 that is given to n with any number you want as the 2D-Array's length.
void setup(){
int n = 3;
int count = 0;
int[][] myArray = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(count % 2 == 0){
myArray[i][j] = 1;
}
else{
myArray[i][j] = 0;
}
System.out.print(myArray[i][j] + " ");
count++;
}
System.out.println();
}
}
Output:
0 1 0
1 0 1
0 1 0
It's not complex, you just have to iterate two loops, that's it. Although you can get the solution on different ways.
public static void main(String[] args) {
int n = 3;
int [][] arr = new int[n][n];
int val = 1;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j] = val;
val = (val == 0) ? 1 : 0;
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
Output:
0 1 0
1 0 1
0 1 0
A simple implementation:
void setup()
{
int n=3;
final int[][] array = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((n * i + j) % 2 == 0) {
array[i][j] = 1;
}
}
}
}
Here is how you can approach your logic , create a counter and negate each time after it prints value.
public static void main(String[] args) {
int counter = 0;
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(counter==0?1:0);
counter = ~counter;
}
System.out.println();
}
}
Output
101
010
101
Here is Online source code.
Recently, I took Linkedin placement test in which there was a question in which output for 4 test cases were wrong for me. I could not figure out what was my mistake becasue inputs/outputs were hidden.
Anyways here was the question:
Find the maximum element from an array where product of any other two elements would be equal to that number and return that number .If no, such element is there then return -1.
Here was my solution:
static int maxElement(int[] arr) {
Arrays.sort(arr);
int max = arr[arr.length-1];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
return -1;
}
I guess you need to find the possible maximum number in the array and the product of two elements in the array.
If I assume this, your code fails for this test case:
int[] arr = {2,4,5,3,7,6}; , where the answer should be 6
Check this below code it will work for above test-case.
Just add one more reverse for loop to check the possible value and product.
static int maxElement(int[] arr) {
Arrays.sort(arr);
for (int k = arr.length-1; k >= 0; k--) {
int max = arr[k];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
}
return -1;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] * a[j]) > maxSum) {
if(list.contains(a[i] * a[j]))
maxSum = a[i] * a[j];
}
}
}
if (maxSum != 0)
return maxSum;
return -1;
I want to write a program that looks for three numbers in array that gives an arithmetic sequence of length three - three numbers a,b and c form an arithmetic sequence of length 3 if: b-a = c-b.
The problem is that he code doesn't print "yes" even when it should, it never get into the command if. I guess I have a problem when writing the math command for b-a = c-b.
public static void main (String[] args) {
int [] a = new int [args.length - 1];W
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i+1]);
}
for (int i = 0; i < a.length - 2; i++) {
for (int j = i+1; j < a.length - 1; j++) {
int b = a[i];
for (int k = j + 1; k < a.length; k++) {
int c = a[k];
if (b - a[i] == c - b) {
System.out.println("yes");
break;
}
}
}
}
}
}
I think this is what you want:
Changes:
First sort the array
When you break from the inner most loop, you
must also break from all the outer loops (check code below)
Arrays.sort(a);
outerLoop:
for (int i = 0; i < a.length - 2; i++) {
for(int j = i+1; j < a.length - 1; j++){
for(int k = j+1; k < a.length; k++){
if(a[j] - a[i] == a[k] - a[j]){
System.out.println("yes");
break outerLoop;
}
}
}
}
Update:
You are missing the first element of the array because of this code:
int [] a = new int [args.length - 1]
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i+1]);
}
Change it to:
int [] a = new int [args.length]
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i]);
}
This works:
public static void main(String[] args) {
int [] a =new int [new Integer(args.length)];
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i]);
}
for(int j=0;j<a.length;j++){
if(j+2<a.length){
if(a[j+1]-a[j] ==a[j+2] - a[j+1])
System.out.println("Yes........." +a[j] +""+ a[j+1] +"" +a[j+2]);
}
}
}
tested with 2 3 4 6(2 3 4 is sequence) and 1 3 5 7 (1 3 5 and 3 5 7 are sequence)
i have a code that create an array of 7 integers than the system have a function that check where array[i] == 0 will place zeros at the most right of the array and at the end the system display the original array and the compressed array like this
original array:
0 5 0 12 0 0 4
compressed array :
5 12 4 0 0 0 0
can anyone help me ??
this is the code
package question3;
import java.util.Scanner;
public class ArrayWithCompression {
static int[] array = new int[7];
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("enter numbers");
for(int i = 0 ; i< array.length; i++){
array[i] = sc.nextInt();
}
System.out.println(compression(array));
}
public static int[] compression(int[] newarray){
for(int i = 0; i< array.length; i++ ){
if(array[i] == 0){
array[i] = i++;
}
array[i] = newarray[i];
}
return newarray;
}
}
You are close to the right answer. I suggest you step through your code in your debugger to see what you are doing.
Since it is hard to give you a hint without giving you the answer...
public static int[] compression(int[] array){
int j = 0;
// iterate over all the numbers in the array.
for (int i : array)
// if the number is not 0 add it back into the array
if (i != 0)
// add it back only counting non-0 for the index
array[j++] = i;
// fill the rest of the array with zero.
Arrays.fill(array, j, array.length, 0);
}
you can try this code in your compression method:
public static int[] compression(int[] newarray){
int []arr= new int[newarray.length];
int j=0;
for(int i = 0; i< newarray.length; i++ ){
if(newarray[i] != 0){
arr[j++] = newarray[i];
}
}
return arr;
}
without using other array :
public static int[] compression(int[] newarray){
for(int i = 0; i< newarray.length; i++ ){
if(newarray[i] != 0){
continue;
}else
{
for(int j = i; j< newarray.length; j++)
{
if(newarray[j] != 0)
{
newarray[i]=newarray[j];
newarray[j]= 0;
break;
}
}
}
}
return newarray;
}
and you can change your main method (printing array ) :
array= compression(array);
for(int i = 0 ; i< array.length; i++){
System.out.println(array[i]);
}
You can try it out like:
for (int i = 0; i < arr.length - i; i++) {// loop decreases as after each loop a zero if found is put at last
for (int j = 0; j < arr.length - 1; j++) { // this loop puts first 0 in last by swapping each nos.
if (arr[j] == 0) {
arr[j] = arr[j + 1];
arr[j + 1] = 0;
}
}
}
return arr;
and in main function:
array = compression(array);
for (int i : array) {
System.out.println(i);
}