remove number from array - java

I need to write a program which for a given array of ints prints all its elements, but each value only once, without repetitions. That is my tutor said. I agree that there are several example here, but I have special conditions like:
Do not create any auxiliary arrays, collections or Strings!
Do not use any classes from packages other than the standard java.lang.
I have been studying Java not so long so here is what I've done:
public class Third {
public static void main(String[] args) {
int[] p = {5, 2, 2, 5, -1, 5, 12, 2, 5, 44, 12, 9};
remove(p);
}
static public void remove(int[] a) {
int min = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] == a[j]) {
a[i] = min;
}
}
}
for (int j = 0; j < a.length; j++) {
if (a[j] != min) {
System.out.println( a[j] );
}
}
}
}
I realize that this is not efficient, because it is not able to print int's min value. So is there any other way to do it correctly ?

Since you said that you want to print the elements of the array only once, but you cannot use any structures or other arrays to achieve that, I believe your best strategy is to simply scan the array and look for elements with the same value from the index forward, and only print if you haven't found any.
For example: for the array {5, 2, 2, 5, -1, 5, 12, 2, 5, 44, 12, 9}, when you look at the first 5, you'll see three more fives to your right so you won't print anything, but when you look at that fourth 5, there'll be non and so you'll print it. When you see -1, for example, you won't see any other -1 to your right, and you'll print it.
public static void main(String[] args) {
int[] p = {5, 2, 2, 5, -1, 5, 12, 2, 5, 44, 12, 9};
remove(p);
}
static public void remove(int[] a) {
for (int i = 0; i < a.length; i++) {
boolean found = false;
for (int j = i + 1; j < a.length; j++) {
if (a[i] == a[j]) {
found = true;
break;
}
}
if (!found)
System.out.println(a[i]);
}
}
And the output for your array will be:
-1
2
5
44
12
9
Every element is only printed once

Based on your question here i am providing solution. In this first i am sorting array in ascending order then i am printing current index a[i] if it is not equal to next index a[i+1] .
program:
int a[] = { 5, 2, 2, 5, -1, 5, 12, 2, 5, 44, 12, 9};
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++)
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
for (int i = 0; i < a.length - 1; i++) {
if (a[i] != a[i + 1]) {
System.out.println(a[i]);
}
if (i == a.length - 2)
System.out.println(a[i + 1]);
}
output:
-1
2
5
9
12
44
Hope it will help you.

Related

How do I check if a number is bigger than all other numbers to the right of it in array

For example I have an array of these values
[10, 14, 2, 1, 5, 7, 8, 0]
If I start with 10 it would be bigger than 2, 1, 5, 7, 8, and 0. Then 14 would be bigger than 2, 1, 5, 7, 8, and 0 and so on.
Is there a way I can loop through a certain array and find the total count of smaller numbers moving from left to right?
Here is what I have tried:
for(int i = 0; i < array.length; i++) {
for(int j = i + 1; j < array.length; j++) {
if(array[i] > array[j]) {
count++;
}
}
Using
[10, 14, 2, 1, 5, 7, 8, 0]
I expect the output of 18, but the actual output is 1.
You got an error in second loop
for(int i = 0; i < array.length; i++) {
for(int j = i + 1; j < array[i].length; j++) { // HERE is mistake, array.length
if(array[i] > array[j]) {
count++;
}
}
on the other hand I am wondering how that does even compile...
The first loop should work array.lenth-1 times, the other way also will give true answer.
int count = 0;
int[] array = new int[]{ 10, 14, 2, 1, 5, 7, 8, 0 };
for(int i = 0; i < array.length-1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
count++;
}
}
}
You almost got it right:
int count = 0;
int[] array = {10, 14, 2, 1, 5, 7, 8, 0};
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
count++;
}
}
}
I think you want total no of smaller elements in the array moving from right to left. If so Here is a solution.
Here is your array.
[10, 14, 2, 1, 5, 7, 8, 0]
int[] countArray= new int[array.length];
for(int i = 0; i < array.length; i++) {
int count=0;
for(int j = i + 1; j < array.length-1; j++) {
if(array[i] > array[j]) {
count++;
}
}
countArray[i]=count;
}
Here is runing code
public class Teas {
public static void main(String[] args) {
int[] array = {
10,
14,
2,
1,
5,
7,
8,
0
};
int count = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
count++;
}
}
}
System.out.println(count);
}
}
Your Logic is correct I think count variable initialization is wrong.
you might initializing in for loop

How can I print an increasing number of elements of an array per line

