0

You're given an array of integers,in case if you see subsequence in which each following bigger than the previous on one(2 3 4 5) you have to rewrite this subsequence in the resulting array like this 2 - 5 and then the rest of the array. So in general what is expected when you have 1 2 3 5 8 10 11 12 13 14 15 the output should be something like 1-3 5 8 10-15.

I have my own idea but can't really implement it so all I managed to do is:

static void CompactArray(int[] arr)
   {
       int[] newArr = new int[arr.length];
       int l = 0;
       for (int i = 0,k=1; i <arr.length ; i+=k,k=1) {
           if(arr[i+1]==arr[i]+1)
           {
               int j = i;
               while (arr[j+1]==arr[j]+1)
               {
                   j++;
                   k++;
           }
           if (k>1)
           {
                
           }
       }
       else if(k==1)
       {
           newArr[i] = arr[i];
       }
   }

In short here I walk through the array and checking if next element is sum of one and previous array element and if so I'm starting to walk as long as condition is true and after that i just rewriting elements under indices and then move to the next.

I expect that people will help me to develop my own solution by giving me suggestions instead of throwing their own based on the tools which language provides because I had that situation on the russian forum and it didn't help me, and also I hope that my explanation is clear because eng isn't my native language so sorry for possible mistakes.

4
  • you may need to describe what the problem is, to get a proper answer. Commented Jul 18, 2020 at 15:45
  • Frankly speaking i'm not sure if i can clarify this more than i did.I don't want to use google translate because it will give me a horrible translation which none will be able to understand including me Commented Jul 18, 2020 at 15:48
  • basically what is needed is to replace sequence where next element is the sum of previous plus one for example 2 3 4 5 with 2-5 in the resulting array Commented Jul 18, 2020 at 15:49
  • @muratgu should i mention that this sequence is in the arrays and if you have the array like 1 2 3 4 6 10 20 21 22 23 the result should be 1-4 6 10 20-23 Commented Jul 18, 2020 at 15:54

1 Answer 1

1

If I understand the problem correctly, you just need to print the result on the screen, so I'd start with declaring the variable which will hold our result string.

var result = string.Empty

Not using other array to store the state will help us keep the code clean and much more readable.

Let's now focus on the main logic. We'd like to loop over the array.

for (int i = 0; i < array.Length; i++)
{
    // Let's store the initial index of current iteration.
    var beginningIndex = i;

    // Jump to the next element, as long as:
    //   - it exists (i + 1 < array.Length)
    //   - and it is greater from current element by 1 (array[i] == array[i+1] - 1)
    while (i + 1 < array.Length && array[i] == array[i+1] - 1)
    {
        i++;
    }
    
    // If the current element is the same as the one we started with, add it to the result string.
    if (i == beginningIndex)
    {
        result += $"{array[i]} ";
    }
    // If it is different element, add the range from beginning element to the one we ended with.
    else
    {
        result += $"{array[beginningIndex]}-{array[i]} ";
    }
}

All that's left is printing the result:

Console.WriteLine(result)

Combining it all together would make the whole function look like:

static void CompactArray(int[] array)
{
    var result = string.Empty;

    for (int i = 0; i < array.Length; i++)
    {
        var beginningIndex = i;
        while (i + 1 < array.Length && array[i] == array[i+1] - 1)
        {
            i++;
        }

        if (i == beginningIndex)
        {
            result += $"{array[i]} ";
        }
        else
        {
            result += $"{array[beginningIndex]}-{array[i]} ";
        }
    }

    Console.WriteLine(result);
}
Sign up to request clarification or add additional context in comments.

8 Comments

mcjmzn I have tested your solution and it works,although it uses a string to get that - character i was thinking how to put char into integer array and that was pain in the ass because i knew it's not possible but i didn't think of a string how possible loophole)) I'll try to figure out the logic of your code after and then hopefully i will be able to write my own solution and thank you so much)))
but wouldn't it be better to use StringBuilder instead of a regular string so not waste memory because i heard that each time you change the string you actually create new object of it and the previous turning into garbage?
Yes it would be better and I strongly encourage you to use it. I used plain strings to keep the code as simple as it can be.
i have a question with another task called spiral matrix and i can't really figure out how to solve it may i ask your help once again?
Sure, I can give it a try :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.