1 of 13

PROBLEM SOLVING TECHNIQUES

By

Dept. of CSE

PVPSIT, Kanuru.

PRASAD V. POTLURI SIDDHARTHA INSTITUTE OF TECHNOLOGY

2 of 13

  • Problem
  • Remove all duplicates from an ordered array and contract the array accordingly.
  • Algorithm development
  • As a starting point for this design let us focus on a specific example.

Department of CSE

2025-26

REMOVAL OF DUPLICATES FROM AN ORDERED ARRAY

PVPSIT (Autonomous)

Problem Solving Techniques

3 of 13

  • Each unique element in the original array has been moved as far to the left as possible.
  • A duplicate pair is identified when two adjacent elements are equal in value.
  • With each comparison, only two situations are possible.

1. a pair of duplicates has been encountered;

2. The two elements are different

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

4 of 13

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

5 of 13

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

6 of 13

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

7 of 13

  • We have two initialization choices:

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

8 of 13

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

9 of 13

  • begin

i:=2

While (a[i-1]<>a[i] and i<n) do i:=i+1;

If a[i-1]<>a[i] then i:=i+1;

J:=i-1

While i<n do

begin

i:=i+1

if a[i-1]<>a[i] then

begin

j:= j+1

a[j]:=a[i]

end

end;

n:=j //reduce the size of array

    • end

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

10 of 13

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

11 of 13

  • Step 0: Start
  • Step 1: Read N
  • Step 2: Set I := 1
  • Step 3: Repeat Step 4, 5 and 6 till I <= N
  • Step 4: Read Element, E
  • Step 5: A[I] = E
  • Step 6: I := I + 1
  • Step 7: Reset I := 2
  • Step 8: Repeat Step 9 till (A[I-1] != A[I] and I < N)
  • Step 9: I := I + 1

Department of CSE

2025-26

Algorithm:

PVPSIT (Autonomous)

Problem Solving Techniques

12 of 13

  • Step 10: if (A[I-1] != A[I]) then I := I + 1
  • Step 11: J := I – 1
  • Step 12: Repeat Step 13 and 14 till I < N
  • Step 13: I := I + 1
  • Step 14: if (A[I-1] != A[I]) then J := J + 1, A[J] := A[I]
  • Step 15: N := J
  • Step 16: Reset I := 1
  • Step 17: Repeat Step 18 and 19 till I <= N
  • Step 18: Display A[I]
  • Step 19: I := I + 1
  • Step 20: Stop

Department of CSE

2025-26

PVPSIT (Autonomous)

Problem Solving Techniques

13 of 13

  • Data compression and text processing problems.

Department of CSE

2025-26

Applications

PVPSIT (Autonomous)

Problem Solving Techniques