I want to print out an increasing number of elements in my array per line but I'm not sure how I could do it.
public static void main(String[] args) {
int[] x = new int[21];
for (int i = 0; i < x.length; i++) {
x[i] = i + 1;
}
System.out.println(Arrays.toString(x));
}
I would like my output to look like:
[1]
[2, 3]
[4, 5, 6]
etc...
instead of what I get right now which is
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
I'm really new to java so any tips would really be appreciated, thanks.
Add this below your code.
for (int i = 0, ctr = 0; i < x.length; ctr++) {
System.out.print("[ ");
for (int j = 0; j <= ctr; i++) {
System.out.print(x[i]);
j++;
if (j <= ctr) {
System.out.print(" ,");
}
}
System.out.println(" ]");
}
This method does not require storage
int start = 1;
int count = 1;
int outer = 6;
for (int y = 0; y < outer; y++) {
System.out.print ("[");
int x = start;
for (; x < start + count; x++) {
System.out.print (x);
if (x < start + count - 1)
System.out.print(",");
}
System.out.println ("]");
count++;
start = x;
}
result
[1]
[2,3]
[4,5,6]
[7,8,9,10]
[11,12,13,14,15]
[16,17,18,19,20,21]
You can use this code
int[] x = new int[21];
for (int i = 0; i < x.length; i++) {
x[i] = i + 1;
}
int start = 0, len = 1;
while(start + len <= x.length) {
int[] newArray = Arrays.copyOfRange(x, start, start + len);
System.out.println(Arrays.toString(newArray));
start += len;
len++;
}
Using two loops you can achieve the result, the outer loop will create an empty array with each iteration and the inner one will populate it with numbers. Also using a third variable to keep track of the last number generated.
public static void main(String[] args) {
int n = 21;
int lastNumber = 0;
int x[] = null;
for(int j = 0; j< n; j++) {
x = new int[j];
for (int i = 0, k = lastNumber; i< j; i++,k++) {
x[i] = k + 1;
}
if(x.length != 0){
lastNumber = x[x.length - 1];
System.out.println(Arrays.toString(x));
}
}
}
Output:
[1]
[2, 3]
[4, 5, 6]
[7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20, 21]

grouping algorithm java n choose k

I have 10 numbers in a vector container contains: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. And I want n numbers in a group, for example:
n = 3
[1,2,3], [4,5,6], [7,8,9], [10]
[2,3,4], [5,6,7], [8,9,10], [1]
Is there an algorithm to find all the combinations?
int[] input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 3;
List<int[]> subsets = new ArrayList<>();
int[] s = new int[k]; // here we'll keep indices
// pointing to elements in input array
if (k <= input.length) {
// first index sequence: 0, 1, 2, ...
for (int i = 0; (s[i] = i) < k - 1; i++);
subsets.add(getSubset(input, s));
for(;;) {
int i;
// find position of item that can be incremented
for (i = k - 1; i >= 0 && s[i] == input.length - k + i; i--);
if (i < 0) {
break;
}
s[i]++; // increment this item
for (++i; i < k; i++) { // fill up remaining items
s[i] = s[i - 1] + 1;
}
subsets.add(getSubset(input, s));
}
}
System.out.println();
for(int i = 0; i < subsets.size();i++) {
int[] result = subsets.get(i);
for(int j = 0; j < result.length; j++) {
System.out.print(result[j]+" ");
}
System.out.println();
}
}
int[] getSubset(int[] input, int[] subset) {
int[] result = new int[subset.length];
for (int i = 0; i < subset.length; i++)
result[i] = input[subset[i]];
return result;
}
Thank you.

Bubblesort doesn't sort properly

I have some code that I've compiled and ran. It is suppose to sort the values from smallest to greatest. Can someone help me find what is going on in the code and not making it sort correctly? I get these numbers
-9 -3 -1 1 6 7 83 19 2 6 4 6 32 66
Can someone help me and tell me what is wrong with the code? Thank you!
int myArray[] = {1, 6, -1, 7, 83, 19, -3, 6, 2, 4, 6, 32, 66, -9};
int n = myArray.length;
myArray = doop(myArray);
for (int i = 0; i < n; i++) {
System.out.println(myArray[i]);
}
private static int[] doop(int[] myArray) {
int n = myArray.length;
int swap;
for (int i = n - 1; i >= 0; i--) {
int j = i;
int min = myArray[i];
while ((j > 0) && (myArray[j - 1] < min)) {
myArray[j] = myArray[j - 1];
j = j - 1;
}
myArray[j] = min;
}
return myArray;
}
In bubble sort you have to compare the only the adjacent elements and traverse the array. Repeating this n-1 times , your array gets sorted and so correct code is:
private static int[] doop(int[] myArray)
{
int n = myArray.length;
for (int i = n - 1; i >= 0; i--)
{
for(int j=n-1;j>0;j--)
{
if(myArray[j]<myArray[j-1])
{
//swapping the elements
myArray[j]=myArray[j]^myArray[j-1];
myArray[j-1]=myArray[j]^myArray[j-1];
myArray[j]=myArray[j]^myArray[j-1];
}
}
}
return myArray;
}
int myArray[] = {1, 6, -1, 7, 83, 19, -3, 6, 2, 4, 6, 32, 66, -9};
int n = myArray.length;
myArray = doop(myArray);
for (int i = 0; i < n; i++) {
System.out.println(myArray[i]);
}
}
private static int[] doop(int[] myArray) {
int n = myArray.length;
int swap;
for (int i = n - 1; i >= 0; i--) {
int j = i;
int min = myArray[i];
while ((j > 0) && (myArray[j - 1] < min)) {
myArray[j] = myArray[j - 1];
j = j - 1;
myArray[j] = min;
}
}
return myArray;
}

