Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 months ago.
Improve this question
I have given an interview in geeks for geeks where I was given a magic match sticks problem, I wasn't able to solve it in time. I solved it myself a day after, I wasn't able to find the solution as well on Stack Overflow, so here is the solution for you if you ever encounter that problem:
//magic match-stick problem
/*in this problem set we are given n number of match boxes,
* you need to add matches in a match that is
* the n number of matches in each box,
* but the catch is instead of adding the matches,
* you need to subtract the matches pair that you will be inserting in place
* of every new match stick until there is only last box left with final match stick score
* eg: 1st match box: 4match sticks: 1,2,3,4 match numbers
* 2nd time: 2 match sticks : (1-2), (3-4)=> 1, 1
* last time: (1-1) => 0
*
* I know I cannot explain the problem correctly in English, but I will be able to explain it to you in hindi v well
* also if you can understand the code, you will understand the question
* this took me one day to solve, many minor mistakes I did
* this is a geeks for geeks interview question
* question name magical matchsticks*/
public class practice{
public static void main(String args[]) {
int a[] = {1,2,3,4,5,6};
int num;
int count;
int n = a.length;
do
{
num=0;
count=0;
if(n==1)
{
System.out.println(a[0]+"last");
}
else if(n>1)
{
if(n%2==0)
{
n=n/2;
for(int i = 0; i<((n)) ; i++)
{
a[i] = a[num]-a[num+1];
if(a[i]<0)
{
a[i] = a[i]*(-1);
System.out.print(a[i]+" "+a[i + 1]);
}
System.out.println(" a ");
num = num + 2;
count++;
}
}
else if(n%2==1)
{
n = (n/2) +1;
for(int i = 0; i<(n); i++)
{
a[i] = a[num]-a[num+1];
if(a[i]<0)
{
a[i] = a[i]*(-1);
System.out.print(a[i]+" "+a[i + 1]);
}
System.out.println("b ");
num = num + 2;
count++;
}
}
}
System.out.println(n);
for(int j = 0; j<a.length;j++)
{
System.out.print(a[j]);
}
System.out.println(" ");
}while(n>1);
for(int k = 0; k<a.length;k++)
{
System.out.print(a[k]);
}
System.out.println(" ");
}
}
This problem will show you all the steps as the program goes on, you will need to make changes so it returns value from a separate class or function.
I'll be starting to provide solutions for competitive programming on this account.
this question is solved with answer given above.
also answer is provided on my github page: https://github.com/bruce-Wayn/java-qns/blob/6a9714225b4884aefbdad0441e721416c5843576/magic-match-sticks.java
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I've been working on a simple Java code where you are to give 5 numbers to the program through the console, which will then tell you what numbers you chose and give you an average. I noticed, however, after I started trying to test my code, that the for loop was basically 'skipping over' all of the code inside of the loop, and I have no idea why. Here's my code:
import java.util.Scanner;
public class numAv {
public static void main(String[] args) {
int num;
int[] numbers = new int[5];
boolean done = false;
Scanner scan = new Scanner(System.in);
System.out.println("Enter five integer numbers one at a time.");
for (int i = 0; i >= 5; i++) {
scan.nextLine();
num = scan.nextInt();
numbers[i] = num;
}
// The code inside the for loop is being skipped; I'm not getting any time to type in an integer.
System.out.println("Your numbers are:");
for (int i = 0; i >= 5; i++) {
System.out.print(numbers[i]);
}
// The same has also happened above; The code within the for loop is essentially being skipped.
num = 0;
for (int i = 0; i >= 5; i++) {
num += numbers[i];
}
num /= (float) 5;
System.out.println("The average of the chosen numbers is " + num);
}
}
Here's what the console outputs:
Enter five integer numbers one at a time.
Your numbers are:
The average of the chosen numbers is: 0
Here:
for (int i = 0; i >= 5; i++) {
i will have a really hard time being zero and bigger than 5 at the same time.
The real answer here: each and any character that you put into your source code matters. There is a big difference between <= and >=, and even between <= and < for that matter. So, when your code doesn't do what you expect it to do: take a piece of paper, and start "running" that code manually. Really write down the values within your variables, and carefully check what the code is doing with them.
Bad condition:
for (int i = 0; i >= 5; i++) {
This will never works, try this:
for (int i = 0; i < 5; i++) {
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this code which suppose to do the following task:
For example , given M=3, and array built like this:
A[0]=1
A[1]=1
A[2]=3
A[3]=3
A[4]=5
A[5]=1
A[6]=3
the function may return 1 or 3
import java.util.*;
class Solution {
int solution(int M, int[] A) {
int N = A.length;
int[] count = new int[M + 1];
for (int i = 0; i <= M; i++)
count[i] = 0;
int maxOccurence = 1;
int index = -1;
for (int i = 0; i < N; i++) {
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
}
return A[index];
}
}
what could be the problem because it is not always working and I can see there is a bug in my program.
1 1 1 1 5 5 5 5 5 this is a case where your code may fail. Check and update the max occurance variable outside the loop too. Above case give enough justice to my point, i hope.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The idea is to type two numbers in the command line. The first being the start and the second being the number of prime numbers after the first number are displayed. For instance if the input was 16 3 the output would be 17 19 23
These numbers would be stored into an array. I've been trying many different things but they haven't worked so far.
I only have this right now
{
public static void main (String[] args)
{
int start =Integer.parseInt(args[0]);
int count =Integer.parseInt(args[1]);
int[] prime = new int[count];
public static boolean check(int a)
{
if (a%2==0)
return false;
for(int i=3;i*i<=a;i+=2)
{
if(a%i==0)
return false;
}
return true;
}
}
Your check function is great. What you need is just a while loop to add prime number to prime[]
public static void main (String[] args)
{
int start =Integer.parseInt(args[0]);
int count =Integer.parseInt(args[1]);
int[] prime = new int[count];
int i = 0;
while (i < count){
if(check(start)){
prime[i] = start;
i++;
}
start++;
}
for (int p : prime){
System.out.println( p);
}
}
If you want a program to do something N times, you can use a loop.
Since this feels like HW I will provide an analogous problem. Count N odd numbers:
int numbersToCount = 5;
int count = 0;
int start = 1;
while (count < numbersToCount) {
if (start % 2 == 1) {
count = count + 1;
// opportunity to print number you are interested in
}
start = start + 1;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How can I find a saddle point of a matrix, which is the highest number in the row and at the same time the highest number in column using Java?
For example, using this matrix:
| 7 2 |
| 1 3 |
| 5 8 |
the saddle points are: 7 and 8.
Here is the portion of my code I wrote to find the highest number in the row and in the column.
int NumRow = 3;
int NumCol = 2;
int [] matrix = new int [NumRow][NumCol];
for ( int i = 0; i < NumRow; i++)
{
max = matrix[i][0];
for ( int j = 1; j < NumCol; j++)
{
if (matrix[i][j]> max)
{
max = matrix[i][j];
}
}
System.out.print(max+" ");
}
System.out.println("\n");
for ( int c = 0; c < NumCol; c++)
{
largest = matrix[c][0];
for (int r = 0; r < NumRow; r++){
if (matrix[r][c] > largest){
largest = matrix[r][c];
}
}
System.out.print(largest+" ");
}
The output is:
7 3 8
7 8
Now I want to find the saddle point using the definition above.
From wikipedia (emphasis mine):
A saddle point is an element of the matrix which is both the largest
element in its column and the smallest element in its row.
You can determine it by going through the matrix in row-order and:
creating an array to store the current-column maximum
storing a current-row-minimum on the fly and store it in an array too
when you are done with this you can compare if an index occurs in both at the same time so that you have an index which is both the column-max and row-min.
Note: your example-matrix does not have any saddle points according to wikipedia's definition.
Unfortunately I have to go. It looks like you were going to get there in the end.
Here is a solution which is based on your description, and the way you describe it should work.
It's not the most efficient, you should improve that. You also should not submit this as an assignment, doing so would only be cheating yourself, you will find future assignments difficult.
public class SaddlePointerFinder{
public static void main(String[] args){
int [] [] matrix = {
{ 7, 2 },
{ 1, 3 },
{ 5, 8 },
};
// i loops though columns
for(int x = 0; x<matrix[0].length; x++){
// this is to store the highest on the ROW
int highestOnTheRow = -1;
// this is to store the index of that highest value
int indexOfHighest = -1;
// x loops through rows
for(int y = 0; y<matrix.length; y++){
if(matrix[y][x] > highestOnTheRow) {
// update the highest
highestOnTheRow = matrix[y][x];
indexOfHighest = y;
}
}
// After checking the whole row and finding the highest, check if it's highest on the column
boolean highest = true;
// here, i checks goes through each row using that column.
for(int i = 0; i<matrix[0].length; i++){
if(matrix[indexOfHighest][i] > highestOnTheRow) {
// one which was higher was found :(
highest = false;
}
}
if(highest){
System.out.println("If the forumla is correct, this is a saddle point: " + highestOnTheRow);
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I think there may be a problem with my 3 way sort algorithm in the following Java program, also any suggestion on optimizing or just a simpler it would be greatly appreciated. The objective of the sort is to have the minus numbers first then zeros and then positive numbers
class ThreeWaySort
{
public static void main(String[] args)
{
int location = 0;
int[] sArray = new int[50];
for (int a = 25; a<= -24; a--)
{
sArray[location] = a;
location++;
}
int i = 0; int j = 0; int k = 50;
while (j!=k)
{
if (sArray[j]==0)
{
j++;
}
else if (sArray[j]<0)
{
int t = sArray[i]; sArray[i] = sArray[j]; sArray[j] = t; // case (ii)
i++; j++;
}
else
{
k--;
int t= sArray[j]; sArray[j] = sArray[k]; sArray[k] = t;
}
}
for (int a = 0; a <= 49; a++)
{
if(sArray[a] >-1)
{
System.out.println();
System.out.println();
System.out.println();
}
if(sArray[a] > 0)
{
System.out.println();
System.out.println();
System.out.println();
}
System.out.print(sArray[a] + " ");
}
}
}
When i run the program as is it costantly print out a zero followed by three line instead of what I'm expecting to be, Numbers below zero in a line, followed by 3 blank lines then any zeros in the array, 3 blank lines, positive numbers in the array.
The loop that populates your array is incorrect:
for (int a = 25; a<= -24; a--)
The variable a starts at 25, which is not less than or equal to -24, so the loop never executes. You should use >=.