How to find a index from a two dimensional array

What I'm trying to do is print the largest number within a two dimensional array and it's index location. I'm able to find the largest number, but I can't seem to figure out how to print it's index location. Anyway, here's what I have so far:
public static void main(String[] args) {
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
double max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}
System.out.println(max);
System.out.println(i + j); //No idea what I should be doing here, just trying out everything I can think of
Right now, you should consistently get 2 * arr.length as the final value. That isn't what you are probably looking for. It looks like you want to know the coordinates for the max value. To do this, you'll need to cache the values of the indexes and then use them later:
public static void main(String[] args) {
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int tmpI = 0;
int tmpJ = 0;
double max = arr[0][0];
// there are some changes here. in addition to the caching
for (int i = 0; i < arr.length; i++) {
int[] inner = arr[i];
// caches inner variable so that it does not have to be looked up
// as often, and it also tests based on the inner loop's length in
// case the inner loop has a different length from the outer loop.
for (int j = 0; j < inner.length; j++) {
if (inner[j] > max) {
max = inner[j];
// store the coordinates of max
tmpI = i; tmpJ = j;
}
}
}
System.out.println(max);
// convert to string before outputting:
System.out.println("The (x,y) is: ("+tmpI+","+tmpJ+")");
Be careful with your array dimensions! The second for-statement most of you have is wrong. It should go to up to arr[i].length:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
tmpI = i; tmpJ = j;
}
}
}
Store i, j whenever you update max.
This would be if you wanted a single index into a flatten array:
public static void main (String[] args) throws java.lang.Exception
{
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int[] flattened = new int[6*3]; // based off above
int maxIndex = 0;
double max = arr[0][0];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
flattened[i + j] = arr[i][j];
if (arr[i][j] > max) {
max = arr[i][j];
maxIndex = i+j;
}
}
}
System.out.println(max);
System.out.println(flattened [maxIndex]);
}
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int max = arr[0][0];
int maxI = 0, maxJ = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
maxI = i;
maxJ = j;
}
}
}
System.out.println(max);
System.out.println(maxI + "," + maxJ);
You've got a two-dimensional array, therefore you need to know both indexes. Adding them together won't do because you lose which-is-which. How about this:
System.out.println("[" + i + "][" + j + "]");
//C++ code
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> b;
vector<int> c;
int Func(int a[][10],int n)
{
int max;
max=a[0][0];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
b.push_back(i);
c.push_back(j);
}
}
}
b.push_back(0);
c.push_back(0);
return max;
}
void display(int a[][10],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int a[10][10],n;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<endl;
display(a,n);
cout<<endl;
cout<<Func(a,n)<<" is the greatest "<<endl;
if(b.size()==1&&c.size()==1)
{
cout<<"Location is (1,1)"<<endl;
}
else
{
b.erase(b.end() - 1);
c.erase(c.end() - 1);
cout<<"Location is "<<"("<<b.back()+1<<","<<c.back()+1<<")"<<endl;
}
return 0;
}
You're just adding the indices i and j together and then printing it to the screen. Since you're running throug the entire loop it's just going to be equal to 2*arr.length-2. What you need to do is store the values of i and j when you encounter a new max value.
For example:
int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}};
int max = arr[0][0]; //dunno why you made it double when you're dealing with integers
int max_row=0;
int max_column=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
max_row=i;
max_column=j;
}
}
System.out.println("The max is: "+max+" at index ["+max_row+"]["+max_column+"]");
Don't sure that you implement effective algorithm, but why you just don't save indices i,j in another variables when you set max.
This is very simple.
if (arr[i][j] > max) {
max = arr[i][j];
maxX = i;
maxY = j;
}
FYI If you want look at "insertion sorting" algorithms if you want better implementation.

Categories